I used python c api and wish to get an array back from python. I returned a python array from the python side and want to transfer the PyObject* result into a c array so I can use it.
Is there anyway I can do that?
Side questions: In what circumstance trying to return an element in the array like
return arr[3]
will cause the PyObject_callobject return NULL?
return arr
does give me something
In Python an "array" is a list datatype. Look at the PyList_* functions in the C API. You can find this reference here for 2.7 and here for 3.5. The functions of interest are:
Py_ssize_t PyList_Size(PyObject *list) to get the number of list members
and
PyObject* PyList_GetItem(PyObject *list, Py_ssize_t index) to get a reference to the member at that index.
A simple use of malloc or similar allocator and a for loop should do the trick.
Also note that only borrowed references are involved, so no reference count maintenance of these objects via void Py_INCREF(PyObject *o) and void Py_DECREF(PyObject *o) is necessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With