how to send python list to C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ilyaw77
    New Member
    • Oct 2009
    • 8

    how to send python list to C

    Hi guys,
    I would appreciate your help here. I'm not experienced Python programmer but I have to implement one project that require passing Python list object to C and iterate it somehow.

    For example:

    in Python: list=[1,2.3,'string']

    in C:

    I do not know if to use PyArg_ParseTupl e is good here:
    Code:
    static PyObject* pJPMTibPublishALot(PyObject *self, PyObject *args){
    	? myVal;
    	
    	if (!PyArg_ParseTuple(args, "?", &myVal))
    		onError("Crap.")
    
    
    	Py_RETURN_NONE;
    
    }
    Thank you for help !!!
    Last edited by bvdet; Mar 30 '10, 10:44 PM. Reason: Add code tags
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    #2
    Can you tell us why you want to do this?

    Have you looked at shedskin? It takes the python code and compiles it into C.

    But if you're trying to do a calculation faster by doing it in C, then it's possible that doing array functions with numpy/scipy will also increase your speed dramatically (ie do whatever calculation to the numpy array, as opposed to looping through the array - in this way the loop is done in the background with C).

    The other possibility is to get python to run the C programmes from the command line.

    There are supposed to be good ways to call C/C++, but I've never got them to work, to be honest, despite having a book, a couple of days, and the internet. But that was some time ago, and perhaps I'd have more success if I tried now. I've just lumped it in the basket of stuff that's supposed to be possible but appears to be tricky!

    Good luck

    Comment

    • ilyaw77
      New Member
      • Oct 2009
      • 8

      #3
      Hi Glenton, thank you for the reply.
      The thing is we use input from Python for input to another C implemented library. After browsing google I found that I can access list using PyList_ set of functions and I tried this simple app: (again l=[1,2])

      Code:
      static PyObject* pJPMTibPublishALot(PyObject *self, PyObject *args){
      	FILE* file;
      	//Py_ssize_t size;
      	PyTypeObject* type = args->ob_type;
      	Py_ssize_t size=0;
      	int i;
      
      	//check if type sent is the list
      	file=fopen("c:\\python.txt","a+");
      	if (type=&PyList_Type){
      		fprintf(file,"The container is a LIST\n");
      	}
      	else{
      		fprintf(file,"The container is NOT a LIST\n");
      	}
      
      	size=PyList_Size(args);
      	fprintf(file,"The size_t value is %d\n",(int)size);
      
      	fclose(file);
      
      	//PyObject* item=PyList_GetItem(args,0);
      	//PyObject_Print(item, stdout, 0);
      	
      	Py_RETURN_NONE;
      
      }
      in my output file I got:

      The container is a LIST
      The size_t value is -1

      Do you know why the Size method failing ?

      Thank you,

      Ilya
      Last edited by bvdet; Mar 31 '10, 03:17 AM. Reason: Add code tags

      Comment

      Working...