python&c-c++扩展模块case-segmentfault
c++代码python&c-c++扩展模块case-segmentfault,python,c++,c,Python,C++,C,c++代码 extern "C" PyObject * test(){ PyObject *oplist = PyList_New(10000); for(uint32_t j = 0; j < 10000; j++){ PyObject* pTuple = PyTuple_New(3); assert(PyTuple_Check(pTuple)); assert(PyTuple_Size(pTuple) == 3);
extern "C" PyObject * test(){
PyObject *oplist = PyList_New(10000);
for(uint32_t j = 0; j < 10000; j++){
PyObject* pTuple = PyTuple_New(3);
assert(PyTuple_Check(pTuple));
assert(PyTuple_Size(pTuple) == 3);
PyTuple_SetItem(pTuple, 0, Py_BuildValue("s", "b"));
PyTuple_SetItem(pTuple, 1, Py_BuildValue("i", 1));
PyTuple_SetItem(pTuple, 2, Py_BuildValue("s", "a"));
PyList_SetItem(oplist, j, pTuple);
}
return oplist;
}
buildcmd
g++-fPIC token2map.cpp-I/usr/local/app/service/virtualenvs/NLP/include/python2.7-shared-o token2map_lib.so
我只展示了一部分代码,请原谅,代码太长了
问题:
在C++代码中,这个函数返回RESILIST是SLAMAR,一切都可以。结果集永远超过215j=215个案例段FALUT。我找不到问题,希望现在的朋友能给我一些建议,我会非常感激的
我有办法解决这个问题
extern "C" PyObject * test(){
PyGILState_STATE gstate = PyGILState_Ensure();
PyObject *oplist = PyTuple_New(10000);
for(int32_t j = 0; j < 10000; j++){
PyObject * pTuple = PyTuple_New(3);
assert(PyTuple_Check(pTuple));
assert(PyTuple_Size(pTuple) == 3);
PyTuple_SetItem(pTuple, 0, Py_BuildValue("s", "b"));
PyTuple_SetItem(pTuple, 1, Py_BuildValue("i", 1));
PyTuple_SetItem(pTuple, 2, Py_BuildValue("s", "a"));
PyTuple_SetItem(oplist, j, pTuple);
}
PyGILState_Release(gstate);
return oplist;
}
但是有没有办法解决这个问题呢?我不认为get GIL locak是一个好方法我无法解释为什么代码会出现seg错误。我确实觉得有趣的是,您能够使用不带参数的函数构建扩展。但是,我可以提供构建和工作的代码:
#define Py_SSIZE_T_CLEAN
#include <Python.h>
#include <inttypes.h>
#ifdef __cplusplus
extern "C" {
#endif
static PyObject *test(PyObject *self, PyObject *ignorethis)
{
PyObject *oplist = PyList_New(10000);
for(uint32_t j = 0; j < 10000; ++j){
PyObject *pTuple = PyTuple_New(3);
PyTuple_SetItem(pTuple, 0, Py_BuildValue("s", "b"));
PyTuple_SetItem(pTuple, 1, Py_BuildValue("i", 1));
PyTuple_SetItem(pTuple, 2, Py_BuildValue("s", "a"));
PyList_SetItem(oplist, j, pTuple);
}
return oplist;
}
static PyMethodDef methods[] = {
{"test", test, METH_NOARGS, "function given by so"}
};
static PyModuleDef foobar = {
PyModuleDef_HEAD_INIT,
"foobar",
"so question module",
-1,
methods
};
PyMODINIT_FUNC PyInit_foobar(void){
PyObject *module;
module = PyModule_Create(&foobar);
return module;
}
#ifdef __cplusplus
}
#endif
您可以使用import foobar加载此模块,然后使用foobar运行。test您永远不会检查这些函数的返回值。如文档所述,如果每个Python API函数返回值,您必须始终检查它的每个返回值。断言不是进行检查的正确方法。无论如何,这个问题似乎没有答案,甚至没有解释函数是如何调用的,因此我投票关闭
#define Py_SSIZE_T_CLEAN
#include <Python.h>
#include <inttypes.h>
#ifdef __cplusplus
extern "C" {
#endif
static PyObject *test(PyObject *self, PyObject *ignorethis)
{
PyObject *oplist = PyList_New(10000);
for(uint32_t j = 0; j < 10000; ++j){
PyObject *pTuple = PyTuple_New(3);
PyTuple_SetItem(pTuple, 0, Py_BuildValue("s", "b"));
PyTuple_SetItem(pTuple, 1, Py_BuildValue("i", 1));
PyTuple_SetItem(pTuple, 2, Py_BuildValue("s", "a"));
PyList_SetItem(oplist, j, pTuple);
}
return oplist;
}
static PyMethodDef methods[] = {
{"test", test, METH_NOARGS, "function given by so"}
};
static PyModuleDef foobar = {
PyModuleDef_HEAD_INIT,
"foobar",
"so question module",
-1,
methods
};
PyMODINIT_FUNC PyInit_foobar(void){
PyObject *module;
module = PyModule_Create(&foobar);
return module;
}
#ifdef __cplusplus
}
#endif