[Numpy-discussion] embedded PyArray_FromDimsAndDataSegmentationFault
Tue May 20 14:01:00 CDT 2008
After all, I figured how to create an numpy in C with the help below.
If called in C, import_array() but actually _import_array() successfully creates all the instances needed for the array.
However, once I call this function from another environment such as Matlab,
PyObject *numpy = PyImport_ImportModule("numpy.core.multiarray");
in __import_array() returns NULL, because numpy.core.multiarray is not found?
Do you think it might depend on the path settings?
As I said, the code works fine from plain C but its odd from within Matlab.
From: firstname.lastname@example.org on behalf of Robert Kern
Sent: Wed 5/14/2008 5:12 PM
To: Discussion of Numerical Python
Subject: Re: [Numpy-discussion] embedded PyArray_FromDimsAndDataSegmentationFault
On Wed, May 14, 2008 at 6:40 PM, Thomas Hrabe <email@example.com> wrote:
>>I didn't know a person could write a stand-alone program using NumPy
>>this way (can you?)
> Well, this is possible when you embed python and use the "simple" objects such as ints, strings, ....
> Why should it be impossible to do it for numpy then?
numpy exposes its API as a pointer to an array which contains function
pointers. import_array() imports the extension module, accesses the
PyCObject that contains this pointer, and sets a global pointer
appropriately. There are #defines macros to emulate the functions by
dereferencing the appropriate element of the array and calling it with
the given macro arguments. The reason you get the error about
returning nothing when the return type of main() is declared int is
because this macro is only intended to work inside of an initmodule()
function of an extension module, whose return type is void.
import_array() includes error handling logic and will return if there
is an error. You get the segfault without import_array() because all
of the functions you try to call are trying to dereference an array
which has not been initialized.
> My plan is to send multidimensional arrays from C to python and to apply some python specific functions to them.
Well, first you need to call Py_Initialize() to start the VM.
Otherwise, you can't import numpy to begin with. I guess you could
write a "void load_numpy(void)" function which just exists to call
import_array(). Just be sure to check the exception state
appropriately after it returns.
But for the most part, it's much better to drive your C code using
Python than the other around.
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
Numpy-discussion mailing list
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Numpy-discussion