Skip to content

Commit b26b84c

Browse files
committed
pythongh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives
1 parent 6716254 commit b26b84c

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

Modules/_xxsubinterpretersmodule.c

+8-14
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ add_new_exception(PyObject *mod, const char *name, PyObject *base)
6161
static int
6262
_release_xid_data(_PyCrossInterpreterData *data, int ignoreexc)
6363
{
64-
PyObject *exctype, *excval, *exctb;
64+
PyObject *exc;
6565
if (ignoreexc) {
66-
PyErr_Fetch(&exctype, &excval, &exctb);
66+
exc = PyErr_GetRaisedException();
6767
}
6868
int res = _PyCrossInterpreterData_Release(data);
6969
if (res < 0) {
@@ -84,7 +84,7 @@ _release_xid_data(_PyCrossInterpreterData *data, int ignoreexc)
8484
}
8585
}
8686
if (ignoreexc) {
87-
PyErr_Restore(exctype, excval, exctb);
87+
PyErr_SetRaisedException(exc);
8888
}
8989
return res;
9090
}
@@ -294,17 +294,17 @@ _sharedexception_free(_sharedexception *exc)
294294
}
295295

296296
static _sharedexception *
297-
_sharedexception_bind(PyObject *exctype, PyObject *exc, PyObject *tb)
297+
_sharedexception_bind(PyObject *exc)
298298
{
299-
assert(exctype != NULL);
299+
assert(exc != NULL);
300300
char *failure = NULL;
301301

302302
_sharedexception *err = _sharedexception_new();
303303
if (err == NULL) {
304304
goto finally;
305305
}
306306

307-
PyObject *name = PyUnicode_FromFormat("%S", exctype);
307+
PyObject *name = PyUnicode_FromFormat("%S", Py_TYPE(exc));
308308
if (name == NULL) {
309309
failure = "unable to format exception type name";
310310
goto finally;
@@ -432,10 +432,7 @@ static int
432432
_run_script(PyInterpreterState *interp, const char *codestr,
433433
_sharedns *shared, _sharedexception **exc)
434434
{
435-
PyObject *exctype = NULL;
436435
PyObject *excval = NULL;
437-
PyObject *tb = NULL;
438-
439436
PyObject *main_mod = _PyInterpreterState_GetMainModule(interp);
440437
if (main_mod == NULL) {
441438
goto error;
@@ -469,12 +466,9 @@ _run_script(PyInterpreterState *interp, const char *codestr,
469466
return 0;
470467

471468
error:
472-
PyErr_Fetch(&exctype, &excval, &tb);
473-
474-
_sharedexception *sharedexc = _sharedexception_bind(exctype, excval, tb);
475-
Py_XDECREF(exctype);
469+
excval = PyErr_GetRaisedException();
470+
_sharedexception *sharedexc = _sharedexception_bind(excval);
476471
Py_XDECREF(excval);
477-
Py_XDECREF(tb);
478472
if (sharedexc == NULL) {
479473
fprintf(stderr, "RunFailedError: script raised an uncaught exception");
480474
PyErr_Clear();

0 commit comments

Comments
 (0)