Skip to content

Commit 3bc0f08

Browse files
committed
gh-116604: Fix test_gc on free-threaded build
The free-threaded GC only does full collections, so it uses a threshold that is a maximum of a fixed value (default 2000) and proportional to the number of live objects. If there were many live objects after the previous collection, then the threshold may be larger than 10,000 causing `test_indirect_calls_with_gc_disabled` to fail. This manually sets the threshold to `(1000, 0, 0)` for the test. The `0` disables the proportional scaling.
1 parent df4784b commit 3bc0f08

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

Lib/test/support/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,16 @@ def disable_gc():
797797
if have_gc:
798798
gc.enable()
799799

800+
@contextlib.contextmanager
801+
def gc_threshold(*args):
802+
import gc
803+
old_threshold = gc.get_threshold()
804+
gc.set_threshold(*args)
805+
try:
806+
yield
807+
finally:
808+
gc.set_threshold(*old_threshold)
809+
800810

801811
def python_is_optimized():
802812
"""Find if Python was built with optimizations."""

Lib/test/test_gc.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from test.support.import_helper import import_module
66
from test.support.os_helper import temp_dir, TESTFN, unlink
77
from test.support.script_helper import assert_python_ok, make_script
8-
from test.support import threading_helper
8+
from test.support import threading_helper, gc_threshold
99

1010
import gc
1111
import sys
@@ -1330,6 +1330,7 @@ def callback(ignored):
13301330
# with an empty __dict__.
13311331
self.assertEqual(x, None)
13321332

1333+
@gc_threshold(1000, 0, 0)
13331334
def test_bug1055820d(self):
13341335
# Corresponds to temp2d.py in the bug report. This is very much like
13351336
# test_bug1055820c, but uses a __del__ method instead of a weakref
@@ -1397,6 +1398,7 @@ def __del__(self):
13971398
# empty __dict__.
13981399
self.assertEqual(x, None)
13991400

1401+
@gc_threshold(1000, 0, 0)
14001402
def test_indirect_calls_with_gc_disabled(self):
14011403
junk = []
14021404
i = 0

0 commit comments

Comments
 (0)