我有一个C ++函数来解析python字符串:
std::string parse_string(PyObject* py_string) {
std::string out;
if (!PyString_Check(py_string)) {
PyErr_SetString(PyExc_TypeError,"expected a string");
return out;
}
out = PyString_AsString(py_string);
return out;
}
我正在从python包装器调用该函数:
PyObject* some_func(PyObject* self, PyObject* args) {
// ...
std::string my_first_string = parse_string(first_py_string);
if (PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError,"more verbose error for this string");
return 0;
}
std::string my_second_string = parse_string(second_py_string);
if (PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError,"some other error for this string");
return 0;
}
// ...
}
这将根据需要抛出一个python异常,但我担心再次调用PyErr_SetString
来提供更详细的消息。它会导致泄漏吗?