Embedding a Numeric array in C
Chris Fonnesbeck
spam at fisher.forestry.uga.edu
Tue May 21 17:52:54 EDT 2002
Thanks for the help ... well, I now thave the following code:
#include <Python.h>
#include <Numeric/arrayobject.h>
static PyMethodDef numpy_methods[] = {
{NULL, NULL}
};
int main() {
PyObject *pmod, *pdict;
PyArrayObject *parray;
char *debug;
int dimensions[1];
Py_Initialize();
debug = "foobar";
pmod = Py_InitModule("Numeric",numpy_methods);
import_array();
printf("%s\n", debug);
pdict = PyModule_GetDict(pmod);
printf("%s\n", debug);
dimensions[0]=10;
parray = (PyArrayObject
*)PyArray_FromDims(1,dimensions,PyArray_DOUBLE);
printf("%s\n", debug);
return 0;
}
However, this segfaults at the PyArray_FromDims() call. Clearly I am
missing the boat here somewhere. I also do not see how I can use the
Numeric module without calling Py_InitModule().
Thanks.
cjf
Pete Shinners <shredwheat at attbi.com> wrote in message news:<3CE9D56C.1090007 at attbi.com>...
> Chris Fonnesbeck wrote:
> > I am trying to find out how to properly embed a Numeric array type in
> > C. The following code compiles, but segfaults when run:
> >
> > #include <Python.h>
> > #include <pythonrun.h>
> > #include <Numeric/arrayobject.h>
> >
> > int main() {
> >
> > PyObject *pmod;
> > char *cstr;
> > Py_Initialize();
> >
> > pmod = PyImport_ImportModule("Numeric");
> > Py_DECREF(pmod);
> > return 0;
> >
> > }
>
> in your "init" function, you need to make a call to "import_array()".
> import_array is actually a 'magic macro' that does a sort of "runtime
> linking" in python. if you do not make this call, all the C functions from
> Numeric will only be NULL (and hence a crash)
>
> no need for the PyImpoert_ImportModule()
More information about the Python-list
mailing list