1 | n/a | import unittest |
---|
2 | n/a | from test.support import (verbose, refcount_test, run_unittest, |
---|
3 | n/a | strip_python_stderr, cpython_only, start_threads, |
---|
4 | n/a | temp_dir, requires_type_collecting) |
---|
5 | n/a | from test.support.script_helper import assert_python_ok, make_script |
---|
6 | n/a | |
---|
7 | n/a | import sys |
---|
8 | n/a | import time |
---|
9 | n/a | import gc |
---|
10 | n/a | import weakref |
---|
11 | n/a | |
---|
12 | n/a | try: |
---|
13 | n/a | import threading |
---|
14 | n/a | except ImportError: |
---|
15 | n/a | threading = None |
---|
16 | n/a | |
---|
17 | n/a | try: |
---|
18 | n/a | from _testcapi import with_tp_del |
---|
19 | n/a | except ImportError: |
---|
20 | n/a | def with_tp_del(cls): |
---|
21 | n/a | class C(object): |
---|
22 | n/a | def __new__(cls, *args, **kwargs): |
---|
23 | n/a | raise TypeError('requires _testcapi.with_tp_del') |
---|
24 | n/a | return C |
---|
25 | n/a | |
---|
26 | n/a | ### Support code |
---|
27 | n/a | ############################################################################### |
---|
28 | n/a | |
---|
29 | n/a | # Bug 1055820 has several tests of longstanding bugs involving weakrefs and |
---|
30 | n/a | # cyclic gc. |
---|
31 | n/a | |
---|
32 | n/a | # An instance of C1055820 has a self-loop, so becomes cyclic trash when |
---|
33 | n/a | # unreachable. |
---|
34 | n/a | class C1055820(object): |
---|
35 | n/a | def __init__(self, i): |
---|
36 | n/a | self.i = i |
---|
37 | n/a | self.loop = self |
---|
38 | n/a | |
---|
39 | n/a | class GC_Detector(object): |
---|
40 | n/a | # Create an instance I. Then gc hasn't happened again so long as |
---|
41 | n/a | # I.gc_happened is false. |
---|
42 | n/a | |
---|
43 | n/a | def __init__(self): |
---|
44 | n/a | self.gc_happened = False |
---|
45 | n/a | |
---|
46 | n/a | def it_happened(ignored): |
---|
47 | n/a | self.gc_happened = True |
---|
48 | n/a | |
---|
49 | n/a | # Create a piece of cyclic trash that triggers it_happened when |
---|
50 | n/a | # gc collects it. |
---|
51 | n/a | self.wr = weakref.ref(C1055820(666), it_happened) |
---|
52 | n/a | |
---|
53 | n/a | @with_tp_del |
---|
54 | n/a | class Uncollectable(object): |
---|
55 | n/a | """Create a reference cycle with multiple __del__ methods. |
---|
56 | n/a | |
---|
57 | n/a | An object in a reference cycle will never have zero references, |
---|
58 | n/a | and so must be garbage collected. If one or more objects in the |
---|
59 | n/a | cycle have __del__ methods, the gc refuses to guess an order, |
---|
60 | n/a | and leaves the cycle uncollected.""" |
---|
61 | n/a | def __init__(self, partner=None): |
---|
62 | n/a | if partner is None: |
---|
63 | n/a | self.partner = Uncollectable(partner=self) |
---|
64 | n/a | else: |
---|
65 | n/a | self.partner = partner |
---|
66 | n/a | def __tp_del__(self): |
---|
67 | n/a | pass |
---|
68 | n/a | |
---|
69 | n/a | ### Tests |
---|
70 | n/a | ############################################################################### |
---|
71 | n/a | |
---|
72 | n/a | class GCTests(unittest.TestCase): |
---|
73 | n/a | def test_list(self): |
---|
74 | n/a | l = [] |
---|
75 | n/a | l.append(l) |
---|
76 | n/a | gc.collect() |
---|
77 | n/a | del l |
---|
78 | n/a | self.assertEqual(gc.collect(), 1) |
---|
79 | n/a | |
---|
80 | n/a | def test_dict(self): |
---|
81 | n/a | d = {} |
---|
82 | n/a | d[1] = d |
---|
83 | n/a | gc.collect() |
---|
84 | n/a | del d |
---|
85 | n/a | self.assertEqual(gc.collect(), 1) |
---|
86 | n/a | |
---|
87 | n/a | def test_tuple(self): |
---|
88 | n/a | # since tuples are immutable we close the loop with a list |
---|
89 | n/a | l = [] |
---|
90 | n/a | t = (l,) |
---|
91 | n/a | l.append(t) |
---|
92 | n/a | gc.collect() |
---|
93 | n/a | del t |
---|
94 | n/a | del l |
---|
95 | n/a | self.assertEqual(gc.collect(), 2) |
---|
96 | n/a | |
---|
97 | n/a | def test_class(self): |
---|
98 | n/a | class A: |
---|
99 | n/a | pass |
---|
100 | n/a | A.a = A |
---|
101 | n/a | gc.collect() |
---|
102 | n/a | del A |
---|
103 | n/a | self.assertNotEqual(gc.collect(), 0) |
---|
104 | n/a | |
---|
105 | n/a | def test_newstyleclass(self): |
---|
106 | n/a | class A(object): |
---|
107 | n/a | pass |
---|
108 | n/a | gc.collect() |
---|
109 | n/a | del A |
---|
110 | n/a | self.assertNotEqual(gc.collect(), 0) |
---|
111 | n/a | |
---|
112 | n/a | def test_instance(self): |
---|
113 | n/a | class A: |
---|
114 | n/a | pass |
---|
115 | n/a | a = A() |
---|
116 | n/a | a.a = a |
---|
117 | n/a | gc.collect() |
---|
118 | n/a | del a |
---|
119 | n/a | self.assertNotEqual(gc.collect(), 0) |
---|
120 | n/a | |
---|
121 | n/a | @requires_type_collecting |
---|
122 | n/a | def test_newinstance(self): |
---|
123 | n/a | class A(object): |
---|
124 | n/a | pass |
---|
125 | n/a | a = A() |
---|
126 | n/a | a.a = a |
---|
127 | n/a | gc.collect() |
---|
128 | n/a | del a |
---|
129 | n/a | self.assertNotEqual(gc.collect(), 0) |
---|
130 | n/a | class B(list): |
---|
131 | n/a | pass |
---|
132 | n/a | class C(B, A): |
---|
133 | n/a | pass |
---|
134 | n/a | a = C() |
---|
135 | n/a | a.a = a |
---|
136 | n/a | gc.collect() |
---|
137 | n/a | del a |
---|
138 | n/a | self.assertNotEqual(gc.collect(), 0) |
---|
139 | n/a | del B, C |
---|
140 | n/a | self.assertNotEqual(gc.collect(), 0) |
---|
141 | n/a | A.a = A() |
---|
142 | n/a | del A |
---|
143 | n/a | self.assertNotEqual(gc.collect(), 0) |
---|
144 | n/a | self.assertEqual(gc.collect(), 0) |
---|
145 | n/a | |
---|
146 | n/a | def test_method(self): |
---|
147 | n/a | # Tricky: self.__init__ is a bound method, it references the instance. |
---|
148 | n/a | class A: |
---|
149 | n/a | def __init__(self): |
---|
150 | n/a | self.init = self.__init__ |
---|
151 | n/a | a = A() |
---|
152 | n/a | gc.collect() |
---|
153 | n/a | del a |
---|
154 | n/a | self.assertNotEqual(gc.collect(), 0) |
---|
155 | n/a | |
---|
156 | n/a | @cpython_only |
---|
157 | n/a | def test_legacy_finalizer(self): |
---|
158 | n/a | # A() is uncollectable if it is part of a cycle, make sure it shows up |
---|
159 | n/a | # in gc.garbage. |
---|
160 | n/a | @with_tp_del |
---|
161 | n/a | class A: |
---|
162 | n/a | def __tp_del__(self): pass |
---|
163 | n/a | class B: |
---|
164 | n/a | pass |
---|
165 | n/a | a = A() |
---|
166 | n/a | a.a = a |
---|
167 | n/a | id_a = id(a) |
---|
168 | n/a | b = B() |
---|
169 | n/a | b.b = b |
---|
170 | n/a | gc.collect() |
---|
171 | n/a | del a |
---|
172 | n/a | del b |
---|
173 | n/a | self.assertNotEqual(gc.collect(), 0) |
---|
174 | n/a | for obj in gc.garbage: |
---|
175 | n/a | if id(obj) == id_a: |
---|
176 | n/a | del obj.a |
---|
177 | n/a | break |
---|
178 | n/a | else: |
---|
179 | n/a | self.fail("didn't find obj in garbage (finalizer)") |
---|
180 | n/a | gc.garbage.remove(obj) |
---|
181 | n/a | |
---|
182 | n/a | @cpython_only |
---|
183 | n/a | def test_legacy_finalizer_newclass(self): |
---|
184 | n/a | # A() is uncollectable if it is part of a cycle, make sure it shows up |
---|
185 | n/a | # in gc.garbage. |
---|
186 | n/a | @with_tp_del |
---|
187 | n/a | class A(object): |
---|
188 | n/a | def __tp_del__(self): pass |
---|
189 | n/a | class B(object): |
---|
190 | n/a | pass |
---|
191 | n/a | a = A() |
---|
192 | n/a | a.a = a |
---|
193 | n/a | id_a = id(a) |
---|
194 | n/a | b = B() |
---|
195 | n/a | b.b = b |
---|
196 | n/a | gc.collect() |
---|
197 | n/a | del a |
---|
198 | n/a | del b |
---|
199 | n/a | self.assertNotEqual(gc.collect(), 0) |
---|
200 | n/a | for obj in gc.garbage: |
---|
201 | n/a | if id(obj) == id_a: |
---|
202 | n/a | del obj.a |
---|
203 | n/a | break |
---|
204 | n/a | else: |
---|
205 | n/a | self.fail("didn't find obj in garbage (finalizer)") |
---|
206 | n/a | gc.garbage.remove(obj) |
---|
207 | n/a | |
---|
208 | n/a | def test_function(self): |
---|
209 | n/a | # Tricky: f -> d -> f, code should call d.clear() after the exec to |
---|
210 | n/a | # break the cycle. |
---|
211 | n/a | d = {} |
---|
212 | n/a | exec("def f(): pass\n", d) |
---|
213 | n/a | gc.collect() |
---|
214 | n/a | del d |
---|
215 | n/a | self.assertEqual(gc.collect(), 2) |
---|
216 | n/a | |
---|
217 | n/a | @refcount_test |
---|
218 | n/a | def test_frame(self): |
---|
219 | n/a | def f(): |
---|
220 | n/a | frame = sys._getframe() |
---|
221 | n/a | gc.collect() |
---|
222 | n/a | f() |
---|
223 | n/a | self.assertEqual(gc.collect(), 1) |
---|
224 | n/a | |
---|
225 | n/a | def test_saveall(self): |
---|
226 | n/a | # Verify that cyclic garbage like lists show up in gc.garbage if the |
---|
227 | n/a | # SAVEALL option is enabled. |
---|
228 | n/a | |
---|
229 | n/a | # First make sure we don't save away other stuff that just happens to |
---|
230 | n/a | # be waiting for collection. |
---|
231 | n/a | gc.collect() |
---|
232 | n/a | # if this fails, someone else created immortal trash |
---|
233 | n/a | self.assertEqual(gc.garbage, []) |
---|
234 | n/a | |
---|
235 | n/a | L = [] |
---|
236 | n/a | L.append(L) |
---|
237 | n/a | id_L = id(L) |
---|
238 | n/a | |
---|
239 | n/a | debug = gc.get_debug() |
---|
240 | n/a | gc.set_debug(debug | gc.DEBUG_SAVEALL) |
---|
241 | n/a | del L |
---|
242 | n/a | gc.collect() |
---|
243 | n/a | gc.set_debug(debug) |
---|
244 | n/a | |
---|
245 | n/a | self.assertEqual(len(gc.garbage), 1) |
---|
246 | n/a | obj = gc.garbage.pop() |
---|
247 | n/a | self.assertEqual(id(obj), id_L) |
---|
248 | n/a | |
---|
249 | n/a | def test_del(self): |
---|
250 | n/a | # __del__ methods can trigger collection, make this to happen |
---|
251 | n/a | thresholds = gc.get_threshold() |
---|
252 | n/a | gc.enable() |
---|
253 | n/a | gc.set_threshold(1) |
---|
254 | n/a | |
---|
255 | n/a | class A: |
---|
256 | n/a | def __del__(self): |
---|
257 | n/a | dir(self) |
---|
258 | n/a | a = A() |
---|
259 | n/a | del a |
---|
260 | n/a | |
---|
261 | n/a | gc.disable() |
---|
262 | n/a | gc.set_threshold(*thresholds) |
---|
263 | n/a | |
---|
264 | n/a | def test_del_newclass(self): |
---|
265 | n/a | # __del__ methods can trigger collection, make this to happen |
---|
266 | n/a | thresholds = gc.get_threshold() |
---|
267 | n/a | gc.enable() |
---|
268 | n/a | gc.set_threshold(1) |
---|
269 | n/a | |
---|
270 | n/a | class A(object): |
---|
271 | n/a | def __del__(self): |
---|
272 | n/a | dir(self) |
---|
273 | n/a | a = A() |
---|
274 | n/a | del a |
---|
275 | n/a | |
---|
276 | n/a | gc.disable() |
---|
277 | n/a | gc.set_threshold(*thresholds) |
---|
278 | n/a | |
---|
279 | n/a | # The following two tests are fragile: |
---|
280 | n/a | # They precisely count the number of allocations, |
---|
281 | n/a | # which is highly implementation-dependent. |
---|
282 | n/a | # For example, disposed tuples are not freed, but reused. |
---|
283 | n/a | # To minimize variations, though, we first store the get_count() results |
---|
284 | n/a | # and check them at the end. |
---|
285 | n/a | @refcount_test |
---|
286 | n/a | def test_get_count(self): |
---|
287 | n/a | gc.collect() |
---|
288 | n/a | a, b, c = gc.get_count() |
---|
289 | n/a | x = [] |
---|
290 | n/a | d, e, f = gc.get_count() |
---|
291 | n/a | self.assertEqual((b, c), (0, 0)) |
---|
292 | n/a | self.assertEqual((e, f), (0, 0)) |
---|
293 | n/a | # This is less fragile than asserting that a equals 0. |
---|
294 | n/a | self.assertLess(a, 5) |
---|
295 | n/a | # Between the two calls to get_count(), at least one object was |
---|
296 | n/a | # created (the list). |
---|
297 | n/a | self.assertGreater(d, a) |
---|
298 | n/a | |
---|
299 | n/a | @refcount_test |
---|
300 | n/a | def test_collect_generations(self): |
---|
301 | n/a | gc.collect() |
---|
302 | n/a | # This object will "trickle" into generation N + 1 after |
---|
303 | n/a | # each call to collect(N) |
---|
304 | n/a | x = [] |
---|
305 | n/a | gc.collect(0) |
---|
306 | n/a | # x is now in gen 1 |
---|
307 | n/a | a, b, c = gc.get_count() |
---|
308 | n/a | gc.collect(1) |
---|
309 | n/a | # x is now in gen 2 |
---|
310 | n/a | d, e, f = gc.get_count() |
---|
311 | n/a | gc.collect(2) |
---|
312 | n/a | # x is now in gen 3 |
---|
313 | n/a | g, h, i = gc.get_count() |
---|
314 | n/a | # We don't check a, d, g since their exact values depends on |
---|
315 | n/a | # internal implementation details of the interpreter. |
---|
316 | n/a | self.assertEqual((b, c), (1, 0)) |
---|
317 | n/a | self.assertEqual((e, f), (0, 1)) |
---|
318 | n/a | self.assertEqual((h, i), (0, 0)) |
---|
319 | n/a | |
---|
320 | n/a | def test_trashcan(self): |
---|
321 | n/a | class Ouch: |
---|
322 | n/a | n = 0 |
---|
323 | n/a | def __del__(self): |
---|
324 | n/a | Ouch.n = Ouch.n + 1 |
---|
325 | n/a | if Ouch.n % 17 == 0: |
---|
326 | n/a | gc.collect() |
---|
327 | n/a | |
---|
328 | n/a | # "trashcan" is a hack to prevent stack overflow when deallocating |
---|
329 | n/a | # very deeply nested tuples etc. It works in part by abusing the |
---|
330 | n/a | # type pointer and refcount fields, and that can yield horrible |
---|
331 | n/a | # problems when gc tries to traverse the structures. |
---|
332 | n/a | # If this test fails (as it does in 2.0, 2.1 and 2.2), it will |
---|
333 | n/a | # most likely die via segfault. |
---|
334 | n/a | |
---|
335 | n/a | # Note: In 2.3 the possibility for compiling without cyclic gc was |
---|
336 | n/a | # removed, and that in turn allows the trashcan mechanism to work |
---|
337 | n/a | # via much simpler means (e.g., it never abuses the type pointer or |
---|
338 | n/a | # refcount fields anymore). Since it's much less likely to cause a |
---|
339 | n/a | # problem now, the various constants in this expensive (we force a lot |
---|
340 | n/a | # of full collections) test are cut back from the 2.2 version. |
---|
341 | n/a | gc.enable() |
---|
342 | n/a | N = 150 |
---|
343 | n/a | for count in range(2): |
---|
344 | n/a | t = [] |
---|
345 | n/a | for i in range(N): |
---|
346 | n/a | t = [t, Ouch()] |
---|
347 | n/a | u = [] |
---|
348 | n/a | for i in range(N): |
---|
349 | n/a | u = [u, Ouch()] |
---|
350 | n/a | v = {} |
---|
351 | n/a | for i in range(N): |
---|
352 | n/a | v = {1: v, 2: Ouch()} |
---|
353 | n/a | gc.disable() |
---|
354 | n/a | |
---|
355 | n/a | @unittest.skipUnless(threading, "test meaningless on builds without threads") |
---|
356 | n/a | def test_trashcan_threads(self): |
---|
357 | n/a | # Issue #13992: trashcan mechanism should be thread-safe |
---|
358 | n/a | NESTING = 60 |
---|
359 | n/a | N_THREADS = 2 |
---|
360 | n/a | |
---|
361 | n/a | def sleeper_gen(): |
---|
362 | n/a | """A generator that releases the GIL when closed or dealloc'ed.""" |
---|
363 | n/a | try: |
---|
364 | n/a | yield |
---|
365 | n/a | finally: |
---|
366 | n/a | time.sleep(0.000001) |
---|
367 | n/a | |
---|
368 | n/a | class C(list): |
---|
369 | n/a | # Appending to a list is atomic, which avoids the use of a lock. |
---|
370 | n/a | inits = [] |
---|
371 | n/a | dels = [] |
---|
372 | n/a | def __init__(self, alist): |
---|
373 | n/a | self[:] = alist |
---|
374 | n/a | C.inits.append(None) |
---|
375 | n/a | def __del__(self): |
---|
376 | n/a | # This __del__ is called by subtype_dealloc(). |
---|
377 | n/a | C.dels.append(None) |
---|
378 | n/a | # `g` will release the GIL when garbage-collected. This |
---|
379 | n/a | # helps assert subtype_dealloc's behaviour when threads |
---|
380 | n/a | # switch in the middle of it. |
---|
381 | n/a | g = sleeper_gen() |
---|
382 | n/a | next(g) |
---|
383 | n/a | # Now that __del__ is finished, subtype_dealloc will proceed |
---|
384 | n/a | # to call list_dealloc, which also uses the trashcan mechanism. |
---|
385 | n/a | |
---|
386 | n/a | def make_nested(): |
---|
387 | n/a | """Create a sufficiently nested container object so that the |
---|
388 | n/a | trashcan mechanism is invoked when deallocating it.""" |
---|
389 | n/a | x = C([]) |
---|
390 | n/a | for i in range(NESTING): |
---|
391 | n/a | x = [C([x])] |
---|
392 | n/a | del x |
---|
393 | n/a | |
---|
394 | n/a | def run_thread(): |
---|
395 | n/a | """Exercise make_nested() in a loop.""" |
---|
396 | n/a | while not exit: |
---|
397 | n/a | make_nested() |
---|
398 | n/a | |
---|
399 | n/a | old_switchinterval = sys.getswitchinterval() |
---|
400 | n/a | sys.setswitchinterval(1e-5) |
---|
401 | n/a | try: |
---|
402 | n/a | exit = [] |
---|
403 | n/a | threads = [] |
---|
404 | n/a | for i in range(N_THREADS): |
---|
405 | n/a | t = threading.Thread(target=run_thread) |
---|
406 | n/a | threads.append(t) |
---|
407 | n/a | with start_threads(threads, lambda: exit.append(1)): |
---|
408 | n/a | time.sleep(1.0) |
---|
409 | n/a | finally: |
---|
410 | n/a | sys.setswitchinterval(old_switchinterval) |
---|
411 | n/a | gc.collect() |
---|
412 | n/a | self.assertEqual(len(C.inits), len(C.dels)) |
---|
413 | n/a | |
---|
414 | n/a | def test_boom(self): |
---|
415 | n/a | class Boom: |
---|
416 | n/a | def __getattr__(self, someattribute): |
---|
417 | n/a | del self.attr |
---|
418 | n/a | raise AttributeError |
---|
419 | n/a | |
---|
420 | n/a | a = Boom() |
---|
421 | n/a | b = Boom() |
---|
422 | n/a | a.attr = b |
---|
423 | n/a | b.attr = a |
---|
424 | n/a | |
---|
425 | n/a | gc.collect() |
---|
426 | n/a | garbagelen = len(gc.garbage) |
---|
427 | n/a | del a, b |
---|
428 | n/a | # a<->b are in a trash cycle now. Collection will invoke |
---|
429 | n/a | # Boom.__getattr__ (to see whether a and b have __del__ methods), and |
---|
430 | n/a | # __getattr__ deletes the internal "attr" attributes as a side effect. |
---|
431 | n/a | # That causes the trash cycle to get reclaimed via refcounts falling to |
---|
432 | n/a | # 0, thus mutating the trash graph as a side effect of merely asking |
---|
433 | n/a | # whether __del__ exists. This used to (before 2.3b1) crash Python. |
---|
434 | n/a | # Now __getattr__ isn't called. |
---|
435 | n/a | self.assertEqual(gc.collect(), 4) |
---|
436 | n/a | self.assertEqual(len(gc.garbage), garbagelen) |
---|
437 | n/a | |
---|
438 | n/a | def test_boom2(self): |
---|
439 | n/a | class Boom2: |
---|
440 | n/a | def __init__(self): |
---|
441 | n/a | self.x = 0 |
---|
442 | n/a | |
---|
443 | n/a | def __getattr__(self, someattribute): |
---|
444 | n/a | self.x += 1 |
---|
445 | n/a | if self.x > 1: |
---|
446 | n/a | del self.attr |
---|
447 | n/a | raise AttributeError |
---|
448 | n/a | |
---|
449 | n/a | a = Boom2() |
---|
450 | n/a | b = Boom2() |
---|
451 | n/a | a.attr = b |
---|
452 | n/a | b.attr = a |
---|
453 | n/a | |
---|
454 | n/a | gc.collect() |
---|
455 | n/a | garbagelen = len(gc.garbage) |
---|
456 | n/a | del a, b |
---|
457 | n/a | # Much like test_boom(), except that __getattr__ doesn't break the |
---|
458 | n/a | # cycle until the second time gc checks for __del__. As of 2.3b1, |
---|
459 | n/a | # there isn't a second time, so this simply cleans up the trash cycle. |
---|
460 | n/a | # We expect a, b, a.__dict__ and b.__dict__ (4 objects) to get |
---|
461 | n/a | # reclaimed this way. |
---|
462 | n/a | self.assertEqual(gc.collect(), 4) |
---|
463 | n/a | self.assertEqual(len(gc.garbage), garbagelen) |
---|
464 | n/a | |
---|
465 | n/a | def test_boom_new(self): |
---|
466 | n/a | # boom__new and boom2_new are exactly like boom and boom2, except use |
---|
467 | n/a | # new-style classes. |
---|
468 | n/a | |
---|
469 | n/a | class Boom_New(object): |
---|
470 | n/a | def __getattr__(self, someattribute): |
---|
471 | n/a | del self.attr |
---|
472 | n/a | raise AttributeError |
---|
473 | n/a | |
---|
474 | n/a | a = Boom_New() |
---|
475 | n/a | b = Boom_New() |
---|
476 | n/a | a.attr = b |
---|
477 | n/a | b.attr = a |
---|
478 | n/a | |
---|
479 | n/a | gc.collect() |
---|
480 | n/a | garbagelen = len(gc.garbage) |
---|
481 | n/a | del a, b |
---|
482 | n/a | self.assertEqual(gc.collect(), 4) |
---|
483 | n/a | self.assertEqual(len(gc.garbage), garbagelen) |
---|
484 | n/a | |
---|
485 | n/a | def test_boom2_new(self): |
---|
486 | n/a | class Boom2_New(object): |
---|
487 | n/a | def __init__(self): |
---|
488 | n/a | self.x = 0 |
---|
489 | n/a | |
---|
490 | n/a | def __getattr__(self, someattribute): |
---|
491 | n/a | self.x += 1 |
---|
492 | n/a | if self.x > 1: |
---|
493 | n/a | del self.attr |
---|
494 | n/a | raise AttributeError |
---|
495 | n/a | |
---|
496 | n/a | a = Boom2_New() |
---|
497 | n/a | b = Boom2_New() |
---|
498 | n/a | a.attr = b |
---|
499 | n/a | b.attr = a |
---|
500 | n/a | |
---|
501 | n/a | gc.collect() |
---|
502 | n/a | garbagelen = len(gc.garbage) |
---|
503 | n/a | del a, b |
---|
504 | n/a | self.assertEqual(gc.collect(), 4) |
---|
505 | n/a | self.assertEqual(len(gc.garbage), garbagelen) |
---|
506 | n/a | |
---|
507 | n/a | def test_get_referents(self): |
---|
508 | n/a | alist = [1, 3, 5] |
---|
509 | n/a | got = gc.get_referents(alist) |
---|
510 | n/a | got.sort() |
---|
511 | n/a | self.assertEqual(got, alist) |
---|
512 | n/a | |
---|
513 | n/a | atuple = tuple(alist) |
---|
514 | n/a | got = gc.get_referents(atuple) |
---|
515 | n/a | got.sort() |
---|
516 | n/a | self.assertEqual(got, alist) |
---|
517 | n/a | |
---|
518 | n/a | adict = {1: 3, 5: 7} |
---|
519 | n/a | expected = [1, 3, 5, 7] |
---|
520 | n/a | got = gc.get_referents(adict) |
---|
521 | n/a | got.sort() |
---|
522 | n/a | self.assertEqual(got, expected) |
---|
523 | n/a | |
---|
524 | n/a | got = gc.get_referents([1, 2], {3: 4}, (0, 0, 0)) |
---|
525 | n/a | got.sort() |
---|
526 | n/a | self.assertEqual(got, [0, 0] + list(range(5))) |
---|
527 | n/a | |
---|
528 | n/a | self.assertEqual(gc.get_referents(1, 'a', 4j), []) |
---|
529 | n/a | |
---|
530 | n/a | def test_is_tracked(self): |
---|
531 | n/a | # Atomic built-in types are not tracked, user-defined objects and |
---|
532 | n/a | # mutable containers are. |
---|
533 | n/a | # NOTE: types with special optimizations (e.g. tuple) have tests |
---|
534 | n/a | # in their own test files instead. |
---|
535 | n/a | self.assertFalse(gc.is_tracked(None)) |
---|
536 | n/a | self.assertFalse(gc.is_tracked(1)) |
---|
537 | n/a | self.assertFalse(gc.is_tracked(1.0)) |
---|
538 | n/a | self.assertFalse(gc.is_tracked(1.0 + 5.0j)) |
---|
539 | n/a | self.assertFalse(gc.is_tracked(True)) |
---|
540 | n/a | self.assertFalse(gc.is_tracked(False)) |
---|
541 | n/a | self.assertFalse(gc.is_tracked(b"a")) |
---|
542 | n/a | self.assertFalse(gc.is_tracked("a")) |
---|
543 | n/a | self.assertFalse(gc.is_tracked(bytearray(b"a"))) |
---|
544 | n/a | self.assertFalse(gc.is_tracked(type)) |
---|
545 | n/a | self.assertFalse(gc.is_tracked(int)) |
---|
546 | n/a | self.assertFalse(gc.is_tracked(object)) |
---|
547 | n/a | self.assertFalse(gc.is_tracked(object())) |
---|
548 | n/a | |
---|
549 | n/a | class UserClass: |
---|
550 | n/a | pass |
---|
551 | n/a | |
---|
552 | n/a | class UserInt(int): |
---|
553 | n/a | pass |
---|
554 | n/a | |
---|
555 | n/a | # Base class is object; no extra fields. |
---|
556 | n/a | class UserClassSlots: |
---|
557 | n/a | __slots__ = () |
---|
558 | n/a | |
---|
559 | n/a | # Base class is fixed size larger than object; no extra fields. |
---|
560 | n/a | class UserFloatSlots(float): |
---|
561 | n/a | __slots__ = () |
---|
562 | n/a | |
---|
563 | n/a | # Base class is variable size; no extra fields. |
---|
564 | n/a | class UserIntSlots(int): |
---|
565 | n/a | __slots__ = () |
---|
566 | n/a | |
---|
567 | n/a | self.assertTrue(gc.is_tracked(gc)) |
---|
568 | n/a | self.assertTrue(gc.is_tracked(UserClass)) |
---|
569 | n/a | self.assertTrue(gc.is_tracked(UserClass())) |
---|
570 | n/a | self.assertTrue(gc.is_tracked(UserInt())) |
---|
571 | n/a | self.assertTrue(gc.is_tracked([])) |
---|
572 | n/a | self.assertTrue(gc.is_tracked(set())) |
---|
573 | n/a | self.assertFalse(gc.is_tracked(UserClassSlots())) |
---|
574 | n/a | self.assertFalse(gc.is_tracked(UserFloatSlots())) |
---|
575 | n/a | self.assertFalse(gc.is_tracked(UserIntSlots())) |
---|
576 | n/a | |
---|
577 | n/a | def test_bug1055820b(self): |
---|
578 | n/a | # Corresponds to temp2b.py in the bug report. |
---|
579 | n/a | |
---|
580 | n/a | ouch = [] |
---|
581 | n/a | def callback(ignored): |
---|
582 | n/a | ouch[:] = [wr() for wr in WRs] |
---|
583 | n/a | |
---|
584 | n/a | Cs = [C1055820(i) for i in range(2)] |
---|
585 | n/a | WRs = [weakref.ref(c, callback) for c in Cs] |
---|
586 | n/a | c = None |
---|
587 | n/a | |
---|
588 | n/a | gc.collect() |
---|
589 | n/a | self.assertEqual(len(ouch), 0) |
---|
590 | n/a | # Make the two instances trash, and collect again. The bug was that |
---|
591 | n/a | # the callback materialized a strong reference to an instance, but gc |
---|
592 | n/a | # cleared the instance's dict anyway. |
---|
593 | n/a | Cs = None |
---|
594 | n/a | gc.collect() |
---|
595 | n/a | self.assertEqual(len(ouch), 2) # else the callbacks didn't run |
---|
596 | n/a | for x in ouch: |
---|
597 | n/a | # If the callback resurrected one of these guys, the instance |
---|
598 | n/a | # would be damaged, with an empty __dict__. |
---|
599 | n/a | self.assertEqual(x, None) |
---|
600 | n/a | |
---|
601 | n/a | def test_bug21435(self): |
---|
602 | n/a | # This is a poor test - its only virtue is that it happened to |
---|
603 | n/a | # segfault on Tim's Windows box before the patch for 21435 was |
---|
604 | n/a | # applied. That's a nasty bug relying on specific pieces of cyclic |
---|
605 | n/a | # trash appearing in exactly the right order in finalize_garbage()'s |
---|
606 | n/a | # input list. |
---|
607 | n/a | # But there's no reliable way to force that order from Python code, |
---|
608 | n/a | # so over time chances are good this test won't really be testing much |
---|
609 | n/a | # of anything anymore. Still, if it blows up, there's _some_ |
---|
610 | n/a | # problem ;-) |
---|
611 | n/a | gc.collect() |
---|
612 | n/a | |
---|
613 | n/a | class A: |
---|
614 | n/a | pass |
---|
615 | n/a | |
---|
616 | n/a | class B: |
---|
617 | n/a | def __init__(self, x): |
---|
618 | n/a | self.x = x |
---|
619 | n/a | |
---|
620 | n/a | def __del__(self): |
---|
621 | n/a | self.attr = None |
---|
622 | n/a | |
---|
623 | n/a | def do_work(): |
---|
624 | n/a | a = A() |
---|
625 | n/a | b = B(A()) |
---|
626 | n/a | |
---|
627 | n/a | a.attr = b |
---|
628 | n/a | b.attr = a |
---|
629 | n/a | |
---|
630 | n/a | do_work() |
---|
631 | n/a | gc.collect() # this blows up (bad C pointer) when it fails |
---|
632 | n/a | |
---|
633 | n/a | @cpython_only |
---|
634 | n/a | def test_garbage_at_shutdown(self): |
---|
635 | n/a | import subprocess |
---|
636 | n/a | code = """if 1: |
---|
637 | n/a | import gc |
---|
638 | n/a | import _testcapi |
---|
639 | n/a | @_testcapi.with_tp_del |
---|
640 | n/a | class X: |
---|
641 | n/a | def __init__(self, name): |
---|
642 | n/a | self.name = name |
---|
643 | n/a | def __repr__(self): |
---|
644 | n/a | return "<X %%r>" %% self.name |
---|
645 | n/a | def __tp_del__(self): |
---|
646 | n/a | pass |
---|
647 | n/a | |
---|
648 | n/a | x = X('first') |
---|
649 | n/a | x.x = x |
---|
650 | n/a | x.y = X('second') |
---|
651 | n/a | del x |
---|
652 | n/a | gc.set_debug(%s) |
---|
653 | n/a | """ |
---|
654 | n/a | def run_command(code): |
---|
655 | n/a | p = subprocess.Popen([sys.executable, "-Wd", "-c", code], |
---|
656 | n/a | stdout=subprocess.PIPE, |
---|
657 | n/a | stderr=subprocess.PIPE) |
---|
658 | n/a | stdout, stderr = p.communicate() |
---|
659 | n/a | p.stdout.close() |
---|
660 | n/a | p.stderr.close() |
---|
661 | n/a | self.assertEqual(p.returncode, 0) |
---|
662 | n/a | self.assertEqual(stdout.strip(), b"") |
---|
663 | n/a | return strip_python_stderr(stderr) |
---|
664 | n/a | |
---|
665 | n/a | stderr = run_command(code % "0") |
---|
666 | n/a | self.assertIn(b"ResourceWarning: gc: 2 uncollectable objects at " |
---|
667 | n/a | b"shutdown; use", stderr) |
---|
668 | n/a | self.assertNotIn(b"<X 'first'>", stderr) |
---|
669 | n/a | # With DEBUG_UNCOLLECTABLE, the garbage list gets printed |
---|
670 | n/a | stderr = run_command(code % "gc.DEBUG_UNCOLLECTABLE") |
---|
671 | n/a | self.assertIn(b"ResourceWarning: gc: 2 uncollectable objects at " |
---|
672 | n/a | b"shutdown", stderr) |
---|
673 | n/a | self.assertTrue( |
---|
674 | n/a | (b"[<X 'first'>, <X 'second'>]" in stderr) or |
---|
675 | n/a | (b"[<X 'second'>, <X 'first'>]" in stderr), stderr) |
---|
676 | n/a | # With DEBUG_SAVEALL, no additional message should get printed |
---|
677 | n/a | # (because gc.garbage also contains normally reclaimable cyclic |
---|
678 | n/a | # references, and its elements get printed at runtime anyway). |
---|
679 | n/a | stderr = run_command(code % "gc.DEBUG_SAVEALL") |
---|
680 | n/a | self.assertNotIn(b"uncollectable objects at shutdown", stderr) |
---|
681 | n/a | |
---|
682 | n/a | @requires_type_collecting |
---|
683 | n/a | def test_gc_main_module_at_shutdown(self): |
---|
684 | n/a | # Create a reference cycle through the __main__ module and check |
---|
685 | n/a | # it gets collected at interpreter shutdown. |
---|
686 | n/a | code = """if 1: |
---|
687 | n/a | class C: |
---|
688 | n/a | def __del__(self): |
---|
689 | n/a | print('__del__ called') |
---|
690 | n/a | l = [C()] |
---|
691 | n/a | l.append(l) |
---|
692 | n/a | """ |
---|
693 | n/a | rc, out, err = assert_python_ok('-c', code) |
---|
694 | n/a | self.assertEqual(out.strip(), b'__del__ called') |
---|
695 | n/a | |
---|
696 | n/a | @requires_type_collecting |
---|
697 | n/a | def test_gc_ordinary_module_at_shutdown(self): |
---|
698 | n/a | # Same as above, but with a non-__main__ module. |
---|
699 | n/a | with temp_dir() as script_dir: |
---|
700 | n/a | module = """if 1: |
---|
701 | n/a | class C: |
---|
702 | n/a | def __del__(self): |
---|
703 | n/a | print('__del__ called') |
---|
704 | n/a | l = [C()] |
---|
705 | n/a | l.append(l) |
---|
706 | n/a | """ |
---|
707 | n/a | code = """if 1: |
---|
708 | n/a | import sys |
---|
709 | n/a | sys.path.insert(0, %r) |
---|
710 | n/a | import gctest |
---|
711 | n/a | """ % (script_dir,) |
---|
712 | n/a | make_script(script_dir, 'gctest', module) |
---|
713 | n/a | rc, out, err = assert_python_ok('-c', code) |
---|
714 | n/a | self.assertEqual(out.strip(), b'__del__ called') |
---|
715 | n/a | |
---|
716 | n/a | def test_get_stats(self): |
---|
717 | n/a | stats = gc.get_stats() |
---|
718 | n/a | self.assertEqual(len(stats), 3) |
---|
719 | n/a | for st in stats: |
---|
720 | n/a | self.assertIsInstance(st, dict) |
---|
721 | n/a | self.assertEqual(set(st), |
---|
722 | n/a | {"collected", "collections", "uncollectable"}) |
---|
723 | n/a | self.assertGreaterEqual(st["collected"], 0) |
---|
724 | n/a | self.assertGreaterEqual(st["collections"], 0) |
---|
725 | n/a | self.assertGreaterEqual(st["uncollectable"], 0) |
---|
726 | n/a | # Check that collection counts are incremented correctly |
---|
727 | n/a | if gc.isenabled(): |
---|
728 | n/a | self.addCleanup(gc.enable) |
---|
729 | n/a | gc.disable() |
---|
730 | n/a | old = gc.get_stats() |
---|
731 | n/a | gc.collect(0) |
---|
732 | n/a | new = gc.get_stats() |
---|
733 | n/a | self.assertEqual(new[0]["collections"], old[0]["collections"] + 1) |
---|
734 | n/a | self.assertEqual(new[1]["collections"], old[1]["collections"]) |
---|
735 | n/a | self.assertEqual(new[2]["collections"], old[2]["collections"]) |
---|
736 | n/a | gc.collect(2) |
---|
737 | n/a | new = gc.get_stats() |
---|
738 | n/a | self.assertEqual(new[0]["collections"], old[0]["collections"] + 1) |
---|
739 | n/a | self.assertEqual(new[1]["collections"], old[1]["collections"]) |
---|
740 | n/a | self.assertEqual(new[2]["collections"], old[2]["collections"] + 1) |
---|
741 | n/a | |
---|
742 | n/a | |
---|
743 | n/a | class GCCallbackTests(unittest.TestCase): |
---|
744 | n/a | def setUp(self): |
---|
745 | n/a | # Save gc state and disable it. |
---|
746 | n/a | self.enabled = gc.isenabled() |
---|
747 | n/a | gc.disable() |
---|
748 | n/a | self.debug = gc.get_debug() |
---|
749 | n/a | gc.set_debug(0) |
---|
750 | n/a | gc.callbacks.append(self.cb1) |
---|
751 | n/a | gc.callbacks.append(self.cb2) |
---|
752 | n/a | self.othergarbage = [] |
---|
753 | n/a | |
---|
754 | n/a | def tearDown(self): |
---|
755 | n/a | # Restore gc state |
---|
756 | n/a | del self.visit |
---|
757 | n/a | gc.callbacks.remove(self.cb1) |
---|
758 | n/a | gc.callbacks.remove(self.cb2) |
---|
759 | n/a | gc.set_debug(self.debug) |
---|
760 | n/a | if self.enabled: |
---|
761 | n/a | gc.enable() |
---|
762 | n/a | # destroy any uncollectables |
---|
763 | n/a | gc.collect() |
---|
764 | n/a | for obj in gc.garbage: |
---|
765 | n/a | if isinstance(obj, Uncollectable): |
---|
766 | n/a | obj.partner = None |
---|
767 | n/a | del gc.garbage[:] |
---|
768 | n/a | del self.othergarbage |
---|
769 | n/a | gc.collect() |
---|
770 | n/a | |
---|
771 | n/a | def preclean(self): |
---|
772 | n/a | # Remove all fluff from the system. Invoke this function |
---|
773 | n/a | # manually rather than through self.setUp() for maximum |
---|
774 | n/a | # safety. |
---|
775 | n/a | self.visit = [] |
---|
776 | n/a | gc.collect() |
---|
777 | n/a | garbage, gc.garbage[:] = gc.garbage[:], [] |
---|
778 | n/a | self.othergarbage.append(garbage) |
---|
779 | n/a | self.visit = [] |
---|
780 | n/a | |
---|
781 | n/a | def cb1(self, phase, info): |
---|
782 | n/a | self.visit.append((1, phase, dict(info))) |
---|
783 | n/a | |
---|
784 | n/a | def cb2(self, phase, info): |
---|
785 | n/a | self.visit.append((2, phase, dict(info))) |
---|
786 | n/a | if phase == "stop" and hasattr(self, "cleanup"): |
---|
787 | n/a | # Clean Uncollectable from garbage |
---|
788 | n/a | uc = [e for e in gc.garbage if isinstance(e, Uncollectable)] |
---|
789 | n/a | gc.garbage[:] = [e for e in gc.garbage |
---|
790 | n/a | if not isinstance(e, Uncollectable)] |
---|
791 | n/a | for e in uc: |
---|
792 | n/a | e.partner = None |
---|
793 | n/a | |
---|
794 | n/a | def test_collect(self): |
---|
795 | n/a | self.preclean() |
---|
796 | n/a | gc.collect() |
---|
797 | n/a | # Algorithmically verify the contents of self.visit |
---|
798 | n/a | # because it is long and tortuous. |
---|
799 | n/a | |
---|
800 | n/a | # Count the number of visits to each callback |
---|
801 | n/a | n = [v[0] for v in self.visit] |
---|
802 | n/a | n1 = [i for i in n if i == 1] |
---|
803 | n/a | n2 = [i for i in n if i == 2] |
---|
804 | n/a | self.assertEqual(n1, [1]*2) |
---|
805 | n/a | self.assertEqual(n2, [2]*2) |
---|
806 | n/a | |
---|
807 | n/a | # Count that we got the right number of start and stop callbacks. |
---|
808 | n/a | n = [v[1] for v in self.visit] |
---|
809 | n/a | n1 = [i for i in n if i == "start"] |
---|
810 | n/a | n2 = [i for i in n if i == "stop"] |
---|
811 | n/a | self.assertEqual(n1, ["start"]*2) |
---|
812 | n/a | self.assertEqual(n2, ["stop"]*2) |
---|
813 | n/a | |
---|
814 | n/a | # Check that we got the right info dict for all callbacks |
---|
815 | n/a | for v in self.visit: |
---|
816 | n/a | info = v[2] |
---|
817 | n/a | self.assertTrue("generation" in info) |
---|
818 | n/a | self.assertTrue("collected" in info) |
---|
819 | n/a | self.assertTrue("uncollectable" in info) |
---|
820 | n/a | |
---|
821 | n/a | def test_collect_generation(self): |
---|
822 | n/a | self.preclean() |
---|
823 | n/a | gc.collect(2) |
---|
824 | n/a | for v in self.visit: |
---|
825 | n/a | info = v[2] |
---|
826 | n/a | self.assertEqual(info["generation"], 2) |
---|
827 | n/a | |
---|
828 | n/a | @cpython_only |
---|
829 | n/a | def test_collect_garbage(self): |
---|
830 | n/a | self.preclean() |
---|
831 | n/a | # Each of these cause four objects to be garbage: Two |
---|
832 | n/a | # Uncolectables and their instance dicts. |
---|
833 | n/a | Uncollectable() |
---|
834 | n/a | Uncollectable() |
---|
835 | n/a | C1055820(666) |
---|
836 | n/a | gc.collect() |
---|
837 | n/a | for v in self.visit: |
---|
838 | n/a | if v[1] != "stop": |
---|
839 | n/a | continue |
---|
840 | n/a | info = v[2] |
---|
841 | n/a | self.assertEqual(info["collected"], 2) |
---|
842 | n/a | self.assertEqual(info["uncollectable"], 8) |
---|
843 | n/a | |
---|
844 | n/a | # We should now have the Uncollectables in gc.garbage |
---|
845 | n/a | self.assertEqual(len(gc.garbage), 4) |
---|
846 | n/a | for e in gc.garbage: |
---|
847 | n/a | self.assertIsInstance(e, Uncollectable) |
---|
848 | n/a | |
---|
849 | n/a | # Now, let our callback handle the Uncollectable instances |
---|
850 | n/a | self.cleanup=True |
---|
851 | n/a | self.visit = [] |
---|
852 | n/a | gc.garbage[:] = [] |
---|
853 | n/a | gc.collect() |
---|
854 | n/a | for v in self.visit: |
---|
855 | n/a | if v[1] != "stop": |
---|
856 | n/a | continue |
---|
857 | n/a | info = v[2] |
---|
858 | n/a | self.assertEqual(info["collected"], 0) |
---|
859 | n/a | self.assertEqual(info["uncollectable"], 4) |
---|
860 | n/a | |
---|
861 | n/a | # Uncollectables should be gone |
---|
862 | n/a | self.assertEqual(len(gc.garbage), 0) |
---|
863 | n/a | |
---|
864 | n/a | |
---|
865 | n/a | class GCTogglingTests(unittest.TestCase): |
---|
866 | n/a | def setUp(self): |
---|
867 | n/a | gc.enable() |
---|
868 | n/a | |
---|
869 | n/a | def tearDown(self): |
---|
870 | n/a | gc.disable() |
---|
871 | n/a | |
---|
872 | n/a | def test_bug1055820c(self): |
---|
873 | n/a | # Corresponds to temp2c.py in the bug report. This is pretty |
---|
874 | n/a | # elaborate. |
---|
875 | n/a | |
---|
876 | n/a | c0 = C1055820(0) |
---|
877 | n/a | # Move c0 into generation 2. |
---|
878 | n/a | gc.collect() |
---|
879 | n/a | |
---|
880 | n/a | c1 = C1055820(1) |
---|
881 | n/a | c1.keep_c0_alive = c0 |
---|
882 | n/a | del c0.loop # now only c1 keeps c0 alive |
---|
883 | n/a | |
---|
884 | n/a | c2 = C1055820(2) |
---|
885 | n/a | c2wr = weakref.ref(c2) # no callback! |
---|
886 | n/a | |
---|
887 | n/a | ouch = [] |
---|
888 | n/a | def callback(ignored): |
---|
889 | n/a | ouch[:] = [c2wr()] |
---|
890 | n/a | |
---|
891 | n/a | # The callback gets associated with a wr on an object in generation 2. |
---|
892 | n/a | c0wr = weakref.ref(c0, callback) |
---|
893 | n/a | |
---|
894 | n/a | c0 = c1 = c2 = None |
---|
895 | n/a | |
---|
896 | n/a | # What we've set up: c0, c1, and c2 are all trash now. c0 is in |
---|
897 | n/a | # generation 2. The only thing keeping it alive is that c1 points to |
---|
898 | n/a | # it. c1 and c2 are in generation 0, and are in self-loops. There's a |
---|
899 | n/a | # global weakref to c2 (c2wr), but that weakref has no callback. |
---|
900 | n/a | # There's also a global weakref to c0 (c0wr), and that does have a |
---|
901 | n/a | # callback, and that callback references c2 via c2wr(). |
---|
902 | n/a | # |
---|
903 | n/a | # c0 has a wr with callback, which references c2wr |
---|
904 | n/a | # ^ |
---|
905 | n/a | # | |
---|
906 | n/a | # | Generation 2 above dots |
---|
907 | n/a | #. . . . . . . .|. . . . . . . . . . . . . . . . . . . . . . . . |
---|
908 | n/a | # | Generation 0 below dots |
---|
909 | n/a | # | |
---|
910 | n/a | # | |
---|
911 | n/a | # ^->c1 ^->c2 has a wr but no callback |
---|
912 | n/a | # | | | | |
---|
913 | n/a | # <--v <--v |
---|
914 | n/a | # |
---|
915 | n/a | # So this is the nightmare: when generation 0 gets collected, we see |
---|
916 | n/a | # that c2 has a callback-free weakref, and c1 doesn't even have a |
---|
917 | n/a | # weakref. Collecting generation 0 doesn't see c0 at all, and c0 is |
---|
918 | n/a | # the only object that has a weakref with a callback. gc clears c1 |
---|
919 | n/a | # and c2. Clearing c1 has the side effect of dropping the refcount on |
---|
920 | n/a | # c0 to 0, so c0 goes away (despite that it's in an older generation) |
---|
921 | n/a | # and c0's wr callback triggers. That in turn materializes a reference |
---|
922 | n/a | # to c2 via c2wr(), but c2 gets cleared anyway by gc. |
---|
923 | n/a | |
---|
924 | n/a | # We want to let gc happen "naturally", to preserve the distinction |
---|
925 | n/a | # between generations. |
---|
926 | n/a | junk = [] |
---|
927 | n/a | i = 0 |
---|
928 | n/a | detector = GC_Detector() |
---|
929 | n/a | while not detector.gc_happened: |
---|
930 | n/a | i += 1 |
---|
931 | n/a | if i > 10000: |
---|
932 | n/a | self.fail("gc didn't happen after 10000 iterations") |
---|
933 | n/a | self.assertEqual(len(ouch), 0) |
---|
934 | n/a | junk.append([]) # this will eventually trigger gc |
---|
935 | n/a | |
---|
936 | n/a | self.assertEqual(len(ouch), 1) # else the callback wasn't invoked |
---|
937 | n/a | for x in ouch: |
---|
938 | n/a | # If the callback resurrected c2, the instance would be damaged, |
---|
939 | n/a | # with an empty __dict__. |
---|
940 | n/a | self.assertEqual(x, None) |
---|
941 | n/a | |
---|
942 | n/a | def test_bug1055820d(self): |
---|
943 | n/a | # Corresponds to temp2d.py in the bug report. This is very much like |
---|
944 | n/a | # test_bug1055820c, but uses a __del__ method instead of a weakref |
---|
945 | n/a | # callback to sneak in a resurrection of cyclic trash. |
---|
946 | n/a | |
---|
947 | n/a | ouch = [] |
---|
948 | n/a | class D(C1055820): |
---|
949 | n/a | def __del__(self): |
---|
950 | n/a | ouch[:] = [c2wr()] |
---|
951 | n/a | |
---|
952 | n/a | d0 = D(0) |
---|
953 | n/a | # Move all the above into generation 2. |
---|
954 | n/a | gc.collect() |
---|
955 | n/a | |
---|
956 | n/a | c1 = C1055820(1) |
---|
957 | n/a | c1.keep_d0_alive = d0 |
---|
958 | n/a | del d0.loop # now only c1 keeps d0 alive |
---|
959 | n/a | |
---|
960 | n/a | c2 = C1055820(2) |
---|
961 | n/a | c2wr = weakref.ref(c2) # no callback! |
---|
962 | n/a | |
---|
963 | n/a | d0 = c1 = c2 = None |
---|
964 | n/a | |
---|
965 | n/a | # What we've set up: d0, c1, and c2 are all trash now. d0 is in |
---|
966 | n/a | # generation 2. The only thing keeping it alive is that c1 points to |
---|
967 | n/a | # it. c1 and c2 are in generation 0, and are in self-loops. There's |
---|
968 | n/a | # a global weakref to c2 (c2wr), but that weakref has no callback. |
---|
969 | n/a | # There are no other weakrefs. |
---|
970 | n/a | # |
---|
971 | n/a | # d0 has a __del__ method that references c2wr |
---|
972 | n/a | # ^ |
---|
973 | n/a | # | |
---|
974 | n/a | # | Generation 2 above dots |
---|
975 | n/a | #. . . . . . . .|. . . . . . . . . . . . . . . . . . . . . . . . |
---|
976 | n/a | # | Generation 0 below dots |
---|
977 | n/a | # | |
---|
978 | n/a | # | |
---|
979 | n/a | # ^->c1 ^->c2 has a wr but no callback |
---|
980 | n/a | # | | | | |
---|
981 | n/a | # <--v <--v |
---|
982 | n/a | # |
---|
983 | n/a | # So this is the nightmare: when generation 0 gets collected, we see |
---|
984 | n/a | # that c2 has a callback-free weakref, and c1 doesn't even have a |
---|
985 | n/a | # weakref. Collecting generation 0 doesn't see d0 at all. gc clears |
---|
986 | n/a | # c1 and c2. Clearing c1 has the side effect of dropping the refcount |
---|
987 | n/a | # on d0 to 0, so d0 goes away (despite that it's in an older |
---|
988 | n/a | # generation) and d0's __del__ triggers. That in turn materializes |
---|
989 | n/a | # a reference to c2 via c2wr(), but c2 gets cleared anyway by gc. |
---|
990 | n/a | |
---|
991 | n/a | # We want to let gc happen "naturally", to preserve the distinction |
---|
992 | n/a | # between generations. |
---|
993 | n/a | detector = GC_Detector() |
---|
994 | n/a | junk = [] |
---|
995 | n/a | i = 0 |
---|
996 | n/a | while not detector.gc_happened: |
---|
997 | n/a | i += 1 |
---|
998 | n/a | if i > 10000: |
---|
999 | n/a | self.fail("gc didn't happen after 10000 iterations") |
---|
1000 | n/a | self.assertEqual(len(ouch), 0) |
---|
1001 | n/a | junk.append([]) # this will eventually trigger gc |
---|
1002 | n/a | |
---|
1003 | n/a | self.assertEqual(len(ouch), 1) # else __del__ wasn't invoked |
---|
1004 | n/a | for x in ouch: |
---|
1005 | n/a | # If __del__ resurrected c2, the instance would be damaged, with an |
---|
1006 | n/a | # empty __dict__. |
---|
1007 | n/a | self.assertEqual(x, None) |
---|
1008 | n/a | |
---|
1009 | n/a | def test_main(): |
---|
1010 | n/a | enabled = gc.isenabled() |
---|
1011 | n/a | gc.disable() |
---|
1012 | n/a | assert not gc.isenabled() |
---|
1013 | n/a | debug = gc.get_debug() |
---|
1014 | n/a | gc.set_debug(debug & ~gc.DEBUG_LEAK) # this test is supposed to leak |
---|
1015 | n/a | |
---|
1016 | n/a | try: |
---|
1017 | n/a | gc.collect() # Delete 2nd generation garbage |
---|
1018 | n/a | run_unittest(GCTests, GCTogglingTests, GCCallbackTests) |
---|
1019 | n/a | finally: |
---|
1020 | n/a | gc.set_debug(debug) |
---|
1021 | n/a | # test gc.enable() even if GC is disabled by default |
---|
1022 | n/a | if verbose: |
---|
1023 | n/a | print("restoring automatic collection") |
---|
1024 | n/a | # make sure to always test gc.enable() |
---|
1025 | n/a | gc.enable() |
---|
1026 | n/a | assert gc.isenabled() |
---|
1027 | n/a | if not enabled: |
---|
1028 | n/a | gc.disable() |
---|
1029 | n/a | |
---|
1030 | n/a | if __name__ == "__main__": |
---|
1031 | n/a | test_main() |
---|