@@ -31,6 +31,11 @@ def __new__(cls, *args, **kwargs):
3131 return C
3232 ContainerNoGC = None
3333
34+ try :
35+ import _testinternalcapi
36+ except ImportError :
37+ _testinternalcapi = None
38+
3439### Support code
3540###############################################################################
3641
@@ -1130,6 +1135,7 @@ def setUp(self):
11301135 def tearDown (self ):
11311136 gc .disable ()
11321137
1138+ @unittest .skipIf (_testinternalcapi is None , "requires _testinternalcapi" )
11331139 @requires_gil_enabled ("Free threading does not support incremental GC" )
11341140 # Use small increments to emulate longer running process in a shorter time
11351141 @gc_threshold (200 , 10 )
@@ -1167,20 +1173,15 @@ def make_ll(depth):
11671173 enabled = gc .isenabled ()
11681174 gc .enable ()
11691175 olds = []
1176+ initial_heap_size = _testinternalcapi .get_heap_size ()
11701177 for i in range (20_000 ):
11711178 newhead = make_ll (20 )
11721179 count += 20
11731180 newhead .surprise = head
11741181 olds .append (newhead )
11751182 if len (olds ) == 20 :
1176- stats = gc .get_stats ()
1177- young = stats [0 ]
1178- incremental = stats [1 ]
1179- old = stats [2 ]
1180- collected = young ['collected' ] + incremental ['collected' ] + old ['collected' ]
1181- count += CORRECTION
1182- live = count - collected
1183- self .assertLess (live , 25000 )
1183+ new_objects = _testinternalcapi .get_heap_size () - initial_heap_size
1184+ self .assertLess (new_objects , 25_000 )
11841185 del olds [:]
11851186 if not enabled :
11861187 gc .disable ()
@@ -1322,7 +1323,8 @@ def test_refcount_errors(self):
13221323 from test.support import gc_collect, SuppressCrashReport
13231324
13241325 a = [1, 2, 3]
1325- b = [a]
1326+ b = [a, a]
1327+ a.append(b)
13261328
13271329 # Avoid coredump when Py_FatalError() calls abort()
13281330 SuppressCrashReport().__enter__()
@@ -1332,6 +1334,8 @@ def test_refcount_errors(self):
13321334 # (to avoid deallocating it):
13331335 import ctypes
13341336 ctypes.pythonapi.Py_DecRef(ctypes.py_object(a))
1337+ del a
1338+ del b
13351339
13361340 # The garbage collector should now have a fatal error
13371341 # when it reaches the broken object
@@ -1360,7 +1364,7 @@ def test_refcount_errors(self):
13601364 self .assertRegex (stderr ,
13611365 br'object type name: list' )
13621366 self .assertRegex (stderr ,
1363- br'object repr : \[1, 2, 3\]' )
1367+ br'object repr : \[1, 2, 3, \[\[...\], \[...\]\] \]' )
13641368
13651369
13661370class GCTogglingTests (unittest .TestCase ):
0 commit comments