Skip to content

Commit e28ffbf

Browse files
Add _PyInterpreterConfig.own_gil.
1 parent fc1e4bc commit e28ffbf

File tree

6 files changed

+38
-12
lines changed

6 files changed

+38
-12
lines changed

Include/cpython/initconfig.h

+3
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ typedef struct {
252252
int allow_threads;
253253
int allow_daemon_threads;
254254
int check_multi_interp_extensions;
255+
int own_gil;
255256
} PyInterpreterConfig;
256257

257258
#define _PyInterpreterConfig_INIT \
@@ -262,6 +263,7 @@ typedef struct {
262263
.allow_threads = 1, \
263264
.allow_daemon_threads = 0, \
264265
.check_multi_interp_extensions = 1, \
266+
.own_gil = 1, \
265267
}
266268

267269
#define _PyInterpreterConfig_LEGACY_INIT \
@@ -272,6 +274,7 @@ typedef struct {
272274
.allow_threads = 1, \
273275
.allow_daemon_threads = 1, \
274276
.check_multi_interp_extensions = 0, \
277+
.own_gil = 0, \
275278
}
276279

277280
/* --- Helper functions --------------------------------------- */

Lib/test/test_capi/test_misc.py

+20-9
Original file line numberDiff line numberDiff line change
@@ -1401,19 +1401,29 @@ def test_configured_settings(self):
14011401
DAEMON_THREADS = 1<<11
14021402
FORK = 1<<15
14031403
EXEC = 1<<16
1404-
1405-
features = ['obmalloc', 'fork', 'exec', 'threads', 'daemon_threads',
1406-
'extensions']
1404+
ALL_FLAGS = (OBMALLOC | FORK | EXEC | THREADS | DAEMON_THREADS
1405+
| EXTENSIONS);
1406+
1407+
features = [
1408+
'obmalloc',
1409+
'fork',
1410+
'exec',
1411+
'threads',
1412+
'daemon_threads',
1413+
'extensions',
1414+
'own_gil',
1415+
]
14071416
kwlist = [f'allow_{n}' for n in features]
14081417
kwlist[0] = 'use_main_obmalloc'
1409-
kwlist[-1] = 'check_multi_interp_extensions'
1418+
kwlist[-2] = 'check_multi_interp_extensions'
1419+
kwlist[-1] = 'own_gil'
14101420

14111421
# expected to work
14121422
for config, expected in {
1413-
(True, True, True, True, True, True):
1414-
OBMALLOC | FORK | EXEC | THREADS | DAEMON_THREADS | EXTENSIONS,
1415-
(True, False, False, False, False, False): OBMALLOC,
1416-
(False, False, False, True, False, True): THREADS | EXTENSIONS,
1423+
(True, True, True, True, True, True, True): ALL_FLAGS,
1424+
(True, False, False, False, False, False, False): OBMALLOC,
1425+
(False, False, False, True, False, True, False):
1426+
THREADS | EXTENSIONS,
14171427
}.items():
14181428
kwargs = dict(zip(kwlist, config))
14191429
expected = {
@@ -1437,7 +1447,7 @@ def test_configured_settings(self):
14371447

14381448
# expected to fail
14391449
for config in [
1440-
(False, False, False, False, False, False),
1450+
(False, False, False, False, False, False, False),
14411451
]:
14421452
kwargs = dict(zip(kwlist, config))
14431453
with self.subTest(config):
@@ -1473,6 +1483,7 @@ def test_overridden_setting_extensions_subinterp_check(self):
14731483
'allow_exec': True,
14741484
'allow_threads': True,
14751485
'allow_daemon_threads': True,
1486+
'own_gil': False,
14761487
}
14771488

14781489
def check(enabled, override):

Lib/test/test_import/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1640,6 +1640,7 @@ class SubinterpImportTests(unittest.TestCase):
16401640
)
16411641
ISOLATED = dict(
16421642
use_main_obmalloc=False,
1643+
own_gil=False,
16431644
)
16441645
NOT_ISOLATED = {k: not v for k, v in ISOLATED.items()}
16451646

Lib/test/test_threading.py

+1
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,7 @@ def func():
13491349
allow_threads={allowed},
13501350
allow_daemon_threads={daemon_allowed},
13511351
check_multi_interp_extensions=False,
1352+
own_gil=False,
13521353
)
13531354
""")
13541355
with test.support.SuppressCrashReport():

Modules/_testcapimodule.c

+10-2
Original file line numberDiff line numberDiff line change
@@ -1488,6 +1488,7 @@ run_in_subinterp_with_config(PyObject *self, PyObject *args, PyObject *kwargs)
14881488
int allow_threads = -1;
14891489
int allow_daemon_threads = -1;
14901490
int check_multi_interp_extensions = -1;
1491+
int own_gil = -1;
14911492
int r;
14921493
PyThreadState *substate, *mainstate;
14931494
/* only initialise 'cflags.cf_flags' to test backwards compatibility */
@@ -1500,13 +1501,15 @@ run_in_subinterp_with_config(PyObject *self, PyObject *args, PyObject *kwargs)
15001501
"allow_threads",
15011502
"allow_daemon_threads",
15021503
"check_multi_interp_extensions",
1504+
"own_gil",
15031505
NULL};
15041506
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
1505-
"s$pppppp:run_in_subinterp_with_config", kwlist,
1507+
"s$ppppppp:run_in_subinterp_with_config", kwlist,
15061508
&code, &use_main_obmalloc,
15071509
&allow_fork, &allow_exec,
15081510
&allow_threads, &allow_daemon_threads,
1509-
&check_multi_interp_extensions)) {
1511+
&check_multi_interp_extensions,
1512+
&own_gil)) {
15101513
return NULL;
15111514
}
15121515
if (use_main_obmalloc < 0) {
@@ -1525,6 +1528,10 @@ run_in_subinterp_with_config(PyObject *self, PyObject *args, PyObject *kwargs)
15251528
PyErr_SetString(PyExc_ValueError, "missing allow_threads");
15261529
return NULL;
15271530
}
1531+
if (own_gil < 0) {
1532+
PyErr_SetString(PyExc_ValueError, "missing own_gil");
1533+
return NULL;
1534+
}
15281535
if (allow_daemon_threads < 0) {
15291536
PyErr_SetString(PyExc_ValueError, "missing allow_daemon_threads");
15301537
return NULL;
@@ -1545,6 +1552,7 @@ run_in_subinterp_with_config(PyObject *self, PyObject *args, PyObject *kwargs)
15451552
.allow_threads = allow_threads,
15461553
.allow_daemon_threads = allow_daemon_threads,
15471554
.check_multi_interp_extensions = check_multi_interp_extensions,
1555+
.own_gil = own_gil,
15481556
};
15491557
PyStatus status = Py_NewInterpreterFromConfig(&substate, &config);
15501558
if (PyStatus_Exception(status)) {

Python/pylifecycle.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,9 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
632632
return status;
633633
}
634634

635-
const PyInterpreterConfig config = _PyInterpreterConfig_LEGACY_INIT;
635+
PyInterpreterConfig config = _PyInterpreterConfig_LEGACY_INIT;
636+
// The main interpreter always has its own GIL.
637+
config.own_gil = 1;
636638
status = init_interp_settings(interp, &config);
637639
if (_PyStatus_EXCEPTION(status)) {
638640
return status;

0 commit comments

Comments
 (0)