Pass a tuple (or list) to a C wrapper function

Java and Swing codecraig at gmail.com
Thu Oct 13 15:23:58 CEST 2005


I got it.   I had get_long_array placed after the method that was
calling it..
i.e.

void doStuf(...) {
   x = get_long_array(...);
}

static long *get_long_array(PyObject *data, int *data_size) {
...
}

...I put get_long_array before it in my code..and its fine.

Thanks

Java and Swing wrote:
> Fredrik,
>   ...I tried using your code...
>
> static long *get_long_array(PyObject *data, int *data_size) {
>     int i, size;
>     long* out;
>     PyObject* seq;
>
>     seq = PySequence_Fast(data, "expected a sequence");
>     if (!seq)
>         return NULL;
>
>     size = PySequence_Size(seq);
>     if (size < 0)
>         return NULL;
>
>     if (data_size)
>         *data_size = size;
>
>     out = (long*) PyMem_Malloc(size * sizeof(long));
>     if (!out) {
>         Py_DECREF(seq);
>         PyErr_NoMemory();
>         return NULL;
>     }
>
>     for (i = 0; i < size; i++)
>         out[i] = PyInt_AsLong(PySequence_Fast_GET_ITEM(seq, i));
>
>     Py_DECREF(seq);
>
>     if (PyErr_Occurred()) {
>         PyMem_Free(out);
>         out = NULL;
>     }
>
>     return out;
>
> }
>
>
> and I get this error..
>
> C:\project\myapp.c(549) : error C2040: 'get_long_array' : 'long
> *(struct _object *,int *)' differs in levels of indirection from 'int
> ()'
>
> any idea?
>
> Fredrik Lundh wrote:
> > Jeremy Moles wrote:
> >
> > > Probably what you want to do though is just keep the tuple as is and
> > > iterate over it using the PySequence_* protocol:
> > >
> > > http://docs.python.org/api/sequence.html
> >
> > I did post a complete and tested example a few days ago, which contained
> > code that showed how to do this.  a complete waste of time, of course.
> > 
> > </F>




More information about the Python-list mailing list