1

発生した Python 例外を std::string または wstring に変換して保存するにはどうすればよいですか? これは私の現在のコードです:

    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を使用しています

4

0 に答える 0