Converting a Python List to a C double *

John Machin sjmachin at lexicon.net
Fri Apr 26 19:30:07 EDT 2002


Alex Martelli <aleax at aleax.it> wrote in message news:<jPfy8.65668$vF6.2006478 at news2.tin.it>...

>     xdata = malloc(listlength * sizeof(double));

Joakim's platform may have not the Cornucopia version of malloc() but
the Mother Hubbard version -- a test of the returned value may be a
good idea.

>     for(i=0; i<listlength; i++) {
>         temp = PySequence_GetItem(thelistofdoubles, i);
>         if(!temp) {
>             free(xdata);
>             return 0;
>         }
>         if(!PyFloat_Check(temp)) {
>             Py_DECREF(temp);
>             free(xdata);
>             PyErr_SetString(PyExc_TypeError, "all items must be float");
>             return 0;
>         }
>         xdata[i] = PyFloat_AS_DOUBLE(temp);
>         Py_DECREF(temp);
>     }



More information about the Python-list mailing list