Skip to content

Commit 4220514

Browse files
colesburyEclips4
andauthored
gh-117478: Add @support.requires_gil_enabled decorator (#117479)
Co-authored-by: Kirill Podoprigora <[email protected]>
1 parent de5ca0b commit 4220514

File tree

7 files changed

+23
-11
lines changed

7 files changed

+23
-11
lines changed

Doc/library/test.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,12 @@ The :mod:`test.support` module defines the following functions:
731731
macOS version is less than the minimum, the test is skipped.
732732

733733

734+
.. decorator:: requires_gil_enabled
735+
736+
Decorator for skipping tests on the free-threaded build. If the
737+
:term:`GIL` is disabled, the test is skipped.
738+
739+
734740
.. decorator:: requires_IEEE_754
735741

736742
Decorator for skipping tests on non-IEEE 754 platforms.

Lib/test/support/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"captured_stdin", "captured_stderr",
3030
# unittest
3131
"is_resource_enabled", "requires", "requires_freebsd_version",
32-
"requires_linux_version", "requires_mac_ver",
32+
"requires_gil_enabled", "requires_linux_version", "requires_mac_ver",
3333
"check_syntax_error",
3434
"requires_gzip", "requires_bz2", "requires_lzma",
3535
"bigmemtest", "bigaddrspacetest", "cpython_only", "get_attribute",
@@ -837,6 +837,11 @@ def check_cflags_pgo():
837837

838838

839839
Py_GIL_DISABLED = bool(sysconfig.get_config_var('Py_GIL_DISABLED'))
840+
841+
def requires_gil_enabled(msg="needs the GIL enabled"):
842+
"""Decorator for skipping tests on the free-threaded build."""
843+
return unittest.skipIf(Py_GIL_DISABLED, msg)
844+
840845
if Py_GIL_DISABLED:
841846
_header = 'PHBBInP'
842847
else:

Lib/test/test_capi/test_mem.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ class C(): pass
152152
self.assertGreaterEqual(count, i*10-4)
153153

154154

155-
# Py_GIL_DISABLED requires mimalloc (not malloc)
156-
@unittest.skipIf(support.Py_GIL_DISABLED, 'need malloc')
155+
# free-threading requires mimalloc (not malloc)
156+
@support.requires_gil_enabled
157157
class PyMemMallocDebugTests(PyMemDebugTests):
158158
PYTHONMALLOC = 'malloc_debug'
159159

Lib/test/test_cext/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ def test_build_c11(self):
4040
def test_build_c99(self):
4141
self.check_build('_test_c99_cext', std='c99')
4242

43-
@unittest.skipIf(support.Py_GIL_DISABLED, 'incompatible with Free Threading')
43+
@support.requires_gil_enabled('incompatible with Free Threading')
4444
def test_build_limited(self):
4545
self.check_build('_test_limited_cext', limited=True)
4646

47-
@unittest.skipIf(support.Py_GIL_DISABLED, 'broken for now with Free Threading')
47+
@support.requires_gil_enabled('broken for now with Free Threading')
4848
def test_build_limited_c11(self):
4949
self.check_build('_test_limited_c11_cext', limited=True, std='c11')
5050

Lib/test/test_concurrent_futures/test_process_pool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def test_saturation(self):
116116
for _ in range(job_count):
117117
sem.release()
118118

119-
@unittest.skipIf(support.Py_GIL_DISABLED, "gh-117344: test is flaky without the GIL")
119+
@support.requires_gil_enabled("gh-117344: test is flaky without the GIL")
120120
def test_idle_process_reuse_one(self):
121121
executor = self.executor
122122
assert executor._max_workers >= 4

Lib/test/test_concurrent_futures/test_thread_pool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def acquire_lock(lock):
4141
sem.release()
4242
executor.shutdown(wait=True)
4343

44-
@unittest.skipIf(support.Py_GIL_DISABLED, "gh-117344: test is flaky without the GIL")
44+
@support.requires_gil_enabled("gh-117344: test is flaky without the GIL")
4545
def test_idle_thread_reuse(self):
4646
executor = self.executor_type()
4747
executor.submit(mul, 21, 2).result()

Lib/test/test_gc.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import unittest
22
import unittest.mock
33
from test.support import (verbose, refcount_test,
4-
cpython_only, requires_subprocess, Py_GIL_DISABLED)
4+
cpython_only, requires_subprocess,
5+
requires_gil_enabled)
56
from test.support.import_helper import import_module
67
from test.support.os_helper import temp_dir, TESTFN, unlink
78
from test.support.script_helper import assert_python_ok, make_script
@@ -362,7 +363,7 @@ def __del__(self):
362363
# To minimize variations, though, we first store the get_count() results
363364
# and check them at the end.
364365
@refcount_test
365-
@unittest.skipIf(Py_GIL_DISABLED, 'needs precise allocation counts')
366+
@requires_gil_enabled('needs precise allocation counts')
366367
def test_get_count(self):
367368
gc.collect()
368369
a, b, c = gc.get_count()
@@ -815,7 +816,7 @@ def test_get_objects(self):
815816
any(l is element for element in gc.get_objects())
816817
)
817818

818-
@unittest.skipIf(Py_GIL_DISABLED, 'need generational GC')
819+
@requires_gil_enabled('need generational GC')
819820
def test_get_objects_generations(self):
820821
gc.collect()
821822
l = []
@@ -1046,7 +1047,7 @@ def setUp(self):
10461047
def tearDown(self):
10471048
gc.disable()
10481049

1049-
@unittest.skipIf(Py_GIL_DISABLED, "Free threading does not support incremental GC")
1050+
@requires_gil_enabled("Free threading does not support incremental GC")
10501051
# Use small increments to emulate longer running process in a shorter time
10511052
@gc_threshold(200, 10)
10521053
def test_incremental_gc_handles_fast_cycle_creation(self):

0 commit comments

Comments
 (0)