[Numpy-discussion] How to call import_array() properly?
Bruce Sherwood
Bruce_Sherwood@ncsu....
Sun Dec 26 16:26:36 CST 2010
In my Python code I have
import cvisual
cvisual.init_numpy()
and in my C++ code I have
void
init_numpy()
{
import_array();
}
import_array() in numpy/core/include/numpy/_multiarray_api.h is a macro:
#if PY_VERSION_HEX >= 0x03000000
#define NUMPY_IMPORT_ARRAY_RETVAL NULL
#else
#define NUMPY_IMPORT_ARRAY_RETVAL
#endif
#define import_array() {if (_import_array() < 0) {PyErr_Print();
PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to
import"); return NUMPY_IMPORT_ARRAY_RETVAL; } }
Note that for Python 3 there is a change so that the macro returns
NULL, whereas for Python 2 it returned nothing.
On Windows and Mac, this works fine for Python 2, and it works fine
for a with Python 3, but with either Microsoft Visual Studio 2008 or
2010 this fails for Python 3 with the message "'void' function
returning a value", presumably due to the return NULL for Python 3,
something that doesn't bother the Mac.
So my dumb question is, how should I call import_array() from my
routine init_numpy() to get around this problem? I have found a
workaround, consisting of defining init_numpy to be of type int on
Windows with Python 3, but this seems like an odd kludge, since it
isn't needed on the Mac, and I think it's also not needed on Linux.
Bruce Sherwood
More information about the NumPy-Discussion
mailing list