ยปCore Development>Code coverage>Lib/test/test_frame.py

Python code coverage for Lib/test/test_frame.py

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