Skip to content

Commit 2dae505

Browse files
authored
gh-117514: Add sys._is_gil_enabled() function (#118514)
The function returns `True` or `False` depending on whether the GIL is currently enabled. In the default build, it always returns `True` because the GIL is always enabled.
1 parent 24e643d commit 2dae505

File tree

5 files changed

+67
-1
lines changed

5 files changed

+67
-1
lines changed

Diff for: Doc/library/sys.rst

+8
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,14 @@ always available.
11991199
return value of :func:`intern` around to benefit from it.
12001200

12011201

1202+
.. function:: _is_gil_enabled()
1203+
1204+
Return :const:`True` if the :term:`GIL` is enabled and :const:`False` if
1205+
it is disabled.
1206+
1207+
.. versionadded:: 3.13
1208+
1209+
12021210
.. function:: is_finalizing()
12031211

12041212
Return :const:`True` if the main Python interpreter is

Diff for: Lib/test/test_sys.py

+6
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,12 @@ def test_getallocatedblocks(self):
10531053
c = sys.getallocatedblocks()
10541054
self.assertIn(c, range(b - 50, b + 50))
10551055

1056+
def test_is_gil_enabled(self):
1057+
if support.Py_GIL_DISABLED:
1058+
self.assertIs(type(sys._is_gil_enabled()), bool)
1059+
else:
1060+
self.assertTrue(sys._is_gil_enabled())
1061+
10561062
def test_is_finalizing(self):
10571063
self.assertIs(sys.is_finalizing(), False)
10581064
# Don't use the atexit module because _Py_Finalizing is only set
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Add ``sys._is_gil_enabled()`` function that returns whether the GIL is
2+
currently enabled. In the default build it always returns ``True`` because
3+
the GIL is always enabled. In the free-threaded build, it may return
4+
``True`` or ``False``.

Diff for: Python/clinic/sysmodule.c.h

+29-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Python/sysmodule.c

+20
Original file line numberDiff line numberDiff line change
@@ -2393,6 +2393,25 @@ sys__get_cpu_count_config_impl(PyObject *module)
23932393
return config->cpu_count;
23942394
}
23952395

2396+
/*[clinic input]
2397+
sys._is_gil_enabled -> bool
2398+
2399+
Return True if the GIL is currently enabled and False otherwise.
2400+
[clinic start generated code]*/
2401+
2402+
static int
2403+
sys__is_gil_enabled_impl(PyObject *module)
2404+
/*[clinic end generated code: output=57732cf53f5b9120 input=7e9c47f15a00e809]*/
2405+
{
2406+
#ifdef Py_GIL_DISABLED
2407+
PyInterpreterState *interp = _PyInterpreterState_GET();
2408+
return interp->ceval.gil->enabled;
2409+
#else
2410+
return 1;
2411+
#endif
2412+
}
2413+
2414+
23962415
static PerfMapState perf_map_state;
23972416

23982417
PyAPI_FUNC(int) PyUnstable_PerfMapState_Init(void) {
@@ -2565,6 +2584,7 @@ static PyMethodDef sys_methods[] = {
25652584
SYS__STATS_DUMP_METHODDEF
25662585
#endif
25672586
SYS__GET_CPU_COUNT_CONFIG_METHODDEF
2587+
SYS__IS_GIL_ENABLED_METHODDEF
25682588
{NULL, NULL} // sentinel
25692589
};
25702590

0 commit comments

Comments
 (0)