31#if PY_VERSION_HEX >= 0x030c0000
32 PyObject* pyvalue = PyErr_GetRaisedException();
34 PyObject* traceback = pyvalue ? PyException_GetTraceback(pyvalue) :
nullptr;
36 PyObject *pytype =
nullptr, *pyvalue =
nullptr, *pytrace =
nullptr;
37 PyErr_Fetch(&pytype, &pyvalue, &pytrace);
39 Py_XINCREF(traceback);
42 if (pytype && pyvalue) {
43 const char* tname = PyExceptionClass_Name(pytype);
45 char* dot = strrchr((
char*)tname,
'.');
52 PyObject* msg = PyObject_Str(pyvalue);
54 fMsg += PyUnicode_AsUTF8(msg);
63 while (traceback && traceback != Py_None) {
64 PyObject* frame = PyObject_GetAttrString(traceback,
"tb_frame");
65 PyObject* code = PyObject_GetAttrString(frame,
"f_code");
72 locFile = PyUnicode_AsUTF8(filenameStr);
73 Py_DECREF(filenameStr);
76 PyObject*
name = PyObject_GetAttrString(code,
"co_name");
78 locName = PyUnicode_AsUTF8(nameStr);
82 PyObject* lineno = PyObject_GetAttrString(traceback,
"tb_lineno");
83 locLine = PyLong_AsLong(lineno);
86 if (locFile ==
"<string>") {
87 PyObject* nextTraceback = PyObject_GetAttrString(traceback,
"tb_next");
89 traceback = nextTraceback;
96 Py_XDECREF(traceback);
98#if PY_VERSION_HEX >= 0x030c0000
99 PyErr_SetRaisedException(pyvalue);
101 PyErr_Restore(pytype, pyvalue, pytrace);
105 fMsg =
"python exception";
107 if (!locFile.empty()) {
110 locFile = locFile.substr(locFile.find_last_of(
"/\\") + 1);
112 fMsg +=
" (at " + locFile +
":" + std::to_string(locLine);
114 if (locName !=
"<module>")
115 fMsg +=
" in " + locName;