@@ -602,23 +602,32 @@ const char* pesapi_get_exception_as_string(pesapi_scope pscope, int with_stack)
602602 {
603603 return nullptr ;
604604 }
605- auto ex = scope->caught ->ex ;
606605 auto globals = PyModule_GetDict (PyImport_AddModule (" __main__" ));
607- PyDict_SetItem (globals, PyUnicode_FromString (" __pesapi_last_exception" ), ex);
606+
607+ PyDict_SetItem (globals, PyUnicode_FromString (" __pesapi_last_exception" ), scope->caught ->value );
608+ #if !(PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 12)
609+ PyDict_SetItem (globals, PyUnicode_FromString (" __pesapi_last_exception" ), scope->caught ->value );
610+ PyDict_SetItem (globals, PyUnicode_FromString (" __pesapi_last_exception_tp" ), scope->caught ->type );
611+ PyDict_SetItem (globals, PyUnicode_FromString (" __pesapi_last_exception_tb" ), scope->caught ->traceback );
612+ #endif
613+
608614 const char * ret;
609615 if (with_stack)
610616 {
617+ #if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 12
611618 PyRun_SimpleString (
612619 " import traceback\n "
613- " try:\n "
614- " raise __pesapi_last_exception\n "
615- " except Exception as e:\n "
616- " __pesapi_last_exception_str = ''.join(traceback.format_exception(type(e), e, e.__traceback__))\n " );
620+ " __pesapi_last_exception_str = ''.join(traceback.format_exception(type(__pesapi_last_exception), __pesapi_last_exception, __pesapi_last_exception.__traceback__))\n " );
621+ #else
622+ PyRun_SimpleString (
623+ " import traceback\n "
624+ " __pesapi_last_exception_str = ''.join(traceback.format_exception(__pesapi_last_exception_tp, __pesapi_last_exception, __pesapi_last_exception_tb))\n " );
625+ #endif
617626 ret = PyUnicode_AsUTF8 (PyDict_GetItem (globals, PyUnicode_FromString (" __pesapi_last_exception_str" )));
618627 }
619628 else
620629 {
621- ret = PyUnicode_AsUTF8 (PyObject_Str (ex ));
630+ ret = PyUnicode_AsUTF8 (PyObject_Str (scope-> caught -> value ));
622631 }
623632 PyDict_DelItemString (globals, " __pesapi_last_exception" );
624633 return ret;
@@ -780,16 +789,13 @@ pesapi_value pesapi_call_function(
780789 else
781790 {
782791 auto scope = (pesapi_scope__*)mapper->getCurrentScope ();
783- #if PY_VERSION_HEX >= 0x030B0000
792+ #if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 12
784793 scope->setCaughtException (PyErr_GetRaisedException ());
785794#else
786795 {
787- PyObject *type = nullptr , *value = nullptr , *tb = nullptr ;
788- PyErr_Fetch (&type, &value, &tb);
789- PyObject *exc = value ? value : type;
790- if (exc) Py_XINCREF (exc);
791- PyErr_Restore (type, value, tb);
792- scope->setCaughtException (exc);
796+ PyObject *type = nullptr , *value = nullptr , *traceback = nullptr ;
797+ PyErr_Fetch (&type, &value, &traceback);
798+ scope->setCaughtException (type, value, traceback);
793799 }
794800#endif
795801 return nullptr ;
@@ -816,16 +822,13 @@ pesapi_value pesapi_eval(pesapi_env env, const uint8_t* code, size_t code_size,
816822 }
817823 }
818824 auto scope = (pesapi_scope__*)mapper->getCurrentScope ();
819- #if PY_VERSION_HEX >= 0x030B0000
825+ #if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 12
820826 scope->setCaughtException (PyErr_GetRaisedException ());
821827#else
822828 {
823- PyObject *type = nullptr , *value = nullptr , *tb = nullptr ;
824- PyErr_Fetch (&type, &value, &tb);
825- PyObject *exc = value ? value : type;
826- if (exc) Py_XINCREF (exc);
827- PyErr_Restore (type, value, tb);
828- scope->setCaughtException (exc);
829+ PyObject *type = nullptr , *value = nullptr , *traceback = nullptr ;
830+ PyErr_Fetch (&type, &value, &traceback);
831+ scope->setCaughtException (type, value, traceback);
829832 }
830833#endif
831834 return nullptr ;
0 commit comments