Skip to content

Commit 29e9fe2

Browse files
[3.15] Fix @reap_threads usage in test_free_threading.test_json (GH-158937) (#159115)
Fix @reap_threads usage in test_free_threading.test_json (GH-158937) Use the decorator on test methods, not on the whole class. Previously, test_json ran no tests: $ ./python -m test -v test_free_threading.test_json -v ... Ran 0 tests in 0.000s NO TESTS RAN Add also a check in @reap_threads to detect misuse (on a class). (cherry picked from commit 565f76f) Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 9461bea commit 29e9fe2

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

‎Lib/test/support/threading_helper.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import _thread
22
import contextlib
33
import functools
4+
import inspect
45
import sys
56
import threading
67
import time
@@ -59,6 +60,11 @@ def reap_threads(func):
5960
"""Use this function when threads are being used. This will
6061
ensure that the threads are cleaned up even when the test fails.
6162
"""
63+
64+
if inspect.isclass(func):
65+
# It should be used on each test method, not on the whole class
66+
raise ValueError("@reap_threads is not compatible with classes")
67+
6268
@functools.wraps(func)
6369
def decorator(*args):
6470
key = threading_setup()

‎Lib/test/test_free_threading/test_json.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ def items(self):
2929
return self.mapping
3030

3131

32-
@threading_helper.reap_threads
3332
@threading_helper.requires_working_threading()
3433
class TestJsonEncoding(CTest):
3534
# Test encoding json with concurrent threads modifying the data cannot
3635
# corrupt the interpreter
3736

37+
@threading_helper.reap_threads
3838
def test_json_mutating_list(self):
3939
def worker(barrier, data, index):
4040
barrier.wait()
@@ -48,6 +48,7 @@ def worker(barrier, data, index):
4848
data = [[], []]
4949
encode_json_helper(self.json, worker, data)
5050

51+
@threading_helper.reap_threads
5152
def test_json_mutating_exact_dict(self):
5253
def worker(barrier, data, index):
5354
barrier.wait()
@@ -65,6 +66,7 @@ def worker(barrier, data, index):
6566
data = [{}, {}]
6667
encode_json_helper(self.json, worker, data)
6768

69+
@threading_helper.reap_threads
6870
def test_json_mutating_mapping(self):
6971
def worker(barrier, data, index):
7072
barrier.wait()

0 commit comments

Comments
 (0)