在没有清除的情况下调用PyErr_SetString两次

时间:2012-04-14 07:46:45

标签: c++ exception-handling cpython

我有一个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来提供更详细的消息。它会导致泄漏吗?

0 个答案:

没有答案