blob: 1c008bee00d8341b0384fba14c0697628130bb66 [file] [log] [blame]
Marc-André Lemburg36619082001-01-17 19:11:13 +00001from test_support import verify, verbose, TestFailed
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00002import gc
3
Neil Schemenauerfaae2662000-09-22 15:26:20 +00004def run_test(name, thunk):
5 if verbose:
6 print "testing %s..." % name,
7 try:
8 thunk()
9 except TestFailed:
10 if verbose:
11 print "failed (expected %s but got %s)" % (result,
12 test_result)
13 raise TestFailed, name
14 else:
15 if verbose:
16 print "ok"
17
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000018def test_list():
19 l = []
20 l.append(l)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000021 gc.collect()
22 del l
Neil Schemenauerfaae2662000-09-22 15:26:20 +000023 if gc.collect() != 1:
24 raise TestFailed
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000025
26def test_dict():
27 d = {}
28 d[1] = d
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000029 gc.collect()
30 del d
Neil Schemenauerfaae2662000-09-22 15:26:20 +000031 if gc.collect() != 1:
32 raise TestFailed
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000033
34def test_tuple():
Neil Schemenauera53cf792000-09-15 22:32:29 +000035 # since tuples are immutable we close the loop with a list
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000036 l = []
37 t = (l,)
38 l.append(t)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000039 gc.collect()
40 del t
41 del l
Neil Schemenauerfaae2662000-09-22 15:26:20 +000042 if gc.collect() != 2:
43 raise TestFailed
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000044
45def test_class():
46 class A:
47 pass
48 A.a = A
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000049 gc.collect()
50 del A
Neil Schemenauerfaae2662000-09-22 15:26:20 +000051 if gc.collect() == 0:
52 raise TestFailed
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000053
54def test_instance():
55 class A:
56 pass
57 a = A()
58 a.a = a
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000059 gc.collect()
60 del a
Neil Schemenauerfaae2662000-09-22 15:26:20 +000061 if gc.collect() == 0:
62 raise TestFailed
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000063
64def test_method():
Neil Schemenauera53cf792000-09-15 22:32:29 +000065 # Tricky: self.__init__ is a bound method, it references the instance.
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000066 class A:
67 def __init__(self):
68 self.init = self.__init__
69 a = A()
70 gc.collect()
71 del a
Neil Schemenauerfaae2662000-09-22 15:26:20 +000072 if gc.collect() == 0:
73 raise TestFailed
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000074
75def test_finalizer():
Neil Schemenauera53cf792000-09-15 22:32:29 +000076 # A() is uncollectable if it is part of a cycle, make sure it shows up
77 # in gc.garbage.
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000078 class A:
79 def __del__(self): pass
80 class B:
81 pass
82 a = A()
83 a.a = a
84 id_a = id(a)
85 b = B()
86 b.b = b
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000087 gc.collect()
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000088 del a
89 del b
Neil Schemenauerfaae2662000-09-22 15:26:20 +000090 if gc.collect() == 0:
91 raise TestFailed
92 for obj in gc.garbage:
93 if id(obj) == id_a:
94 del obj.a
95 break
96 else:
97 raise TestFailed
98 gc.garbage.remove(obj)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000099
100def test_function():
Neil Schemenauera53cf792000-09-15 22:32:29 +0000101 # Tricky: f -> d -> f, code should call d.clear() after the exec to
102 # break the cycle.
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000103 d = {}
104 exec("def f(): pass\n") in d
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000105 gc.collect()
106 del d
Neil Schemenauerfaae2662000-09-22 15:26:20 +0000107 if gc.collect() != 2:
108 raise TestFailed
109
110def test_saveall():
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)
115 l = []
116 l.append(l)
117 id_l = id(l)
118 del l
119 gc.collect()
120 try:
121 for obj in gc.garbage:
122 if id(obj) == id_l:
123 del obj[:]
124 break
125 else:
126 raise TestFailed
127 gc.garbage.remove(obj)
128 finally:
129 gc.set_debug(debug)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000130
Neil Schemenauera53cf792000-09-15 22:32:29 +0000131def test_del():
132 # __del__ methods can trigger collection, make this to happen
133 thresholds = gc.get_threshold()
134 gc.enable()
135 gc.set_threshold(1)
136
Fred Drake004d5e62000-10-23 17:22:08 +0000137 class A:
138 def __del__(self):
139 dir(self)
Neil Schemenauera53cf792000-09-15 22:32:29 +0000140 a = A()
141 del a
142
143 gc.disable()
144 apply(gc.set_threshold, thresholds)
Fred Drake004d5e62000-10-23 17:22:08 +0000145
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000146
147def test_all():
Neil Schemenauerfaae2662000-09-22 15:26:20 +0000148 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)
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000158
Neil Schemenauerfaae2662000-09-22 15:26:20 +0000159def test():
160 if verbose:
161 print "disabling automatic collection"
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000162 enabled = gc.isenabled()
163 gc.disable()
Marc-André Lemburg36619082001-01-17 19:11:13 +0000164 verify(not gc.isenabled() )
Neil Schemenauerfaae2662000-09-22 15:26:20 +0000165 debug = gc.get_debug()
166 gc.set_debug(debug & ~gc.DEBUG_LEAK) # this test is supposed to leak
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000167
Neil Schemenauerfaae2662000-09-22 15:26:20 +0000168 try:
169 test_all()
170 finally:
171 gc.set_debug(debug)
172 # test gc.enable() even if GC is disabled by default
173 if verbose:
174 print "restoring automatic collection"
175 # make sure to always test gc.enable()
176 gc.enable()
Marc-André Lemburg36619082001-01-17 19:11:13 +0000177 verify(gc.isenabled())
Neil Schemenauerfaae2662000-09-22 15:26:20 +0000178 if not enabled:
179 gc.disable()
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000180
181
Neil Schemenauerfaae2662000-09-22 15:26:20 +0000182test()