Skip to content
Merged
Next Next commit
Add _PyInterpreterConfig.own_gil.
  • Loading branch information
ericsnowcurrently committed May 5, 2023
commit a73f36f403286af0b120b79785fb718c6ef03877
3 changes: 3 additions & 0 deletions Include/cpython/initconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ typedef struct {
int allow_threads;
int allow_daemon_threads;
int check_multi_interp_extensions;
int own_gil;
} PyInterpreterConfig;

#define _PyInterpreterConfig_INIT \
Expand All @@ -262,6 +263,7 @@ typedef struct {
.allow_threads = 1, \
.allow_daemon_threads = 0, \
.check_multi_interp_extensions = 1, \
.own_gil = 1, \
}

#define _PyInterpreterConfig_LEGACY_INIT \
Expand All @@ -272,6 +274,7 @@ typedef struct {
.allow_threads = 1, \
.allow_daemon_threads = 1, \
.check_multi_interp_extensions = 0, \
.own_gil = 0, \
}

/* --- Helper functions --------------------------------------- */
Expand Down
29 changes: 20 additions & 9 deletions Lib/test/test_capi/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1401,19 +1401,29 @@ def test_configured_settings(self):
DAEMON_THREADS = 1<<11
FORK = 1<<15
EXEC = 1<<16

features = ['obmalloc', 'fork', 'exec', 'threads', 'daemon_threads',
'extensions']
ALL_FLAGS = (OBMALLOC | FORK | EXEC | THREADS | DAEMON_THREADS
| EXTENSIONS);

features = [
'obmalloc',
'fork',
'exec',
'threads',
'daemon_threads',
'extensions',
'own_gil',
]
kwlist = [f'allow_{n}' for n in features]
kwlist[0] = 'use_main_obmalloc'
kwlist[-1] = 'check_multi_interp_extensions'
kwlist[-2] = 'check_multi_interp_extensions'
kwlist[-1] = 'own_gil'

# expected to work
for config, expected in {
(True, True, True, True, True, True):
OBMALLOC | FORK | EXEC | THREADS | DAEMON_THREADS | EXTENSIONS,
(True, False, False, False, False, False): OBMALLOC,
(False, False, False, True, False, True): THREADS | EXTENSIONS,
(True, True, True, True, True, True, True): ALL_FLAGS,
(True, False, False, False, False, False, False): OBMALLOC,
(False, False, False, True, False, True, False):
THREADS | EXTENSIONS,
}.items():
kwargs = dict(zip(kwlist, config))
expected = {
Expand All @@ -1437,7 +1447,7 @@ def test_configured_settings(self):

# expected to fail
for config in [
(False, False, False, False, False, False),
(False, False, False, False, False, False, False),
]:
kwargs = dict(zip(kwlist, config))
with self.subTest(config):
Expand Down Expand Up @@ -1473,6 +1483,7 @@ def test_overridden_setting_extensions_subinterp_check(self):
'allow_exec': True,
'allow_threads': True,
'allow_daemon_threads': True,
'own_gil': False,
}

def check(enabled, override):
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1640,6 +1640,7 @@ class SubinterpImportTests(unittest.TestCase):
)
ISOLATED = dict(
use_main_obmalloc=False,
own_gil=False,
)
NOT_ISOLATED = {k: not v for k, v in ISOLATED.items()}

Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,7 @@ def func():
allow_threads={allowed},
allow_daemon_threads={daemon_allowed},
check_multi_interp_extensions=False,
own_gil=False,
)
""")
with test.support.SuppressCrashReport():
Expand Down
12 changes: 10 additions & 2 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1488,6 +1488,7 @@ run_in_subinterp_with_config(PyObject *self, PyObject *args, PyObject *kwargs)
int allow_threads = -1;
int allow_daemon_threads = -1;
int check_multi_interp_extensions = -1;
int own_gil = -1;
int r;
PyThreadState *substate, *mainstate;
/* only initialise 'cflags.cf_flags' to test backwards compatibility */
Expand All @@ -1500,13 +1501,15 @@ run_in_subinterp_with_config(PyObject *self, PyObject *args, PyObject *kwargs)
"allow_threads",
"allow_daemon_threads",
"check_multi_interp_extensions",
"own_gil",
NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"s$pppppp:run_in_subinterp_with_config", kwlist,
"s$ppppppp:run_in_subinterp_with_config", kwlist,
&code, &use_main_obmalloc,
&allow_fork, &allow_exec,
&allow_threads, &allow_daemon_threads,
&check_multi_interp_extensions)) {
&check_multi_interp_extensions,
&own_gil)) {
return NULL;
}
if (use_main_obmalloc < 0) {
Expand All @@ -1525,6 +1528,10 @@ run_in_subinterp_with_config(PyObject *self, PyObject *args, PyObject *kwargs)
PyErr_SetString(PyExc_ValueError, "missing allow_threads");
return NULL;
}
if (own_gil < 0) {
PyErr_SetString(PyExc_ValueError, "missing own_gil");
return NULL;
}
if (allow_daemon_threads < 0) {
PyErr_SetString(PyExc_ValueError, "missing allow_daemon_threads");
return NULL;
Expand All @@ -1545,6 +1552,7 @@ run_in_subinterp_with_config(PyObject *self, PyObject *args, PyObject *kwargs)
.allow_threads = allow_threads,
.allow_daemon_threads = allow_daemon_threads,
.check_multi_interp_extensions = check_multi_interp_extensions,
.own_gil = own_gil,
};
PyStatus status = Py_NewInterpreterFromConfig(&substate, &config);
if (PyStatus_Exception(status)) {
Expand Down
4 changes: 3 additions & 1 deletion Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,9 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
return status;
}

const PyInterpreterConfig config = _PyInterpreterConfig_LEGACY_INIT;
PyInterpreterConfig config = _PyInterpreterConfig_LEGACY_INIT;
// The main interpreter always has its own GIL.
config.own_gil = 1;
status = init_interp_settings(interp, &config);
if (_PyStatus_EXCEPTION(status)) {
return status;
Expand Down