From f05944ecf054f5fb2150cb855e94e2b1420a8c3c Mon Sep 17 00:00:00 2001 From: Ralf Gommers Date: Wed, 17 Apr 2024 20:57:10 +0200 Subject: [PATCH] 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. --- mesonpy/__init__.py | 6 ++++++ tests/test_wheel.py | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/mesonpy/__init__.py b/mesonpy/__init__.py index eee05a69c..27f24fa73 100644 --- a/mesonpy/__init__.py +++ b/mesonpy/__init__.py @@ -758,6 +758,12 @@ def __init__( if not value: self._limited_api = False + if self._limited_api: + if bool(sysconfig.get_config_var('Py_GIL_DISABLED')): + raise BuildError('Package targets Python\'s Limited API, which is not supported by free-threaded ' + 'CPython. The `python.allow_limited_api` Meson build option may be used to ' + 'override the package default.') + def _run(self, cmd: Sequence[str]) -> None: """Invoke a subprocess.""" # Flush the line to ensure that the log line with the executed diff --git a/tests/test_wheel.py b/tests/test_wheel.py index 659ecbf5e..ffff9cdcb 100644 --- a/tests/test_wheel.py +++ b/tests/test_wheel.py @@ -39,6 +39,8 @@ 'win32': '.dll', }.get(sys.platform, '.so') +NOGIL_BUILD = bool(sysconfig.get_config_var("Py_GIL_DISABLED")) + # Test against the wheel tag generated by packaging module. tag = next(packaging.tags.sys_tags()) ABI = tag.abi @@ -287,6 +289,7 @@ def test_skip_subprojects(package_subproject, tmp_path, arg): # Requires Meson 1.3.0, see https://github.com/mesonbuild/meson/pull/11745. @pytest.mark.skipif(MESON_VERSION < (1, 2, 99), reason='Meson version too old') +@pytest.mark.skipif(NOGIL_BUILD, reason='Free-threaded CPython does not support the limited API') def test_limited_api(wheel_limited_api): artifact = wheel.wheelfile.WheelFile(wheel_limited_api) name = artifact.parsed_filename @@ -297,6 +300,7 @@ def test_limited_api(wheel_limited_api): # Requires Meson 1.3.0, see https://github.com/mesonbuild/meson/pull/11745. @pytest.mark.skipif(MESON_VERSION < (1, 2, 99), reason='Meson version too old') +@pytest.mark.skipif(NOGIL_BUILD, reason='Free-threaded CPython does not support the limited API') @pytest.mark.xfail('__pypy__' in sys.builtin_module_names, reason='PyPy does not use special modules suffix for stable ABI') def test_limited_api_bad(package_limited_api, tmp_path): with pytest.raises(mesonpy.BuildError, match='The package declares compatibility with Python limited API but '):