[Numpy-discussion] PyArray_FromDims segfault??
Xavier Gnata
gnata at obs.univ-lyon1.fr
Sat Dec 30 18:38:50 CST 2006
Hello,
I would like to use PyObject_CallObject to call the imshow function from
matplolib from a C code.
So, the first step is to create a 2D PyArrayObject from a C array.
I have read the Numpy book which is great but now I'm puzzled:
My goal is to convert a C array of doubles into a 2D numpy array object.
PyArray_SimpleNewFromData(nd, dims, typenum, data) seems to be the
function I need but I did not manage to write/find any compiling *and*
not segfaulting code performing this convertion :(
Ok, maybe PyArray_SimpleNewFromData is to complex to begin with.
Let's try with PyArray_FromDims... segfault also :(
My code looks so simpleKK I cannot see what I'm doing the wrong way :(
#include <python2.4/Python.h>
#include <numpy/arrayobject.h>
int
main (int argc, char *argv[])
{
PyArrayObject *array;
int i, length = 100;
double* a;
double x;
Py_Initialize ();
array = (PyArrayObject *)
PyArray_FromDims(1, &length, PyArray_DOUBLE);
a = new double[100];
for (i = 0; i < length; i++) {
x = (double) i/(length-1);
a[i] = x;
}
Py_Finalize ();
return 0;
}
g++ imshow.cpp -o imshow -lpython2.4
-I/usr/lib/python2.4/site-packages/numpy/core/include/
./imshow -> Segmentation fault
I really need the Py_Initialize/Py_Finalize calls because it is
'embedding python' (and not extending :)).
To sum up, any code taking an double* and 2 int standind for the dimX
and the dimY and returning the corresponding PyObject would be very
much appreciated :) I don't care if it has to copy the data. The simpler
the better.
In [2]: numpy.__version__
Out[2]: '1.0.2.dev3491'
Xavier.
ps : I really want to use only the C API first at all to learn it and
because it really looks like as the simple way to fit my needs. Ok
PyObject_CallObject needs quite a lot of additionnal code but it is
always almost the same :)
--
############################################
Xavier Gnata
CRAL - Observatoire de Lyon
9, avenue Charles André
69561 Saint Genis Laval cedex
Phone: +33 4 78 86 85 28
Fax: +33 4 78 86 83 86
E-mail: gnata at obs.univ-lyon1.fr
############################################
More information about the Numpy-discussion
mailing list