Skip to content

Commit

Permalink
Example no-site flag for subinterpreters module
Browse files Browse the repository at this point in the history
  • Loading branch information
tonybaloney committed Apr 19, 2023
1 parent e1dde48 commit 3658e81
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
7 changes: 5 additions & 2 deletions Include/cpython/initconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand All @@ -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, \
Expand All @@ -275,6 +277,7 @@ typedef struct {
.allow_daemon_threads = 1, \
.check_multi_interp_extensions = 0, \
.own_gil = 0, \
.no_site = nosite, \
}

/* --- Helper functions --------------------------------------- */
Expand Down
12 changes: 6 additions & 6 deletions Modules/_xxsubinterpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -504,19 +504,19 @@ 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;
}

// Create and initialize the new interpreter.
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);
Expand Down
5 changes: 3 additions & 2 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 3658e81

Please sign in to comment.