Skip to content

Commit 4cd374b

Browse files
rgommersdnicolodi
authored andcommitted
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 d940353 commit 4cd374b

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

mesonpy/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ def __init__(
747747
self._metadata.requires_python.prereleases = True
748748
if platform.python_version().rstrip('+') not in self._metadata.requires_python:
749749
raise MesonBuilderError(
750-
f'Package requires Python version {self._metadata.requires_python}, '
750+
f'The package requires Python version {self._metadata.requires_python}, '
751751
f'running on {platform.python_version()}')
752752

753753
# limited API
@@ -759,6 +759,11 @@ def __init__(
759759
if not value:
760760
self._limited_api = False
761761

762+
if self._limited_api and bool(sysconfig.get_config_var('Py_GIL_DISABLED')):
763+
raise BuildError(
764+
'The package targets Python\'s Limited API, which is not supported by free-threaded CPython. '
765+
'The "python.allow_limited_api" Meson build option may be used to override the package default.')
766+
762767
def _run(self, cmd: Sequence[str]) -> None:
763768
"""Invoke a subprocess."""
764769
# Flush the line to ensure that the log line with the executed

tests/test_project.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424

2525
def test_unsupported_python_version(package_unsupported_python_version):
26-
with pytest.raises(mesonpy.MesonBuilderError, match='Package requires Python version ==1.0.0'):
26+
with pytest.raises(mesonpy.MesonBuilderError, match='The package requires Python version ==1.0.0'):
2727
with mesonpy._project():
2828
pass
2929

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)