diff --git a/Include/cpython/initconfig.h b/Include/cpython/initconfig.h index 00831e0f84c955..3a9b189584857b 100644 --- a/Include/cpython/initconfig.h +++ b/Include/cpython/initconfig.h @@ -253,9 +253,10 @@ typedef struct { int allow_daemon_threads; int check_multi_interp_extensions; int own_gil; + int site_import; } _PyInterpreterConfig; -#define _PyInterpreterConfig_INIT \ +#define _PyInterpreterConfig_INIT(site) \ { \ .use_main_obmalloc = 0, \ .allow_fork = 0, \ @@ -264,6 +265,7 @@ typedef struct { .allow_daemon_threads = 0, \ .check_multi_interp_extensions = 1, \ .own_gil = 1, \ + .site_import = site, \ } #define _PyInterpreterConfig_LEGACY_INIT \ @@ -275,6 +277,7 @@ typedef struct { .allow_daemon_threads = 1, \ .check_multi_interp_extensions = 0, \ .own_gil = 0, \ + .site_import = 1, \ } /* --- Helper functions --------------------------------------- */ diff --git a/Modules/_xxsubinterpretersmodule.c b/Modules/_xxsubinterpretersmodule.c index 0e45f0a37c44c5..0039a4ae9f735b 100644 --- a/Modules/_xxsubinterpretersmodule.c +++ b/Modules/_xxsubinterpretersmodule.c @@ -503,19 +503,22 @@ _run_script_in_interpreter(PyObject *mod, PyInterpreterState *interp, static PyObject * interp_create(PyObject *self, PyObject *args, PyObject *kwds) { - - static char *kwlist[] = {"isolated", NULL}; - int isolated = 1; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|$i:create", kwlist, - &isolated)) { + static char *kwlist[] = {"isolated", "site", NULL}; + int isolated = 1, site=1; + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|$ii:create", kwlist, + &isolated, &site)) { + return NULL; + } + if (!isolated && !site){ + // no site requires isolated + PyErr_SetString(PyExc_ValueError, "isolated must be True when combined with site=False"); return NULL; } - // Create and initialize the new interpreter. PyThreadState *save_tstate = _PyThreadState_GET(); assert(save_tstate != NULL); const _PyInterpreterConfig config = isolated - ? (_PyInterpreterConfig)_PyInterpreterConfig_INIT + ? (_PyInterpreterConfig)_PyInterpreterConfig_INIT(site) : (_PyInterpreterConfig)_PyInterpreterConfig_LEGACY_INIT; // XXX Possible GILState issues? PyThreadState *tstate = NULL; diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 8ae90559a35cac..aa93f065996a97 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -581,6 +581,10 @@ init_interp_settings(PyInterpreterState *interp, const _PyInterpreterConfig *con interp->feature_flags |= Py_RTFLAGS_MULTI_INTERP_EXTENSIONS; } + if (!config->site_import) { + interp->config.site_import = 0; + } + return _PyStatus_OK(); }