1 | n/a | import types |
---|
2 | n/a | import unittest |
---|
3 | n/a | import weakref |
---|
4 | n/a | |
---|
5 | n/a | from test import support |
---|
6 | n/a | |
---|
7 | n/a | |
---|
8 | n/a | class ClearTest(unittest.TestCase): |
---|
9 | n/a | """ |
---|
10 | n/a | Tests for frame.clear(). |
---|
11 | n/a | """ |
---|
12 | n/a | |
---|
13 | n/a | def inner(self, x=5, **kwargs): |
---|
14 | n/a | 1/0 |
---|
15 | n/a | |
---|
16 | n/a | def outer(self, **kwargs): |
---|
17 | n/a | try: |
---|
18 | n/a | self.inner(**kwargs) |
---|
19 | n/a | except ZeroDivisionError as e: |
---|
20 | n/a | exc = e |
---|
21 | n/a | return exc |
---|
22 | n/a | |
---|
23 | n/a | def clear_traceback_frames(self, tb): |
---|
24 | n/a | """ |
---|
25 | n/a | Clear all frames in a traceback. |
---|
26 | n/a | """ |
---|
27 | n/a | while tb is not None: |
---|
28 | n/a | tb.tb_frame.clear() |
---|
29 | n/a | tb = tb.tb_next |
---|
30 | n/a | |
---|
31 | n/a | def test_clear_locals(self): |
---|
32 | n/a | class C: |
---|
33 | n/a | pass |
---|
34 | n/a | c = C() |
---|
35 | n/a | wr = weakref.ref(c) |
---|
36 | n/a | exc = self.outer(c=c) |
---|
37 | n/a | del c |
---|
38 | n/a | support.gc_collect() |
---|
39 | n/a | # A reference to c is held through the frames |
---|
40 | n/a | self.assertIsNot(None, wr()) |
---|
41 | n/a | self.clear_traceback_frames(exc.__traceback__) |
---|
42 | n/a | support.gc_collect() |
---|
43 | n/a | # The reference was released by .clear() |
---|
44 | n/a | self.assertIs(None, wr()) |
---|
45 | n/a | |
---|
46 | n/a | def test_clear_generator(self): |
---|
47 | n/a | endly = False |
---|
48 | n/a | def g(): |
---|
49 | n/a | nonlocal endly |
---|
50 | n/a | try: |
---|
51 | n/a | yield |
---|
52 | n/a | inner() |
---|
53 | n/a | finally: |
---|
54 | n/a | endly = True |
---|
55 | n/a | gen = g() |
---|
56 | n/a | next(gen) |
---|
57 | n/a | self.assertFalse(endly) |
---|
58 | n/a | # Clearing the frame closes the generator |
---|
59 | n/a | gen.gi_frame.clear() |
---|
60 | n/a | self.assertTrue(endly) |
---|
61 | n/a | |
---|
62 | n/a | def test_clear_executing(self): |
---|
63 | n/a | # Attempting to clear an executing frame is forbidden. |
---|
64 | n/a | try: |
---|
65 | n/a | 1/0 |
---|
66 | n/a | except ZeroDivisionError as e: |
---|
67 | n/a | f = e.__traceback__.tb_frame |
---|
68 | n/a | with self.assertRaises(RuntimeError): |
---|
69 | n/a | f.clear() |
---|
70 | n/a | with self.assertRaises(RuntimeError): |
---|
71 | n/a | f.f_back.clear() |
---|
72 | n/a | |
---|
73 | n/a | def test_clear_executing_generator(self): |
---|
74 | n/a | # Attempting to clear an executing generator frame is forbidden. |
---|
75 | n/a | endly = False |
---|
76 | n/a | def g(): |
---|
77 | n/a | nonlocal endly |
---|
78 | n/a | try: |
---|
79 | n/a | 1/0 |
---|
80 | n/a | except ZeroDivisionError as e: |
---|
81 | n/a | f = e.__traceback__.tb_frame |
---|
82 | n/a | with self.assertRaises(RuntimeError): |
---|
83 | n/a | f.clear() |
---|
84 | n/a | with self.assertRaises(RuntimeError): |
---|
85 | n/a | f.f_back.clear() |
---|
86 | n/a | yield f |
---|
87 | n/a | finally: |
---|
88 | n/a | endly = True |
---|
89 | n/a | gen = g() |
---|
90 | n/a | f = next(gen) |
---|
91 | n/a | self.assertFalse(endly) |
---|
92 | n/a | # Clearing the frame closes the generator |
---|
93 | n/a | f.clear() |
---|
94 | n/a | self.assertTrue(endly) |
---|
95 | n/a | |
---|
96 | n/a | @support.cpython_only |
---|
97 | n/a | def test_clear_refcycles(self): |
---|
98 | n/a | # .clear() doesn't leave any refcycle behind |
---|
99 | n/a | with support.disable_gc(): |
---|
100 | n/a | class C: |
---|
101 | n/a | pass |
---|
102 | n/a | c = C() |
---|
103 | n/a | wr = weakref.ref(c) |
---|
104 | n/a | exc = self.outer(c=c) |
---|
105 | n/a | del c |
---|
106 | n/a | self.assertIsNot(None, wr()) |
---|
107 | n/a | self.clear_traceback_frames(exc.__traceback__) |
---|
108 | n/a | self.assertIs(None, wr()) |
---|
109 | n/a | |
---|
110 | n/a | |
---|
111 | n/a | class FrameLocalsTest(unittest.TestCase): |
---|
112 | n/a | """ |
---|
113 | n/a | Tests for the .f_locals attribute. |
---|
114 | n/a | """ |
---|
115 | n/a | |
---|
116 | n/a | def make_frames(self): |
---|
117 | n/a | def outer(): |
---|
118 | n/a | x = 5 |
---|
119 | n/a | y = 6 |
---|
120 | n/a | def inner(): |
---|
121 | n/a | z = x + 2 |
---|
122 | n/a | 1/0 |
---|
123 | n/a | t = 9 |
---|
124 | n/a | return inner() |
---|
125 | n/a | try: |
---|
126 | n/a | outer() |
---|
127 | n/a | except ZeroDivisionError as e: |
---|
128 | n/a | tb = e.__traceback__ |
---|
129 | n/a | frames = [] |
---|
130 | n/a | while tb: |
---|
131 | n/a | frames.append(tb.tb_frame) |
---|
132 | n/a | tb = tb.tb_next |
---|
133 | n/a | return frames |
---|
134 | n/a | |
---|
135 | n/a | def test_locals(self): |
---|
136 | n/a | f, outer, inner = self.make_frames() |
---|
137 | n/a | outer_locals = outer.f_locals |
---|
138 | n/a | self.assertIsInstance(outer_locals.pop('inner'), types.FunctionType) |
---|
139 | n/a | self.assertEqual(outer_locals, {'x': 5, 'y': 6}) |
---|
140 | n/a | inner_locals = inner.f_locals |
---|
141 | n/a | self.assertEqual(inner_locals, {'x': 5, 'z': 7}) |
---|
142 | n/a | |
---|
143 | n/a | def test_clear_locals(self): |
---|
144 | n/a | # Test f_locals after clear() (issue #21897) |
---|
145 | n/a | f, outer, inner = self.make_frames() |
---|
146 | n/a | outer.clear() |
---|
147 | n/a | inner.clear() |
---|
148 | n/a | self.assertEqual(outer.f_locals, {}) |
---|
149 | n/a | self.assertEqual(inner.f_locals, {}) |
---|
150 | n/a | |
---|
151 | n/a | def test_locals_clear_locals(self): |
---|
152 | n/a | # Test f_locals before and after clear() (to exercise caching) |
---|
153 | n/a | f, outer, inner = self.make_frames() |
---|
154 | n/a | outer.f_locals |
---|
155 | n/a | inner.f_locals |
---|
156 | n/a | outer.clear() |
---|
157 | n/a | inner.clear() |
---|
158 | n/a | self.assertEqual(outer.f_locals, {}) |
---|
159 | n/a | self.assertEqual(inner.f_locals, {}) |
---|
160 | n/a | |
---|
161 | n/a | |
---|
162 | n/a | if __name__ == "__main__": |
---|
163 | n/a | unittest.main() |
---|