blob: 3e252367eb28359cd041ea3f4cd365676b12b710 [file] [log] [blame]
Tim Petersc7b6bed2002-07-17 00:34:26 +00001import sys
Skip Montanaro599bd5e2004-11-04 04:31:30 +00002import unittest
Guido van Rossum34d19282007-08-09 01:03:29 +00003import io
Fred Drakec19425d2000-06-28 15:07:31 +00004import atexit
Antoine Pitrou2d350fd2013-08-01 20:56:12 +02005import _testcapi
Benjamin Petersonee8712c2008-05-20 21:35:26 +00006from test import support
Fred Drakec19425d2000-06-28 15:07:31 +00007
Collin Winter670e6922007-03-21 02:57:17 +00008### helpers
9def h1():
10 print("h1")
11
12def h2():
13 print("h2")
14
15def h3():
16 print("h3")
17
18def h4(*args, **kwargs):
19 print("h4", args, kwargs)
20
21def raise1():
22 raise TypeError
23
24def raise2():
25 raise SystemError
26
Antoine Pitrou2d350fd2013-08-01 20:56:12 +020027
28class GeneralTest(unittest.TestCase):
29
Collin Winter670e6922007-03-21 02:57:17 +000030 def setUp(self):
Victor Stinner457ab062011-01-05 23:47:00 +000031 self.save_stdout = sys.stdout
32 self.save_stderr = sys.stderr
Guido van Rossum34d19282007-08-09 01:03:29 +000033 self.stream = io.StringIO()
Collin Winter670e6922007-03-21 02:57:17 +000034 sys.stdout = sys.stderr = self.stream
35 atexit._clear()
Guido van Rossumd8faa362007-04-27 19:54:29 +000036
Collin Winter670e6922007-03-21 02:57:17 +000037 def tearDown(self):
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000038 sys.stdout = self.save_stdout
39 sys.stderr = self.save_stderr
Collin Winter670e6922007-03-21 02:57:17 +000040 atexit._clear()
41
Skip Montanaro599bd5e2004-11-04 04:31:30 +000042 def test_args(self):
43 # be sure args are handled properly
Collin Winter670e6922007-03-21 02:57:17 +000044 atexit.register(h1)
45 atexit.register(h4)
46 atexit.register(h4, 4, kw="abc")
47 atexit._run_exitfuncs()
48
49 self.assertEqual(self.stream.getvalue(),
50 "h4 (4,) {'kw': 'abc'}\nh4 () {}\nh1\n")
Fred Drakec19425d2000-06-28 15:07:31 +000051
Benjamin Petersonb8401c72008-09-23 03:14:49 +000052 def test_badargs(self):
53 atexit.register(lambda: 1, 0, 0, (x for x in (1,2)), 0, 0)
54 self.assertRaises(TypeError, atexit._run_exitfuncs)
55
Skip Montanaro599bd5e2004-11-04 04:31:30 +000056 def test_order(self):
57 # be sure handlers are executed in reverse order
Collin Winter670e6922007-03-21 02:57:17 +000058 atexit.register(h1)
59 atexit.register(h2)
60 atexit.register(h3)
61 atexit._run_exitfuncs()
Guido van Rossumd8faa362007-04-27 19:54:29 +000062
Collin Winter670e6922007-03-21 02:57:17 +000063 self.assertEqual(self.stream.getvalue(), "h3\nh2\nh1\n")
Fred Drakec19425d2000-06-28 15:07:31 +000064
Skip Montanaro599bd5e2004-11-04 04:31:30 +000065 def test_raise(self):
66 # be sure raises are handled properly
Collin Winter670e6922007-03-21 02:57:17 +000067 atexit.register(raise1)
68 atexit.register(raise2)
Guido van Rossumd8faa362007-04-27 19:54:29 +000069
Collin Winter670e6922007-03-21 02:57:17 +000070 self.assertRaises(TypeError, atexit._run_exitfuncs)
Guido van Rossumd8faa362007-04-27 19:54:29 +000071
Victor Stinner358e11d2011-01-05 03:54:25 +000072 def test_raise_unnormalized(self):
73 # Issue #10756: Make sure that an unnormalized exception is
74 # handled properly
75 atexit.register(lambda: 1 / 0)
76
77 self.assertRaises(ZeroDivisionError, atexit._run_exitfuncs)
78 self.assertIn("ZeroDivisionError", self.stream.getvalue())
79
Collin Winter670e6922007-03-21 02:57:17 +000080 def test_stress(self):
81 a = [0]
82 def inc():
83 a[0] += 1
Guido van Rossumd8faa362007-04-27 19:54:29 +000084
Collin Winter670e6922007-03-21 02:57:17 +000085 for i in range(128):
86 atexit.register(inc)
87 atexit._run_exitfuncs()
Guido van Rossumd8faa362007-04-27 19:54:29 +000088
Collin Winter670e6922007-03-21 02:57:17 +000089 self.assertEqual(a[0], 128)
Guido van Rossumd8faa362007-04-27 19:54:29 +000090
Collin Winter670e6922007-03-21 02:57:17 +000091 def test_clear(self):
92 a = [0]
93 def inc():
94 a[0] += 1
Guido van Rossumd8faa362007-04-27 19:54:29 +000095
Collin Winter670e6922007-03-21 02:57:17 +000096 atexit.register(inc)
97 atexit._clear()
98 atexit._run_exitfuncs()
Guido van Rossumd8faa362007-04-27 19:54:29 +000099
Collin Winter670e6922007-03-21 02:57:17 +0000100 self.assertEqual(a[0], 0)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000101
Collin Winter670e6922007-03-21 02:57:17 +0000102 def test_unregister(self):
103 a = [0]
104 def inc():
105 a[0] += 1
106 def dec():
107 a[0] -= 1
Guido van Rossumd8faa362007-04-27 19:54:29 +0000108
109 for i in range(4):
Collin Winter670e6922007-03-21 02:57:17 +0000110 atexit.register(inc)
111 atexit.register(dec)
112 atexit.unregister(inc)
113 atexit._run_exitfuncs()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000114
Collin Winter670e6922007-03-21 02:57:17 +0000115 self.assertEqual(a[0], -1)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000116
Collin Winter670e6922007-03-21 02:57:17 +0000117 def test_bound_methods(self):
118 l = []
119 atexit.register(l.append, 5)
120 atexit._run_exitfuncs()
121 self.assertEqual(l, [5])
Guido van Rossumd8faa362007-04-27 19:54:29 +0000122
Collin Winter670e6922007-03-21 02:57:17 +0000123 atexit.unregister(l.append)
124 atexit._run_exitfuncs()
125 self.assertEqual(l, [5])
Guido van Rossumd8faa362007-04-27 19:54:29 +0000126
Tim Peters012b69c2002-07-16 19:30:59 +0000127
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200128class SubinterpreterTest(unittest.TestCase):
129
130 def test_callbacks_leak(self):
131 # This test shows a leak in refleak mode if atexit doesn't
132 # take care to free callbacks in its per-subinterpreter module
133 # state.
134 n = atexit._ncallbacks()
135 code = r"""if 1:
136 import atexit
137 def f():
138 pass
139 atexit.register(f)
140 del atexit
141 """
142 ret = _testcapi.run_in_subinterp(code)
143 self.assertEqual(ret, 0)
144 self.assertEqual(atexit._ncallbacks(), n)
145
146 def test_callbacks_leak_refcycle(self):
147 # Similar to the above, but with a refcycle through the atexit
148 # module.
149 n = atexit._ncallbacks()
150 code = r"""if 1:
151 import atexit
152 def f():
153 pass
154 atexit.register(f)
155 atexit.__atexit = atexit
156 """
157 ret = _testcapi.run_in_subinterp(code)
158 self.assertEqual(ret, 0)
159 self.assertEqual(atexit._ncallbacks(), n)
160
161
Skip Montanaro599bd5e2004-11-04 04:31:30 +0000162def test_main():
Antoine Pitrou2d350fd2013-08-01 20:56:12 +0200163 support.run_unittest(__name__)
164
Tim Peters012b69c2002-07-16 19:30:59 +0000165
Skip Montanaro599bd5e2004-11-04 04:31:30 +0000166if __name__ == "__main__":
167 test_main()