https://github.com/python/cpython/commit/29e9fe279a6f077dc1f4dc6e2e316b34df9d74a3
commit: 29e9fe279a6f077dc1f4dc6e2e316b34df9d74a3
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: vstinner <[email protected]>
date: 2026-10-10T12:44:25Z
summary:

[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 565f76fee1fe1f3ea39aee203e2ed4887a67bc11)

Co-authored-by: Victor Stinner <[email protected]>

files:
M Lib/test/support/threading_helper.py
M Lib/test/test_free_threading/test_json.py

diff --git a/Lib/test/support/threading_helper.py 
b/Lib/test/support/threading_helper.py
index cf87233f0e2e93..31be31ac9a9dad 100644
--- a/Lib/test/support/threading_helper.py
+++ b/Lib/test/support/threading_helper.py
@@ -1,6 +1,7 @@
 import _thread
 import contextlib
 import functools
+import inspect
 import sys
 import threading
 import time
@@ -59,6 +60,11 @@ def reap_threads(func):
     """Use this function when threads are being used.  This will
     ensure that the threads are cleaned up even when the test fails.
     """
+
+    if inspect.isclass(func):
+        # It should be used on each test method, not on the whole class
+        raise ValueError("@reap_threads is not compatible with classes")
+
     @functools.wraps(func)
     def decorator(*args):
         key = threading_setup()
diff --git a/Lib/test/test_free_threading/test_json.py 
b/Lib/test/test_free_threading/test_json.py
index 010eb322a15b84..7e3adcf0e81ae2 100644
--- a/Lib/test/test_free_threading/test_json.py
+++ b/Lib/test/test_free_threading/test_json.py
@@ -29,12 +29,12 @@ def items(self):
         return self.mapping
 
 
-@threading_helper.reap_threads
 @threading_helper.requires_working_threading()
 class TestJsonEncoding(CTest):
     # Test encoding json with concurrent threads modifying the data cannot
     # corrupt the interpreter
 
+    @threading_helper.reap_threads
     def test_json_mutating_list(self):
         def worker(barrier, data, index):
             barrier.wait()
@@ -48,6 +48,7 @@ def worker(barrier, data, index):
         data = [[], []]
         encode_json_helper(self.json, worker, data)
 
+    @threading_helper.reap_threads
     def test_json_mutating_exact_dict(self):
         def worker(barrier, data, index):
             barrier.wait()
@@ -65,6 +66,7 @@ def worker(barrier, data, index):
         data = [{}, {}]
         encode_json_helper(self.json, worker, data)
 
+    @threading_helper.reap_threads
     def test_json_mutating_mapping(self):
         def worker(barrier, data, index):
             barrier.wait()

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to