Skip to content

Commit d1eb21f

Browse files
committed
add test that GIL stays disabled
1 parent e84ba56 commit d1eb21f

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

tests/test_gil.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""Test that the GIL is disabled on free-threaded Python builds."""
2+
3+
from __future__ import annotations
4+
5+
import sys
6+
import sysconfig
7+
8+
import pytest
9+
10+
_is_free_threaded = bool(sysconfig.get_config_var("Py_GIL_DISABLED"))
11+
12+
13+
@pytest.mark.skipif(not _is_free_threaded, reason="not a free-threaded python build")
14+
def test_gil_disabled() -> None:
15+
"""Assert that the GIL is disabled on free-threaded Python.
16+
17+
On free-threaded builds (3.13t, 3.14t, ...), the GIL should be disabled by default.
18+
If a C extension that hasn't declared ``Py_MOD_GIL_NOT_USED`` gets imported, the GIL
19+
is silently re-enabled and a ``RuntimeWarning`` is emitted. Combined with
20+
``filterwarnings = ["error"]`` in our pytest config, this test ensures that no such
21+
extension has been loaded during the test session.
22+
"""
23+
assert not sys._is_gil_enabled(), (
24+
"The GIL has been re-enabled. A C extension that does not declare "
25+
"Py_MOD_GIL_NOT_USED was loaded during the test session."
26+
)

0 commit comments

Comments
 (0)