passing Python list to C extension function

j vickroy jvickroy at sec.noaa.gov
Tue Apr 25 16:59:33 EDT 2000


I finally discovered my error after carefully re-reading the tutorial on
extending Python; the C function should be:

static PyObject *c_extender_total_it (self, parameter)
    PyObject *self;
    PyObject *parameter;

j vickroy wrote:

> Hello all,
>
> I am trying to pass a Python list to a C extension module function and
> retrieve a result.  Using Python 1.5.2 on Win NT 4.0, the C function
> generates a memory error when trying to parse () the Python list
> parameter.
>
> Any assistence would be appreciated.
>
> Thanks for your time,
>     - jv
>
> Here is the Python code:
>
> import c_extender
> list = []
> for i in xrange (10):
>     list .append ( float (i) )
>  print 'total:', c_extender .total_it (list)
>
> ... and here is the C function.
>
> #include "Python.h" // Python-C interface definitions
>
> static PyObject *c_extender_total_it (parameter)
>     PyObject *parameter;
>      /*
>      */
>     {
>     int i;
>     float result;
>     double *array;
>     double c_double;
>     PyObject *python_list;
>
>     printf ("\t <executes this statement> \n");
>     // interpret parameter as a Python list
>         if (! PyArg_ParseTuple (parameter, "O!", &PyList_Type,
> &python_list) ) return NULL;
>     // *** memory exception reported here ***
>     printf ("\t <does NOT execute this statement> \n");
>
>     // perform calculation here
>
>     free (array);
>     return Py_BuildValue ("total", result);
>     }




More information about the Python-list mailing list