gpt4 book ai didi

python & c-c++扩展模块案例段错误

转载 作者:行者123 更新时间:2023-11-30 14:32:55 24 4
gpt4 key购买 nike

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);
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;
}

Python代码

LID = ctypes.CDLL('%s/token2map_lib.so' % '.')
LID.test.restype = py_object
LID.test()

构建cmd
g++ -fPIC token2map.cpp -I/usr/local/app/service/virtualenvs/NLP/include/python2.7 -shared -o token2map_lib.so
我只展示了部分代码,请原谅我,总代码太长

问题:在c++代码中,这个函数返回的res_list很小,一切正常。永远,结果集超过 215(j = 215) 个情况段falut。我找不到问题,希望现在的 friend 能给我一些建议,我将非常感激。

我有办法解决这个问题

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;
}

但是有其他方法可以解决这个问题吗?我不认为获得 GIL locak 是一个好方法

最佳答案

我无法向您解释为什么您的代码会出现段错误。我确实发现有趣的是,您能够使用不带参数的函数构建扩展。不过,我可以提供可以构建和工作的代码:

#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 & c-c++扩展模块案例段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59621664/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com