[Numpy-discussion] About extending NumPy with c

David Cournapeau cournape at gmail.com
Sat Jan 13 10:14:02 CST 2007


On 1/13/07, asaf david <asafdav2 at gmail.com> wrote:
> Hello
> i want to write a c function that receive and process a numpy array. to do
> so, i'm following this tutorial
> http://numpy.scipy.org/numpydoc/numpy-13.html
> as a start, i copy pasted the "A Simple Example" from the tutorial and
> compiled the module as usual with no errors/warnings.
> however, when trying to run the function on a legal numpy array, i'm getting
> an error message:
> "python.exe has encountered a problem and needs to close, send, dont send
> etc'
> i've narrowed it down and it happens when running this line
>
> if (!PyArg_ParseTuple(args, "O!", &PyArray_Type, &array))
>
> any ideas how to solve it?
> thanks
Well, the problem is pretty basic, but you would have a really hard
time to find it.... The problem happens at that line, but not because
of that line... It would happen with any call to the numpy C API (the
first one for that matter).

> PyMODINIT_FUNC initfoo() {
>     Py_InitModule3("foo", foo_methods, "bla bla");
> }
>
Here is your problem ! in your init function, you HAVE to call import_array():

PyMODINIT_FUNC initfoo() {
     Py_InitModule("foo", foo_methods, "bla bla");
     import_array();
}

This initializes the numpy C API. Without this call, all functions of
the numpy C API are actually dangling pointers, and do not point to
any valid function.

David


More information about the Numpy-discussion mailing list