diff --git a/.github/workflows/QA.yaml b/.github/workflows/QA.yaml index 602cb5be..9f25c467 100644 --- a/.github/workflows/QA.yaml +++ b/.github/workflows/QA.yaml @@ -1,5 +1,9 @@ name: QA -on: [push] +on: + pull_request: + push: + branches: + - main env: LIBZIM_DL_VERSION: "9.8.2-1" diff --git a/.github/workflows/Tests.yaml b/.github/workflows/Tests.yaml index b440a12f..56c8ee4d 100644 --- a/.github/workflows/Tests.yaml +++ b/.github/workflows/Tests.yaml @@ -1,5 +1,9 @@ name: Tests -on: [push] +on: + pull_request: + push: + branches: + - main env: LIBZIM_DL_VERSION: "9.8.2-1" diff --git a/CHANGELOG.md b/CHANGELOG.md index b77fb54b..f9e62076 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed + +- Declare the extension free-threading compatible so that importing it on a free-threaded CPython no longer forces the GIL back on (#240) + ## [3.13.1] - 2026-10-05 ### Changed diff --git a/README.md b/README.md index 551ee797..f8ebebdc 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ Our [PyPI wheels](https://pypi.org/project/libzim/) bundle a [recent release](ht Wheels are available for CPython only (but can be built for Pypy). -Free-threaded CPython is not supported. If you use a free-threaded CPython, GIL must be turned on (using the environment variable PYTHON_GIL or the command-line option -X gil). If you don't turn it on yourself, GIL will be forced-on and you will get a warning. Only few methods support the GIL to be disabled. +Free-threaded CPython is supported: the extension declares itself free-threading compatible, so importing it no longer forces the GIL back on. Only few methods support the GIL to be disabled; you must still prevent concurrent access to shared objects yourself (see [Thread safety](#thread-safety)). Users on other platforms can install the source distribution (see [Building](#Building) below). diff --git a/libzim/libzim.pyx b/libzim/libzim.pyx index db63e4c8..aa1fea9a 100644 --- a/libzim/libzim.pyx +++ b/libzim/libzim.pyx @@ -17,6 +17,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +# cython: freethreading_compatible = True + # Make our libzim module a package by setting a __path__ # There is no real path here, but it will be passed to our module finder. """openZIM's file format library binding diff --git a/pyproject.toml b/pyproject.toml index fd096fa1..d02d0408 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -122,11 +122,6 @@ skip = "pp* *win32*" test-requires = ["pytest"] test-command = "py.test {project}/tests/" -# Override test command for free-threaded builds to force GIL on -[[tool.cibuildwheel.overrides]] -select = "cp313t-* cp314t-*" -test-command = "python -X gil=1 -m pytest {project}/tests/" - manylinux-x86_64-image = "manylinux_2_28" manylinux-aarch64-image = "manylinux_2_28" manylinux-pypy_x86_64-image = "manylinux_2_28" diff --git a/setup.py b/setup.py index 731d367d..04c7080b 100755 --- a/setup.py +++ b/setup.py @@ -391,6 +391,9 @@ def get_cython_extension() -> list[Extension]: define_macros: list[tuple[str, str | None]] = [] compiler_directives = {"language_level": "3"} + if getattr(sys, "_is_gil_enabled", None) is not None and not sys._is_gil_enabled(): + define_macros.append(("Py_GIL_DISABLED", "1")) + if config.profiling: define_macros += [ ("CYTHON_TRACE", "1"), diff --git a/tests/test_libzim_creator.py b/tests/test_libzim_creator.py index 360048e2..75cf0aa8 100644 --- a/tests/test_libzim_creator.py +++ b/tests/test_libzim_creator.py @@ -159,16 +159,7 @@ def get_creator_output(fpath, verbose): pass """.replace("{fpath}", str(fpath)).replace("{verbose}", str(verbose)) - # Build command with appropriate GIL settings for free-threaded Python - cmd = [sys.executable] - - # Check if we're running in free-threaded mode (Python 3.13+) - if sys.version_info >= (3, 13) and hasattr(sys, "_is_gil_enabled"): - # If GIL is enabled, explicitly pass -X gil=1 to subprocess - if sys._is_gil_enabled(): - cmd.extend(["-X", "gil=1"]) - - cmd.extend(["-c", code]) + cmd = [sys.executable, "-c", code] ps = subprocess.run( cmd,