10000 gh-109860: Use a New Thread State When Switching Interpreters, When Necessary by ericsnowcurrently · Pull Request #110245 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-109860: Use a New Thread State When Switching Interpreters, When Necessary #110245

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

Merged
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix handling of no-memory in _sharedexception_bind().
  • Loading branch information
ericsnowcurrently committed Oct 2, 2023
commit 845c2bd36c1bdf96dba060329b07a6f05f0225bd
19 changes: 14 additions & 5 deletions Modules/_xxsubinterpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ _sharedns_apply(_sharedns *shared, PyObject *ns)
typedef struct _sharedexception {
PyInterpreterState *interp;
#define ERR_NOT_SET 0
#define ERR_ALREADY_RUNNING 1
#define ERR_NO_MEMORY 1
#define ERR_ALREADY_RUNNING 2
int code;
const char *name;
const char *msg;
Expand Down Expand Up @@ -286,6 +287,7 @@ _sharedexception_bind(PyObject *exc, int code, _sharedexception *sharedexc)
PyObject *nameobj = PyUnicode_FromString(Py_TYPE(exc)->tp_name);
if (nameobj == NULL) {
failure = "unable to format exception type name";
code = ERR_NO_MEMORY;
goto error;
}
sharedexc->name = _copy_raw_string(nameobj);
Expand All @@ -296,13 +298,15 @@ _sharedexception_bind(PyObject *exc, int code, _sharedexception *sharedexc)
} else {
failure = "unable to encode and copy exception type name";
}
code = ERR_NO_MEMORY;
goto error;
}

if (exc != NULL) {
PyObject *msgobj = PyUnicode_FromFormat("%S", exc);
if (msgobj == NULL) {
failure = "unable to format exception message";
code = ERR_NO_MEMORY;
goto error;
}
sharedexc->msg = _copy_raw_string(msgobj);
Expand All @@ -313,18 +317,21 @@ _sharedexception_bind(PyObject *exc, int code, _sharedexception *sharedexc)
} else {
failure = "unable to encode and copy exception message";
}
code = ERR_NO_MEMORY;
goto error;
}
}
PyErr_NoMemory();

return NULL;

error:
assert(failure != NULL);
PyErr_Clear();
_sharedexception_clear(sharedexc);
*sharedexc = no_exception;
*sharedexc = (_sharedexception){
.interp = sharedexc->interp,
.code = code,
};
return failure;
}

Expand All @@ -344,6 +351,9 @@ _sharedexception_apply(_sharedexception *exc, PyObject *wrapperclass)
assert(exc->code == ERR_NOT_SET);
PyErr_SetString(wrapperclass, exc->msg);
}
else if (exc->code == ERR_NO_MEMORY) {
PyErr_NoMemory();
}
else if (exc->code == ERR_ALREADY_RUNNING) {
assert(exc->interp != NULL);
assert(_PyInterpreterState_IsRunningMain(exc->interp));
Expand Down Expand Up @@ -439,13 +449,12 @@ _run_script(PyInterpreterState *interp, const char *codestr,
fprintf(stderr,
"RunFailedError: script raised an uncaught exception (%s)",
failure);
PyErr_Clear();
}
Py_XDECREF(excval);
assert(!PyErr_Occurred());
if (errcode != ERR_ALREADY_RUNNING) {
_PyInterpreterState_SetNotRunningMain(interp);
}
assert(!PyErr_Occurred());
return -1;
}

Expand Down
0