Numpy/capi Error With Import_array() When Compiling Multiple Modules
Solution 1:
I had a similar problem, as the link you've posted points out, the root of all evil is that the PyArray_API
is defined static, which means that each translation unit has it's own PyArray_API
which is initialized with PyArray_API = NULL
by default. Thus import_array()
must be called once for every *.cpp
file. In your case it should be sufficient to call it in pycapi_utils.cpp
and also once in model.cpp
. You can also test if array_import is necessary before actualy calling it with:
if(PyArray_API == NULL)
{
import_array();
}
Solution 2:
So apparently if I include in the pycapi_utils
module a simple initialization routine such as:
//pycapi_utils.h
...
...
voidinit_numpy();
//pycapi_utils.cpp
...
...
voidinit_numpy(){
Py_Initialize;
import_array();
}
and then I invoke this routine at the beginning of any function / method that uses Numpy objects in my C code, it works. That is, the above code is edited as:
//pycapi_utils.cpp
...
...
PyObject* array_double_to_pyobj(...){
init_numpy();
...
...
}
//model.cpp
...
...
PyObject* simulate(...){
init_numpy();
...
...
}
My only concern at this point is whether there is a way to minimize number of calls to init_numpy()
, or regardless I have to call it from any function that I define within my CPP modules that uses Numpy objects...
Post a Comment for "Numpy/capi Error With Import_array() When Compiling Multiple Modules"