将失败,并抛出C++ OutuxOfLangeExt.,python,c++,python-c-api,python-extensions,Python,C++,Python C Api,Python Extensions" /> 将失败,并抛出C++ OutuxOfLangeExt.,python,c++,python-c-api,python-extensions,Python,C++,Python C Api,Python Extensions" />

Python在返回C++;映射值 我正在编写一个测试,以将数据保存在C++地图中。我正在使用Python C-Api。 添加int值并获取映射的长度没有问题。 但是当我从映射中获得一个值并想将其返回到Python时,解释器就会崩溃。 这是我的代码: //some includes int VerboseLog = 99; typedef map<const char*, int> SessionKeeper; static SessionKeeper openSessions; static PyObject* getSession(PyObject* self, PyObject* args) { cout << "get" << endl; const char *key; if (!PyArg_ParseTuple(args, "z*", &key)) { PyErr_SetString(PyExc_Exception, "UUID konnte nicht ausgelesen werden"); PyErr_PrintEx(1); return NULL; } int r = openSessions.at(key); return Py_BuildValue("i", r); } static PyObject *addSession(PyObject* self, PyObject* args) { cout << "Hello" << endl; const char *key; int toAppend; if (!PyArg_ParseTuple(args, "si", &key, &toAppend)) { PyErr_SetString(PyExc_Exception, "Ein Parameter konnte nicht ausgelesen werden"); PyErr_PrintEx(1); return NULL; } openSessions[key] = toAppend; cout << openSessions.size() << endl; cout << openSessions[key] << endl; Py_RETURN_NONE; } static PyObject* length(PyObject *self){ cout << "length: "; int i = openSessions.size(); return Py_BuildValue("i",i); } static PyMethodDef SessionKeeper_methods[] = { /*Note the third entry (METH_VARARGS). This is a flag telling the interpreter the calling convention to be used for the C function. It should normally always be METH_VARARGS or METH_VARARGS | METH_KEYWORDS; a value of 0 means that an obsolete variant of PyArg_ParseTuple() is used.*/ { "length", (PyCFunction)length, METH_VARARGS, "return length of a Session" }, { "addSession", (PyCFunction)addSession, METH_VARARGS, "add a Session." }, { "getSession", (PyCFunction)getSession, METH_VARARGS, "get a Session" }, { NULL } }; static PyModuleDef sessionKeeperMod = { PyModuleDef_HEAD_INIT, u8"SessionKeeper", NULL, -1, SessionKeeper_methods }; static PyModuleDef CpluplusModules_ModDef = { PyModuleDef_HEAD_INIT, u8"CPlusPlusMouldes", u8"Enthält alle Erweietrungen", -1, NULL, NULL, NULL, NULL, NULL }; PyMODINIT_FUNC PyInit_CPlusPlusModules(void) { PyObject* m, *sessionMod; if (PyType_Ready(&TAModulType) < 0) return NULL; if (PyType_Ready(&DatabaseReader_Type) < 0) return NULL; if (!(sessionMod = PyModule_Create(&sessionKeeperMod))) { cout << "fail" << endl; return NULL; } m = PyModule_Create(&CpluplusModules_ModDef); if (m == NULL) return NULL; Py_INCREF(&TAModulType); Py_INCREF(&DatabaseReader_Type); PyModule_AddObject(m, "TAModul", (PyObject *)&TAModulType); PyModule_AddObject(m, "DBReader", (PyObject *)&DatabaseReader_Type); PyModule_AddObject(m, "SessionKeeper", sessionMod); return m; } //有些包括 int VerboseLog=99; typedef地图会话管理员; 静态SessionKeeper开放会话; 静态PyObject*getSession(PyObject*self,PyObject*args){ 因此,很可能 >将失败,并抛出C++ OutuxOfLangeExt.

Python在返回C++;映射值 我正在编写一个测试,以将数据保存在C++地图中。我正在使用Python C-Api。 添加int值并获取映射的长度没有问题。 但是当我从映射中获得一个值并想将其返回到Python时,解释器就会崩溃。 这是我的代码: //some includes int VerboseLog = 99; typedef map<const char*, int> SessionKeeper; static SessionKeeper openSessions; static PyObject* getSession(PyObject* self, PyObject* args) { cout << "get" << endl; const char *key; if (!PyArg_ParseTuple(args, "z*", &key)) { PyErr_SetString(PyExc_Exception, "UUID konnte nicht ausgelesen werden"); PyErr_PrintEx(1); return NULL; } int r = openSessions.at(key); return Py_BuildValue("i", r); } static PyObject *addSession(PyObject* self, PyObject* args) { cout << "Hello" << endl; const char *key; int toAppend; if (!PyArg_ParseTuple(args, "si", &key, &toAppend)) { PyErr_SetString(PyExc_Exception, "Ein Parameter konnte nicht ausgelesen werden"); PyErr_PrintEx(1); return NULL; } openSessions[key] = toAppend; cout << openSessions.size() << endl; cout << openSessions[key] << endl; Py_RETURN_NONE; } static PyObject* length(PyObject *self){ cout << "length: "; int i = openSessions.size(); return Py_BuildValue("i",i); } static PyMethodDef SessionKeeper_methods[] = { /*Note the third entry (METH_VARARGS). This is a flag telling the interpreter the calling convention to be used for the C function. It should normally always be METH_VARARGS or METH_VARARGS | METH_KEYWORDS; a value of 0 means that an obsolete variant of PyArg_ParseTuple() is used.*/ { "length", (PyCFunction)length, METH_VARARGS, "return length of a Session" }, { "addSession", (PyCFunction)addSession, METH_VARARGS, "add a Session." }, { "getSession", (PyCFunction)getSession, METH_VARARGS, "get a Session" }, { NULL } }; static PyModuleDef sessionKeeperMod = { PyModuleDef_HEAD_INIT, u8"SessionKeeper", NULL, -1, SessionKeeper_methods }; static PyModuleDef CpluplusModules_ModDef = { PyModuleDef_HEAD_INIT, u8"CPlusPlusMouldes", u8"Enthält alle Erweietrungen", -1, NULL, NULL, NULL, NULL, NULL }; PyMODINIT_FUNC PyInit_CPlusPlusModules(void) { PyObject* m, *sessionMod; if (PyType_Ready(&TAModulType) < 0) return NULL; if (PyType_Ready(&DatabaseReader_Type) < 0) return NULL; if (!(sessionMod = PyModule_Create(&sessionKeeperMod))) { cout << "fail" << endl; return NULL; } m = PyModule_Create(&CpluplusModules_ModDef); if (m == NULL) return NULL; Py_INCREF(&TAModulType); Py_INCREF(&DatabaseReader_Type); PyModule_AddObject(m, "TAModul", (PyObject *)&TAModulType); PyModule_AddObject(m, "DBReader", (PyObject *)&DatabaseReader_Type); PyModule_AddObject(m, "SessionKeeper", sessionMod); return m; } //有些包括 int VerboseLog=99; typedef地图会话管理员; 静态SessionKeeper开放会话; 静态PyObject*getSession(PyObject*self,PyObject*args){ 因此,很可能 >将失败,并抛出C++ OutuxOfLangeExt.,python,c++,python-c-api,python-extensions,Python,C++,Python C Api,Python Extensions,你应该先把线包起来 int r = openSessions.at(key); 使用尝试{} catch < /Cord>块,以阻止 OutoFuxLeave/Cuth>异常通过Python解释器传播(这不是C++所以不能处理)。 然后,您应该更改映射以匹配字符串内容上的键。最简单的方法是使用map感谢这是问题的一部分,我还必须在解析函数中使用“s”instate或“z*”

你应该先把线包起来

int r = openSessions.at(key);

使用<代码>尝试{} catch < /Cord>块,以阻止<代码> OutoFuxLeave/Cuth>异常通过Python解释器传播(这不是C++所以不能处理)。


然后,您应该更改映射以匹配字符串内容上的键。最简单的方法是使用
map

感谢这是问题的一部分,我还必须在解析函数中使用“s”instate或“z*”