annotate Lib/test/test_gc.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 11d6e538a279
children 5a2ef447b80d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
1 import unittest
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
2 from test.test_support import verbose, run_unittest
18508
8de91042173f Test GC of frame objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15957
diff changeset
3 import sys
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
4 import gc
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
5 import weakref
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
6
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
7 ### Support code
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
8 ###############################################################################
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41400
diff changeset
9
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
10 # Bug 1055820 has several tests of longstanding bugs involving weakrefs and
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
11 # cyclic gc.
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
12
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
13 # An instance of C1055820 has a self-loop, so becomes cyclic trash when
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
14 # unreachable.
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
15 class C1055820(object):
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
16 def __init__(self, i):
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
17 self.i = i
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
18 self.loop = self
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
19
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
20 class GC_Detector(object):
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
21 # Create an instance I. Then gc hasn't happened again so long as
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
22 # I.gc_happened is false.
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
23
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
24 def __init__(self):
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
25 self.gc_happened = False
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
26
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
27 def it_happened(ignored):
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
28 self.gc_happened = True
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
29
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
30 # Create a piece of cyclic trash that triggers it_happened when
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
31 # gc collects it.
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
32 self.wr = weakref.ref(C1055820(666), it_happened)
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
33
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
34
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41400
diff changeset
35 ### Tests
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
36 ###############################################################################
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41400
diff changeset
37
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
38 class GCTests(unittest.TestCase):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
39 def test_list(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
40 l = []
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
41 l.append(l)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
42 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
43 del l
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
44 self.assertEqual(gc.collect(), 1)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
45
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
46 def test_dict(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
47 d = {}
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
48 d[1] = d
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
49 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
50 del d
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
51 self.assertEqual(gc.collect(), 1)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
52
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
53 def test_tuple(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
54 # since tuples are immutable we close the loop with a list
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
55 l = []
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
56 t = (l,)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
57 l.append(t)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
58 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
59 del t
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
60 del l
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
61 self.assertEqual(gc.collect(), 2)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
62
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
63 def test_class(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
64 class A:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
65 pass
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
66 A.a = A
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
67 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
68 del A
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
69 self.assertNotEqual(gc.collect(), 0)
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
70
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
71 def test_newstyleclass(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
72 class A(object):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
73 pass
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
74 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
75 del A
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
76 self.assertNotEqual(gc.collect(), 0)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
77
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
78 def test_instance(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
79 class A:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
80 pass
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
81 a = A()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
82 a.a = a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
83 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
84 del a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
85 self.assertNotEqual(gc.collect(), 0)
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
86
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
87 def test_newinstance(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
88 class A(object):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
89 pass
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
90 a = A()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
91 a.a = a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
92 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
93 del a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
94 self.assertNotEqual(gc.collect(), 0)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
95 class B(list):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
96 pass
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
97 class C(B, A):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
98 pass
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
99 a = C()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
100 a.a = a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
101 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
102 del a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
103 self.assertNotEqual(gc.collect(), 0)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
104 del B, C
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
105 self.assertNotEqual(gc.collect(), 0)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
106 A.a = A()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
107 del A
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
108 self.assertNotEqual(gc.collect(), 0)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
109 self.assertEqual(gc.collect(), 0)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
110
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
111 def test_method(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
112 # Tricky: self.__init__ is a bound method, it references the instance.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
113 class A:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
114 def __init__(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
115 self.init = self.__init__
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
116 a = A()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
117 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
118 del a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
119 self.assertNotEqual(gc.collect(), 0)
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
120
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
121 def test_finalizer(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
122 # A() is uncollectable if it is part of a cycle, make sure it shows up
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
123 # in gc.garbage.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
124 class A:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
125 def __del__(self): pass
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
126 class B:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
127 pass
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
128 a = A()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
129 a.a = a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
130 id_a = id(a)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
131 b = B()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
132 b.b = b
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
133 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
134 del a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
135 del b
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
136 self.assertNotEqual(gc.collect(), 0)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
137 for obj in gc.garbage:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
138 if id(obj) == id_a:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
139 del obj.a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
140 break
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
141 else:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
142 self.fail("didn't find obj in garbage (finalizer)")
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
143 gc.garbage.remove(obj)
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
144
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
145 def test_finalizer_newclass(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
146 # A() is uncollectable if it is part of a cycle, make sure it shows up
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
147 # in gc.garbage.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
148 class A(object):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
149 def __del__(self): pass
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
150 class B(object):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
151 pass
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
152 a = A()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
153 a.a = a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
154 id_a = id(a)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
155 b = B()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
156 b.b = b
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
157 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
158 del a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
159 del b
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
160 self.assertNotEqual(gc.collect(), 0)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
161 for obj in gc.garbage:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
162 if id(obj) == id_a:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
163 del obj.a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
164 break
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
165 else:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
166 self.fail("didn't find obj in garbage (finalizer)")
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
167 gc.garbage.remove(obj)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
168
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
169 def test_function(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
170 # Tricky: f -> d -> f, code should call d.clear() after the exec to
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
171 # break the cycle.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
172 d = {}
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
173 exec("def f(): pass\n") in d
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
174 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
175 del d
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
176 self.assertEqual(gc.collect(), 2)
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
177
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
178 def test_frame(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
179 def f():
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
180 frame = sys._getframe()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
181 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
182 f()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
183 self.assertEqual(gc.collect(), 1)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
184
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
185 def test_saveall(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
186 # Verify that cyclic garbage like lists show up in gc.garbage if the
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
187 # SAVEALL option is enabled.
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
188
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
189 # First make sure we don't save away other stuff that just happens to
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
190 # be waiting for collection.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
191 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
192 # if this fails, someone else created immortal trash
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
193 self.assertEqual(gc.garbage, [])
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
194
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
195 L = []
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
196 L.append(L)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
197 id_L = id(L)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
198
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
199 debug = gc.get_debug()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
200 gc.set_debug(debug | gc.DEBUG_SAVEALL)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
201 del L
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
202 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
203 gc.set_debug(debug)
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
204
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
205 self.assertEqual(len(gc.garbage), 1)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
206 obj = gc.garbage.pop()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
207 self.assertEqual(id(obj), id_L)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
208
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
209 def test_del(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
210 # __del__ methods can trigger collection, make this to happen
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
211 thresholds = gc.get_threshold()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
212 gc.enable()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
213 gc.set_threshold(1)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
214
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
215 class A:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
216 def __del__(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
217 dir(self)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
218 a = A()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
219 del a
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
220
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
221 gc.disable()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
222 gc.set_threshold(*thresholds)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
223
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
224 def test_del_newclass(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
225 # __del__ methods can trigger collection, make this to happen
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
226 thresholds = gc.get_threshold()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
227 gc.enable()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
228 gc.set_threshold(1)
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
229
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
230 class A(object):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
231 def __del__(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
232 dir(self)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
233 a = A()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
234 del a
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
235
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
236 gc.disable()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
237 gc.set_threshold(*thresholds)
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
238
45368
c44752d90fd2 Re-enable tests, they were failing since gc.collect() clears the various freelists.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45365
diff changeset
239 # The following two tests are fragile:
c44752d90fd2 Re-enable tests, they were failing since gc.collect() clears the various freelists.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45365
diff changeset
240 # They precisely count the number of allocations,
c44752d90fd2 Re-enable tests, they were failing since gc.collect() clears the various freelists.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45365
diff changeset
241 # which is highly implementation-dependent.
c44752d90fd2 Re-enable tests, they were failing since gc.collect() clears the various freelists.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45365
diff changeset
242 # For example:
c44752d90fd2 Re-enable tests, they were failing since gc.collect() clears the various freelists.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45365
diff changeset
243 # - disposed tuples are not freed, but reused
c44752d90fd2 Re-enable tests, they were failing since gc.collect() clears the various freelists.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45365
diff changeset
244 # - the call to assertEqual somehow avoids building its args tuple
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
245 def test_get_count(self):
45368
c44752d90fd2 Re-enable tests, they were failing since gc.collect() clears the various freelists.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45365
diff changeset
246 # Avoid future allocation of method object
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52288
diff changeset
247 assertEqual = self._baseAssertEqual
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
248 gc.collect()
45368
c44752d90fd2 Re-enable tests, they were failing since gc.collect() clears the various freelists.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45365
diff changeset
249 assertEqual(gc.get_count(), (0, 0, 0))
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
250 a = dict()
45368
c44752d90fd2 Re-enable tests, they were failing since gc.collect() clears the various freelists.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45365
diff changeset
251 # since gc.collect(), we created two objects:
c44752d90fd2 Re-enable tests, they were failing since gc.collect() clears the various freelists.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45365
diff changeset
252 # the dict, and the tuple returned by get_count()
c44752d90fd2 Re-enable tests, they were failing since gc.collect() clears the various freelists.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45365
diff changeset
253 assertEqual(gc.get_count(), (2, 0, 0))
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
254
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
255 def test_collect_generations(self):
45368
c44752d90fd2 Re-enable tests, they were failing since gc.collect() clears the various freelists.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45365
diff changeset
256 # Avoid future allocation of method object
c44752d90fd2 Re-enable tests, they were failing since gc.collect() clears the various freelists.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45365
diff changeset
257 assertEqual = self.assertEqual
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
258 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
259 a = dict()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
260 gc.collect(0)
45368
c44752d90fd2 Re-enable tests, they were failing since gc.collect() clears the various freelists.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45365
diff changeset
261 assertEqual(gc.get_count(), (0, 1, 0))
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
262 gc.collect(1)
45368
c44752d90fd2 Re-enable tests, they were failing since gc.collect() clears the various freelists.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45365
diff changeset
263 assertEqual(gc.get_count(), (0, 0, 1))
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
264 gc.collect(2)
45368
c44752d90fd2 Re-enable tests, they were failing since gc.collect() clears the various freelists.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45365
diff changeset
265 assertEqual(gc.get_count(), (0, 0, 0))
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
266
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
267 def test_trashcan(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
268 class Ouch:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
269 n = 0
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
270 def __del__(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
271 Ouch.n = Ouch.n + 1
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
272 if Ouch.n % 17 == 0:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
273 gc.collect()
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41400
diff changeset
274
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
275 # "trashcan" is a hack to prevent stack overflow when deallocating
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
276 # very deeply nested tuples etc. It works in part by abusing the
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
277 # type pointer and refcount fields, and that can yield horrible
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
278 # problems when gc tries to traverse the structures.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
279 # If this test fails (as it does in 2.0, 2.1 and 2.2), it will
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
280 # most likely die via segfault.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
281
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
282 # Note: In 2.3 the possibility for compiling without cyclic gc was
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
283 # removed, and that in turn allows the trashcan mechanism to work
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
284 # via much simpler means (e.g., it never abuses the type pointer or
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
285 # refcount fields anymore). Since it's much less likely to cause a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
286 # problem now, the various constants in this expensive (we force a lot
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
287 # of full collections) test are cut back from the 2.2 version.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
288 gc.enable()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
289 N = 150
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
290 for count in range(2):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
291 t = []
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
292 for i in range(N):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
293 t = [t, Ouch()]
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
294 u = []
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
295 for i in range(N):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
296 u = [u, Ouch()]
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
297 v = {}
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
298 for i in range(N):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
299 v = {1: v, 2: Ouch()}
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
300 gc.disable()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
301
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
302 def test_boom(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
303 class Boom:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
304 def __getattr__(self, someattribute):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
305 del self.attr
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
306 raise AttributeError
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41400
diff changeset
307
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
308 a = Boom()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
309 b = Boom()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
310 a.attr = b
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
311 b.attr = a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
312
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
313 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
314 garbagelen = len(gc.garbage)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
315 del a, b
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
316 # a<->b are in a trash cycle now. Collection will invoke
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
317 # Boom.__getattr__ (to see whether a and b have __del__ methods), and
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
318 # __getattr__ deletes the internal "attr" attributes as a side effect.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
319 # That causes the trash cycle to get reclaimed via refcounts falling to
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
320 # 0, thus mutating the trash graph as a side effect of merely asking
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
321 # whether __del__ exists. This used to (before 2.3b1) crash Python.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
322 # Now __getattr__ isn't called.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
323 self.assertEqual(gc.collect(), 4)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
324 self.assertEqual(len(gc.garbage), garbagelen)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
325
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
326 def test_boom2(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
327 class Boom2:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
328 def __init__(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
329 self.x = 0
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
330
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
331 def __getattr__(self, someattribute):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
332 self.x += 1
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
333 if self.x > 1:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
334 del self.attr
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
335 raise AttributeError
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41400
diff changeset
336
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
337 a = Boom2()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
338 b = Boom2()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
339 a.attr = b
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
340 b.attr = a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
341
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
342 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
343 garbagelen = len(gc.garbage)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
344 del a, b
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
345 # Much like test_boom(), except that __getattr__ doesn't break the
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
346 # cycle until the second time gc checks for __del__. As of 2.3b1,
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
347 # there isn't a second time, so this simply cleans up the trash cycle.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
348 # We expect a, b, a.__dict__ and b.__dict__ (4 objects) to get
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
349 # reclaimed this way.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
350 self.assertEqual(gc.collect(), 4)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
351 self.assertEqual(len(gc.garbage), garbagelen)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
352
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
353 def test_boom_new(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
354 # boom__new and boom2_new are exactly like boom and boom2, except use
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
355 # new-style classes.
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
356
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
357 class Boom_New(object):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
358 def __getattr__(self, someattribute):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
359 del self.attr
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
360 raise AttributeError
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41400
diff changeset
361
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
362 a = Boom_New()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
363 b = Boom_New()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
364 a.attr = b
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
365 b.attr = a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
366
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
367 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
368 garbagelen = len(gc.garbage)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
369 del a, b
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
370 self.assertEqual(gc.collect(), 4)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
371 self.assertEqual(len(gc.garbage), garbagelen)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
372
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
373 def test_boom2_new(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
374 class Boom2_New(object):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
375 def __init__(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
376 self.x = 0
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
377
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
378 def __getattr__(self, someattribute):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
379 self.x += 1
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
380 if self.x > 1:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
381 del self.attr
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
382 raise AttributeError
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41400
diff changeset
383
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
384 a = Boom2_New()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
385 b = Boom2_New()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
386 a.attr = b
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
387 b.attr = a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
388
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
389 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
390 garbagelen = len(gc.garbage)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
391 del a, b
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
392 self.assertEqual(gc.collect(), 4)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
393 self.assertEqual(len(gc.garbage), garbagelen)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
394
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
395 def test_get_referents(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
396 alist = [1, 3, 5]
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
397 got = gc.get_referents(alist)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
398 got.sort()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
399 self.assertEqual(got, alist)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
401 atuple = tuple(alist)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
402 got = gc.get_referents(atuple)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
403 got.sort()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
404 self.assertEqual(got, alist)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
405
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
406 adict = {1: 3, 5: 7}
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
407 expected = [1, 3, 5, 7]
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
408 got = gc.get_referents(adict)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
409 got.sort()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
410 self.assertEqual(got, expected)
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
411
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
412 got = gc.get_referents([1, 2], {3: 4}, (0, 0, 0))
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
413 got.sort()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
414 self.assertEqual(got, [0, 0] + range(5))
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
415
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
416 self.assertEqual(gc.get_referents(1, 'a', 4j), [])
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
417
52288
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
418 def test_is_tracked(self):
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
419 # Atomic built-in types are not tracked, user-defined objects and
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
420 # mutable containers are.
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
421 # NOTE: types with special optimizations (e.g. tuple) have tests
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
422 # in their own test files instead.
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
423 self.assertFalse(gc.is_tracked(None))
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
424 self.assertFalse(gc.is_tracked(1))
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
425 self.assertFalse(gc.is_tracked(1.0))
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
426 self.assertFalse(gc.is_tracked(1.0 + 5.0j))
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
427 self.assertFalse(gc.is_tracked(True))
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
428 self.assertFalse(gc.is_tracked(False))
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
429 self.assertFalse(gc.is_tracked("a"))
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
430 self.assertFalse(gc.is_tracked(u"a"))
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
431 self.assertFalse(gc.is_tracked(bytearray("a")))
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
432 self.assertFalse(gc.is_tracked(type))
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
433 self.assertFalse(gc.is_tracked(int))
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
434 self.assertFalse(gc.is_tracked(object))
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
435 self.assertFalse(gc.is_tracked(object()))
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
436
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
437 class OldStyle:
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
438 pass
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
439 class NewStyle(object):
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
440 pass
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
441 self.assertTrue(gc.is_tracked(gc))
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
442 self.assertTrue(gc.is_tracked(OldStyle))
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
443 self.assertTrue(gc.is_tracked(OldStyle()))
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
444 self.assertTrue(gc.is_tracked(NewStyle))
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
445 self.assertTrue(gc.is_tracked(NewStyle()))
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
446 self.assertTrue(gc.is_tracked([]))
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
447 self.assertTrue(gc.is_tracked(set()))
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
448
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
449 def test_bug1055820b(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
450 # Corresponds to temp2b.py in the bug report.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
451
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
452 ouch = []
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
453 def callback(ignored):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
454 ouch[:] = [wr() for wr in WRs]
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
455
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
456 Cs = [C1055820(i) for i in range(2)]
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
457 WRs = [weakref.ref(c, callback) for c in Cs]
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
458 c = None
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
459
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
460 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
461 self.assertEqual(len(ouch), 0)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
462 # Make the two instances trash, and collect again. The bug was that
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
463 # the callback materialized a strong reference to an instance, but gc
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
464 # cleared the instance's dict anyway.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
465 Cs = None
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
466 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
467 self.assertEqual(len(ouch), 2) # else the callbacks didn't run
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
468 for x in ouch:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
469 # If the callback resurrected one of these guys, the instance
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
470 # would be damaged, with an empty __dict__.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
471 self.assertEqual(x, None)
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
472
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
473 class GCTogglingTests(unittest.TestCase):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
474 def setUp(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
475 gc.enable()
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41400
diff changeset
476
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
477 def tearDown(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
478 gc.disable()
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41400
diff changeset
479
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
480 def test_bug1055820c(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
481 # Corresponds to temp2c.py in the bug report. This is pretty
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
482 # elaborate.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
483
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
484 c0 = C1055820(0)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
485 # Move c0 into generation 2.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
486 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
487
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
488 c1 = C1055820(1)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
489 c1.keep_c0_alive = c0
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
490 del c0.loop # now only c1 keeps c0 alive
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
491
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
492 c2 = C1055820(2)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
493 c2wr = weakref.ref(c2) # no callback!
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
494
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
495 ouch = []
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
496 def callback(ignored):
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
497 ouch[:] = [c2wr()]
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
498
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
499 # The callback gets associated with a wr on an object in generation 2.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
500 c0wr = weakref.ref(c0, callback)
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
501
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
502 c0 = c1 = c2 = None
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
503
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
504 # What we've set up: c0, c1, and c2 are all trash now. c0 is in
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
505 # generation 2. The only thing keeping it alive is that c1 points to
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
506 # it. c1 and c2 are in generation 0, and are in self-loops. There's a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
507 # global weakref to c2 (c2wr), but that weakref has no callback.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
508 # There's also a global weakref to c0 (c0wr), and that does have a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
509 # callback, and that callback references c2 via c2wr().
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
510 #
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
511 # c0 has a wr with callback, which references c2wr
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
512 # ^
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
513 # |
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
514 # | Generation 2 above dots
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
515 #. . . . . . . .|. . . . . . . . . . . . . . . . . . . . . . . .
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
516 # | Generation 0 below dots
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
517 # |
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
518 # |
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
519 # ^->c1 ^->c2 has a wr but no callback
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
520 # | | | |
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
521 # <--v <--v
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
522 #
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
523 # So this is the nightmare: when generation 0 gets collected, we see
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
524 # that c2 has a callback-free weakref, and c1 doesn't even have a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
525 # weakref. Collecting generation 0 doesn't see c0 at all, and c0 is
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
526 # the only object that has a weakref with a callback. gc clears c1
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
527 # and c2. Clearing c1 has the side effect of dropping the refcount on
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
528 # c0 to 0, so c0 goes away (despite that it's in an older generation)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
529 # and c0's wr callback triggers. That in turn materializes a reference
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
530 # to c2 via c2wr(), but c2 gets cleared anyway by gc.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
531
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
532 # We want to let gc happen "naturally", to preserve the distinction
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
533 # between generations.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
534 junk = []
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
535 i = 0
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
536 detector = GC_Detector()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
537 while not detector.gc_happened:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
538 i += 1
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
539 if i > 10000:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
540 self.fail("gc didn't happen after 10000 iterations")
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
541 self.assertEqual(len(ouch), 0)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
542 junk.append([]) # this will eventually trigger gc
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
543
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
544 self.assertEqual(len(ouch), 1) # else the callback wasn't invoked
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
545 for x in ouch:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
546 # If the callback resurrected c2, the instance would be damaged,
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
547 # with an empty __dict__.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
548 self.assertEqual(x, None)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
549
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
550 def test_bug1055820d(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
551 # Corresponds to temp2d.py in the bug report. This is very much like
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
552 # test_bug1055820c, but uses a __del__ method instead of a weakref
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
553 # callback to sneak in a resurrection of cyclic trash.
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
554
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
555 ouch = []
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
556 class D(C1055820):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
557 def __del__(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
558 ouch[:] = [c2wr()]
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
559
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
560 d0 = D(0)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
561 # Move all the above into generation 2.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
562 gc.collect()
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
563
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
564 c1 = C1055820(1)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
565 c1.keep_d0_alive = d0
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
566 del d0.loop # now only c1 keeps d0 alive
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
567
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
568 c2 = C1055820(2)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
569 c2wr = weakref.ref(c2) # no callback!
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
570
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
571 d0 = c1 = c2 = None
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
572
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
573 # What we've set up: d0, c1, and c2 are all trash now. d0 is in
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
574 # generation 2. The only thing keeping it alive is that c1 points to
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
575 # it. c1 and c2 are in generation 0, and are in self-loops. There's
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
576 # a global weakref to c2 (c2wr), but that weakref has no callback.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
577 # There are no other weakrefs.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
578 #
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
579 # d0 has a __del__ method that references c2wr
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
580 # ^
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
581 # |
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
582 # | Generation 2 above dots
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
583 #. . . . . . . .|. . . . . . . . . . . . . . . . . . . . . . . .
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
584 # | Generation 0 below dots
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
585 # |
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
586 # |
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
587 # ^->c1 ^->c2 has a wr but no callback
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
588 # | | | |
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
589 # <--v <--v
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
590 #
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
591 # So this is the nightmare: when generation 0 gets collected, we see
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
592 # that c2 has a callback-free weakref, and c1 doesn't even have a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
593 # weakref. Collecting generation 0 doesn't see d0 at all. gc clears
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
594 # c1 and c2. Clearing c1 has the side effect of dropping the refcount
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
595 # on d0 to 0, so d0 goes away (despite that it's in an older
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
596 # generation) and d0's __del__ triggers. That in turn materializes
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
597 # a reference to c2 via c2wr(), but c2 gets cleared anyway by gc.
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
598
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
599 # We want to let gc happen "naturally", to preserve the distinction
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
600 # between generations.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
601 detector = GC_Detector()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
602 junk = []
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
603 i = 0
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
604 while not detector.gc_happened:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
605 i += 1
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
606 if i > 10000:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
607 self.fail("gc didn't happen after 10000 iterations")
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
608 self.assertEqual(len(ouch), 0)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
609 junk.append([]) # this will eventually trigger gc
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
610
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
611 self.assertEqual(len(ouch), 1) # else __del__ wasn't invoked
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
612 for x in ouch:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
613 # If __del__ resurrected c2, the instance would be damaged, with an
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
614 # empty __dict__.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
615 self.assertEqual(x, None)
13878
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13211
diff changeset
616
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
617 def test_main():
13878
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13211
diff changeset
618 enabled = gc.isenabled()
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13211
diff changeset
619 gc.disable()
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
620 assert not gc.isenabled()
14757
b4ea57870f2a - Add test for new SAVEALL debugging flag
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14610
diff changeset
621 debug = gc.get_debug()
b4ea57870f2a - Add test for new SAVEALL debugging flag
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14610
diff changeset
622 gc.set_debug(debug & ~gc.DEBUG_LEAK) # this test is supposed to leak
13878
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13211
diff changeset
623
14757
b4ea57870f2a - Add test for new SAVEALL debugging flag
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14610
diff changeset
624 try:
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
625 gc.collect() # Delete 2nd generation garbage
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
626 run_unittest(GCTests, GCTogglingTests)
14757
b4ea57870f2a - Add test for new SAVEALL debugging flag
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14610
diff changeset
627 finally:
b4ea57870f2a - Add test for new SAVEALL debugging flag
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14610
diff changeset
628 gc.set_debug(debug)
b4ea57870f2a - Add test for new SAVEALL debugging flag
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14610
diff changeset
629 # test gc.enable() even if GC is disabled by default
b4ea57870f2a - Add test for new SAVEALL debugging flag
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14610
diff changeset
630 if verbose:
b4ea57870f2a - Add test for new SAVEALL debugging flag
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14610
diff changeset
631 print "restoring automatic collection"
b4ea57870f2a - Add test for new SAVEALL debugging flag
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14610
diff changeset
632 # make sure to always test gc.enable()
b4ea57870f2a - Add test for new SAVEALL debugging flag
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14610
diff changeset
633 gc.enable()
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
634 assert gc.isenabled()
14757
b4ea57870f2a - Add test for new SAVEALL debugging flag
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14610
diff changeset
635 if not enabled:
b4ea57870f2a - Add test for new SAVEALL debugging flag
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14610
diff changeset
636 gc.disable()
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41400
diff changeset
637
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
638 if __name__ == "__main__":
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
639 test_main()