问题描述:

I was trying to use the Python C api with numpy and Python3 and it is throwing a seg fault inside import_array(). I tried to debug it using gdb and it showed a seg fault for PyModule_GetDef() . This is my code

#include<Python.h>

#include <numpy/ndarrayobject.h>

static PyObject* testBasic(PyObject *self, PyObject *args)

{

printf("Successful \n");

return PyLong_FromLong(1);

}

static PyObject* testMmult(PyObject* self, PyObject *args) {

PyArrayObject* obj;

// if(!PyArg_ParseTuple(args, "O!",&PyArray_Type, &obj)){

// return NULL;

// }

/* if(!PyArg_ParseTuple(args,"i",&a)){

return NULL;

}

*/

// Py_INCREF(obj);

//PyArray_Return(obj);

return PyLong_FromLong(1);

}

static PyMethodDef testPythonMethods[] =

{

{"testBasic",testBasic,METH_VARARGS,"testing whether everything compiles."},

{"testMmult",testMmult,METH_VARARGS,"testing mmult"},

{NULL,NULL,0,NULL}

};

static struct PyModuleDef testPythonModule = {

PyModuleDef_HEAD_INIT,

"testPythonModule",

NULL,

-1,

testPythonMethods,

};

PyMODINIT_FUNC

PyInit_testPython(void){

(void)PyModule_Create(&testPythonModule);

import_array();

}

Anyone else faced this kind of problem before ?

相关阅读:
Top