[PYTHON IMAGE-SIG] passing tuple to c and back

Dirk Engelmann Dirk.Engelmann@iwr.uni-heidelberg.de
Wed, 12 Feb 1997 16:16:13 +0100 (MET)


I would like to pass a tuple from python to a C-array. 
Then after some calculations in C a tuple should be returned to the
calling  python program.

I tried to do like this:

The Python calling sequence looks like

  x = ( 1, 2, 3 ) 
  y = c_module( x ) 
  print y

The C-module makes some trouble.

(my attempts to pass simple objects as intergers and floats
are OK)

PySequence_SetItem(vec, i, newobj) returns -1 , obviously 
vec is not a seqence object ?!

Is there any proposition to get it working?

Thanks for help!


Dirk Engelmann


Here is the module in C:

static PyObject *c_module( PyObject *self, PyObject *args )  
    PyObject *vec, *obj[N],*newobj ; 
    int x[N]; 
    int   i, n;

    if ( !PyArg_ParseTuple( args, "O", &vec ) ) 
       return NULL; 

    if (!PySequence_Check(vec)) 
      return NULL; 
    for (i = 0; i < n; i++) 
      obj[i] = PySequence_GetItem(vec, i); 

    /* here: insert calculations on x */
    for (i = 0; i < n; i++) 
         newobj = PyFloat_FromDouble( x[i] );
         PySequence_SetItem(vec, i, newobj); 

    return Py_BuildValue("O",vec);  

IMAGE-SIG - SIG on Image Processing with Python

send messages to: image-sig@python.org
administrivia to: image-sig-request@python.org