passing Python list to C extension function

j vickroy jvickroy at sec.noaa.gov
Tue Apr 25 13:53:42 EDT 2000


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