8000 bpo-30439: Add some helpful low-level functions for subinterpreters. by ericsnowcurrently · Pull Request #1802 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-30439: Add some helpful low-level functions for subinterpreters. #1802

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

Closed
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
8eb5adf
Add the _interpreters module to the stdlib.
ericsnowcurrently Dec 14, 2016
457567f
Add create() and destroy().
ericsnowcurrently Dec 29, 2016
e6af9f3
Finish nearly all the create/destroy tests.
ericsnowcurrently Jan 1, 2017
335967e
Add run_string().
ericsnowcurrently Dec 29, 2016
d2df973
Get tricky tests working.
ericsnowcurrently Jan 2, 2017
b70a496
Add a test for a still running interpreter when main exits.
ericsnowcurrently Jan 2, 2017
6f2d28f
Add run_string_unrestricted().
ericsnowcurrently Jan 4, 2017
80a931b
Exit out of the child process.
ericsnowcurrently Jan 4, 2017
6aa7d9c
Resolve several TODOs.
ericsnowcurrently Jan 4, 2017
083e13c
Set up the execution namespace before switching threads.
ericsnowcurrently Jan 4, 2017
ce081a7
Run in a copy of __main__.
ericsnowcurrently Jan 4, 2017
ec05cf5
Close stdin and stdout after the proc finishes.
ericsnowcurrently Jan 4, 2017
fe90466
Clean up a test.
ericsnowcurrently Jan 4, 2017
9082017
Chain exceptions during cleanup.
ericsnowcurrently Jan 4, 2017
21865d4
Finish the module docs.
ericsnowcurrently Jan 4, 2017
0342a4f
Fix docs.
ericsnowcurrently May 23, 2017
e9d9b04
Fix includes.
ericsnowcurrently Dec 5, 2017
722ae94
Add _interpreters.is_shareable().
ericsnowcurrently Nov 28, 2017
061ae13
Add _PyObject_CheckShareable().
ericsnowcurrently Dec 4, 2017
9a365ec
Add _PyCrossInterpreterData.
ericsnowcurrently Dec 4, 2017
8f299d4
Use the shared data in run() safely.
ericsnowcurrently Dec 7, 2017
92029a1
Do not use a copy of the __main__ ns.
ericsnowcurrently Dec 7, 2017
52c9c2f
Never return the execution namespace.
ericsnowcurrently Dec 8, 2017
a797883
Group sharing-related code.
ericsnowcurrently Dec 8, 2017
777838a
Fix a refcount.
ericsnowcurrently Dec 8, 2017
ab8f175
Add get_current() and enumerate().
ericsnowcurrently Dec 29, 2016
c50c51c
Add is_running().
ericsnowcurrently Dec 29, 2016
271d20d
Add get_main().
ericsnowcurrently Jan 4, 2017
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
Do not use a copy of the __main__ ns.
  • Loading branch information
ericsnowcurrently committed Dec 7, 2017
commit 92029a12c8dac316a338347d1c54c8a068501b5e
102 changes: 7 additions & 95 deletions Modules/_interpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,81 +168,6 @@ _ensure_not_running(PyInterpreterState *interp)
return 0;
}

static PyObject *
_copy_module(PyObject *m, const char *name)
{
PyObject *orig = PyModule_GetDict(m); // borrowed
if (orig == NULL) {
return NULL;
}

PyObject *copy = PyModule_New(name);
if (copy == NULL) {
return NULL;
}
PyObject *ns = PyModule_GetDict(copy); // borrowed
if (ns == NULL)
goto error;

if (PyDict_Merge(ns, orig, 1) < 0)
goto error;
return copy;

error:
Py_DECREF(copy);
return NULL;
}

static PyObject *
_copy_module_ns(PyInterpreterState *interp,
const char *name, const char *tempname)
{
// Get the namespace in which to execute. This involves creating
// a new module, updating it from the __main__ module and the given
// updates (if any), replacing the __main__ with the new module in
// sys.modules, and then using the new module's __dict__. At the
// end we restore the original __main__ module.
PyObject *main_mod = PyMapping_GetItemString(interp->modules, name);
if (main_mod == NULL)
return NULL;
PyObject *m = _copy_module(main_mod, name);
if (m == NULL) {
Py_DECREF(main_mod);
return NULL;
}
if (tempname != NULL) {
if (PyMapping_SetItemString(interp->modules, tempname, main_mod) < 0) {
Py_DECREF(main_mod);
Py_DECREF(m);
return NULL;
}
}
Py_DECREF(main_mod);
if (PyMapping_SetItemString(interp->modules, name, m) < 0) {
Py_DECREF(m);
return NULL;
}
PyObject *ns = PyModule_GetDict(m); // borrowed
Py_INCREF(ns);
Py_DECREF(m);
return ns;
}

static int
_restore_module(PyInterpreterState *interp,
const char *name, const char *tempname)
{
PyObject *main_mod = PyMapping_GetItemString(interp->modules, tempname);
if (main_mod == NULL)
return -1;
if (PyMapping_SetItemString(interp->modules, name, main_mod) < 0) {
Py_DECREF(main_mod);
return -1;
}
Py_DECREF(main_mod);
return 0;
}

struct _shareditem {
Py_UNICODE *name;
Py_ssize_t namelen;
Expand Down Expand Up @@ -344,27 +269,20 @@ static PyObject *
_run_script(PyInterpreterState *interp, const char *codestr,
struct _shareditem *shared, struct _shared_exception **exc)
{
// XXX Do not copy.

// Get a copy of the __main__ module.
//
// This involves creating a new module, updating it from the
// __main__ module and the given updates (if any), replacing the
// __main__ with the new module in sys.modules, and then using the
// new module's __dict__. At the end we restore the original
// __main__ module.
PyObject *ns = _copy_module_ns(interp, "__main__", "_orig___main___");
if (ns == NULL) {
PyObject *main_mod = PyMapping_GetItemString(interp->modules, "__main__");
if (main_mod == NULL)
return NULL;
PyObject *ns = PyModule_GetDict(main_mod); // borrowed
Py_DECREF(main_mod);
if (ns == NULL)
return NULL;
}

// Apply the cross-interpreter data.
if (shared != NULL) {
for (struct _shareditem *item=shared; item->name != NULL; item += 1) {
if (_shareditem_apply(shared, ns) != 0) {
Py_DECREF(ns);
ns = NULL;
goto done;
return NULL;
}
}
}
Expand All @@ -379,12 +297,6 @@ _run_script(PyInterpreterState *interp, const char *codestr,
Py_DECREF(result); // We throw away the result.
}

done:
// Restore __main__.
if (_restore_module(interp, "__main__", "_orig___main___") != 0) {
// XXX How to propagate this exception...
//_PyErr_ChainExceptions(exc, value, tb);
}
return ns;
}

Expand Down
0