Debian Codegraph @

Lib/test/test_gc.py

raw
1 import unittest
2 import unittest.mock
3 from test import support
4 from test.support import (verbose, refcount_test,
5 cpython_only, requires_subprocess,
6 requires_gil_enabled,
7 Py_GIL_DISABLED)
8 from test.support.import_helper import import_module
9 from test.support.os_helper import temp_dir, TESTFN, unlink
10 from test.support.script_helper import assert_python_ok, make_script
11 from test.support import threading_helper, gc_threshold
12
13 import gc
14 import sys
15 import sysconfig
16 import textwrap
17 import threading
18 import time
19 import weakref
20
21 try:
22 import _testcapi
23 from _testcapi import with_tp_del
24 from _testcapi import ContainerNoGC
25 except ImportError:
26 _testcapi = None
27 def with_tp_del(cls):
28 class C(object):
29 def __new__(cls, *args, **kwargs):
30 raise unittest.SkipTest('requires _testcapi.with_tp_del')
31 return C
32 ContainerNoGC = None
33
34 try:
35 import _testinternalcapi
36 except ImportError:
37 _testinternalcapi = None
38
39 ### Support code
40 ###############################################################################
41
42 # Bug 1055820 has several tests of longstanding bugs involving weakrefs and
43 # cyclic gc.
44
45 # An instance of C1055820 has a self-loop, so becomes cyclic trash when
46 # unreachable.
47 class C1055820(object):
48 def __init__(self, i):
49 self.i = i
50 self.loop = self
51
52 class GC_Detector(object):
53 # Create an instance I. Then gc hasn't happened again so long as
54 # I.gc_happened is false.
55
56 def __init__(self):
57 self.gc_happened = False
58
59 def it_happened(ignored):
60 self.gc_happened = True
61
62 # Create a piece of cyclic trash that triggers it_happened when
63 # gc collects it.
64 self.wr = weakref.ref(C1055820(666), it_happened)
65
66 @with_tp_del
67 class Uncollectable(object):
68 """Create a reference cycle with multiple __del__ methods.
69
70 An object in a reference cycle will never have zero references,
71 and so must be garbage collected. If one or more objects in the
72 cycle have __del__ methods, the gc refuses to guess an order,
73 and leaves the cycle uncollected."""
74 def __init__(self, partner=None):
75 if partner is None:
76 self.partner = Uncollectable(partner=self)
77 else:
78 self.partner = partner
79 def __tp_del__(self):
80 pass
81
82 if sysconfig.get_config_vars().get('PY_CFLAGS', ''):
83 BUILD_WITH_NDEBUG = ('-DNDEBUG' in sysconfig.get_config_vars()['PY_CFLAGS'])
84 else:
85 # Usually, sys.gettotalrefcount() is only present if Python has been
86 # compiled in debug mode. If it's missing, expect that Python has
87 # been released in release mode: with NDEBUG defined.
88 BUILD_WITH_NDEBUG = (not hasattr(sys, 'gettotalrefcount'))
89
90 ### Tests
91 ###############################################################################
92
93 class GCTests(unittest.TestCase):
94 def test_list(self):
95 l = []
96 l.append(l)
97 gc.collect()
98 del l
99 self.assertEqual(gc.collect(), 1)
100
101 def test_dict(self):
102 d = {}
103 d[1] = d
104 gc.collect()
105 del d
106 self.assertEqual(gc.collect(), 1)
107
108 def test_tuple(self):
109 # since tuples are immutable we close the loop with a list
110 l = []
111 t = (l,)
112 l.append(t)
113 gc.collect()
114 del t
115 del l
116 self.assertEqual(gc.collect(), 2)
117
118 def test_class(self):
119 class A:
120 pass
121 A.a = A
122 gc.collect()
123 del A
124 self.assertNotEqual(gc.collect(), 0)
125
126 def test_newstyleclass(self):
127 class A(object):
128 pass
129 gc.collect()
130 del A
131 self.assertNotEqual(gc.collect(), 0)
132
133 def test_instance(self):
134 class A:
135 pass
136 a = A()
137 a.a = a
138 gc.collect()
139 del a
140 self.assertNotEqual(gc.collect(), 0)
141
142 def test_newinstance(self):
143 class A(object):
144 pass
145 a = A()
146 a.a = a
147 gc.collect()
148 del a
149 self.assertNotEqual(gc.collect(), 0)
150 class B(list):
151 pass
152 class C(B, A):
153 pass
154 a = C()
155 a.a = a
156 gc.collect()
157 del a
158 self.assertNotEqual(gc.collect(), 0)
159 del B, C
160 self.assertNotEqual(gc.collect(), 0)
161 A.a = A()
162 del A
163 self.assertNotEqual(gc.collect(), 0)
164 self.assertEqual(gc.collect(), 0)
165
166 def test_method(self):
167 # Tricky: self.__init__ is a bound method, it references the instance.
168 class A:
169 def __init__(self):
170 self.init = self.__init__
171 a = A()
172 gc.collect()
173 del a
174 self.assertNotEqual(gc.collect(), 0)
175
176 @cpython_only
177 def test_legacy_finalizer(self):
178 # A() is uncollectable if it is part of a cycle, make sure it shows up
179 # in gc.garbage.
180 @with_tp_del
181 class A:
182 def __tp_del__(self): pass
183 class B:
184 pass
185 a = A()
186 a.a = a
187 id_a = id(a)
188 b = B()
189 b.b = b
190 gc.collect()
191 del a
192 del b
193 self.assertNotEqual(gc.collect(), 0)
194 for obj in gc.garbage:
195 if id(obj) == id_a:
196 del obj.a
197 break
198 else:
199 self.fail("didn't find obj in garbage (finalizer)")
200 gc.garbage.remove(obj)
201
202 @cpython_only
203 def test_legacy_finalizer_newclass(self):
204 # A() is uncollectable if it is part of a cycle, make sure it shows up
205 # in gc.garbage.
206 @with_tp_del
207 class A(object):
208 def __tp_del__(self): pass
209 class B(object):
210 pass
211 a = A()
212 a.a = a
213 id_a = id(a)
214 b = B()
215 b.b = b
216 gc.collect()
217 del a
218 del b
219 self.assertNotEqual(gc.collect(), 0)
220 for obj in gc.garbage:
221 if id(obj) == id_a:
222 del obj.a
223 break
224 else:
225 self.fail("didn't find obj in garbage (finalizer)")
226 gc.garbage.remove(obj)
227
228 def test_function(self):
229 # Tricky: f -> d -> f, code should call d.clear() after the exec to
230 # break the cycle.
231 d = {}
232 exec("def f(): pass\n", d)
233 gc.collect()
234 del d
235 # In the free-threaded build, the count returned by `gc.collect()`
236 # is 3 because it includes f's code object.
237 self.assertIn(gc.collect(), (2, 3))
238
239 def test_function_tp_clear_leaves_consistent_state(self):
240 # https://github.com/python/cpython/issues/91636
241 code = """if 1:
242
243 import gc
244 import weakref
245
246 class LateFin:
247 __slots__ = ('ref',)
248
249 def __del__(self):
250
251 # 8. Now `latefin`'s finalizer is called. Here we
252 # obtain a reference to `func`, which is currently
253 # undergoing `tp_clear`.
254 global func
255 func = self.ref()
256
257 class Cyclic(tuple):
258 __slots__ = ()
259
260 # 4. The finalizers of all garbage objects are called. In
261 # this case this is only us as `func` doesn't have a
262 # finalizer.
263 def __del__(self):
264
265 # 5. Create a weakref to `func` now. In previous
266 # versions of Python, this would avoid having it
267 # cleared by the garbage collector before calling
268 # the finalizers. Now, weakrefs get cleared after
269 # calling finalizers.
270 self[1].ref = weakref.ref(self[0])
271
272 # 6. Drop the global reference to `latefin`. The only
273 # remaining reference is the one we have.
274 global latefin
275 del latefin
276
277 # 7. Now `func` is `tp_clear`-ed. This drops the last
278 # reference to `Cyclic`, which gets `tp_dealloc`-ed.
279 # This drops the last reference to `latefin`.
280
281 latefin = LateFin()
282 def func():
283 pass
284 cyc = tuple.__new__(Cyclic, (func, latefin))
285
286 # 1. Create a reference cycle of `cyc` and `func`.
287 func.__module__ = cyc
288
289 # 2. Make the cycle unreachable, but keep the global reference
290 # to `latefin` so that it isn't detected as garbage. This
291 # way its finalizer will not be called immediately.
292 del func, cyc
293
294 # 3. Invoke garbage collection,
295 # which will find `cyc` and `func` as garbage.
296 gc.collect()
297
298 # 9. Previously, this would crash because the weakref
299 # created in the finalizer revealed the function after
300 # `tp_clear` was called and `func_qualname`
301 # had been NULL-ed out by func_clear(). Now, we clear
302 # weakrefs to unreachable objects before calling `tp_clear`
303 # but after calling finalizers.
304 print(f"{func=}")
305 """
306 rc, stdout, stderr = assert_python_ok("-c", code)
307 self.assertEqual(rc, 0)
308 # The `func` global is None because the weakref was cleared.
309 self.assertRegex(stdout, rb"""\A\s*func=None""")
310 self.assertFalse(stderr)
311
312 @refcount_test
313 def test_frame(self):
314 def f():
315 frame = sys._getframe()
316 gc.collect()
317 f()
318 self.assertEqual(gc.collect(), 1)
319
320 def test_saveall(self):
321 # Verify that cyclic garbage like lists show up in gc.garbage if the
322 # SAVEALL option is enabled.
323
324 # First make sure we don't save away other stuff that just happens to
325 # be waiting for collection.
326 gc.collect()
327 # if this fails, someone else created immortal trash
328 self.assertEqual(gc.garbage, [])
329
330 L = []
331 L.append(L)
332 id_L = id(L)
333
334 debug = gc.get_debug()
335 gc.set_debug(debug | gc.DEBUG_SAVEALL)
336 del L
337 gc.collect()
338 gc.set_debug(debug)
339
340 self.assertEqual(len(gc.garbage), 1)
341 obj = gc.garbage.pop()
342 self.assertEqual(id(obj), id_L)
343
344 def test_del(self):
345 # __del__ methods can trigger collection, make this to happen
346 thresholds = gc.get_threshold()
347 gc.enable()
348 gc.set_threshold(1)
349
350 class A:
351 def __del__(self):
352 dir(self)
353 a = A()
354 del a
355
356 gc.disable()
357 gc.set_threshold(*thresholds)
358
359 def test_del_newclass(self):
360 # __del__ methods can trigger collection, make this to happen
361 thresholds = gc.get_threshold()
362 gc.enable()
363 gc.set_threshold(1)
364
365 class A(object):
366 def __del__(self):
367 dir(self)
368 a = A()
369 del a
370
371 gc.disable()
372 gc.set_threshold(*thresholds)
373
374 # The following two tests are fragile:
375 # They precisely count the number of allocations,
376 # which is highly implementation-dependent.
377 # For example, disposed tuples are not freed, but reused.
378 # To minimize variations, though, we first store the get_count() results
379 # and check them at the end.
380 @refcount_test
381 @requires_gil_enabled('needs precise allocation counts')
382 def test_get_count(self):
383 gc.collect()
384 a, b, c = gc.get_count()
385 x = []
386 d, e, f = gc.get_count()
387 self.assertEqual((b, c), (0, 0))
388 self.assertEqual((e, f), (0, 0))
389 # This is less fragile than asserting that a equals 0.
390 self.assertLess(a, 5)
391 # Between the two calls to get_count(), at least one object was
392 # created (the list).
393 self.assertGreater(d, a)
394
395 @refcount_test
396 def test_collect_generations(self):
397 gc.collect()
398 # This object will "trickle" into generation N + 1 after
399 # each call to collect(N)
400 x = []
401 gc.collect(0)
402 # x is now in gen 1
403 a, b, c = gc.get_count()
404 gc.collect(1)
405 # x is now in gen 2
406 d, e, f = gc.get_count()
407 gc.collect(2)
408 # x is now in gen 3
409 g, h, i = gc.get_count()
410 # We don't check a, d, g since their exact values depends on
411 # internal implementation details of the interpreter.
412 self.assertEqual((b, c), (1, 0))
413 self.assertEqual((e, f), (0, 1))
414 self.assertEqual((h, i), (0, 0))
415
416 def test_trashcan(self):
417 class Ouch:
418 n = 0
419 def __del__(self):
420 Ouch.n = Ouch.n + 1
421 if Ouch.n % 17 == 0:
422 gc.collect()
423
424 # "trashcan" is a hack to prevent stack overflow when deallocating
425 # very deeply nested tuples etc. It works in part by abusing the
426 # type pointer and refcount fields, and that can yield horrible
427 # problems when gc tries to traverse the structures.
428 # If this test fails (as it does in 2.0, 2.1 and 2.2), it will
429 # most likely die via segfault.
430
431 # Note: In 2.3 the possibility for compiling without cyclic gc was
432 # removed, and that in turn allows the trashcan mechanism to work
433 # via much simpler means (e.g., it never abuses the type pointer or
434 # refcount fields anymore). Since it's much less likely to cause a
435 # problem now, the various constants in this expensive (we force a lot
436 # of full collections) test are cut back from the 2.2 version.
437 gc.enable()
438 N = 150
439 for count in range(2):
440 t = []
441 for i in range(N):
442 t = [t, Ouch()]
443 u = []
444 for i in range(N):
445 u = [u, Ouch()]
446 v = {}
447 for i in range(N):
448 v = {1: v, 2: Ouch()}
449 gc.disable()
450
451 @threading_helper.requires_working_threading()
452 def test_trashcan_threads(self):
453 # Issue #13992: trashcan mechanism should be thread-safe
454 NESTING = 60
455 N_THREADS = 2
456
457 def sleeper_gen():
458 """A generator that releases the GIL when closed or dealloc'ed."""
459 try:
460 yield
461 finally:
462 time.sleep(0.000001)
463
464 class C(list):
465 # Appending to a list is atomic, which avoids the use of a lock.
466 inits = []
467 dels = []
468 def __init__(self, alist):
469 self[:] = alist
470 C.inits.append(None)
471 def __del__(self):
472 # This __del__ is called by subtype_dealloc().
473 C.dels.append(None)
474 # `g` will release the GIL when garbage-collected. This
475 # helps assert subtype_dealloc's behaviour when threads
476 # switch in the middle of it.
477 g = sleeper_gen()
478 next(g)
479 # Now that __del__ is finished, subtype_dealloc will proceed
480 # to call list_dealloc, which also uses the trashcan mechanism.
481
482 def make_nested():
483 """Create a sufficiently nested container object so that the
484 trashcan mechanism is invoked when deallocating it."""
485 x = C([])
486 for i in range(NESTING):
487 x = [C([x])]
488 del x
489
490 def run_thread():
491 """Exercise make_nested() in a loop."""
492 while not exit:
493 make_nested()
494
495 old_switchinterval = sys.getswitchinterval()
496 support.setswitchinterval(1e-5)
497 try:
498 exit = []
499 threads = []
500 for i in range(N_THREADS):
501 t = threading.Thread(target=run_thread)
502 threads.append(t)
503 with threading_helper.start_threads(threads, lambda: exit.append(1)):
504 time.sleep(1.0)
505 finally:
506 sys.setswitchinterval(old_switchinterval)
507 gc.collect()
508 self.assertEqual(len(C.inits), len(C.dels))
509
510 def test_boom(self):
511 class Boom:
512 def __getattr__(self, someattribute):
513 del self.attr
514 raise AttributeError
515
516 a = Boom()
517 b = Boom()
518 a.attr = b
519 b.attr = a
520
521 gc.collect()
522 garbagelen = len(gc.garbage)
523 del a, b
524 # a<->b are in a trash cycle now. Collection will invoke
525 # Boom.__getattr__ (to see whether a and b have __del__ methods), and
526 # __getattr__ deletes the internal "attr" attributes as a side effect.
527 # That causes the trash cycle to get reclaimed via refcounts falling to
528 # 0, thus mutating the trash graph as a side effect of merely asking
529 # whether __del__ exists. This used to (before 2.3b1) crash Python.
530 # Now __getattr__ isn't called.
531 self.assertEqual(gc.collect(), 2)
532 self.assertEqual(len(gc.garbage), garbagelen)
533
534 def test_boom2(self):
535 class Boom2:
536 def __init__(self):
537 self.x = 0
538
539 def __getattr__(self, someattribute):
540 self.x += 1
541 if self.x > 1:
542 del self.attr
543 raise AttributeError
544
545 a = Boom2()
546 b = Boom2()
547 a.attr = b
548 b.attr = a
549
550 gc.collect()
551 garbagelen = len(gc.garbage)
552 del a, b
553 # Much like test_boom(), except that __getattr__ doesn't break the
554 # cycle until the second time gc checks for __del__. As of 2.3b1,
555 # there isn't a second time, so this simply cleans up the trash cycle.
556 # We expect a, b, a.__dict__ and b.__dict__ (4 objects) to get
557 # reclaimed this way.
558 self.assertEqual(gc.collect(), 2)
559 self.assertEqual(len(gc.garbage), garbagelen)
560
561 def test_get_referents(self):
562 alist = [1, 3, 5]
563 got = gc.get_referents(alist)
564 got.sort()
565 self.assertEqual(got, alist)
566
567 atuple = tuple(alist)
568 got = gc.get_referents(atuple)
569 got.sort()
570 self.assertEqual(got, alist)
571
572 adict = {1: 3, 5: 7}
573 expected = [1, 3, 5, 7]
574 got = gc.get_referents(adict)
575 got.sort()
576 self.assertEqual(got, expected)
577
578 got = gc.get_referents([1, 2], {3: 4}, (0, 0, 0))
579 got.sort()
580 self.assertEqual(got, [0, 0] + list(range(5)))
581
582 self.assertEqual(gc.get_referents(1, 'a', 4j), [])
583
584 def test_is_tracked(self):
585 # Atomic built-in types are not tracked, user-defined objects and
586 # mutable containers are.
587 # NOTE: types with special optimizations (e.g. tuple) have tests
588 # in their own test files instead.
589 self.assertFalse(gc.is_tracked(None))
590 self.assertFalse(gc.is_tracked(1))
591 self.assertFalse(gc.is_tracked(1.0))
592 self.assertFalse(gc.is_tracked(1.0 + 5.0j))
593 self.assertFalse(gc.is_tracked(True))
594 self.assertFalse(gc.is_tracked(False))
595 self.assertFalse(gc.is_tracked(b"a"))
596 self.assertFalse(gc.is_tracked("a"))
597 self.assertFalse(gc.is_tracked(bytearray(b"a")))
598 self.assertFalse(gc.is_tracked(type))
599 self.assertFalse(gc.is_tracked(int))
600 self.assertFalse(gc.is_tracked(object))
601 self.assertFalse(gc.is_tracked(object()))
602
603 class UserClass:
604 pass
605
606 class UserInt(int):
607 pass
608
609 # Base class is object; no extra fields.
610 class UserClassSlots:
611 __slots__ = ()
612
613 # Base class is fixed size larger than object; no extra fields.
614 class UserFloatSlots(float):
615 __slots__ = ()
616
617 # Base class is variable size; no extra fields.
618 class UserIntSlots(int):
619 __slots__ = ()
620
621 self.assertTrue(gc.is_tracked(gc))
622 self.assertTrue(gc.is_tracked(UserClass))
623 self.assertTrue(gc.is_tracked(UserClass()))
624 self.assertTrue(gc.is_tracked(UserInt()))
625 self.assertTrue(gc.is_tracked([]))
626 self.assertTrue(gc.is_tracked(set()))
627 self.assertTrue(gc.is_tracked(UserClassSlots()))
628 self.assertTrue(gc.is_tracked(UserFloatSlots()))
629 self.assertTrue(gc.is_tracked(UserIntSlots()))
630
631 def test_is_finalized(self):
632 # Objects not tracked by the always gc return false
633 self.assertFalse(gc.is_finalized(3))
634
635 storage = []
636 class Lazarus:
637 def __del__(self):
638 storage.append(self)
639
640 lazarus = Lazarus()
641 self.assertFalse(gc.is_finalized(lazarus))
642
643 del lazarus
644 gc.collect()
645
646 lazarus = storage.pop()
647 self.assertTrue(gc.is_finalized(lazarus))
648
649 def test_bug1055820b(self):
650 # Corresponds to temp2b.py in the bug report.
651
652 ouch = []
653 def callback(ignored):
654 ouch[:] = [wr() for wr in WRs]
655
656 Cs = [C1055820(i) for i in range(2)]
657 WRs = [weakref.ref(c, callback) for c in Cs]
658 c = None
659
660 gc.collect()
661 self.assertEqual(len(ouch), 0)
662 # Make the two instances trash, and collect again. The bug was that
663 # the callback materialized a strong reference to an instance, but gc
664 # cleared the instance's dict anyway.
665 Cs = None
666 gc.collect()
667 self.assertEqual(len(ouch), 2) # else the callbacks didn't run
668 for x in ouch:
669 # If the callback resurrected one of these guys, the instance
670 # would be damaged, with an empty __dict__.
671 self.assertEqual(x, None)
672
673 def test_bug21435(self):
674 # This is a poor test - its only virtue is that it happened to
675 # segfault on Tim's Windows box before the patch for 21435 was
676 # applied. That's a nasty bug relying on specific pieces of cyclic
677 # trash appearing in exactly the right order in finalize_garbage()'s
678 # input list.
679 # But there's no reliable way to force that order from Python code,
680 # so over time chances are good this test won't really be testing much
681 # of anything anymore. Still, if it blows up, there's _some_
682 # problem ;-)
683 gc.collect()
684
685 class A:
686 pass
687
688 class B:
689 def __init__(self, x):
690 self.x = x
691
692 def __del__(self):
693 self.attr = None
694
695 def do_work():
696 a = A()
697 b = B(A())
698
699 a.attr = b
700 b.attr = a
701
702 do_work()
703 gc.collect() # this blows up (bad C pointer) when it fails
704
705 @cpython_only
706 @requires_subprocess()
707 @unittest.skipIf(_testcapi is None, "requires _testcapi")
708 def test_garbage_at_shutdown(self):
709 import subprocess
710 code = """if 1:
711 import gc
712 import _testcapi
713 @_testcapi.with_tp_del
714 class X:
715 def __init__(self, name):
716 self.name = name
717 def __repr__(self):
718 return "<X %%r>" %% self.name
719 def __tp_del__(self):
720 pass
721
722 x = X('first')
723 x.x = x
724 x.y = X('second')
725 del x
726 gc.set_debug(%s)
727 """
728 def run_command(code):
729 p = subprocess.Popen([sys.executable, "-Wd", "-c", code],
730 stdout=subprocess.PIPE,
731 stderr=subprocess.PIPE)
732 stdout, stderr = p.communicate()
733 p.stdout.close()
734 p.stderr.close()
735 self.assertEqual(p.returncode, 0)
736 self.assertEqual(stdout, b"")
737 return stderr
738
739 stderr = run_command(code % "0")
740 self.assertIn(b"ResourceWarning: gc: 2 uncollectable objects at "
741 b"shutdown; use", stderr)
742 self.assertNotIn(b"<X 'first'>", stderr)
743 one_line_re = b"gc: uncollectable <X 0x[0-9A-Fa-f]+>"
744 expected_re = one_line_re + b"\r?\n" + one_line_re
745 self.assertNotRegex(stderr, expected_re)
746 # With DEBUG_UNCOLLECTABLE, the garbage list gets printed
747 stderr = run_command(code % "gc.DEBUG_UNCOLLECTABLE")
748 self.assertIn(b"ResourceWarning: gc: 2 uncollectable objects at "
749 b"shutdown", stderr)
750 self.assertTrue(
751 (b"[<X 'first'>, <X 'second'>]" in stderr) or
752 (b"[<X 'second'>, <X 'first'>]" in stderr), stderr)
753 # we expect two lines with uncollectable objects
754 self.assertRegex(stderr, expected_re)
755 # With DEBUG_SAVEALL, no additional message should get printed
756 # (because gc.garbage also contains normally reclaimable cyclic
757 # references, and its elements get printed at runtime anyway).
758 stderr = run_command(code % "gc.DEBUG_SAVEALL")
759 self.assertNotIn(b"uncollectable objects at shutdown", stderr)
760
761 def test_gc_main_module_at_shutdown(self):
762 # Create a reference cycle through the __main__ module and check
763 # it gets collected at interpreter shutdown.
764 code = """if 1:
765 class C:
766 def __del__(self):
767 print('__del__ called')
768 l = [C()]
769 l.append(l)
770 """
771 rc, out, err = assert_python_ok('-c', code)
772 self.assertEqual(out.strip(), b'__del__ called')
773
774 def test_gc_ordinary_module_at_shutdown(self):
775 # Same as above, but with a non-__main__ module.
776 with temp_dir() as script_dir:
777 module = """if 1:
778 class C:
779 def __del__(self):
780 print('__del__ called')
781 l = [C()]
782 l.append(l)
783 """
784 code = """if 1:
785 import sys
786 sys.path.insert(0, %r)
787 import gctest
788 """ % (script_dir,)
789 make_script(script_dir, 'gctest', module)
790 rc, out, err = assert_python_ok('-c', code)
791 self.assertEqual(out.strip(), b'__del__ called')
792
793 @unittest.skipIf(Py_GIL_DISABLED, "requires GC generations or increments")
794 def test_gc_debug_stats(self):
795 # Checks that debug information is printed to stderr
796 # when DEBUG_STATS is set.
797 code = """if 1:
798 import gc
799 gc.set_debug(%s)
800 gc.collect()
801 """
802 _, _, err = assert_python_ok("-c", code % "gc.DEBUG_STATS")
803 self.assertRegex(err, b"gc: collecting generation [0-9]+")
804 self.assertRegex(
805 err,
806 b"gc: objects in each generation: [0-9]+ [0-9]+ [0-9]+",
807 )
808 self.assertRegex(
809 err, b"gc: objects in permanent generation: [0-9]+"
810 )
811 self.assertRegex(
812 err,
813 b"gc: done, .* unreachable, .* uncollectable, .* elapsed",
814 )
815
816 _, _, err = assert_python_ok("-c", code % "0")
817 self.assertNotIn(b"elapsed", err)
818
819 def test_global_del_SystemExit(self):
820 code = """if 1:
821 class ClassWithDel:
822 def __del__(self):
823 print('__del__ called')
824 a = ClassWithDel()
825 a.link = a
826 raise SystemExit(0)"""
827 self.addCleanup(unlink, TESTFN)
828 with open(TESTFN, 'w', encoding="utf-8") as script:
829 script.write(code)
830 rc, out, err = assert_python_ok(TESTFN)
831 self.assertEqual(out.strip(), b'__del__ called')
832
833 def test_get_stats(self):
834 stats = gc.get_stats()
835 self.assertEqual(len(stats), 3)
836 for st in stats:
837 self.assertIsInstance(st, dict)
838 self.assertEqual(set(st),
839 {"collected", "collections", "uncollectable"})
840 self.assertGreaterEqual(st["collected"], 0)
841 self.assertGreaterEqual(st["collections"], 0)
842 self.assertGreaterEqual(st["uncollectable"], 0)
843 # Check that collection counts are incremented correctly
844 if gc.isenabled():
845 self.addCleanup(gc.enable)
846 gc.disable()
847 old = gc.get_stats()
848 gc.collect(0)
849 new = gc.get_stats()
850 self.assertEqual(new[0]["collections"], old[0]["collections"] + 1)
851 self.assertEqual(new[1]["collections"], old[1]["collections"])
852 self.assertEqual(new[2]["collections"], old[2]["collections"])
853 gc.collect(2)
854 new = gc.get_stats()
855 self.assertEqual(new[0]["collections"], old[0]["collections"] + 1)
856 self.assertEqual(new[1]["collections"], old[1]["collections"])
857 self.assertEqual(new[2]["collections"], old[2]["collections"] + 1)
858
859 def test_freeze(self):
860 gc.freeze()
861 self.assertGreater(gc.get_freeze_count(), 0)
862 gc.unfreeze()
863 self.assertEqual(gc.get_freeze_count(), 0)
864
865 def test_get_objects(self):
866 gc.collect()
867 l = []
868 l.append(l)
869 self.assertTrue(
870 any(l is element for element in gc.get_objects())
871 )
872
873 @requires_gil_enabled('need generational GC')
874 def test_get_objects_generations(self):
875 gc.collect()
876 l = []
877 l.append(l)
878 self.assertTrue(
879 any(l is element for element in gc.get_objects(generation=0))
880 )
881 self.assertFalse(
882 any(l is element for element in gc.get_objects(generation=1))
883 )
884 self.assertFalse(
885 any(l is element for element in gc.get_objects(generation=2))
886 )
887 gc.collect(generation=0)
888 self.assertFalse(
889 any(l is element for element in gc.get_objects(generation=0))
890 )
891 self.assertTrue(
892 any(l is element for element in gc.get_objects(generation=1))
893 )
894 self.assertFalse(
895 any(l is element for element in gc.get_objects(generation=2))
896 )
897 gc.collect(generation=1)
898 self.assertFalse(
899 any(l is element for element in gc.get_objects(generation=0))
900 )
901 self.assertFalse(
902 any(l is element for element in gc.get_objects(generation=1))
903 )
904 self.assertTrue(
905 any(l is element for element in gc.get_objects(generation=2))
906 )
907 gc.collect(generation=2)
908 self.assertFalse(
909 any(l is element for element in gc.get_objects(generation=0))
910 )
911 self.assertFalse(
912 any(l is element for element in gc.get_objects(generation=1))
913 )
914 self.assertTrue(
915 any(l is element for element in gc.get_objects(generation=2))
916 )
917 del l
918 gc.collect()
919
920 def test_get_objects_arguments(self):
921 gc.collect()
922 self.assertEqual(len(gc.get_objects()),
923 len(gc.get_objects(generation=None)))
924
925 self.assertRaises(ValueError, gc.get_objects, 1000)
926 self.assertRaises(ValueError, gc.get_objects, -1000)
927 self.assertRaises(TypeError, gc.get_objects, "1")
928 self.assertRaises(TypeError, gc.get_objects, 1.234)
929
930 def test_resurrection_only_happens_once_per_object(self):
931 class A: # simple self-loop
932 def __init__(self):
933 self.me = self
934
935 class Lazarus(A):
936 resurrected = 0
937 resurrected_instances = []
938
939 def __del__(self):
940 Lazarus.resurrected += 1
941 Lazarus.resurrected_instances.append(self)
942
943 gc.collect()
944 gc.disable()
945
946 # We start with 0 resurrections
947 laz = Lazarus()
948 self.assertEqual(Lazarus.resurrected, 0)
949
950 # Deleting the instance and triggering a collection
951 # resurrects the object
952 del laz
953 gc.collect()
954 self.assertEqual(Lazarus.resurrected, 1)
955 self.assertEqual(len(Lazarus.resurrected_instances), 1)
956
957 # Clearing the references and forcing a collection
958 # should not resurrect the object again.
959 Lazarus.resurrected_instances.clear()
960 self.assertEqual(Lazarus.resurrected, 1)
961 gc.collect()
962 self.assertEqual(Lazarus.resurrected, 1)
963
964 gc.enable()
965
966 def test_resurrection_is_transitive(self):
967 class Cargo:
968 def __init__(self):
969 self.me = self
970
971 class Lazarus:
972 resurrected_instances = []
973
974 def __del__(self):
975 Lazarus.resurrected_instances.append(self)
976
977 gc.collect()
978 gc.disable()
979
980 laz = Lazarus()
981 cargo = Cargo()
982 cargo_id = id(cargo)
983
984 # Create a cycle between cargo and laz
985 laz.cargo = cargo
986 cargo.laz = laz
987
988 # Drop the references, force a collection and check that
989 # everything was resurrected.
990 del laz, cargo
991 gc.collect()
992 self.assertEqual(len(Lazarus.resurrected_instances), 1)
993 instance = Lazarus.resurrected_instances.pop()
994 self.assertHasAttr(instance, "cargo")
995 self.assertEqual(id(instance.cargo), cargo_id)
996
997 gc.collect()
998 gc.enable()
999
1000 def test_resurrection_does_not_block_cleanup_of_other_objects(self):
1001
1002 # When a finalizer resurrects objects, stats were reporting them as
1003 # having been collected. This affected both collect()'s return
1004 # value and the dicts returned by get_stats().
1005 N = 100
1006
1007 class A: # simple self-loop
1008 def __init__(self):
1009 self.me = self
1010
1011 class Z(A): # resurrecting __del__
1012 def __del__(self):
1013 zs.append(self)
1014
1015 zs = []
1016
1017 def getstats():
1018 d = gc.get_stats()[-1]
1019 return d['collected'], d['uncollectable']
1020
1021 gc.collect()
1022 gc.disable()
1023
1024 # No problems if just collecting A() instances.
1025 oldc, oldnc = getstats()
1026 for i in range(N):
1027 A()
1028 t = gc.collect()
1029 c, nc = getstats()
1030 self.assertEqual(t, N) # instance objects
1031 self.assertEqual(c - oldc, N)
1032 self.assertEqual(nc - oldnc, 0)
1033
1034 # But Z() is not actually collected.
1035 oldc, oldnc = c, nc
1036 Z()
1037 # Nothing is collected - Z() is merely resurrected.
1038 t = gc.collect()
1039 c, nc = getstats()
1040 self.assertEqual(t, 0)
1041 self.assertEqual(c - oldc, 0)
1042 self.assertEqual(nc - oldnc, 0)
1043
1044 # Z() should not prevent anything else from being collected.
1045 oldc, oldnc = c, nc
1046 for i in range(N):
1047 A()
1048 Z()
1049 t = gc.collect()
1050 c, nc = getstats()
1051 self.assertEqual(t, N)
1052 self.assertEqual(c - oldc, N)
1053 self.assertEqual(nc - oldnc, 0)
1054
1055 # The A() trash should have been reclaimed already but the
1056 # 2 copies of Z are still in zs (and the associated dicts).
1057 oldc, oldnc = c, nc
1058 zs.clear()
1059 t = gc.collect()
1060 c, nc = getstats()
1061 self.assertEqual(t, 2)
1062 self.assertEqual(c - oldc, 2)
1063 self.assertEqual(nc - oldnc, 0)
1064
1065 gc.enable()
1066
1067 @unittest.skipIf(ContainerNoGC is None,
1068 'requires ContainerNoGC extension type')
1069 def test_trash_weakref_clear(self):
1070 # Test that trash weakrefs are properly cleared (bpo-38006).
1071 #
1072 # Structure we are creating:
1073 #
1074 # Z <- Y <- A--+--> WZ -> C
1075 # ^ |
1076 # +--+
1077 # where:
1078 # WZ is a weakref to Z with callback C
1079 # Y doesn't implement tp_traverse
1080 # A contains a reference to itself, Y and WZ
1081 #
1082 # A, Y, Z, WZ are all trash. The GC doesn't know that Z is trash
1083 # because Y does not implement tp_traverse. To show the bug, WZ needs
1084 # to live long enough so that Z is deallocated before it. Then, if
1085 # gcmodule is buggy, when Z is being deallocated, C will run.
1086 #
1087 # To ensure WZ lives long enough, we put it in a second reference
1088 # cycle. That trick only works due to the ordering of the GC prev/next
1089 # linked lists. So, this test is a bit fragile.
1090 #
1091 # The bug reported in bpo-38006 is caused because the GC did not
1092 # clear WZ before starting the process of calling tp_clear on the
1093 # trash. Normally, handle_weakrefs() would find the weakref via Z and
1094 # clear it. However, since the GC cannot find Z, WR is not cleared and
1095 # it can execute during delete_garbage(). That can lead to disaster
1096 # since the callback might tinker with objects that have already had
1097 # tp_clear called on them (leaving them in possibly invalid states).
1098
1099 callback = unittest.mock.Mock()
1100
1101 class A:
1102 __slots__ = ['a', 'y', 'wz']
1103
1104 class Z:
1105 pass
1106
1107 # setup required object graph, as described above
1108 a = A()
1109 a.a = a
1110 a.y = ContainerNoGC(Z())
1111 a.wz = weakref.ref(a.y.value, callback)
1112 # create second cycle to keep WZ alive longer
1113 wr_cycle = [a.wz]
1114 wr_cycle.append(wr_cycle)
1115 # ensure trash unrelated to this test is gone
1116 gc.collect()
1117 gc.disable()
1118 # release references and create trash
1119 del a, wr_cycle
1120 gc.collect()
1121 # if called, it means there is a bug in the GC. The weakref should be
1122 # cleared before Z dies.
1123 callback.assert_not_called()
1124 gc.enable()
1125
1126 @cpython_only
1127 def test_get_referents_on_capsule(self):
1128 # gh-124538: Calling gc.get_referents() on an untracked capsule must not crash.
1129 import _datetime
1130 import _socket
1131 untracked_capsule = _datetime.datetime_CAPI
1132 tracked_capsule = _socket.CAPI
1133
1134 # For whoever sees this in the future: if this is failing
1135 # after making datetime's capsule tracked, that's fine -- this isn't something
1136 # users are relying on. Just find a different capsule that is untracked.
1137 self.assertFalse(gc.is_tracked(untracked_capsule))
1138 self.assertTrue(gc.is_tracked(tracked_capsule))
1139
1140 self.assertEqual(len(gc.get_referents(untracked_capsule)), 0)
1141 gc.get_referents(tracked_capsule)
1142
1143 @cpython_only
1144 def test_get_objects_during_gc(self):
1145 # gh-125859: Calling gc.get_objects() or gc.get_referrers() during a
1146 # collection should not crash.
1147 test = self
1148 collected = False
1149
1150 class GetObjectsOnDel:
1151 def __del__(self):
1152 nonlocal collected
1153 collected = True
1154 objs = gc.get_objects()
1155 # NB: can't use "in" here because some objects override __eq__
1156 for obj in objs:
1157 test.assertTrue(obj is not self)
1158 test.assertEqual(gc.get_referrers(self), [])
1159
1160 obj = GetObjectsOnDel()
1161 obj.cycle = obj
1162 del obj
1163
1164 gc.collect()
1165 self.assertTrue(collected)
1166
1167 def test_traverse_frozen_objects(self):
1168 # See GH-126312: Objects that were not frozen could traverse over
1169 # a frozen object on the free-threaded build, which would cause
1170 # a negative reference count.
1171 x = [1, 2, 3]
1172 gc.freeze()
1173 y = [x]
1174 y.append(y)
1175 del y
1176 gc.collect()
1177 gc.unfreeze()
1178
1179 def test_deferred_refcount_frozen(self):
1180 # Also from GH-126312: objects that use deferred reference counting
1181 # weren't ignored if they were frozen. Unfortunately, it's pretty
1182 # difficult to come up with a case that triggers this.
1183 #
1184 # Calling gc.collect() while the garbage collector is frozen doesn't
1185 # trigger this normally, but it *does* if it's inside unittest for whatever
1186 # reason. We can't call unittest from inside a test, so it has to be
1187 # in a subprocess.
1188 source = textwrap.dedent("""
1189 import gc
1190 import unittest
1191
1192
1193 class Test(unittest.TestCase):
1194 def test_something(self):
1195 gc.freeze()
1196 gc.collect()
1197 gc.unfreeze()
1198
1199
1200 if __name__ == "__main__":
1201 unittest.main()
1202 """)
1203 assert_python_ok("-c", source)
1204
1205
1206 @unittest.skipUnless(Py_GIL_DISABLED, "requires free-threaded GC")
1207 @unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
1208 def test_tuple_untrack_counts(self):
1209 # This ensures that the free-threaded GC is counting untracked tuples
1210 # in the "long_lived_total" count. This is required to avoid
1211 # performance issues from running the GC too frequently. See
1212 # GH-142531 as an example.
1213 gc.collect()
1214 count = _testinternalcapi.get_long_lived_total()
1215 n = 20_000
1216 tuples = [(x,) for x in range(n)]
1217 gc.collect()
1218 new_count = _testinternalcapi.get_long_lived_total()
1219 self.assertFalse(gc.is_tracked(tuples[0]))
1220 # Use n // 2 just in case some other objects were collected.
1221 self.assertTrue(new_count - count > (n // 2))
1222
1223 @requires_gil_enabled('need generational GC')
1224 @unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
1225 def test_heap_size(self):
1226 count = _testinternalcapi.get_tracked_heap_size()
1227 l = []
1228 self.assertEqual(count + 1, _testinternalcapi.get_tracked_heap_size())
1229 del l
1230 self.assertEqual(count, _testinternalcapi.get_tracked_heap_size())
1231
1232
1233 class GCCallbackTests(unittest.TestCase):
1234 def setUp(self):
1235 # Save gc state and disable it.
1236 self.enabled = gc.isenabled()
1237 gc.disable()
1238 self.debug = gc.get_debug()
1239 gc.set_debug(0)
1240 gc.callbacks.append(self.cb1)
1241 gc.callbacks.append(self.cb2)
1242 self.othergarbage = []
1243
1244 def tearDown(self):
1245 # Restore gc state
1246 del self.visit
1247 gc.callbacks.remove(self.cb1)
1248 gc.callbacks.remove(self.cb2)
1249 gc.set_debug(self.debug)
1250 if self.enabled:
1251 gc.enable()
1252 # destroy any uncollectables
1253 gc.collect()
1254 for obj in gc.garbage:
1255 if isinstance(obj, Uncollectable):
1256 obj.partner = None
1257 del gc.garbage[:]
1258 del self.othergarbage
1259 gc.collect()
1260
1261 def preclean(self):
1262 # Remove all fluff from the system. Invoke this function
1263 # manually rather than through self.setUp() for maximum
1264 # safety.
1265 self.visit = []
1266 gc.collect()
1267 garbage, gc.garbage[:] = gc.garbage[:], []
1268 self.othergarbage.append(garbage)
1269 self.visit = []
1270
1271 def cb1(self, phase, info):
1272 self.visit.append((1, phase, dict(info)))
1273
1274 def cb2(self, phase, info):
1275 self.visit.append((2, phase, dict(info)))
1276 if phase == "stop" and hasattr(self, "cleanup"):
1277 # Clean Uncollectable from garbage
1278 uc = [e for e in gc.garbage if isinstance(e, Uncollectable)]
1279 gc.garbage[:] = [e for e in gc.garbage
1280 if not isinstance(e, Uncollectable)]
1281 for e in uc:
1282 e.partner = None
1283
1284 def test_collect(self):
1285 self.preclean()
1286 gc.collect()
1287 # Algorithmically verify the contents of self.visit
1288 # because it is long and tortuous.
1289
1290 # Count the number of visits to each callback
1291 n = [v[0] for v in self.visit]
1292 n1 = [i for i in n if i == 1]
1293 n2 = [i for i in n if i == 2]
1294 self.assertEqual(n1, [1]*2)
1295 self.assertEqual(n2, [2]*2)
1296
1297 # Count that we got the right number of start and stop callbacks.
1298 n = [v[1] for v in self.visit]
1299 n1 = [i for i in n if i == "start"]
1300 n2 = [i for i in n if i == "stop"]
1301 self.assertEqual(n1, ["start"]*2)
1302 self.assertEqual(n2, ["stop"]*2)
1303
1304 # Check that we got the right info dict for all callbacks
1305 for v in self.visit:
1306 info = v[2]
1307 self.assertTrue("generation" in info)
1308 self.assertTrue("collected" in info)
1309 self.assertTrue("uncollectable" in info)
1310
1311 def test_collect_generation(self):
1312 self.preclean()
1313 gc.collect(2)
1314 for v in self.visit:
1315 info = v[2]
1316 self.assertEqual(info["generation"], 2)
1317
1318 @cpython_only
1319 def test_collect_garbage(self):
1320 self.preclean()
1321 # Each of these cause two objects to be garbage:
1322 Uncollectable()
1323 Uncollectable()
1324 C1055820(666)
1325 gc.collect()
1326 for v in self.visit:
1327 if v[1] != "stop":
1328 continue
1329 info = v[2]
1330 self.assertEqual(info["collected"], 1)
1331 self.assertEqual(info["uncollectable"], 4)
1332
1333 # We should now have the Uncollectables in gc.garbage
1334 self.assertEqual(len(gc.garbage), 4)
1335 for e in gc.garbage:
1336 self.assertIsInstance(e, Uncollectable)
1337
1338 # Now, let our callback handle the Uncollectable instances
1339 self.cleanup=True
1340 self.visit = []
1341 gc.garbage[:] = []
1342 gc.collect()
1343 for v in self.visit:
1344 if v[1] != "stop":
1345 continue
1346 info = v[2]
1347 self.assertEqual(info["collected"], 0)
1348 self.assertEqual(info["uncollectable"], 2)
1349
1350 # Uncollectables should be gone
1351 self.assertEqual(len(gc.garbage), 0)
1352
1353
1354 @requires_subprocess()
1355 @unittest.skipIf(BUILD_WITH_NDEBUG,
1356 'built with -NDEBUG')
1357 def test_refcount_errors(self):
1358 self.preclean()
1359 # Verify the "handling" of objects with broken refcounts
1360
1361 # Skip the test if ctypes is not available
1362 import_module("ctypes")
1363
1364 import subprocess
1365 code = textwrap.dedent('''
1366 from test.support import gc_collect, SuppressCrashReport
1367
1368 a = [1, 2, 3]
1369 b = [a, a]
1370 a.append(b)
1371
1372 # Avoid coredump when Py_FatalError() calls abort()
1373 SuppressCrashReport().__enter__()
1374
1375 # Simulate the refcount of "a" being too low (compared to the
1376 # references held on it by live data), but keeping it above zero
1377 # (to avoid deallocating it):
1378 import ctypes
1379 ctypes.pythonapi.Py_DecRef(ctypes.py_object(a))
1380 del a
1381 del b
1382
1383 # The garbage collector should now have a fatal error
1384 # when it reaches the broken object
1385 gc_collect()
1386 ''')
1387 p = subprocess.Popen([sys.executable, "-c", code],
1388 stdout=subprocess.PIPE,
1389 stderr=subprocess.PIPE)
1390 stdout, stderr = p.communicate()
1391 p.stdout.close()
1392 p.stderr.close()
1393 # Verify that stderr has a useful error message:
1394 self.assertRegex(stderr,
1395 br'gc.*\.c:[0-9]+: .*: Assertion "gc_get_refs\(.+\) .*" failed.')
1396 self.assertRegex(stderr,
1397 br'refcount is too small')
1398 # "address : 0x7fb5062efc18"
1399 # "address : 7FB5062EFC18"
1400 address_regex = br'[0-9a-fA-Fx]+'
1401 self.assertRegex(stderr,
1402 br'object address : ' + address_regex)
1403 self.assertRegex(stderr,
1404 br'object refcount : 1')
1405 self.assertRegex(stderr,
1406 br'object type : ' + address_regex)
1407 self.assertRegex(stderr,
1408 br'object type name: list')
1409 self.assertRegex(stderr,
1410 br'object repr : \[1, 2, 3, \[\[...\], \[...\]\]\]')
1411
1412
1413 class GCTogglingTests(unittest.TestCase):
1414 def setUp(self):
1415 gc.enable()
1416
1417 def tearDown(self):
1418 gc.disable()
1419
1420 @unittest.skipIf(Py_GIL_DISABLED, "requires GC generations or increments")
1421 def test_bug1055820c(self):
1422 # Corresponds to temp2c.py in the bug report. This is pretty
1423 # elaborate.
1424
1425 c0 = C1055820(0)
1426 # Move c0 into generation 2.
1427 gc.collect()
1428
1429 c1 = C1055820(1)
1430 c1.keep_c0_alive = c0
1431 del c0.loop # now only c1 keeps c0 alive
1432
1433 c2 = C1055820(2)
1434 c2wr = weakref.ref(c2) # no callback!
1435
1436 ouch = []
1437 def callback(ignored):
1438 ouch[:] = [c2wr()]
1439
1440 # The callback gets associated with a wr on an object in generation 2.
1441 c0wr = weakref.ref(c0, callback)
1442
1443 c0 = c1 = c2 = None
1444
1445 # What we've set up: c0, c1, and c2 are all trash now. c0 is in
1446 # generation 2. The only thing keeping it alive is that c1 points to
1447 # it. c1 and c2 are in generation 0, and are in self-loops. There's a
1448 # global weakref to c2 (c2wr), but that weakref has no callback.
1449 # There's also a global weakref to c0 (c0wr), and that does have a
1450 # callback, and that callback references c2 via c2wr().
1451 #
1452 # c0 has a wr with callback, which references c2wr
1453 # ^
1454 # |
1455 # | Generation 2 above dots
1456 #. . . . . . . .|. . . . . . . . . . . . . . . . . . . . . . . .
1457 # | Generation 0 below dots
1458 # |
1459 # |
1460 # ^->c1 ^->c2 has a wr but no callback
1461 # | | | |
1462 # <--v <--v
1463 #
1464 # So this is the nightmare: when generation 0 gets collected, we see
1465 # that c2 has a callback-free weakref, and c1 doesn't even have a
1466 # weakref. Collecting generation 0 doesn't see c0 at all, and c0 is
1467 # the only object that has a weakref with a callback. gc clears c1
1468 # and c2. Clearing c1 has the side effect of dropping the refcount on
1469 # c0 to 0, so c0 goes away (despite that it's in an older generation)
1470 # and c0's wr callback triggers. That in turn materializes a reference
1471 # to c2 via c2wr(), but c2 gets cleared anyway by gc.
1472
1473 # We want to let gc happen "naturally", to preserve the distinction
1474 # between generations.
1475 junk = []
1476 i = 0
1477 detector = GC_Detector()
1478 if Py_GIL_DISABLED:
1479 # The free-threaded build doesn't have multiple generations, so
1480 # just trigger a GC manually.
1481 gc.collect()
1482 assert not detector.gc_happened
1483 while not detector.gc_happened:
1484 i += 1
1485 if i > 10000:
1486 self.fail("gc didn't happen after 10000 iterations")
1487 self.assertEqual(len(ouch), 0)
1488 junk.append([]) # this will eventually trigger gc
1489
1490 self.assertEqual(len(ouch), 1) # else the callback wasn't invoked
1491 for x in ouch:
1492 # If the callback resurrected c2, the instance would be damaged,
1493 # with an empty __dict__.
1494 self.assertEqual(x, None)
1495
1496 @gc_threshold(1000, 0, 0)
1497 def test_bug1055820d(self):
1498 # Corresponds to temp2d.py in the bug report. This is very much like
1499 # test_bug1055820c, but uses a __del__ method instead of a weakref
1500 # callback to sneak in a resurrection of cyclic trash.
1501
1502 ouch = []
1503 class D(C1055820):
1504 def __del__(self):
1505 ouch[:] = [c2wr()]
1506
1507 d0 = D(0)
1508 # Move all the above into generation 2.
1509 gc.collect()
1510
1511 c1 = C1055820(1)
1512 c1.keep_d0_alive = d0
1513 del d0.loop # now only c1 keeps d0 alive
1514
1515 c2 = C1055820(2)
1516 c2wr = weakref.ref(c2) # no callback!
1517
1518 d0 = c1 = c2 = None
1519
1520 # What we've set up: d0, c1, and c2 are all trash now. d0 is in
1521 # generation 2. The only thing keeping it alive is that c1 points to
1522 # it. c1 and c2 are in generation 0, and are in self-loops. There's
1523 # a global weakref to c2 (c2wr), but that weakref has no callback.
1524 # There are no other weakrefs.
1525 #
1526 # d0 has a __del__ method that references c2wr
1527 # ^
1528 # |
1529 # | Generation 2 above dots
1530 #. . . . . . . .|. . . . . . . . . . . . . . . . . . . . . . . .
1531 # | Generation 0 below dots
1532 # |
1533 # |
1534 # ^->c1 ^->c2 has a wr but no callback
1535 # | | | |
1536 # <--v <--v
1537 #
1538 # So this is the nightmare: when generation 0 gets collected, we see
1539 # that c2 has a callback-free weakref, and c1 doesn't even have a
1540 # weakref. Collecting generation 0 doesn't see d0 at all. gc clears
1541 # c1 and c2. Clearing c1 has the side effect of dropping the refcount
1542 # on d0 to 0, so d0 goes away (despite that it's in an older
1543 # generation) and d0's __del__ triggers. That in turn materializes
1544 # a reference to c2 via c2wr(), but c2 gets cleared anyway by gc.
1545
1546 # We want to let gc happen "naturally", to preserve the distinction
1547 # between generations.
1548 detector = GC_Detector()
1549 junk = []
1550 i = 0
1551 if Py_GIL_DISABLED:
1552 # The free-threaded build doesn't have multiple generations, so
1553 # just trigger a GC manually.
1554 gc.collect()
1555 while not detector.gc_happened:
1556 i += 1
1557 if i > 10000:
1558 self.fail("gc didn't happen after 10000 iterations")
1559 self.assertEqual(len(ouch), 0)
1560 junk.append([]) # this will eventually trigger gc
1561
1562 self.assertEqual(len(ouch), 1) # else __del__ wasn't invoked
1563 for x in ouch:
1564 # If __del__ resurrected c2, the instance would be damaged, with an
1565 # empty __dict__.
1566 self.assertEqual(x, None)
1567
1568 @gc_threshold(1000, 0, 0)
1569 def test_indirect_calls_with_gc_disabled(self):
1570 junk = []
1571 i = 0
1572 detector = GC_Detector()
1573 while not detector.gc_happened:
1574 i += 1
1575 if i > 10000:
1576 self.fail("gc didn't happen after 10000 iterations")
1577 junk.append([]) # this will eventually trigger gc
1578
1579 try:
1580 gc.disable()
1581 junk = []
1582 i = 0
1583 detector = GC_Detector()
1584 while not detector.gc_happened:
1585 i += 1
1586 if i > 10000:
1587 break
1588 junk.append([]) # this may eventually trigger gc (if it is enabled)
1589
1590 self.assertEqual(i, 10001)
1591 finally:
1592 gc.enable()
1593
1594 # Ensure that setting *threshold0* to zero disables collection.
1595 @gc_threshold(0)
1596 def test_threshold_zero(self):
1597 junk = []
1598 i = 0
1599 detector = GC_Detector()
1600 while not detector.gc_happened:
1601 i += 1
1602 if i > 50000:
1603 break
1604 junk.append([]) # this may eventually trigger gc (if it is enabled)
1605
1606 self.assertEqual(i, 50001)
1607
1608
1609 class PythonFinalizationTests(unittest.TestCase):
1610 def test_ast_fini(self):
1611 # bpo-44184: Regression test for subtype_dealloc() when deallocating
1612 # an AST instance also destroy its AST type: subtype_dealloc() must
1613 # not access the type memory after deallocating the instance, since
1614 # the type memory can be freed as well. The test is also related to
1615 # _PyAST_Fini() which clears references to AST types.
1616 code = textwrap.dedent("""
1617 import ast
1618 import codecs
1619 from test import support
1620
1621 # Small AST tree to keep their AST types alive
1622 tree = ast.parse("def f(x, y): return 2*x-y")
1623
1624 # Store the tree somewhere to survive until the last GC collection
1625 support.late_deletion(tree)
1626 """)
1627 assert_python_ok("-c", code)
1628
1629 def test_warnings_fini(self):
1630 # See https://github.com/python/cpython/issues/137384
1631 code = textwrap.dedent('''
1632 import asyncio
1633 from contextvars import ContextVar
1634
1635 context_loop = ContextVar("context_loop", default=None)
1636 loop = asyncio.new_event_loop()
1637 context_loop.set(loop)
1638 ''')
1639
1640 assert_python_ok("-c", code)
1641
1642
1643 def setUpModule():
1644 global enabled, debug
1645 enabled = gc.isenabled()
1646 gc.disable()
1647 assert not gc.isenabled()
1648 debug = gc.get_debug()
1649 gc.set_debug(debug & ~gc.DEBUG_LEAK) # this test is supposed to leak
1650 gc.collect() # Delete 2nd generation garbage
1651
1652
1653 def tearDownModule():
1654 gc.set_debug(debug)
1655 # test gc.enable() even if GC is disabled by default
1656 if verbose:
1657 print("restoring automatic collection")
1658 # make sure to always test gc.enable()
1659 gc.enable()
1660 assert gc.isenabled()
1661 if not enabled:
1662 gc.disable()
1663
1664
1665 if __name__ == "__main__":
1666 unittest.main()