summaryrefslogtreecommitdiff
path: root/Modules
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2020-05-01 11:33:44 +0200
committerGitHub <noreply@github.com>2020-05-01 11:33:44 +0200
commit252346acd937ddba4845331994b8ff4f90349625 (patch)
tree71d6298db81733d652abaa510c09ba90078efff0 /Modules
parent8bcfd31cc01e068bca78aa42a87c24aea6ebc6b1 (diff)
downloadcpython-git-252346acd937ddba4845331994b8ff4f90349625.tar.gz
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.c8
-rw-r--r--Modules/_threadmodule.c8
-rw-r--r--Modules/_winapi.c8
-rw-r--r--Modules/_xxsubinterpretersmodule.c14
-rw-r--r--Modules/posixmodule.c7
5 files changed, 37 insertions, 8 deletions
diff --git a/Modules/_posixsubprocess.c b/Modules/_posixsubprocess.c
index 60dd78d92a..add2962189 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 b3d90b22c5..77baba4847 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 1b28adb0b3..e1672c4785 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 15e80559ec..de11c09087 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 3d3f6ac969..0163b0757a 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) {