1 from test_support
import verify
, verbose
, TestFailed
4 def run_test(name
, thunk
):
6 print "testing %s..." % name
,
11 print "failed (expected %s but got %s)" % (result
,
13 raise TestFailed
, name
35 # since tuples are immutable we close the loop with a list
65 # Tricky: self.__init__ is a bound method, it references the instance.
68 self
.init
= self
.__init
__
76 # A() is uncollectable if it is part of a cycle, make sure it shows up
79 def __del__(self
): pass
92 for obj
in gc
.garbage
:
98 gc
.garbage
.remove(obj
)
101 # Tricky: f -> d -> f, code should call d.clear() after the exec to
104 exec("def f(): pass\n") in d
107 if gc
.collect() != 2:
111 # Verify that cyclic garbage like lists show up in gc.garbage if the
112 # SAVEALL option is enabled.
113 debug
= gc
.get_debug()
114 gc
.set_debug(debug | gc
.DEBUG_SAVEALL
)
121 for obj
in gc
.garbage
:
127 gc
.garbage
.remove(obj
)
132 # __del__ methods can trigger collection, make this to happen
133 thresholds
= gc
.get_threshold()
144 apply(gc
.set_threshold
, thresholds
)
148 run_test("lists", test_list
)
149 run_test("dicts", test_dict
)
150 run_test("tuples", test_tuple
)
151 run_test("classes", test_class
)
152 run_test("instances", test_instance
)
153 run_test("methods", test_method
)
154 run_test("functions", test_function
)
155 run_test("finalizers", test_finalizer
)
156 run_test("__del__", test_del
)
157 run_test("saveall", test_saveall
)
161 print "disabling automatic collection"
162 enabled
= gc
.isenabled()
164 verify(not gc
.isenabled() )
165 debug
= gc
.get_debug()
166 gc
.set_debug(debug
& ~gc
.DEBUG_LEAK
) # this test is supposed to leak
172 # test gc.enable() even if GC is disabled by default
174 print "restoring automatic collection"
175 # make sure to always test gc.enable()
177 verify(gc
.isenabled())