Reputation: 9278
While attempting to read a Python list filled with float numbers and to populate real channels[7]
with their values (I'm using F2C, so real is just a typedef for float), all I am able to retrieve from it are zero values. Can you point out the error in the code below?
static PyObject *orbital_spectra(PyObject *self, PyObject *args) {
PyListObject *input = (PyListObject*)PyList_New(0);
real channels[7], coefficients[7], values[240];
int i;
if (!PyArg_ParseTuple(args, "O!", &PyList_Type, &input)) {
return NULL;
}
for (i = 0; i < PyList_Size(input); i++) {
printf("%f\n", PyList_GetItem(input, (Py_ssize_t)i)); // <--- Prints zeros
}
//....
}
Upvotes: 2
Views: 4575
Reputation: 129994
Few things I see in this code.
PyListObject
.PyList_GetItem
returns a PyObject
, not a float. Use PyFloat_AsDouble
to extract the value.PyList_GetItem
returns NULL
, then an exception has been thrown, and you should check for it.Upvotes: 2
Reputation: 376052
PyList_GetItem
will return a PyObject*
. You need to convert that to a number C understands. Try changing your code to this:
printf("%f\n", PyFloat_AsDouble(PyList_GetItem(input, (Py_ssize_t)i)));
Upvotes: 3