1 from test
.test_support
import verify
, verbose
, TestFailed
, vereq
5 def expect(actual
, expected
, name
):
7 raise TestFailed
, "test_%s: actual %r, expected %r" % (
8 name
, actual
, expected
)
10 def expect_nonzero(actual
, name
):
12 raise TestFailed
, "test_%s: unexpected zero" % name
14 def run_test(name
, thunk
):
16 print "testing %s..." % name
,
26 expect(gc
.collect(), 1, "list")
33 expect(gc
.collect(), 1, "dict")
36 # since tuples are immutable we close the loop with a list
43 expect(gc
.collect(), 2, "tuple")
51 expect_nonzero(gc
.collect(), "class")
53 def test_newstyleclass():
58 expect_nonzero(gc
.collect(), "staticclass")
67 expect_nonzero(gc
.collect(), "instance")
69 def test_newinstance():
76 expect_nonzero(gc
.collect(), "newinstance")
85 expect_nonzero(gc
.collect(), "newinstance(2)")
87 expect_nonzero(gc
.collect(), "newinstance(3)")
90 expect_nonzero(gc
.collect(), "newinstance(4)")
91 expect(gc
.collect(), 0, "newinstance(5)")
94 # Tricky: self.__init__ is a bound method, it references the instance.
97 self
.init
= self
.__init
__
101 expect_nonzero(gc
.collect(), "method")
103 def test_finalizer():
104 # A() is uncollectable if it is part of a cycle, make sure it shows up
107 def __del__(self
): pass
118 expect_nonzero(gc
.collect(), "finalizer")
119 for obj
in gc
.garbage
:
124 raise TestFailed
, "didn't find obj in garbage (finalizer)"
125 gc
.garbage
.remove(obj
)
127 def test_finalizer_newclass():
128 # A() is uncollectable if it is part of a cycle, make sure it shows up
131 def __del__(self
): pass
142 expect_nonzero(gc
.collect(), "finalizer")
143 for obj
in gc
.garbage
:
148 raise TestFailed
, "didn't find obj in garbage (finalizer)"
149 gc
.garbage
.remove(obj
)
152 # Tricky: f -> d -> f, code should call d.clear() after the exec to
155 exec("def f(): pass\n") in d
158 expect(gc
.collect(), 2, "function")
162 frame
= sys
._getframe
()
165 expect(gc
.collect(), 1, "frame")
169 # Verify that cyclic garbage like lists show up in gc.garbage if the
170 # SAVEALL option is enabled.
172 # First make sure we don't save away other stuff that just happens to
173 # be waiting for collection.
175 vereq(gc
.garbage
, []) # if this fails, someone else created immortal trash
181 debug
= gc
.get_debug()
182 gc
.set_debug(debug | gc
.DEBUG_SAVEALL
)
187 vereq(len(gc
.garbage
), 1)
188 obj
= gc
.garbage
.pop()
192 # __del__ methods can trigger collection, make this to happen
193 thresholds
= gc
.get_threshold()
204 gc
.set_threshold(*thresholds
)
206 def test_del_newclass():
207 # __del__ methods can trigger collection, make this to happen
208 thresholds
= gc
.get_threshold()
219 gc
.set_threshold(*thresholds
)
229 # "trashcan" is a hack to prevent stack overflow when deallocating
230 # very deeply nested tuples etc. It works in part by abusing the
231 # type pointer and refcount fields, and that can yield horrible
232 # problems when gc tries to traverse the structures.
233 # If this test fails (as it does in 2.0, 2.1 and 2.2), it will
234 # most likely die via segfault.
236 # Note: In 2.3 the possibility for compiling without cyclic gc was
237 # removed, and that in turn allows the trashcan mechanism to work
238 # via much simpler means (e.g., it never abuses the type pointer or
239 # refcount fields anymore). Since it's much less likely to cause a
240 # problem now, the various constants in this expensive (we force a lot
241 # of full collections) test are cut back from the 2.2 version.
244 for count
in range(2):
253 v
= {1: v
, 2: Ouch()}
257 def __getattr__(self
, someattribute
):
268 garbagelen
= len(gc
.garbage
)
270 # a<->b are in a trash cycle now. Collection will invoke Boom.__getattr__
271 # (to see whether a and b have __del__ methods), and __getattr__ deletes
272 # the internal "attr" attributes as a side effect. That causes the
273 # trash cycle to get reclaimed via refcounts falling to 0, thus mutating
274 # the trash graph as a side effect of merely asking whether __del__
275 # exists. This used to (before 2.3b1) crash Python. Now __getattr__
277 expect(gc
.collect(), 4, "boom")
278 expect(len(gc
.garbage
), garbagelen
, "boom")
284 def __getattr__(self
, someattribute
):
297 garbagelen
= len(gc
.garbage
)
299 # Much like test_boom(), except that __getattr__ doesn't break the
300 # cycle until the second time gc checks for __del__. As of 2.3b1,
301 # there isn't a second time, so this simply cleans up the trash cycle.
302 # We expect a, b, a.__dict__ and b.__dict__ (4 objects) to get reclaimed
304 expect(gc
.collect(), 4, "boom2")
305 expect(len(gc
.garbage
), garbagelen
, "boom2")
307 # boom__new and boom2_new are exactly like boom and boom2, except use
310 class Boom_New(object):
311 def __getattr__(self
, someattribute
):
322 garbagelen
= len(gc
.garbage
)
324 expect(gc
.collect(), 4, "boom_new")
325 expect(len(gc
.garbage
), garbagelen
, "boom_new")
327 class Boom2_New(object):
331 def __getattr__(self
, someattribute
):
337 def test_boom2_new():
344 garbagelen
= len(gc
.garbage
)
346 expect(gc
.collect(), 4, "boom2_new")
347 expect(len(gc
.garbage
), garbagelen
, "boom2_new")
349 def test_get_referents():
351 got
= gc
.get_referents(alist
)
353 expect(got
, alist
, "get_referents")
355 atuple
= tuple(alist
)
356 got
= gc
.get_referents(atuple
)
358 expect(got
, alist
, "get_referents")
361 expected
= [1, 3, 5, 7]
362 got
= gc
.get_referents(adict
)
364 expect(got
, expected
, "get_referents")
366 got
= gc
.get_referents([1, 2], {3: 4}, (0, 0, 0))
368 expect(got
, [0, 0] + range(5), "get_referents")
370 expect(gc
.get_referents(1, 'a', 4j
), [], "get_referents")
373 gc
.collect() # Delete 2nd generation garbage
374 run_test("lists", test_list
)
375 run_test("dicts", test_dict
)
376 run_test("tuples", test_tuple
)
377 run_test("classes", test_class
)
378 run_test("new style classes", test_newstyleclass
)
379 run_test("instances", test_instance
)
380 run_test("new instances", test_newinstance
)
381 run_test("methods", test_method
)
382 run_test("functions", test_function
)
383 run_test("frames", test_frame
)
384 run_test("finalizers", test_finalizer
)
385 run_test("finalizers (new class)", test_finalizer_newclass
)
386 run_test("__del__", test_del
)
387 run_test("__del__ (new class)", test_del_newclass
)
388 run_test("saveall", test_saveall
)
389 run_test("trashcan", test_trashcan
)
390 run_test("boom", test_boom
)
391 run_test("boom2", test_boom2
)
392 run_test("boom_new", test_boom_new
)
393 run_test("boom2_new", test_boom2_new
)
394 run_test("get_referents", test_get_referents
)
398 print "disabling automatic collection"
399 enabled
= gc
.isenabled()
401 verify(not gc
.isenabled())
402 debug
= gc
.get_debug()
403 gc
.set_debug(debug
& ~gc
.DEBUG_LEAK
) # this test is supposed to leak
409 # test gc.enable() even if GC is disabled by default
411 print "restoring automatic collection"
412 # make sure to always test gc.enable()
414 verify(gc
.isenabled())