Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives in sub interpreters module #102472

Merged
merged 1 commit into from
Mar 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions Modules/_xxsubinterpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ add_new_exception(PyObject *mod, const char *name, PyObject *base)
static int
_release_xid_data(_PyCrossInterpreterData *data, int ignoreexc)
{
PyObject *exctype, *excval, *exctb;
PyObject *exc;
if (ignoreexc) {
PyErr_Fetch(&exctype, &excval, &exctb);
exc = PyErr_GetRaisedException();
}
int res = _PyCrossInterpreterData_Release(data);
if (res < 0) {
Expand All @@ -84,7 +84,7 @@ _release_xid_data(_PyCrossInterpreterData *data, int ignoreexc)
}
}
if (ignoreexc) {
PyErr_Restore(exctype, excval, exctb);
PyErr_SetRaisedException(exc);
}
return res;
}
Expand Down Expand Up @@ -294,17 +294,17 @@ _sharedexception_free(_sharedexception *exc)
}

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;
Expand Down Expand Up @@ -432,10 +432,7 @@ static int
_run_script(PyInterpreterState *interp, const char *codestr,
_sharedns *shared, _sharedexception **exc)
{
PyObject *exctype = NULL;
PyObject *excval = NULL;
PyObject *tb = NULL;

PyObject *main_mod = _PyInterpreterState_GetMainModule(interp);
if (main_mod == NULL) {
goto error;
Expand Down Expand Up @@ -469,12 +466,9 @@ _run_script(PyInterpreterState *interp, const char *codestr,
return 0;

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();
Expand Down