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, PyArrayObjects 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.
gcccommand succeed (with just a warning), when you haven't told it where to find the numpy header files? I would expect an argument of the form-I /path/to/installed/numpy/core/includeas part of thatgcccomand. (Also, what platform and OS are you using? Which version of gcc? Which version of numpy?)-I/usr/include/python2.7suffices on my systems (see edit) and I am pretty sure that this is unrelated to the problem. Numpy’sget_includeshould give you the relevant flags for your system. I added the specifications for which I could produce the warning.get_include()return on your system(s)?