blob: 8068b35c21d7fdacaca40548c45b5d50be30a012 [file] [log] [blame]
Tim Petersc708c0a2002-08-10 21:20:54 +00001from test.test_support import verify, verbose, TestFailed, vereq
Neil Schemenauer88c761a2001-07-12 13:25:53 +00002import sys
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00003import gc
Tim Petersead8b7a2004-10-30 23:09:22 +00004import weakref
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00005
Guido van Rossumc907bd82001-10-02 19:49:47 +00006def expect(actual, expected, name):
7 if actual != expected:
Tim Peters0f81ab62003-04-08 16:39:48 +00008 raise TestFailed, "test_%s: actual %r, expected %r" % (
Guido van Rossumc907bd82001-10-02 19:49:47 +00009 name, actual, expected)
10
Guido van Rossum048eb752001-10-02 21:24:57 +000011def expect_nonzero(actual, name):
12 if actual == 0:
13 raise TestFailed, "test_%s: unexpected zero" % name
Guido van Rossumc907bd82001-10-02 19:49:47 +000014
Neil Schemenauerfaae2662000-09-22 15:26:20 +000015def run_test(name, thunk):
16 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000017 print("testing %s..." % name, end=' ')
Guido van Rossumc907bd82001-10-02 19:49:47 +000018 thunk()
19 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000020 print("ok")
Neil Schemenauerfaae2662000-09-22 15:26:20 +000021
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000022def test_list():
23 l = []
24 l.append(l)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000025 gc.collect()
26 del l
Guido van Rossumc907bd82001-10-02 19:49:47 +000027 expect(gc.collect(), 1, "list")
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000028
29def test_dict():
30 d = {}
31 d[1] = d
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000032 gc.collect()
33 del d
Guido van Rossumc907bd82001-10-02 19:49:47 +000034 expect(gc.collect(), 1, "dict")
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000035
36def test_tuple():
Neil Schemenauera53cf792000-09-15 22:32:29 +000037 # since tuples are immutable we close the loop with a list
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000038 l = []
39 t = (l,)
40 l.append(t)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000041 gc.collect()
42 del t
43 del l
Guido van Rossumc907bd82001-10-02 19:49:47 +000044 expect(gc.collect(), 2, "tuple")
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000045
46def test_class():
47 class A:
48 pass
49 A.a = A
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000050 gc.collect()
51 del A
Guido van Rossum048eb752001-10-02 21:24:57 +000052 expect_nonzero(gc.collect(), "class")
53
Tim Peters1ce150c2001-10-15 22:49:27 +000054def test_newstyleclass():
Guido van Rossum048eb752001-10-02 21:24:57 +000055 class A(object):
Tim Peters1ce150c2001-10-15 22:49:27 +000056 pass
Guido van Rossum048eb752001-10-02 21:24:57 +000057 gc.collect()
58 del A
59 expect_nonzero(gc.collect(), "staticclass")
60
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000061def test_instance():
62 class A:
63 pass
64 a = A()
65 a.a = a
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000066 gc.collect()
67 del a
Guido van Rossum048eb752001-10-02 21:24:57 +000068 expect_nonzero(gc.collect(), "instance")
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000069
Guido van Rossum9475a232001-10-05 20:51:39 +000070def test_newinstance():
71 class A(object):
72 pass
73 a = A()
74 a.a = a
75 gc.collect()
76 del a
77 expect_nonzero(gc.collect(), "newinstance")
78 class B(list):
79 pass
80 class C(B, A):
81 pass
82 a = C()
83 a.a = a
84 gc.collect()
85 del a
86 expect_nonzero(gc.collect(), "newinstance(2)")
Guido van Rossum05e01ee2002-06-12 14:38:04 +000087 del B, C
88 expect_nonzero(gc.collect(), "newinstance(3)")
89 A.a = A()
90 del A
91 expect_nonzero(gc.collect(), "newinstance(4)")
92 expect(gc.collect(), 0, "newinstance(5)")
Guido van Rossum9475a232001-10-05 20:51:39 +000093
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000094def test_method():
Neil Schemenauera53cf792000-09-15 22:32:29 +000095 # Tricky: self.__init__ is a bound method, it references the instance.
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000096 class A:
97 def __init__(self):
98 self.init = self.__init__
99 a = A()
100 gc.collect()
101 del a
Guido van Rossum048eb752001-10-02 21:24:57 +0000102 expect_nonzero(gc.collect(), "method")
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000103
104def test_finalizer():
Neil Schemenauera53cf792000-09-15 22:32:29 +0000105 # A() is uncollectable if it is part of a cycle, make sure it shows up
106 # in gc.garbage.
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000107 class A:
108 def __del__(self): pass
109 class B:
110 pass
111 a = A()
112 a.a = a
113 id_a = id(a)
114 b = B()
115 b.b = b
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000116 gc.collect()
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000117 del a
118 del b
Guido van Rossum048eb752001-10-02 21:24:57 +0000119 expect_nonzero(gc.collect(), "finalizer")
Neil Schemenauerfaae2662000-09-22 15:26:20 +0000120 for obj in gc.garbage:
121 if id(obj) == id_a:
122 del obj.a
123 break
124 else:
Guido van Rossumc907bd82001-10-02 19:49:47 +0000125 raise TestFailed, "didn't find obj in garbage (finalizer)"
Neil Schemenauerfaae2662000-09-22 15:26:20 +0000126 gc.garbage.remove(obj)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000127
Guido van Rossum4aa21aa2002-08-09 17:38:16 +0000128def test_finalizer_newclass():
129 # A() is uncollectable if it is part of a cycle, make sure it shows up
130 # in gc.garbage.
131 class A(object):
132 def __del__(self): pass
133 class B(object):
134 pass
135 a = A()
136 a.a = a
137 id_a = id(a)
138 b = B()
139 b.b = b
140 gc.collect()
141 del a
142 del b
143 expect_nonzero(gc.collect(), "finalizer")
144 for obj in gc.garbage:
145 if id(obj) == id_a:
146 del obj.a
147 break
148 else:
149 raise TestFailed, "didn't find obj in garbage (finalizer)"
150 gc.garbage.remove(obj)
151
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000152def test_function():
Neil Schemenauera53cf792000-09-15 22:32:29 +0000153 # Tricky: f -> d -> f, code should call d.clear() after the exec to
154 # break the cycle.
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000155 d = {}
Georg Brandl7cae87c2006-09-06 06:51:57 +0000156 exec("def f(): pass\n", d)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000157 gc.collect()
158 del d
Guido van Rossumc907bd82001-10-02 19:49:47 +0000159 expect(gc.collect(), 2, "function")
Neil Schemenauerfaae2662000-09-22 15:26:20 +0000160
Neil Schemenauer88c761a2001-07-12 13:25:53 +0000161def test_frame():
162 def f():
163 frame = sys._getframe()
164 gc.collect()
165 f()
Guido van Rossumc907bd82001-10-02 19:49:47 +0000166 expect(gc.collect(), 1, "frame")
Neil Schemenauer88c761a2001-07-12 13:25:53 +0000167
168
Neil Schemenauerfaae2662000-09-22 15:26:20 +0000169def test_saveall():
170 # Verify that cyclic garbage like lists show up in gc.garbage if the
171 # SAVEALL option is enabled.
Tim Petersc708c0a2002-08-10 21:20:54 +0000172
173 # First make sure we don't save away other stuff that just happens to
174 # be waiting for collection.
175 gc.collect()
176 vereq(gc.garbage, []) # if this fails, someone else created immortal trash
177
Tim Petersd92ae842002-08-11 04:15:09 +0000178 L = []
179 L.append(L)
180 id_L = id(L)
Tim Peters4803c122002-08-10 21:29:56 +0000181
Tim Petersa1ad3f02002-08-10 21:32:16 +0000182 debug = gc.get_debug()
183 gc.set_debug(debug | gc.DEBUG_SAVEALL)
Tim Petersd92ae842002-08-11 04:15:09 +0000184 del L
Neil Schemenauerfaae2662000-09-22 15:26:20 +0000185 gc.collect()
Tim Petersa1ad3f02002-08-10 21:32:16 +0000186 gc.set_debug(debug)
187
188 vereq(len(gc.garbage), 1)
Tim Petersd92ae842002-08-11 04:15:09 +0000189 obj = gc.garbage.pop()
190 vereq(id(obj), id_L)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000191
Neil Schemenauera53cf792000-09-15 22:32:29 +0000192def test_del():
193 # __del__ methods can trigger collection, make this to happen
194 thresholds = gc.get_threshold()
195 gc.enable()
196 gc.set_threshold(1)
197
Fred Drake004d5e62000-10-23 17:22:08 +0000198 class A:
199 def __del__(self):
200 dir(self)
Neil Schemenauera53cf792000-09-15 22:32:29 +0000201 a = A()
202 del a
203
204 gc.disable()
Tim Petersd92ae842002-08-11 04:15:09 +0000205 gc.set_threshold(*thresholds)
Fred Drake004d5e62000-10-23 17:22:08 +0000206
Guido van Rossum4aa21aa2002-08-09 17:38:16 +0000207def test_del_newclass():
208 # __del__ methods can trigger collection, make this to happen
209 thresholds = gc.get_threshold()
210 gc.enable()
211 gc.set_threshold(1)
212
213 class A(object):
214 def __del__(self):
215 dir(self)
216 a = A()
217 del a
218
219 gc.disable()
Tim Petersd92ae842002-08-11 04:15:09 +0000220 gc.set_threshold(*thresholds)
Guido van Rossum4aa21aa2002-08-09 17:38:16 +0000221
Barry Warsawd3c38ff2006-03-07 09:46:03 +0000222def test_get_count():
223 gc.collect()
224 expect(gc.get_count(), (0, 0, 0), "get_count()")
225 a = dict()
226 expect(gc.get_count(), (1, 0, 0), "get_count()")
227
228def test_collect_generations():
229 gc.collect()
230 a = dict()
231 gc.collect(0)
232 expect(gc.get_count(), (0, 1, 0), "collect(0)")
233 gc.collect(1)
234 expect(gc.get_count(), (0, 0, 1), "collect(1)")
235 gc.collect(2)
236 expect(gc.get_count(), (0, 0, 0), "collect(1)")
237
Tim Petersd2225592002-03-28 21:08:30 +0000238class Ouch:
239 n = 0
240 def __del__(self):
241 Ouch.n = Ouch.n + 1
Tim Petersc62b95e2002-07-11 19:07:45 +0000242 if Ouch.n % 17 == 0:
Tim Petersd2225592002-03-28 21:08:30 +0000243 gc.collect()
244
245def test_trashcan():
246 # "trashcan" is a hack to prevent stack overflow when deallocating
247 # very deeply nested tuples etc. It works in part by abusing the
248 # type pointer and refcount fields, and that can yield horrible
249 # problems when gc tries to traverse the structures.
250 # If this test fails (as it does in 2.0, 2.1 and 2.2), it will
251 # most likely die via segfault.
252
Tim Petersc62b95e2002-07-11 19:07:45 +0000253 # Note: In 2.3 the possibility for compiling without cyclic gc was
254 # removed, and that in turn allows the trashcan mechanism to work
255 # via much simpler means (e.g., it never abuses the type pointer or
256 # refcount fields anymore). Since it's much less likely to cause a
257 # problem now, the various constants in this expensive (we force a lot
258 # of full collections) test are cut back from the 2.2 version.
Tim Petersd2225592002-03-28 21:08:30 +0000259 gc.enable()
Tim Petersc62b95e2002-07-11 19:07:45 +0000260 N = 150
261 for count in range(2):
Tim Petersd2225592002-03-28 21:08:30 +0000262 t = []
263 for i in range(N):
264 t = [t, Ouch()]
265 u = []
266 for i in range(N):
267 u = [u, Ouch()]
268 v = {}
269 for i in range(N):
270 v = {1: v, 2: Ouch()}
271 gc.disable()
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000272
Tim Petersbf384c22003-04-06 00:11:39 +0000273class Boom:
Tim Peters2f74fdd2003-04-05 17:46:04 +0000274 def __getattr__(self, someattribute):
Jeremy Hyltondee38ac2003-04-04 20:00:04 +0000275 del self.attr
276 raise AttributeError
277
278def test_boom():
Tim Petersbf384c22003-04-06 00:11:39 +0000279 a = Boom()
280 b = Boom()
Jeremy Hyltondee38ac2003-04-04 20:00:04 +0000281 a.attr = b
282 b.attr = a
283
284 gc.collect()
Tim Peters2f74fdd2003-04-05 17:46:04 +0000285 garbagelen = len(gc.garbage)
Jeremy Hyltondee38ac2003-04-04 20:00:04 +0000286 del a, b
Tim Petersbf384c22003-04-06 00:11:39 +0000287 # a<->b are in a trash cycle now. Collection will invoke Boom.__getattr__
Tim Peters2f74fdd2003-04-05 17:46:04 +0000288 # (to see whether a and b have __del__ methods), and __getattr__ deletes
289 # the internal "attr" attributes as a side effect. That causes the
290 # trash cycle to get reclaimed via refcounts falling to 0, thus mutating
291 # the trash graph as a side effect of merely asking whether __del__
Tim Petersf6b80452003-04-07 19:21:15 +0000292 # exists. This used to (before 2.3b1) crash Python. Now __getattr__
293 # isn't called.
294 expect(gc.collect(), 4, "boom")
Tim Peters2f74fdd2003-04-05 17:46:04 +0000295 expect(len(gc.garbage), garbagelen, "boom")
Jeremy Hyltondee38ac2003-04-04 20:00:04 +0000296
Tim Petersbf384c22003-04-06 00:11:39 +0000297class Boom2:
298 def __init__(self):
299 self.x = 0
300
301 def __getattr__(self, someattribute):
302 self.x += 1
303 if self.x > 1:
304 del self.attr
305 raise AttributeError
306
307def test_boom2():
308 a = Boom2()
309 b = Boom2()
310 a.attr = b
311 b.attr = a
312
313 gc.collect()
314 garbagelen = len(gc.garbage)
315 del a, b
316 # Much like test_boom(), except that __getattr__ doesn't break the
317 # cycle until the second time gc checks for __del__. As of 2.3b1,
318 # there isn't a second time, so this simply cleans up the trash cycle.
319 # We expect a, b, a.__dict__ and b.__dict__ (4 objects) to get reclaimed
320 # this way.
321 expect(gc.collect(), 4, "boom2")
322 expect(len(gc.garbage), garbagelen, "boom2")
323
Tim Peters8805e662003-04-08 19:44:13 +0000324# boom__new and boom2_new are exactly like boom and boom2, except use
325# new-style classes.
326
327class Boom_New(object):
328 def __getattr__(self, someattribute):
329 del self.attr
330 raise AttributeError
331
332def test_boom_new():
333 a = Boom_New()
334 b = Boom_New()
335 a.attr = b
336 b.attr = a
337
338 gc.collect()
339 garbagelen = len(gc.garbage)
340 del a, b
341 expect(gc.collect(), 4, "boom_new")
342 expect(len(gc.garbage), garbagelen, "boom_new")
343
344class Boom2_New(object):
345 def __init__(self):
346 self.x = 0
347
348 def __getattr__(self, someattribute):
349 self.x += 1
350 if self.x > 1:
351 del self.attr
352 raise AttributeError
353
354def test_boom2_new():
355 a = Boom2_New()
356 b = Boom2_New()
357 a.attr = b
358 b.attr = a
359
360 gc.collect()
361 garbagelen = len(gc.garbage)
362 del a, b
363 expect(gc.collect(), 4, "boom2_new")
364 expect(len(gc.garbage), garbagelen, "boom2_new")
365
Tim Peters730f5532003-04-08 17:17:17 +0000366def test_get_referents():
Tim Peters0f81ab62003-04-08 16:39:48 +0000367 alist = [1, 3, 5]
Tim Peters730f5532003-04-08 17:17:17 +0000368 got = gc.get_referents(alist)
Tim Peters0f81ab62003-04-08 16:39:48 +0000369 got.sort()
Tim Peters730f5532003-04-08 17:17:17 +0000370 expect(got, alist, "get_referents")
Tim Peters0f81ab62003-04-08 16:39:48 +0000371
372 atuple = tuple(alist)
Tim Peters730f5532003-04-08 17:17:17 +0000373 got = gc.get_referents(atuple)
Tim Peters0f81ab62003-04-08 16:39:48 +0000374 got.sort()
Tim Peters730f5532003-04-08 17:17:17 +0000375 expect(got, alist, "get_referents")
Tim Peters0f81ab62003-04-08 16:39:48 +0000376
377 adict = {1: 3, 5: 7}
378 expected = [1, 3, 5, 7]
Tim Peters730f5532003-04-08 17:17:17 +0000379 got = gc.get_referents(adict)
Tim Peters0f81ab62003-04-08 16:39:48 +0000380 got.sort()
Tim Peters730f5532003-04-08 17:17:17 +0000381 expect(got, expected, "get_referents")
Tim Peters0f81ab62003-04-08 16:39:48 +0000382
Tim Peters730f5532003-04-08 17:17:17 +0000383 got = gc.get_referents([1, 2], {3: 4}, (0, 0, 0))
Tim Peters0f81ab62003-04-08 16:39:48 +0000384 got.sort()
Tim Peters730f5532003-04-08 17:17:17 +0000385 expect(got, [0, 0] + range(5), "get_referents")
Tim Peters0f81ab62003-04-08 16:39:48 +0000386
Tim Peters730f5532003-04-08 17:17:17 +0000387 expect(gc.get_referents(1, 'a', 4j), [], "get_referents")
Tim Peters0f81ab62003-04-08 16:39:48 +0000388
Tim Petersead8b7a2004-10-30 23:09:22 +0000389# Bug 1055820 has several tests of longstanding bugs involving weakrefs and
390# cyclic gc.
391
392# An instance of C1055820 has a self-loop, so becomes cyclic trash when
393# unreachable.
394class C1055820(object):
395 def __init__(self, i):
396 self.i = i
397 self.loop = self
398
399class GC_Detector(object):
400 # Create an instance I. Then gc hasn't happened again so long as
401 # I.gc_happened is false.
402
403 def __init__(self):
404 self.gc_happened = False
405
406 def it_happened(ignored):
407 self.gc_happened = True
408
409 # Create a piece of cyclic trash that triggers it_happened when
410 # gc collects it.
411 self.wr = weakref.ref(C1055820(666), it_happened)
412
413def test_bug1055820b():
414 # Corresponds to temp2b.py in the bug report.
415
416 ouch = []
417 def callback(ignored):
418 ouch[:] = [wr() for wr in WRs]
419
420 Cs = [C1055820(i) for i in range(2)]
421 WRs = [weakref.ref(c, callback) for c in Cs]
422 c = None
423
424 gc.collect()
425 expect(len(ouch), 0, "bug1055820b")
426 # Make the two instances trash, and collect again. The bug was that
427 # the callback materialized a strong reference to an instance, but gc
428 # cleared the instance's dict anyway.
429 Cs = None
430 gc.collect()
431 expect(len(ouch), 2, "bug1055820b") # else the callbacks didn't run
432 for x in ouch:
433 # If the callback resurrected one of these guys, the instance
434 # would be damaged, with an empty __dict__.
435 expect(x, None, "bug1055820b")
436
437def test_bug1055820c():
438 # Corresponds to temp2c.py in the bug report. This is pretty elaborate.
439
440 c0 = C1055820(0)
441 # Move c0 into generation 2.
442 gc.collect()
443
444 c1 = C1055820(1)
445 c1.keep_c0_alive = c0
446 del c0.loop # now only c1 keeps c0 alive
447
448 c2 = C1055820(2)
449 c2wr = weakref.ref(c2) # no callback!
450
451 ouch = []
452 def callback(ignored):
453 ouch[:] = [c2wr()]
454
455 # The callback gets associated with a wr on an object in generation 2.
456 c0wr = weakref.ref(c0, callback)
457
458 c0 = c1 = c2 = None
459
460 # What we've set up: c0, c1, and c2 are all trash now. c0 is in
461 # generation 2. The only thing keeping it alive is that c1 points to it.
462 # c1 and c2 are in generation 0, and are in self-loops. There's a global
463 # weakref to c2 (c2wr), but that weakref has no callback. There's also
464 # a global weakref to c0 (c0wr), and that does have a callback, and that
465 # callback references c2 via c2wr().
466 #
467 # c0 has a wr with callback, which references c2wr
468 # ^
469 # |
470 # | Generation 2 above dots
471 #. . . . . . . .|. . . . . . . . . . . . . . . . . . . . . . . .
472 # | Generation 0 below dots
473 # |
474 # |
475 # ^->c1 ^->c2 has a wr but no callback
476 # | | | |
477 # <--v <--v
478 #
479 # So this is the nightmare: when generation 0 gets collected, we see that
480 # c2 has a callback-free weakref, and c1 doesn't even have a weakref.
481 # Collecting generation 0 doesn't see c0 at all, and c0 is the only object
482 # that has a weakref with a callback. gc clears c1 and c2. Clearing c1
483 # has the side effect of dropping the refcount on c0 to 0, so c0 goes
484 # away (despite that it's in an older generation) and c0's wr callback
485 # triggers. That in turn materializes a reference to c2 via c2wr(), but
486 # c2 gets cleared anyway by gc.
487
488 # We want to let gc happen "naturally", to preserve the distinction
489 # between generations.
490 junk = []
491 i = 0
492 detector = GC_Detector()
493 while not detector.gc_happened:
494 i += 1
495 if i > 10000:
496 raise TestFailed("gc didn't happen after 10000 iterations")
497 expect(len(ouch), 0, "bug1055820c")
498 junk.append([]) # this will eventually trigger gc
499
500 expect(len(ouch), 1, "bug1055820c") # else the callback wasn't invoked
501 for x in ouch:
502 # If the callback resurrected c2, the instance would be damaged,
503 # with an empty __dict__.
504 expect(x, None, "bug1055820c")
505
506def test_bug1055820d():
507 # Corresponds to temp2d.py in the bug report. This is very much like
508 # test_bug1055820c, but uses a __del__ method instead of a weakref
509 # callback to sneak in a resurrection of cyclic trash.
510
511 ouch = []
512 class D(C1055820):
513 def __del__(self):
514 ouch[:] = [c2wr()]
515
516 d0 = D(0)
517 # Move all the above into generation 2.
518 gc.collect()
519
520 c1 = C1055820(1)
521 c1.keep_d0_alive = d0
522 del d0.loop # now only c1 keeps d0 alive
523
524 c2 = C1055820(2)
525 c2wr = weakref.ref(c2) # no callback!
526
527 d0 = c1 = c2 = None
528
529 # What we've set up: d0, c1, and c2 are all trash now. d0 is in
530 # generation 2. The only thing keeping it alive is that c1 points to it.
531 # c1 and c2 are in generation 0, and are in self-loops. There's a global
532 # weakref to c2 (c2wr), but that weakref has no callback. There are no
533 # other weakrefs.
534 #
535 # d0 has a __del__ method that references c2wr
536 # ^
537 # |
538 # | Generation 2 above dots
539 #. . . . . . . .|. . . . . . . . . . . . . . . . . . . . . . . .
540 # | Generation 0 below dots
541 # |
542 # |
543 # ^->c1 ^->c2 has a wr but no callback
544 # | | | |
545 # <--v <--v
546 #
547 # So this is the nightmare: when generation 0 gets collected, we see that
548 # c2 has a callback-free weakref, and c1 doesn't even have a weakref.
549 # Collecting generation 0 doesn't see d0 at all. gc clears c1 and c2.
550 # Clearing c1 has the side effect of dropping the refcount on d0 to 0, so
551 # d0 goes away (despite that it's in an older generation) and d0's __del__
552 # triggers. That in turn materializes a reference to c2 via c2wr(), but
553 # c2 gets cleared anyway by gc.
554
555 # We want to let gc happen "naturally", to preserve the distinction
556 # between generations.
557 detector = GC_Detector()
558 junk = []
559 i = 0
560 while not detector.gc_happened:
561 i += 1
562 if i > 10000:
563 raise TestFailed("gc didn't happen after 10000 iterations")
564 expect(len(ouch), 0, "bug1055820d")
565 junk.append([]) # this will eventually trigger gc
566
567 expect(len(ouch), 1, "bug1055820d") # else __del__ wasn't invoked
568 for x in ouch:
569 # If __del__ resurrected c2, the instance would be damaged, with an
570 # empty __dict__.
571 expect(x, None, "bug1055820d")
572
573
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000574def test_all():
Guido van Rossumc907bd82001-10-02 19:49:47 +0000575 gc.collect() # Delete 2nd generation garbage
Neil Schemenauerfaae2662000-09-22 15:26:20 +0000576 run_test("lists", test_list)
577 run_test("dicts", test_dict)
578 run_test("tuples", test_tuple)
579 run_test("classes", test_class)
Tim Peters1ce150c2001-10-15 22:49:27 +0000580 run_test("new style classes", test_newstyleclass)
Neil Schemenauerfaae2662000-09-22 15:26:20 +0000581 run_test("instances", test_instance)
Guido van Rossum9475a232001-10-05 20:51:39 +0000582 run_test("new instances", test_newinstance)
Neil Schemenauerfaae2662000-09-22 15:26:20 +0000583 run_test("methods", test_method)
584 run_test("functions", test_function)
Neil Schemenauer88c761a2001-07-12 13:25:53 +0000585 run_test("frames", test_frame)
Neil Schemenauerfaae2662000-09-22 15:26:20 +0000586 run_test("finalizers", test_finalizer)
Guido van Rossum4aa21aa2002-08-09 17:38:16 +0000587 run_test("finalizers (new class)", test_finalizer_newclass)
Neil Schemenauerfaae2662000-09-22 15:26:20 +0000588 run_test("__del__", test_del)
Guido van Rossum4aa21aa2002-08-09 17:38:16 +0000589 run_test("__del__ (new class)", test_del_newclass)
Barry Warsawd3c38ff2006-03-07 09:46:03 +0000590 run_test("get_count()", test_get_count)
591 run_test("collect(n)", test_collect_generations)
Neil Schemenauerfaae2662000-09-22 15:26:20 +0000592 run_test("saveall", test_saveall)
Tim Petersd2225592002-03-28 21:08:30 +0000593 run_test("trashcan", test_trashcan)
Jeremy Hyltondee38ac2003-04-04 20:00:04 +0000594 run_test("boom", test_boom)
Tim Petersbf384c22003-04-06 00:11:39 +0000595 run_test("boom2", test_boom2)
Tim Peters8805e662003-04-08 19:44:13 +0000596 run_test("boom_new", test_boom_new)
597 run_test("boom2_new", test_boom2_new)
Tim Peters730f5532003-04-08 17:17:17 +0000598 run_test("get_referents", test_get_referents)
Tim Petersead8b7a2004-10-30 23:09:22 +0000599 run_test("bug1055820b", test_bug1055820b)
600
601 gc.enable()
602 try:
603 run_test("bug1055820c", test_bug1055820c)
604 finally:
605 gc.disable()
606
607 gc.enable()
608 try:
609 run_test("bug1055820d", test_bug1055820d)
610 finally:
611 gc.disable()
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000612
Neil Schemenauerfaae2662000-09-22 15:26:20 +0000613def test():
614 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000615 print("disabling automatic collection")
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000616 enabled = gc.isenabled()
617 gc.disable()
Guido van Rossum3fcd4522002-06-13 11:53:12 +0000618 verify(not gc.isenabled())
Neil Schemenauerfaae2662000-09-22 15:26:20 +0000619 debug = gc.get_debug()
620 gc.set_debug(debug & ~gc.DEBUG_LEAK) # this test is supposed to leak
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000621
Neil Schemenauerfaae2662000-09-22 15:26:20 +0000622 try:
623 test_all()
624 finally:
625 gc.set_debug(debug)
626 # test gc.enable() even if GC is disabled by default
627 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000628 print("restoring automatic collection")
Neil Schemenauerfaae2662000-09-22 15:26:20 +0000629 # make sure to always test gc.enable()
630 gc.enable()
Marc-André Lemburg36619082001-01-17 19:11:13 +0000631 verify(gc.isenabled())
Neil Schemenauerfaae2662000-09-22 15:26:20 +0000632 if not enabled:
633 gc.disable()
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000634
635
Neil Schemenauerfaae2662000-09-22 15:26:20 +0000636test()