Skip to content

Commit 4dc442f

Browse files
committed
Don't force PYTHON_GIL=0, instead fail if anything re-enables
1 parent 0582f43 commit 4dc442f

3 files changed

Lines changed: 31 additions & 7 deletions

File tree

.ci/install.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ python3 -m pip install --only-binary=:all: pyarrow || true
3939
# PyQt6 doesn't support PyPy3
4040
if [[ $GHA_PYTHON_VERSION == 3.* ]]; then
4141
sudo apt-get -qq install libegl1 libxcb-cursor0 libxcb-icccm4 libxcb-image0 libxcb-keysyms1 libxcb-randr0 libxcb-render-util0 libxcb-shape0 libxkbcommon-x11-0
42-
# TODO Update condition when pyqt6 supports free-threading
43-
if ! [[ "$PYTHON_GIL" == "0" ]]; then python3 -m pip install pyqt6 ; fi
42+
# pyqt6 doesn't yet support free-threading; only install if a wheel is available
43+
python3 -m pip install --only-binary=:all: pyqt6 || true
4444
fi
4545

4646
# webp

.github/workflows/test.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,6 @@ jobs:
7373
".ci/*.sh"
7474
"pyproject.toml"
7575
76-
- name: Set PYTHON_GIL
77-
if: endsWith(matrix.python-version, 't')
78-
run: |
79-
echo "PYTHON_GIL=0" >> $GITHUB_ENV
80-
8176
- name: Build system information
8277
run: python3 .github/workflows/system-info.py
8378

Tests/conftest.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
from __future__ import annotations
22

33
import io
4+
import sys
5+
import sysconfig
46

57
import pytest
68

9+
FREE_THREADED_BUILD = bool(sysconfig.get_config_var("Py_GIL_DISABLED"))
10+
11+
gil_enabled_at_start = True
12+
if FREE_THREADED_BUILD:
13+
gil_enabled_at_start = sys._is_gil_enabled() # type: ignore[attr-defined]
14+
715

816
def pytest_report_header(config: pytest.Config) -> str:
917
try:
@@ -16,6 +24,27 @@ def pytest_report_header(config: pytest.Config) -> str:
1624
return f"pytest_report_header failed: {e}"
1725

1826

27+
def pytest_terminal_summary(
28+
terminalreporter: pytest.TerminalReporter, exitstatus: int, config: pytest.Config
29+
) -> None:
30+
if (
31+
FREE_THREADED_BUILD
32+
and not gil_enabled_at_start
33+
and sys._is_gil_enabled() # type: ignore[attr-defined]
34+
):
35+
tr = terminalreporter
36+
tr.ensure_newline()
37+
tr.section("GIL re-enabled", sep="=", red=True, bold=True)
38+
tr.line("The GIL was re-enabled at runtime during the tests.")
39+
tr.line("This can happen with no test failures if the RuntimeWarning")
40+
tr.line("raised by Python when this happens is filtered by a test.")
41+
tr.line("")
42+
tr.line("Please ensure all new C modules declare support for running")
43+
tr.line("without the GIL. Any new tests that intentionally imports")
44+
tr.line("code that re-enables the GIL should do so in a subprocess.")
45+
pytest.exit("GIL re-enabled during tests", returncode=1)
46+
47+
1948
def pytest_configure(config: pytest.Config) -> None:
2049
config.addinivalue_line(
2150
"markers",

0 commit comments

Comments
 (0)