Skip to content

Commit a62f73a

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 a62f73a

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

mesonpy/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,12 @@ 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(f'Package targets Python\'s Limited API, which is not supported by free-threaded '
764+
'CPython. The `python.allow_limited_api` Meson build option may be used to '
765+
'override the package default.')
766+
761767
def _run(self, cmd: Sequence[str]) -> None:
762768
"""Invoke a subprocess."""
763769
# Flush the line to ensure that the log line with the executed

tests/test_wheel.py

Lines changed: 4 additions & 0 deletions
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
@@ -297,6 +300,7 @@ def test_limited_api(wheel_limited_api):
297300

298301
# Requires Meson 1.3.0, see https://github.com/mesonbuild/meson/pull/11745.
299302
@pytest.mark.skipif(MESON_VERSION < (1, 2, 99), reason='Meson version too old')
303+
@pytest.mark.skipif(NOGIL_BUILD, reason='Free-threaded CPython does not support the limited API')
300304
@pytest.mark.xfail('__pypy__' in sys.builtin_module_names, reason='PyPy does not use special modules suffix for stable ABI')
301305
def test_limited_api_bad(package_limited_api, tmp_path):
302306
with pytest.raises(mesonpy.BuildError, match='The package declares compatibility with Python limited API but '):

0 commit comments

Comments
 (0)