gh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives in sub interpreters module by iritkatriel · Pull Request #102472 · python/cpython
static _sharedexception * _sharedexception_bind(PyObject *exctype, PyObject *exc, PyObject *tb) _sharedexception_bind(PyObject *exc) { assert(exctype != NULL); assert(exc != NULL); char *failure = NULL;
_sharedexception *err = _sharedexception_new(); if (err == NULL) { goto finally; }
PyObject *name = PyUnicode_FromFormat("%S", exctype); PyObject *name = PyUnicode_FromFormat("%S", Py_TYPE(exc)); if (name == NULL) { failure = "unable to format exception type name"; goto finally;
PyObject *main_mod = _PyInterpreterState_GetMainModule(interp); if (main_mod == NULL) { goto error;
error: PyErr_Fetch(&exctype, &excval, &tb);
_sharedexception *sharedexc = _sharedexception_bind(exctype, excval, tb); Py_XDECREF(exctype); excval = PyErr_GetRaisedException(); _sharedexception *sharedexc = _sharedexception_bind(excval); Py_XDECREF(excval); Py_XDECREF(tb); if (sharedexc == NULL) { fprintf(stderr, "RunFailedError: script raised an uncaught exception"); PyErr_Clear();