I am creating a module for Python with Numpy using the C API and encounter weird incompatibilities with the output of PyArray_SimpleNew
, which I would like to understand. But first a minimal example:
# include <Python.h>
# include <numpy/arrayobject.h>
void foo()
{
Py_Initialize();
import_array();
npy_intp dims[1] = {42};
PyObject * A = PyArray_SimpleNew(1,dims,NPY_DOUBLE); // Line A
Py_Finalize();
}
int main()
{
foo();
return 0;
}
If I compile this with gcc source.c -lpython2.7 -I/usr/include/python2.7 --pedantic
, I get (with a reference to Line A):
ISO C forbids conversion of object pointer to function pointer type
So, apparently, PyArrayObject
s are expected to be function pointers for some reason.
According to the documentation (e.g., here), PyArray_SimpleNew
has a return of type PyObject *
and thus the above should be perfectly fine. Moreover, I do not get similar warnings with other functions returning PyObject *
.
Now, while this is only a warning we are talking about and my programs using PyArray_SimpleNew
work as intended, all this indicates that the Numpy C API is not working as I think it is (or has a bug). Therefore I would like to understand the reason behind this.
I produced the above on the following systems:
- GCC 4.7.2 (Debian 4.7.2-5), Numpy 1.6.2
- GCC 4.8.2 (Ubuntu 4.8.2-19ubuntu1), Numpy 1.8.2
In neither case, the situation changes with # define NPY_NO_DEPRECATED_API NPY_1_8_API_VERSION
.