annotate Lib/test/test_gc.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 5a2ef447b80d
children
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
78855
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
4 import time
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
5 import gc
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
6 import weakref
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
7
78855
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
8 try:
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
9 import threading
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
10 except ImportError:
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
11 threading = None
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
12
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
13 ### Support code
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
14 ###############################################################################
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41400
diff changeset
15
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
16 # 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
17 # cyclic gc.
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
18
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
19 # 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
20 # unreachable.
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
21 class C1055820(object):
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
22 def __init__(self, i):
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
23 self.i = i
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
24 self.loop = self
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
25
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
26 class GC_Detector(object):
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
27 # 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
28 # I.gc_happened is false.
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 def __init__(self):
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
31 self.gc_happened = False
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
32
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
33 def it_happened(ignored):
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
34 self.gc_happened = True
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
35
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
36 # 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
37 # gc collects it.
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
38 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
39
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
40
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41400
diff changeset
41 ### Tests
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
42 ###############################################################################
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41400
diff changeset
43
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
44 class GCTests(unittest.TestCase):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
45 def test_list(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
46 l = []
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
47 l.append(l)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
48 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
49 del l
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
50 self.assertEqual(gc.collect(), 1)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
51
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
52 def test_dict(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
53 d = {}
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
54 d[1] = d
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
55 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
56 del d
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
57 self.assertEqual(gc.collect(), 1)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
58
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
59 def test_tuple(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
60 # 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
61 l = []
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
62 t = (l,)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
63 l.append(t)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
64 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
65 del t
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
66 del l
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
67 self.assertEqual(gc.collect(), 2)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
68
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
69 def test_class(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
70 class A:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
71 pass
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
72 A.a = A
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
73 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
74 del A
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
75 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
76
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
77 def test_newstyleclass(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
78 class A(object):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
79 pass
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
80 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
81 del A
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
82 self.assertNotEqual(gc.collect(), 0)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
83
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
84 def test_instance(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
85 class A:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
86 pass
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
87 a = A()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
88 a.a = a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
89 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
90 del a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
91 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
92
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
93 def test_newinstance(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
94 class A(object):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
95 pass
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
96 a = A()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
97 a.a = a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
98 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
99 del a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
100 self.assertNotEqual(gc.collect(), 0)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
101 class B(list):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
102 pass
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
103 class C(B, A):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
104 pass
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
105 a = C()
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 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
108 del a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
109 self.assertNotEqual(gc.collect(), 0)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
110 del B, C
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
111 self.assertNotEqual(gc.collect(), 0)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
112 A.a = A()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
113 del A
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
114 self.assertNotEqual(gc.collect(), 0)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
115 self.assertEqual(gc.collect(), 0)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
116
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
117 def test_method(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
118 # 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
119 class A:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
120 def __init__(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
121 self.init = self.__init__
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
122 a = A()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
123 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
124 del a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
125 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
126
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
127 def test_finalizer(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
128 # 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
129 # in gc.garbage.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
130 class A:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
131 def __del__(self): pass
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
132 class B:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
133 pass
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
134 a = A()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
135 a.a = a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
136 id_a = id(a)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
137 b = B()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
138 b.b = b
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
139 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
140 del a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
141 del b
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
142 self.assertNotEqual(gc.collect(), 0)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
143 for obj in gc.garbage:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
144 if id(obj) == id_a:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
145 del obj.a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
146 break
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
147 else:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
148 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
149 gc.garbage.remove(obj)
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
150
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
151 def test_finalizer_newclass(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
152 # 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
153 # in gc.garbage.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
154 class A(object):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
155 def __del__(self): pass
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
156 class B(object):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
157 pass
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
158 a = A()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
159 a.a = a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
160 id_a = id(a)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
161 b = B()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
162 b.b = b
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
163 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
164 del a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
165 del b
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
166 self.assertNotEqual(gc.collect(), 0)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
167 for obj in gc.garbage:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
168 if id(obj) == id_a:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
169 del obj.a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
170 break
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
171 else:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
172 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
173 gc.garbage.remove(obj)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
174
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
175 def test_function(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
176 # 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
177 # break the cycle.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
178 d = {}
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
179 exec("def f(): pass\n") in d
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
180 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
181 del d
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
182 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
183
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
184 def test_frame(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
185 def f():
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
186 frame = sys._getframe()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
187 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
188 f()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
189 self.assertEqual(gc.collect(), 1)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
190
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
191 def test_saveall(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
192 # 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
193 # SAVEALL option is enabled.
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
194
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
195 # 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
196 # be waiting for collection.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
197 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
198 # if this fails, someone else created immortal trash
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
199 self.assertEqual(gc.garbage, [])
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
200
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
201 L = []
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
202 L.append(L)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
203 id_L = id(L)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
204
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
205 debug = gc.get_debug()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
206 gc.set_debug(debug | gc.DEBUG_SAVEALL)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
207 del L
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
208 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
209 gc.set_debug(debug)
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
210
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
211 self.assertEqual(len(gc.garbage), 1)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
212 obj = gc.garbage.pop()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
213 self.assertEqual(id(obj), id_L)
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 def test_del(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
216 # __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
217 thresholds = gc.get_threshold()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
218 gc.enable()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
219 gc.set_threshold(1)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
220
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
221 class A:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
222 def __del__(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
223 dir(self)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
224 a = A()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
225 del a
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
226
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
227 gc.disable()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
228 gc.set_threshold(*thresholds)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
229
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
230 def test_del_newclass(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
231 # __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
232 thresholds = gc.get_threshold()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
233 gc.enable()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
234 gc.set_threshold(1)
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 class A(object):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
237 def __del__(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
238 dir(self)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
239 a = A()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
240 del a
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
241
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
242 gc.disable()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
243 gc.set_threshold(*thresholds)
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
244
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
245 # 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
246 # 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
247 # 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
248 # 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
249 # - 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
250 # - 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
251 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
252 # 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
253 assertEqual = self._baseAssertEqual
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
254 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
255 assertEqual(gc.get_count(), (0, 0, 0))
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
256 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
257 # 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
258 # 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
259 assertEqual(gc.get_count(), (2, 0, 0))
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
260
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
261 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
262 # 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
263 assertEqual = self.assertEqual
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
264 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
265 a = dict()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
266 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
267 assertEqual(gc.get_count(), (0, 1, 0))
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
268 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
269 assertEqual(gc.get_count(), (0, 0, 1))
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
270 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
271 assertEqual(gc.get_count(), (0, 0, 0))
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
272
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
273 def test_trashcan(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
274 class Ouch:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
275 n = 0
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
276 def __del__(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
277 Ouch.n = Ouch.n + 1
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
278 if Ouch.n % 17 == 0:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
279 gc.collect()
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41400
diff changeset
280
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
281 # "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
282 # 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
283 # 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
284 # problems when gc tries to traverse the structures.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
285 # 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
286 # most likely die via segfault.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
287
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
288 # 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
289 # 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
290 # 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
291 # 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
292 # 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
293 # 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
294 gc.enable()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
295 N = 150
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
296 for count in range(2):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
297 t = []
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 t = [t, Ouch()]
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
300 u = []
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
301 for i in range(N):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
302 u = [u, Ouch()]
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
303 v = {}
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
304 for i in range(N):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
305 v = {1: v, 2: Ouch()}
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
306 gc.disable()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
307
78855
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
308 @unittest.skipUnless(threading, "test meaningless on builds without threads")
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
309 def test_trashcan_threads(self):
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
310 # Issue #13992: trashcan mechanism should be thread-safe
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
311 NESTING = 60
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
312 N_THREADS = 2
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
313
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
314 def sleeper_gen():
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
315 """A generator that releases the GIL when closed or dealloc'ed."""
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
316 try:
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
317 yield
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
318 finally:
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
319 time.sleep(0.000001)
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
320
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
321 class C(list):
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
322 # Appending to a list is atomic, which avoids the use of a lock.
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
323 inits = []
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
324 dels = []
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
325 def __init__(self, alist):
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
326 self[:] = alist
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
327 C.inits.append(None)
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
328 def __del__(self):
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
329 # This __del__ is called by subtype_dealloc().
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
330 C.dels.append(None)
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
331 # `g` will release the GIL when garbage-collected. This
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
332 # helps assert subtype_dealloc's behaviour when threads
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
333 # switch in the middle of it.
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
334 g = sleeper_gen()
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
335 next(g)
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
336 # Now that __del__ is finished, subtype_dealloc will proceed
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
337 # to call list_dealloc, which also uses the trashcan mechanism.
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
338
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
339 def make_nested():
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
340 """Create a sufficiently nested container object so that the
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
341 trashcan mechanism is invoked when deallocating it."""
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
342 x = C([])
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
343 for i in range(NESTING):
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
344 x = [C([x])]
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
345 del x
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
346
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
347 def run_thread():
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
348 """Exercise make_nested() in a loop."""
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
349 while not exit:
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
350 make_nested()
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
351
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
352 old_checkinterval = sys.getcheckinterval()
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
353 sys.setcheckinterval(3)
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
354 try:
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
355 exit = False
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
356 threads = []
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
357 for i in range(N_THREADS):
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
358 t = threading.Thread(target=run_thread)
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
359 threads.append(t)
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
360 for t in threads:
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
361 t.start()
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
362 time.sleep(1.0)
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
363 exit = True
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
364 for t in threads:
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
365 t.join()
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
366 finally:
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
367 sys.setcheckinterval(old_checkinterval)
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
368 gc.collect()
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
369 self.assertEqual(len(C.inits), len(C.dels))
5a2ef447b80d Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
Antoine Pitrou <solipsis@pitrou.net>
parents: 52540
diff changeset
370
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
371 def test_boom(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
372 class Boom:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
373 def __getattr__(self, someattribute):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
374 del self.attr
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
375 raise AttributeError
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41400
diff changeset
376
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
377 a = Boom()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
378 b = Boom()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
379 a.attr = b
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
380 b.attr = a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
381
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
382 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
383 garbagelen = len(gc.garbage)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
384 del a, b
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
385 # 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
386 # 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
387 # __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
388 # 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
389 # 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
390 # 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
391 # Now __getattr__ isn't called.
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_boom2(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
396 class Boom2:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
397 def __init__(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
398 self.x = 0
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
399
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
400 def __getattr__(self, someattribute):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
401 self.x += 1
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
402 if self.x > 1:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
403 del self.attr
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
404 raise AttributeError
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41400
diff changeset
405
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
406 a = Boom2()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
407 b = Boom2()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
408 a.attr = b
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
409 b.attr = a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
410
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
411 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
412 garbagelen = len(gc.garbage)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
413 del a, b
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
414 # 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
415 # 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
416 # 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
417 # 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
418 # reclaimed this way.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
419 self.assertEqual(gc.collect(), 4)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
420 self.assertEqual(len(gc.garbage), garbagelen)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
421
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
422 def test_boom_new(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
423 # 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
424 # new-style classes.
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
425
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
426 class Boom_New(object):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
427 def __getattr__(self, someattribute):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
428 del self.attr
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
429 raise AttributeError
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41400
diff changeset
430
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
431 a = Boom_New()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
432 b = Boom_New()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
433 a.attr = b
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
434 b.attr = a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
435
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
436 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
437 garbagelen = len(gc.garbage)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
438 del a, b
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
439 self.assertEqual(gc.collect(), 4)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
440 self.assertEqual(len(gc.garbage), garbagelen)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
441
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
442 def test_boom2_new(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
443 class Boom2_New(object):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
444 def __init__(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
445 self.x = 0
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
446
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
447 def __getattr__(self, someattribute):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
448 self.x += 1
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
449 if self.x > 1:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
450 del self.attr
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
451 raise AttributeError
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41400
diff changeset
452
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
453 a = Boom2_New()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
454 b = Boom2_New()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
455 a.attr = b
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
456 b.attr = a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
457
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
458 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
459 garbagelen = len(gc.garbage)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
460 del a, b
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
461 self.assertEqual(gc.collect(), 4)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
462 self.assertEqual(len(gc.garbage), garbagelen)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
463
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
464 def test_get_referents(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
465 alist = [1, 3, 5]
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
466 got = gc.get_referents(alist)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
467 got.sort()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
468 self.assertEqual(got, alist)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
469
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
470 atuple = tuple(alist)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
471 got = gc.get_referents(atuple)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
472 got.sort()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
473 self.assertEqual(got, alist)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
474
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
475 adict = {1: 3, 5: 7}
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
476 expected = [1, 3, 5, 7]
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
477 got = gc.get_referents(adict)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
478 got.sort()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
479 self.assertEqual(got, expected)
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
480
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
481 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
482 got.sort()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
483 self.assertEqual(got, [0, 0] + range(5))
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
484
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
485 self.assertEqual(gc.get_referents(1, 'a', 4j), [])
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
486
52288
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
487 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
488 # 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
489 # 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
490 # 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
491 # 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
492 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
493 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
494 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
495 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
496 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
497 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
498 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
499 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
500 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
501 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
502 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
503 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
504 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
505
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
506 class OldStyle:
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
507 pass
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
508 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
509 pass
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 45368
diff changeset
510 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
511 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
512 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
513 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
514 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
515 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
516 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
517
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
518 def test_bug1055820b(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
519 # Corresponds to temp2b.py in the bug report.
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 ouch = []
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
522 def callback(ignored):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
523 ouch[:] = [wr() for wr in WRs]
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
524
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
525 Cs = [C1055820(i) for i in range(2)]
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
526 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
527 c = None
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
528
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
529 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
530 self.assertEqual(len(ouch), 0)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
531 # 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
532 # 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
533 # cleared the instance's dict anyway.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
534 Cs = None
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
535 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
536 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
537 for x in ouch:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
538 # 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
539 # would be damaged, with an empty __dict__.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
540 self.assertEqual(x, None)
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
541
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
542 class GCTogglingTests(unittest.TestCase):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
543 def setUp(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
544 gc.enable()
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41400
diff changeset
545
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
546 def tearDown(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
547 gc.disable()
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41400
diff changeset
548
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
549 def test_bug1055820c(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
550 # 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
551 # elaborate.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
552
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
553 c0 = C1055820(0)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
554 # Move c0 into generation 2.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
555 gc.collect()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
556
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
557 c1 = C1055820(1)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
558 c1.keep_c0_alive = c0
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
559 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
560
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
561 c2 = C1055820(2)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
562 c2wr = weakref.ref(c2) # no callback!
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
563
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
564 ouch = []
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
565 def callback(ignored):
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
566 ouch[:] = [c2wr()]
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 # 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
569 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
570
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
571 c0 = 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: 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
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 a
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
576 # 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'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
578 # callback, and that callback references c2 via c2wr().
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
579 #
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
580 # 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
581 # ^
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
582 # |
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
583 # | Generation 2 above dots
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
584 #. . . . . . . .|. . . . . . . . . . . . . . . . . . . . . . . .
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
585 # | Generation 0 below dots
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 # |
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
588 # ^->c1 ^->c2 has a wr but no callback
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
589 # | | | |
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
590 # <--v <--v
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
591 #
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
592 # 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
593 # 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
594 # 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
595 # 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
596 # 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
597 # 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
598 # 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
599 # 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
600
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
601 # 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
602 # between generations.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
603 junk = []
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
604 i = 0
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
605 detector = GC_Detector()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
606 while not detector.gc_happened:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
607 i += 1
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
608 if i > 10000:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
609 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
610 self.assertEqual(len(ouch), 0)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
611 junk.append([]) # this will eventually trigger gc
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
612
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
613 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
614 for x in ouch:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
615 # 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
616 # with an empty __dict__.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
617 self.assertEqual(x, None)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
618
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
619 def test_bug1055820d(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
620 # 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
621 # 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
622 # 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
623
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
624 ouch = []
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
625 class D(C1055820):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
626 def __del__(self):
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
627 ouch[:] = [c2wr()]
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
628
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
629 d0 = D(0)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
630 # Move all the above into generation 2.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
631 gc.collect()
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
632
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
633 c1 = C1055820(1)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
634 c1.keep_d0_alive = d0
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
635 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
636
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
637 c2 = C1055820(2)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
638 c2wr = weakref.ref(c2) # no callback!
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
639
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
640 d0 = c1 = c2 = None
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 28532
diff changeset
641
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
642 # 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
643 # 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
644 # 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
645 # 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
646 # There are no other weakrefs.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
647 #
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
648 # d0 has a __del__ method that references c2wr
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
649 # ^
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
650 # |
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
651 # | Generation 2 above dots
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
652 #. . . . . . . .|. . . . . . . . . . . . . . . . . . . . . . . .
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
653 # | Generation 0 below dots
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
654 # |
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
655 # |
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
656 # ^->c1 ^->c2 has a wr but no callback
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
657 # | | | |
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
658 # <--v <--v
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
659 #
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
660 # 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
661 # 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
662 # 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
663 # 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
664 # 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
665 # 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
666 # 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
667
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
668 # 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
669 # between generations.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
670 detector = GC_Detector()
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
671 junk = []
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
672 i = 0
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
673 while not detector.gc_happened:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
674 i += 1
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
675 if i > 10000:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
676 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
677 self.assertEqual(len(ouch), 0)
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
678 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
679
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
680 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
681 for x in ouch:
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
682 # 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
683 # empty __dict__.
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
684 self.assertEqual(x, None)
13878
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13211
diff changeset
685
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
686 def test_main():
13878
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13211
diff changeset
687 enabled = gc.isenabled()
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13211
diff changeset
688 gc.disable()
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
689 assert not gc.isenabled()
14757
b4ea57870f2a - Add test for new SAVEALL debugging flag
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14610
diff changeset
690 debug = gc.get_debug()
b4ea57870f2a - Add test for new SAVEALL debugging flag
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14610
diff changeset
691 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
692
14757
b4ea57870f2a - Add test for new SAVEALL debugging flag
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14610
diff changeset
693 try:
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
694 gc.collect() # Delete 2nd generation garbage
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
695 run_unittest(GCTests, GCTogglingTests)
14757
b4ea57870f2a - Add test for new SAVEALL debugging flag
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14610
diff changeset
696 finally:
b4ea57870f2a - Add test for new SAVEALL debugging flag
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14610
diff changeset
697 gc.set_debug(debug)
b4ea57870f2a - Add test for new SAVEALL debugging flag
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14610
diff changeset
698 # 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
699 if verbose:
b4ea57870f2a - Add test for new SAVEALL debugging flag
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14610
diff changeset
700 print "restoring automatic collection"
b4ea57870f2a - Add test for new SAVEALL debugging flag
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14610
diff changeset
701 # 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
702 gc.enable()
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
703 assert gc.isenabled()
14757
b4ea57870f2a - Add test for new SAVEALL debugging flag
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14610
diff changeset
704 if not enabled:
b4ea57870f2a - Add test for new SAVEALL debugging flag
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14610
diff changeset
705 gc.disable()
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41400
diff changeset
706
41400
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
707 if __name__ == "__main__":
9408dcd3ce88 Convert test_gc to use unittest.
Collin Winter <collinw@gmail.com>
parents: 36863
diff changeset
708 test_main()