33from test import support
44from test .support import (verbose , refcount_test ,
55 cpython_only , requires_subprocess ,
6- requires_gil_enabled )
6+ requires_gil_enabled , suppress_immortalization ,
7+ Py_GIL_DISABLED )
78from test .support .import_helper import import_module
89from test .support .os_helper import temp_dir , TESTFN , unlink
910from test .support .script_helper import assert_python_ok , make_script
@@ -109,6 +110,7 @@ def test_tuple(self):
109110 del l
110111 self .assertEqual (gc .collect (), 2 )
111112
113+ @suppress_immortalization ()
112114 def test_class (self ):
113115 class A :
114116 pass
@@ -117,6 +119,7 @@ class A:
117119 del A
118120 self .assertNotEqual (gc .collect (), 0 )
119121
122+ @suppress_immortalization ()
120123 def test_newstyleclass (self ):
121124 class A (object ):
122125 pass
@@ -133,6 +136,7 @@ class A:
133136 del a
134137 self .assertNotEqual (gc .collect (), 0 )
135138
139+ @suppress_immortalization ()
136140 def test_newinstance (self ):
137141 class A (object ):
138142 pass
@@ -219,6 +223,7 @@ class B(object):
219223 self .fail ("didn't find obj in garbage (finalizer)" )
220224 gc .garbage .remove (obj )
221225
226+ @suppress_immortalization ()
222227 def test_function (self ):
223228 # Tricky: f -> d -> f, code should call d.clear() after the exec to
224229 # break the cycle.
@@ -561,6 +566,7 @@ def test_get_referents(self):
561566
562567 self .assertEqual (gc .get_referents (1 , 'a' , 4j ), [])
563568
569+ @suppress_immortalization ()
564570 def test_is_tracked (self ):
565571 # Atomic built-in types are not tracked, user-defined objects and
566572 # mutable containers are.
@@ -598,7 +604,9 @@ class UserFloatSlots(float):
598604 class UserIntSlots (int ):
599605 __slots__ = ()
600606
601- self .assertTrue (gc .is_tracked (gc ))
607+ if not Py_GIL_DISABLED :
608+ # gh-117783: modules may be immortalized in free-threaded build
609+ self .assertTrue (gc .is_tracked (gc ))
602610 self .assertTrue (gc .is_tracked (UserClass ))
603611 self .assertTrue (gc .is_tracked (UserClass ()))
604612 self .assertTrue (gc .is_tracked (UserInt ()))
@@ -1347,6 +1355,10 @@ def callback(ignored):
13471355 junk = []
13481356 i = 0
13491357 detector = GC_Detector ()
1358+ if Py_GIL_DISABLED :
1359+ # The free-threaded build doesn't have multiple generations, so
1360+ # just trigger a GC manually.
1361+ gc .collect ()
13501362 while not detector .gc_happened :
13511363 i += 1
13521364 if i > 10000 :
@@ -1415,6 +1427,10 @@ def __del__(self):
14151427 detector = GC_Detector ()
14161428 junk = []
14171429 i = 0
1430+ if Py_GIL_DISABLED :
1431+ # The free-threaded build doesn't have multiple generations, so
1432+ # just trigger a GC manually.
1433+ gc .collect ()
14181434 while not detector .gc_happened :
14191435 i += 1
14201436 if i > 10000 :
0 commit comments