1 | n/a | import sys |
---|
2 | n/a | import unittest |
---|
3 | n/a | import io |
---|
4 | n/a | import atexit |
---|
5 | n/a | from test import support |
---|
6 | n/a | |
---|
7 | n/a | ### helpers |
---|
8 | n/a | def h1(): |
---|
9 | n/a | print("h1") |
---|
10 | n/a | |
---|
11 | n/a | def h2(): |
---|
12 | n/a | print("h2") |
---|
13 | n/a | |
---|
14 | n/a | def h3(): |
---|
15 | n/a | print("h3") |
---|
16 | n/a | |
---|
17 | n/a | def h4(*args, **kwargs): |
---|
18 | n/a | print("h4", args, kwargs) |
---|
19 | n/a | |
---|
20 | n/a | def raise1(): |
---|
21 | n/a | raise TypeError |
---|
22 | n/a | |
---|
23 | n/a | def raise2(): |
---|
24 | n/a | raise SystemError |
---|
25 | n/a | |
---|
26 | n/a | |
---|
27 | n/a | class GeneralTest(unittest.TestCase): |
---|
28 | n/a | |
---|
29 | n/a | def setUp(self): |
---|
30 | n/a | self.save_stdout = sys.stdout |
---|
31 | n/a | self.save_stderr = sys.stderr |
---|
32 | n/a | self.stream = io.StringIO() |
---|
33 | n/a | sys.stdout = sys.stderr = self.stream |
---|
34 | n/a | atexit._clear() |
---|
35 | n/a | |
---|
36 | n/a | def tearDown(self): |
---|
37 | n/a | sys.stdout = self.save_stdout |
---|
38 | n/a | sys.stderr = self.save_stderr |
---|
39 | n/a | atexit._clear() |
---|
40 | n/a | |
---|
41 | n/a | def test_args(self): |
---|
42 | n/a | # be sure args are handled properly |
---|
43 | n/a | atexit.register(h1) |
---|
44 | n/a | atexit.register(h4) |
---|
45 | n/a | atexit.register(h4, 4, kw="abc") |
---|
46 | n/a | atexit._run_exitfuncs() |
---|
47 | n/a | |
---|
48 | n/a | self.assertEqual(self.stream.getvalue(), |
---|
49 | n/a | "h4 (4,) {'kw': 'abc'}\nh4 () {}\nh1\n") |
---|
50 | n/a | |
---|
51 | n/a | def test_badargs(self): |
---|
52 | n/a | atexit.register(lambda: 1, 0, 0, (x for x in (1,2)), 0, 0) |
---|
53 | n/a | self.assertRaises(TypeError, atexit._run_exitfuncs) |
---|
54 | n/a | |
---|
55 | n/a | def test_order(self): |
---|
56 | n/a | # be sure handlers are executed in reverse order |
---|
57 | n/a | atexit.register(h1) |
---|
58 | n/a | atexit.register(h2) |
---|
59 | n/a | atexit.register(h3) |
---|
60 | n/a | atexit._run_exitfuncs() |
---|
61 | n/a | |
---|
62 | n/a | self.assertEqual(self.stream.getvalue(), "h3\nh2\nh1\n") |
---|
63 | n/a | |
---|
64 | n/a | def test_raise(self): |
---|
65 | n/a | # be sure raises are handled properly |
---|
66 | n/a | atexit.register(raise1) |
---|
67 | n/a | atexit.register(raise2) |
---|
68 | n/a | |
---|
69 | n/a | self.assertRaises(TypeError, atexit._run_exitfuncs) |
---|
70 | n/a | |
---|
71 | n/a | def test_raise_unnormalized(self): |
---|
72 | n/a | # Issue #10756: Make sure that an unnormalized exception is |
---|
73 | n/a | # handled properly |
---|
74 | n/a | atexit.register(lambda: 1 / 0) |
---|
75 | n/a | |
---|
76 | n/a | self.assertRaises(ZeroDivisionError, atexit._run_exitfuncs) |
---|
77 | n/a | self.assertIn("ZeroDivisionError", self.stream.getvalue()) |
---|
78 | n/a | |
---|
79 | n/a | def test_print_tracebacks(self): |
---|
80 | n/a | # Issue #18776: the tracebacks should be printed when errors occur. |
---|
81 | n/a | def f(): |
---|
82 | n/a | 1/0 # one |
---|
83 | n/a | def g(): |
---|
84 | n/a | 1/0 # two |
---|
85 | n/a | def h(): |
---|
86 | n/a | 1/0 # three |
---|
87 | n/a | atexit.register(f) |
---|
88 | n/a | atexit.register(g) |
---|
89 | n/a | atexit.register(h) |
---|
90 | n/a | |
---|
91 | n/a | self.assertRaises(ZeroDivisionError, atexit._run_exitfuncs) |
---|
92 | n/a | stderr = self.stream.getvalue() |
---|
93 | n/a | self.assertEqual(stderr.count("ZeroDivisionError"), 3) |
---|
94 | n/a | self.assertIn("# one", stderr) |
---|
95 | n/a | self.assertIn("# two", stderr) |
---|
96 | n/a | self.assertIn("# three", stderr) |
---|
97 | n/a | |
---|
98 | n/a | def test_stress(self): |
---|
99 | n/a | a = [0] |
---|
100 | n/a | def inc(): |
---|
101 | n/a | a[0] += 1 |
---|
102 | n/a | |
---|
103 | n/a | for i in range(128): |
---|
104 | n/a | atexit.register(inc) |
---|
105 | n/a | atexit._run_exitfuncs() |
---|
106 | n/a | |
---|
107 | n/a | self.assertEqual(a[0], 128) |
---|
108 | n/a | |
---|
109 | n/a | def test_clear(self): |
---|
110 | n/a | a = [0] |
---|
111 | n/a | def inc(): |
---|
112 | n/a | a[0] += 1 |
---|
113 | n/a | |
---|
114 | n/a | atexit.register(inc) |
---|
115 | n/a | atexit._clear() |
---|
116 | n/a | atexit._run_exitfuncs() |
---|
117 | n/a | |
---|
118 | n/a | self.assertEqual(a[0], 0) |
---|
119 | n/a | |
---|
120 | n/a | def test_unregister(self): |
---|
121 | n/a | a = [0] |
---|
122 | n/a | def inc(): |
---|
123 | n/a | a[0] += 1 |
---|
124 | n/a | def dec(): |
---|
125 | n/a | a[0] -= 1 |
---|
126 | n/a | |
---|
127 | n/a | for i in range(4): |
---|
128 | n/a | atexit.register(inc) |
---|
129 | n/a | atexit.register(dec) |
---|
130 | n/a | atexit.unregister(inc) |
---|
131 | n/a | atexit._run_exitfuncs() |
---|
132 | n/a | |
---|
133 | n/a | self.assertEqual(a[0], -1) |
---|
134 | n/a | |
---|
135 | n/a | def test_bound_methods(self): |
---|
136 | n/a | l = [] |
---|
137 | n/a | atexit.register(l.append, 5) |
---|
138 | n/a | atexit._run_exitfuncs() |
---|
139 | n/a | self.assertEqual(l, [5]) |
---|
140 | n/a | |
---|
141 | n/a | atexit.unregister(l.append) |
---|
142 | n/a | atexit._run_exitfuncs() |
---|
143 | n/a | self.assertEqual(l, [5]) |
---|
144 | n/a | |
---|
145 | n/a | |
---|
146 | n/a | class SubinterpreterTest(unittest.TestCase): |
---|
147 | n/a | |
---|
148 | n/a | def test_callbacks_leak(self): |
---|
149 | n/a | # This test shows a leak in refleak mode if atexit doesn't |
---|
150 | n/a | # take care to free callbacks in its per-subinterpreter module |
---|
151 | n/a | # state. |
---|
152 | n/a | n = atexit._ncallbacks() |
---|
153 | n/a | code = r"""if 1: |
---|
154 | n/a | import atexit |
---|
155 | n/a | def f(): |
---|
156 | n/a | pass |
---|
157 | n/a | atexit.register(f) |
---|
158 | n/a | del atexit |
---|
159 | n/a | """ |
---|
160 | n/a | ret = support.run_in_subinterp(code) |
---|
161 | n/a | self.assertEqual(ret, 0) |
---|
162 | n/a | self.assertEqual(atexit._ncallbacks(), n) |
---|
163 | n/a | |
---|
164 | n/a | def test_callbacks_leak_refcycle(self): |
---|
165 | n/a | # Similar to the above, but with a refcycle through the atexit |
---|
166 | n/a | # module. |
---|
167 | n/a | n = atexit._ncallbacks() |
---|
168 | n/a | code = r"""if 1: |
---|
169 | n/a | import atexit |
---|
170 | n/a | def f(): |
---|
171 | n/a | pass |
---|
172 | n/a | atexit.register(f) |
---|
173 | n/a | atexit.__atexit = atexit |
---|
174 | n/a | """ |
---|
175 | n/a | ret = support.run_in_subinterp(code) |
---|
176 | n/a | self.assertEqual(ret, 0) |
---|
177 | n/a | self.assertEqual(atexit._ncallbacks(), n) |
---|
178 | n/a | |
---|
179 | n/a | |
---|
180 | n/a | if __name__ == "__main__": |
---|
181 | n/a | unittest.main() |
---|