8000 gh-99741: Clean Up the _xxsubinterpreters Module by ericsnowcurrently · Pull Request #99940 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-99741: Clean Up the _xxsubinterpreters Module #99940

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
Import _xxsubinterpreters in a subinterpreter if necessary.
  • Loading branch information
ericsnowcurrently committed Dec 2, 2022
commit 51645f773800c06a1108344a4456a575ba4d7ec6
40 changes: 30 additions & 10 deletions Modules/_xxsubinterpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ _get_current_interp(void)
static PyObject *
_get_current_module(void)
{
// It might not have been imported yet in the current interpreter.
// However, it will (almost) always have been imported already
// in the main interpreter.
// XXX Do the import in _sharedns_apply()
// and use PyImport_GetModule() here instead.
PyObject *mod = PyImport_ImportModule(MODULE_NAME);
// We ensured it was imported in _run_script().
PyObject *name = PyUnicode_FromString(MODULE_NAME);
if (name == NULL) {
return NULL;
}
PyObject *mod = PyImport_GetModule(name);
Py_DECREF(name);
if (mod == NULL) {
return NULL;
}
Expand Down Expand Up @@ -180,9 +181,12 @@ _sharedns_free(_sharedns *shared)
PyMem_Free(shared);
}

static PyTypeObject ChannelIDtype;

static _sharedns *
_get_shared_ns(PyObject *shareable)
_get_shared_ns(PyObject *shareable, int *needs_import)
{
*needs_import = 0;
if (shareable == NULL || shareable == Py_None) {
return NULL;
}
Expand All @@ -204,6 +208,9 @@ _get_shared_ns(PyObject *shareable)
if (_sharednsitem_init(&shared->items[i], key, value) != 0) {
break;
}
if (Py_TYPE(value) == &ChannelIDtype) {
*needs_import = 1;
}
}
if (PyErr_Occurred()) {
_sharedns_free(shared);
Expand Down Expand Up @@ -2021,12 +2028,24 @@ _ensure_not_running(PyInterpreterState *interp)

static int
_run_script(PyInterpreterState *interp, const char *codestr,
_sharedns *shared, _sharedexception **exc)
_sharedns *shared, int needs_import,
_sharedexception **exc)
{
PyObject *exctype = NULL;
PyObject *excval = NULL;
PyObject *tb = NULL;

if (needs_import) {
// It might not have been imported yet in the current interpreter.
// However, it will (almost) always have been imported already
// in the main interpreter.
PyObject *mod = PyImport_ImportModule(MODULE_NAME);
if (mod == NULL) {
goto error;
}
Py_DECREF(mod);
}

PyObject *main_mod = _PyInterpreterState_GetMainModule(interp);
if (main_mod == NULL) {
goto error;
Expand Down Expand Up @@ -2086,7 +2105,8 @@ _run_script_in_interpreter(PyObject *mod, PyInterpreterState *interp,
return -1;
}

_sharedns *shared = _get_shared_ns(shareables);
int needs_import = 0;
_sharedns *shared = _get_shared_ns(shareables, &needs_import);
if (shared == NULL && PyErr_Occurred()) {
return -1;
}
Expand All @@ -2102,7 +2122,7 @@ _run_script_in_interpreter(PyObject *mod, PyInterpreterState *interp,

// Run the script.
_sharedexception *exc = NULL;
int result = _run_script(interp, codestr, shared, &exc);
int result = _run_script(interp, codestr, shared, needs_import, &exc);

// Switch back.
if (save_tstate != NULL) {
Expand Down
0