如何转换并保存发生在std :: string或wstring中的Python异常?这是我目前的代码:
String ScriptErrorHandler::the_error()
{
if(PyErr_Occurred())
{
PyObject *error_type, *the_error, *the_traceback, *py_error_string, *py_error_unicode;
PyErr_Fetch(&error_type, &the_error, &the_traceback);
PyErr_NormalizeException(&error_type, &the_error, &the_traceback);
if((error_type != NULL))
{
String the_error_string, the_traceback_string;
if (the_error != NULL)
{
py_error_string = PyObject_Str(the_error);
py_error_unicode = PyUnicode_FromObject(py_error_string);
const char* char_string = PyUnicode_AS_DATA(py_error_unicode);
auto char_length = PyUnicode_GET_DATA_SIZE(py_error_string);
the_error_string = String(char_string, char_length);
}
if(the_traceback != NULL)
auto the_traceback_string = PyBytes_AsString(the_traceback);
String str_error(the_error_string);
String str_traceback(the_traceback_string);
String message(str_error + "\n Traceback: " + str_traceback);
Py_XDECREF(error_type);
Py_XDECREF(the_error);
Py_XDECREF(the_traceback);
return message;
}
}
return StringUtil::BLANK;
}
调试显示the_error_string只包含'i',它是错误消息的第一个字符,应该是'invalid ..blabla'。
我做错了什么?
我正在使用Python 3.1