我试图使用Python C api和numpy以及Python3,它在import_array()中抛出一个seg错误。我尝试使用gdb调试它,它为PyModule_GetDef()显示了一个seg错误。这是我的代码
#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();
}
之前还有其他人遇到过这类问题吗?