1 from test
.test_support
import verify
, verbose
, TestFailed
, vereq
6 def expect(actual
, expected
, name
):
8 raise TestFailed
, "test_%s: actual %r, expected %r" % (
9 name
, actual
, expected
)
11 def expect_nonzero(actual
, name
):
13 raise TestFailed
, "test_%s: unexpected zero" % name
15 def run_test(name
, thunk
):
17 print "testing %s..." % name
,
27 expect(gc
.collect(), 1, "list")
34 expect(gc
.collect(), 1, "dict")
37 # since tuples are immutable we close the loop with a list
44 expect(gc
.collect(), 2, "tuple")
52 expect_nonzero(gc
.collect(), "class")
54 def test_newstyleclass():
59 expect_nonzero(gc
.collect(), "staticclass")
68 expect_nonzero(gc
.collect(), "instance")
70 def test_newinstance():
77 expect_nonzero(gc
.collect(), "newinstance")
86 expect_nonzero(gc
.collect(), "newinstance(2)")
88 expect_nonzero(gc
.collect(), "newinstance(3)")
91 expect_nonzero(gc
.collect(), "newinstance(4)")
92 expect(gc
.collect(), 0, "newinstance(5)")
95 # Tricky: self.__init__ is a bound method, it references the instance.
98 self
.init
= self
.__init
__
102 expect_nonzero(gc
.collect(), "method")
104 def test_finalizer():
105 # A() is uncollectable if it is part of a cycle, make sure it shows up
108 def __del__(self
): pass
119 expect_nonzero(gc
.collect(), "finalizer")
120 for obj
in gc
.garbage
:
125 raise TestFailed
, "didn't find obj in garbage (finalizer)"
126 gc
.garbage
.remove(obj
)
128 def test_finalizer_newclass():
129 # A() is uncollectable if it is part of a cycle, make sure it shows up
132 def __del__(self
): pass
143 expect_nonzero(gc
.collect(), "finalizer")
144 for obj
in gc
.garbage
:
149 raise TestFailed
, "didn't find obj in garbage (finalizer)"
150 gc
.garbage
.remove(obj
)
153 # Tricky: f -> d -> f, code should call d.clear() after the exec to
156 exec("def f(): pass\n") in d
159 expect(gc
.collect(), 2, "function")
163 frame
= sys
._getframe
()
166 expect(gc
.collect(), 1, "frame")
170 # Verify that cyclic garbage like lists show up in gc.garbage if the
171 # SAVEALL option is enabled.
173 # First make sure we don't save away other stuff that just happens to
174 # be waiting for collection.
176 vereq(gc
.garbage
, []) # if this fails, someone else created immortal trash
182 debug
= gc
.get_debug()
183 gc
.set_debug(debug | gc
.DEBUG_SAVEALL
)
188 vereq(len(gc
.garbage
), 1)
189 obj
= gc
.garbage
.pop()
193 # __del__ methods can trigger collection, make this to happen
194 thresholds
= gc
.get_threshold()
205 gc
.set_threshold(*thresholds
)
207 def test_del_newclass():
208 # __del__ methods can trigger collection, make this to happen
209 thresholds
= gc
.get_threshold()
220 gc
.set_threshold(*thresholds
)
230 # "trashcan" is a hack to prevent stack overflow when deallocating
231 # very deeply nested tuples etc. It works in part by abusing the
232 # type pointer and refcount fields, and that can yield horrible
233 # problems when gc tries to traverse the structures.
234 # If this test fails (as it does in 2.0, 2.1 and 2.2), it will
235 # most likely die via segfault.
237 # Note: In 2.3 the possibility for compiling without cyclic gc was
238 # removed, and that in turn allows the trashcan mechanism to work
239 # via much simpler means (e.g., it never abuses the type pointer or
240 # refcount fields anymore). Since it's much less likely to cause a
241 # problem now, the various constants in this expensive (we force a lot
242 # of full collections) test are cut back from the 2.2 version.
245 for count
in range(2):
254 v
= {1: v
, 2: Ouch()}
258 def __getattr__(self
, someattribute
):
269 garbagelen
= len(gc
.garbage
)
271 # a<->b are in a trash cycle now. Collection will invoke Boom.__getattr__
272 # (to see whether a and b have __del__ methods), and __getattr__ deletes
273 # the internal "attr" attributes as a side effect. That causes the
274 # trash cycle to get reclaimed via refcounts falling to 0, thus mutating
275 # the trash graph as a side effect of merely asking whether __del__
276 # exists. This used to (before 2.3b1) crash Python. Now __getattr__
278 expect(gc
.collect(), 4, "boom")
279 expect(len(gc
.garbage
), garbagelen
, "boom")
285 def __getattr__(self
, someattribute
):
298 garbagelen
= len(gc
.garbage
)
300 # Much like test_boom(), except that __getattr__ doesn't break the
301 # cycle until the second time gc checks for __del__. As of 2.3b1,
302 # there isn't a second time, so this simply cleans up the trash cycle.
303 # We expect a, b, a.__dict__ and b.__dict__ (4 objects) to get reclaimed
305 expect(gc
.collect(), 4, "boom2")
306 expect(len(gc
.garbage
), garbagelen
, "boom2")
308 # boom__new and boom2_new are exactly like boom and boom2, except use
311 class Boom_New(object):
312 def __getattr__(self
, someattribute
):
323 garbagelen
= len(gc
.garbage
)
325 expect(gc
.collect(), 4, "boom_new")
326 expect(len(gc
.garbage
), garbagelen
, "boom_new")
328 class Boom2_New(object):
332 def __getattr__(self
, someattribute
):
338 def test_boom2_new():
345 garbagelen
= len(gc
.garbage
)
347 expect(gc
.collect(), 4, "boom2_new")
348 expect(len(gc
.garbage
), garbagelen
, "boom2_new")
350 def test_get_referents():
352 got
= gc
.get_referents(alist
)
354 expect(got
, alist
, "get_referents")
356 atuple
= tuple(alist
)
357 got
= gc
.get_referents(atuple
)
359 expect(got
, alist
, "get_referents")
362 expected
= [1, 3, 5, 7]
363 got
= gc
.get_referents(adict
)
365 expect(got
, expected
, "get_referents")
367 got
= gc
.get_referents([1, 2], {3: 4}, (0, 0, 0))
369 expect(got
, [0, 0] + range(5), "get_referents")
371 expect(gc
.get_referents(1, 'a', 4j
), [], "get_referents")
373 # Bug 1055820 has several tests of longstanding bugs involving weakrefs and
376 # An instance of C1055820 has a self-loop, so becomes cyclic trash when
378 class C1055820(object):
379 def __init__(self
, i
):
383 class GC_Detector(object):
384 # Create an instance I. Then gc hasn't happened again so long as
385 # I.gc_happened is false.
388 self
.gc_happened
= False
390 def it_happened(ignored
):
391 self
.gc_happened
= True
393 # Create a piece of cyclic trash that triggers it_happened when
395 self
.wr
= weakref
.ref(C1055820(666), it_happened
)
397 def test_bug1055820b():
398 # Corresponds to temp2b.py in the bug report.
401 def callback(ignored
):
402 ouch
[:] = [wr() for wr
in WRs
]
404 Cs
= [C1055820(i
) for i
in range(2)]
405 WRs
= [weakref
.ref(c
, callback
) for c
in Cs
]
409 expect(len(ouch
), 0, "bug1055820b")
410 # Make the two instances trash, and collect again. The bug was that
411 # the callback materialized a strong reference to an instance, but gc
412 # cleared the instance's dict anyway.
415 expect(len(ouch
), 2, "bug1055820b") # else the callbacks didn't run
417 # If the callback resurrected one of these guys, the instance
418 # would be damaged, with an empty __dict__.
419 expect(x
, None, "bug1055820b")
421 def test_bug1055820c():
422 # Corresponds to temp2c.py in the bug report. This is pretty elaborate.
425 # Move c0 into generation 2.
429 c1
.keep_c0_alive
= c0
430 del c0
.loop
# now only c1 keeps c0 alive
433 c2wr
= weakref
.ref(c2
) # no callback!
436 def callback(ignored
):
439 # The callback gets associated with a wr on an object in generation 2.
440 c0wr
= weakref
.ref(c0
, callback
)
444 # What we've set up: c0, c1, and c2 are all trash now. c0 is in
445 # generation 2. The only thing keeping it alive is that c1 points to it.
446 # c1 and c2 are in generation 0, and are in self-loops. There's a global
447 # weakref to c2 (c2wr), but that weakref has no callback. There's also
448 # a global weakref to c0 (c0wr), and that does have a callback, and that
449 # callback references c2 via c2wr().
451 # c0 has a wr with callback, which references c2wr
454 # | Generation 2 above dots
455 #. . . . . . . .|. . . . . . . . . . . . . . . . . . . . . . . .
456 # | Generation 0 below dots
459 # ^->c1 ^->c2 has a wr but no callback
463 # So this is the nightmare: when generation 0 gets collected, we see that
464 # c2 has a callback-free weakref, and c1 doesn't even have a weakref.
465 # Collecting generation 0 doesn't see c0 at all, and c0 is the only object
466 # that has a weakref with a callback. gc clears c1 and c2. Clearing c1
467 # has the side effect of dropping the refcount on c0 to 0, so c0 goes
468 # away (despite that it's in an older generation) and c0's wr callback
469 # triggers. That in turn materializes a reference to c2 via c2wr(), but
470 # c2 gets cleared anyway by gc.
472 # We want to let gc happen "naturally", to preserve the distinction
473 # between generations.
476 detector
= GC_Detector()
477 while not detector
.gc_happened
:
480 raise TestFailed("gc didn't happen after 10000 iterations")
481 expect(len(ouch
), 0, "bug1055820c")
482 junk
.append([]) # this will eventually trigger gc
484 expect(len(ouch
), 1, "bug1055820c") # else the callback wasn't invoked
486 # If the callback resurrected c2, the instance would be damaged,
487 # with an empty __dict__.
488 expect(x
, None, "bug1055820c")
490 def test_bug1055820d():
491 # Corresponds to temp2d.py in the bug report. This is very much like
492 # test_bug1055820c, but uses a __del__ method instead of a weakref
493 # callback to sneak in a resurrection of cyclic trash.
501 # Move all the above into generation 2.
505 c1
.keep_d0_alive
= d0
506 del d0
.loop
# now only c1 keeps d0 alive
509 c2wr
= weakref
.ref(c2
) # no callback!
513 # What we've set up: d0, c1, and c2 are all trash now. d0 is in
514 # generation 2. The only thing keeping it alive is that c1 points to it.
515 # c1 and c2 are in generation 0, and are in self-loops. There's a global
516 # weakref to c2 (c2wr), but that weakref has no callback. There are no
519 # d0 has a __del__ method that references c2wr
522 # | Generation 2 above dots
523 #. . . . . . . .|. . . . . . . . . . . . . . . . . . . . . . . .
524 # | Generation 0 below dots
527 # ^->c1 ^->c2 has a wr but no callback
531 # So this is the nightmare: when generation 0 gets collected, we see that
532 # c2 has a callback-free weakref, and c1 doesn't even have a weakref.
533 # Collecting generation 0 doesn't see d0 at all. gc clears c1 and c2.
534 # Clearing c1 has the side effect of dropping the refcount on d0 to 0, so
535 # d0 goes away (despite that it's in an older generation) and d0's __del__
536 # triggers. That in turn materializes a reference to c2 via c2wr(), but
537 # c2 gets cleared anyway by gc.
539 # We want to let gc happen "naturally", to preserve the distinction
540 # between generations.
541 detector
= GC_Detector()
544 while not detector
.gc_happened
:
547 raise TestFailed("gc didn't happen after 10000 iterations")
548 expect(len(ouch
), 0, "bug1055820d")
549 junk
.append([]) # this will eventually trigger gc
551 expect(len(ouch
), 1, "bug1055820d") # else __del__ wasn't invoked
553 # If __del__ resurrected c2, the instance would be damaged, with an
555 expect(x
, None, "bug1055820d")
559 gc
.collect() # Delete 2nd generation garbage
560 run_test("lists", test_list
)
561 run_test("dicts", test_dict
)
562 run_test("tuples", test_tuple
)
563 run_test("classes", test_class
)
564 run_test("new style classes", test_newstyleclass
)
565 run_test("instances", test_instance
)
566 run_test("new instances", test_newinstance
)
567 run_test("methods", test_method
)
568 run_test("functions", test_function
)
569 run_test("frames", test_frame
)
570 run_test("finalizers", test_finalizer
)
571 run_test("finalizers (new class)", test_finalizer_newclass
)
572 run_test("__del__", test_del
)
573 run_test("__del__ (new class)", test_del_newclass
)
574 run_test("saveall", test_saveall
)
575 run_test("trashcan", test_trashcan
)
576 run_test("boom", test_boom
)
577 run_test("boom2", test_boom2
)
578 run_test("boom_new", test_boom_new
)
579 run_test("boom2_new", test_boom2_new
)
580 run_test("get_referents", test_get_referents
)
581 run_test("bug1055820b", test_bug1055820b
)
585 run_test("bug1055820c", test_bug1055820c
)
591 run_test("bug1055820d", test_bug1055820d
)
597 print "disabling automatic collection"
598 enabled
= gc
.isenabled()
600 verify(not gc
.isenabled())
601 debug
= gc
.get_debug()
602 gc
.set_debug(debug
& ~gc
.DEBUG_LEAK
) # this test is supposed to leak
608 # test gc.enable() even if GC is disabled by default
610 print "restoring automatic collection"
611 # make sure to always test gc.enable()
613 verify(gc
.isenabled())