I'm trying to call a Python function in C using Python C API. I use PyObject_Call
to call the Python function which returns me a PyObject. If the returned PyObject is a numpy array or a list then how should I handle it? Basically if I want to cast it into a C array, or just get the pointer out, how should I do it?
Here is an outline of the code. The func
is numpy.power
.
double Array[4] = {1.0, 2.0, 3.0, 4.0};
int Integer = 2;
double *call_func(PyObject *func, double array[], int integer){
PyObject *args;
PyObject *kwargs;
PyObject *result;
npy_intp dims[1] = {4};
double *retval;
PyGILState_STATE state = PyGILState_Ensure();
args = Py_BuildValue("(Od)", PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE, Array), integer);
kwargs = NULL;
result = PyObject_Call(func, args, kwargs);
Py_DECREF(args);
Py_XDECREF(kwargs);
retval = ???
Py_DECREF(result);
PyGILState_Release(state);
return retval;
}
...