From 3658e818ce82adcee556a9bd1e944aace6350907 Mon Sep 17 00:00:00 2001 From: Anthony Shaw Date: Wed, 19 Apr 2023 19:47:50 +1000 Subject: [PATCH] Example no-site flag for subinterpreters module --- Include/cpython/initconfig.h | 7 +++++-- Modules/_xxsubinterpretersmodule.c | 12 ++++++------ Python/pylifecycle.c | 5 +++-- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/Include/cpython/initconfig.h b/Include/cpython/initconfig.h index 00831e0f84c955..d52a84691dbc03 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 no_site; } _PyInterpreterConfig; -#define _PyInterpreterConfig_INIT \ +#define _PyInterpreterConfig_INIT(nosite) \ { \ .use_main_obmalloc = 0, \ .allow_fork = 0, \ @@ -264,9 +265,10 @@ typedef struct { .allow_daemon_threads = 0, \ .check_multi_interp_extensions = 1, \ .own_gil = 1, \ + .no_site = nosite, \ } -#define _PyInterpreterConfig_LEGACY_INIT \ +#define _PyInterpreterConfig_LEGACY_INIT(nosite) \ { \ .use_main_obmalloc = 1, \ .allow_fork = 1, \ @@ -275,6 +277,7 @@ typedef struct { .allow_daemon_threads = 1, \ .check_multi_interp_extensions = 0, \ .own_gil = 0, \ + .no_site = nosite, \ } /* --- Helper functions --------------------------------------- */ diff --git a/Modules/_xxsubinterpretersmodule.c b/Modules/_xxsubinterpretersmodule.c index 0e45f0a37c44c5..636fe3051513cf 100644 --- a/Modules/_xxsubinterpretersmodule.c +++ b/Modules/_xxsubinterpretersmodule.c @@ -504,10 +504,10 @@ 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", "nosite", NULL}; + int isolated = 1, nosite=1; + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|$ii:create", kwlist, + &isolated, &nosite)) { return NULL; } @@ -515,8 +515,8 @@ interp_create(PyObject *self, PyObject *args, PyObject *kwds) PyThreadState *save_tstate = _PyThreadState_GET(); assert(save_tstate != NULL); const _PyInterpreterConfig config = isolated - ? (_PyInterpreterConfig)_PyInterpreterConfig_INIT - : (_PyInterpreterConfig)_PyInterpreterConfig_LEGACY_INIT; + ? (_PyInterpreterConfig)_PyInterpreterConfig_INIT(nosite) + : (_PyInterpreterConfig)_PyInterpreterConfig_LEGACY_INIT(nosite); // XXX Possible GILState issues? PyThreadState *tstate = NULL; PyStatus status = _Py_NewInterpreterFromConfig(&tstate, &config); diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 8ae90559a35cac..a36bf7c706f4e2 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -634,7 +634,7 @@ pycore_create_interpreter(_PyRuntimeState *runtime, return status; } - _PyInterpreterConfig config = _PyInterpreterConfig_LEGACY_INIT; + _PyInterpreterConfig config = _PyInterpreterConfig_LEGACY_INIT(0); // The main interpreter always has its own GIL. config.own_gil = 1; status = init_interp_settings(interp, &config); @@ -2078,6 +2078,7 @@ new_interpreter(PyThreadState **tstate_p, const _PyInterpreterConfig *config) goto error; } + interp->config.site_import = !config->no_site; status = init_interp_main(tstate); if (_PyStatus_EXCEPTION(status)) { goto error; @@ -2115,7 +2116,7 @@ PyThreadState * Py_NewInterpreter(void) { PyThreadState *tstate = NULL; - const _PyInterpreterConfig config = _PyInterpreterConfig_LEGACY_INIT; + const _PyInterpreterConfig config = _PyInterpreterConfig_LEGACY_INIT(0); PyStatus status = _Py_NewInterpreterFromConfig(&tstate, &config); if (_PyStatus_EXCEPTION(status)) { Py_ExitStatusException(status);