# [Numpy-discussion] About extending NumPy with c

asaf david asafdav2 at gmail.com
Sat Jan 13 04:59:51 CST 2007

```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

here's my complete c file:

#include <python.h>
#include <numeric/arrayobject.h>

static PyObject *trace(PyObject *self, PyObject *args)
{
PyArrayObject *array;
double sum;
int i, n;

if (!PyArg_ParseTuple(args, "O!", &PyArray_Type, &array))
return NULL;

if (array->nd != 2 || array->descr->type_num != PyArray_DOUBLE) {
PyErr_SetString(PyExc_ValueError, "array must be two-dimensional and
of type float");
return NULL;
}

n = array->dimensions[0];
if (n > array->dimensions[1])
n = array->dimensions[1];
sum = 0.;
for (i = 0; i < n; i++)
sum += *(double *)(array->data + i*array->strides[0] +
i*array->strides[1]);
return PyFloat_FromDouble(sum);
}

static PyMethodDef foo_methods[] = {
{ "trace", trace, METH_VARARGS, NULL},
{ NULL, NULL, 0, NULL }
};

PyMODINIT_FUNC initfoo() {
Py_InitModule3("foo", foo_methods, "bla bla");
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20070113/58b6b77b/attachment.html
```