[Numpy-discussion] How to call import_array() properly?

Bruce Sherwood Bruce_Sherwood@ncsu....
Sun Dec 26 16:42:33 CST 2010


I made a mistake: the Mac behaves the same way when I repeat the
experiment. I guess I simply have to define init_numpy() to be of type
int for Python 3 on both machines. Nevertheless, if you see a more
elegant coding, I'd be interested. Thanks.

Bruce Sherwood

On Sun, Dec 26, 2010 at 3:26 PM, Bruce Sherwood <Bruce_Sherwood@ncsu.edu> wrote:
> 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