[Numpy-discussion] Complete and build the example from the "Using Numpy C-API" documentation.

Warren Weckesser warren.weckesser at enthought.com
Tue Jul 20 19:04:42 EDT 2010


Warren Weckesser wrote:
> I would like to take the example shown here:
>
>   http://docs.scipy.org/doc/numpy/user/c-info.how-to-extend.html#example
>
> and complete the code so I can build and test the extension module.  Is 
> that example currently correct?  It is slightly different from the older 
> example in the numpy book, so someone must have had a reason to change it. 
>
> I've added the C array of PyMethodDefs and the module init function, but 
> I'm still searching for the correct #includes for the numpy stuff.  Any 
> suggestions (or pointers to similar simple but complete examples) would 
> be appreciated.
>
>   

I've made some progress, and now I'm getting a bus error.
With the following extension module, calling example.foo results
in a bus error, regardless of the arguments given.  The error
occurs in the call to PyArg_ParseTuple.  I'm using same format
string as in the example in the numpy docs, "O!".  Any ideas?

-----
#include <stdio.h>
#include "Python.h"
#include "numpy/arrayobject.h"

static PyObject *foo_wrap(PyObject *dummy, PyObject *args)
{
    PyObject *arg1 = NULL;

    printf("boo\n");
    if (!PyArg_ParseTuple(args, "O!", &PyArray_Type, &arg1)) {
        printf("bad\n");
        return NULL;
    }
    printf("here\n");
    return Py_None;
}

static PyMethodDef wrappers[] = {
    {"foo", foo_wrap, METH_VARARGS, "just a test"},
    {NULL, NULL, 0, NULL}        /* Sentinel */
};

PyMODINIT_FUNC
initexample(void)
{
    (void) Py_InitModule("example", wrappers);
}
-----
> Warren
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>   




More information about the NumPy-Discussion mailing list