Skip to content

Commit bc400fe

Browse files
committed
MAINT: detect free-threaded CPython ("nogil") and handle limited API
The free-threaded CPython build does not support the limited API yet, and won't in the near future. To avoid either cryptic build failures or a successfull build yielding an `abi3` tag which would not be correct, raise a clear error and don't attempt to build.
1 parent 07e64f6 commit bc400fe

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

mesonpy/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,11 @@ def __init__(
758758
if not value:
759759
self._limited_api = False
760760

761+
if self._limited_api:
762+
if bool(sysconfig.get_config_var("Py_GIL_DISABLED")):
763+
raise BuildError('Free-threaded CPython does not support '
764+
'the Python limited API')
765+
761766
def _run(self, cmd: Sequence[str]) -> None:
762767
"""Invoke a subprocess."""
763768
# Flush the line to ensure that the log line with the executed

tests/test_wheel.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
'win32': '.dll',
4040
}.get(sys.platform, '.so')
4141

42+
NOGIL_BUILD = bool(sysconfig.get_config_var("Py_GIL_DISABLED"))
43+
4244
# Test against the wheel tag generated by packaging module.
4345
tag = next(packaging.tags.sys_tags())
4446
ABI = tag.abi
@@ -287,6 +289,7 @@ def test_skip_subprojects(package_subproject, tmp_path, arg):
287289

288290
# Requires Meson 1.3.0, see https://github.com/mesonbuild/meson/pull/11745.
289291
@pytest.mark.skipif(MESON_VERSION < (1, 2, 99), reason='Meson version too old')
292+
@pytest.mark.skipif(NOGIL_BUILD, reason='Free-threaded CPython does not support the limited API')
290293
def test_limited_api(wheel_limited_api):
291294
artifact = wheel.wheelfile.WheelFile(wheel_limited_api)
292295
name = artifact.parsed_filename
@@ -299,7 +302,11 @@ def test_limited_api(wheel_limited_api):
299302
@pytest.mark.skipif(MESON_VERSION < (1, 2, 99), reason='Meson version too old')
300303
@pytest.mark.xfail('__pypy__' in sys.builtin_module_names, reason='PyPy does not use special modules suffix for stable ABI')
301304
def test_limited_api_bad(package_limited_api, tmp_path):
302-
with pytest.raises(mesonpy.BuildError, match='The package declares compatibility with Python limited API but '):
305+
msg_start = 'The package declares compatibility with Python limited API but '
306+
if NOGIL_BUILD:
307+
msg_start = 'Free-threaded CPython does not support '
308+
309+
with pytest.raises(mesonpy.BuildError, match=msg_start):
303310
with mesonpy._project({'setup-args': ['-Dextra=true']}) as project:
304311
project.wheel(tmp_path)
305312

0 commit comments

Comments
 (0)