diff options
author | Victor Stinner <vstinner@python.org> | 2020-05-01 09:33:44 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-01 09:33:44 (GMT) |
commit | 252346acd937ddba4845331994b8ff4f90349625 (patch) | |
tree | 71d6298db81733d652abaa510c09ba90078efff0 /Modules | |
parent | 8bcfd31cc01e068bca78aa42a87c24aea6ebc6b1 (diff) | |
download | cpython-252346acd937ddba4845331994b8ff4f90349625.zip cpython-252346acd937ddba4845331994b8ff4f90349625.tar.gz cpython-252346acd937ddba4845331994b8ff4f90349625.tar.bz2 |
bpo-40453: Add PyConfig._isolated_subinterpreter (GH-19820)
An isolated subinterpreter cannot spawn threads, spawn a child
process or call os.fork().
* Add private _Py_NewInterpreter(isolated_subinterpreter) function.
* Add isolated=True keyword-only parameter to
_xxsubinterpreters.create().
* Allow again os.fork() in "non-isolated" subinterpreters.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_posixsubprocess.c | 8 | ||||
-rw-r--r-- | Modules/_threadmodule.c | 8 | ||||
-rw-r--r-- | Modules/_winapi.c | 8 | ||||
-rw-r--r-- | Modules/_xxsubinterpretersmodule.c | 14 | ||||
-rw-r--r-- | Modules/posixmodule.c | 7 |
5 files changed, 37 insertions, 8 deletions
diff --git a/Modules/_posixsubprocess.c b/Modules/_posixsubprocess.c index 60dd78d..add2962 100644 --- a/Modules/_posixsubprocess.c +++ b/Modules/_posixsubprocess.c @@ -663,6 +663,14 @@ subprocess_fork_exec(PyObject* self, PyObject *args) return NULL; } + PyInterpreterState *interp = PyInterpreterState_Get(); + const PyConfig *config = _PyInterpreterState_GetConfig(interp); + if (config->_isolated_interpreter) { + PyErr_SetString(PyExc_RuntimeError, + "subprocess not supported for isolated subinterpreters"); + return NULL; + } + /* We need to call gc.disable() when we'll be calling preexec_fn */ if (preexec_fn != Py_None) { PyObject *result; diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c index b3d90b2..77baba4 100644 --- a/Modules/_threadmodule.c +++ b/Modules/_threadmodule.c @@ -1085,6 +1085,14 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs) "optional 3rd arg must be a dictionary"); return NULL; } + + PyInterpreterState *interp = _PyInterpreterState_GET(); + if (interp->config._isolated_interpreter) { + PyErr_SetString(PyExc_RuntimeError, + "thread is not supported for isolated subinterpreters"); + return NULL; + } + boot = PyMem_NEW(struct bootstate, 1); if (boot == NULL) return PyErr_NoMemory(); diff --git a/Modules/_winapi.c b/Modules/_winapi.c index 1b28adb..e1672c4 100644 --- a/Modules/_winapi.c +++ b/Modules/_winapi.c @@ -1080,6 +1080,14 @@ _winapi_CreateProcess_impl(PyObject *module, return NULL; } + PyInterpreterState *interp = PyInterpreterState_Get(); + const PyConfig *config = _PyInterpreterState_GetConfig(interp); + if (config->_isolated_interpreter) { + PyErr_SetString(PyExc_RuntimeError, + "subprocess not supported for isolated subinterpreters"); + return NULL; + } + ZeroMemory(&si, sizeof(si)); si.StartupInfo.cb = sizeof(si); diff --git a/Modules/_xxsubinterpretersmodule.c b/Modules/_xxsubinterpretersmodule.c index 15e8055..de11c09 100644 --- a/Modules/_xxsubinterpretersmodule.c +++ b/Modules/_xxsubinterpretersmodule.c @@ -1999,16 +1999,20 @@ _global_channels(void) { } static PyObject * -interp_create(PyObject *self, PyObject *args) +interp_create(PyObject *self, PyObject *args, PyObject *kwds) { - if (!PyArg_UnpackTuple(args, "create", 0, 0)) { + + static char *kwlist[] = {"isolated", NULL}; + int isolated = 1; + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|$i:create", kwlist, + &isolated)) { return NULL; } // Create and initialize the new interpreter. PyThreadState *save_tstate = PyThreadState_Swap(NULL); // XXX Possible GILState issues? - PyThreadState *tstate = Py_NewInterpreter(); + PyThreadState *tstate = _Py_NewInterpreter(isolated); PyThreadState_Swap(save_tstate); if (tstate == NULL) { /* Since no new thread state was created, there is no exception to @@ -2547,8 +2551,8 @@ channel__channel_id(PyObject *self, PyObject *args, PyObject *kwds) } static PyMethodDef module_functions[] = { - {"create", (PyCFunction)interp_create, - METH_VARARGS, create_doc}, + {"create", (PyCFunction)(void(*)(void))interp_create, + METH_VARARGS | METH_KEYWORDS, create_doc}, {"destroy", (PyCFunction)(void(*)(void))interp_destroy, METH_VARARGS | METH_KEYWORDS, destroy_doc}, {"list_all", interp_list_all, diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 3d3f6ac..0163b07 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -6243,9 +6243,10 @@ os_fork_impl(PyObject *module) /*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/ { pid_t pid; - - if (_PyInterpreterState_GET() != PyInterpreterState_Main()) { - PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); + PyInterpreterState *interp = _PyInterpreterState_GET(); + if (interp->config._isolated_interpreter) { + PyErr_SetString(PyExc_RuntimeError, + "fork not supported for isolated subinterpreters"); return NULL; } if (PySys_Audit("os.fork", NULL) < 0) { |