Tim Peters | c7b6bed | 2002-07-17 00:34:26 +0000 | [diff] [blame] | 1 | import sys |
Skip Montanaro | 599bd5e | 2004-11-04 04:31:30 +0000 | [diff] [blame] | 2 | import unittest |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 3 | import io |
Fred Drake | c19425d | 2000-06-28 15:07:31 +0000 | [diff] [blame] | 4 | import atexit |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 5 | import _testcapi |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 6 | from test import support |
Fred Drake | c19425d | 2000-06-28 15:07:31 +0000 | [diff] [blame] | 7 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 8 | ### helpers |
| 9 | def h1(): |
| 10 | print("h1") |
| 11 | |
| 12 | def h2(): |
| 13 | print("h2") |
| 14 | |
| 15 | def h3(): |
| 16 | print("h3") |
| 17 | |
| 18 | def h4(*args, **kwargs): |
| 19 | print("h4", args, kwargs) |
| 20 | |
| 21 | def raise1(): |
| 22 | raise TypeError |
| 23 | |
| 24 | def raise2(): |
| 25 | raise SystemError |
| 26 | |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 27 | |
| 28 | class GeneralTest(unittest.TestCase): |
| 29 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 30 | def setUp(self): |
Victor Stinner | 457ab06 | 2011-01-05 23:47:00 +0000 | [diff] [blame] | 31 | self.save_stdout = sys.stdout |
| 32 | self.save_stderr = sys.stderr |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 33 | self.stream = io.StringIO() |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 34 | sys.stdout = sys.stderr = self.stream |
| 35 | atexit._clear() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 36 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 37 | def tearDown(self): |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 38 | sys.stdout = self.save_stdout |
| 39 | sys.stderr = self.save_stderr |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 40 | atexit._clear() |
| 41 | |
Skip Montanaro | 599bd5e | 2004-11-04 04:31:30 +0000 | [diff] [blame] | 42 | def test_args(self): |
| 43 | # be sure args are handled properly |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 44 | 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 Drake | c19425d | 2000-06-28 15:07:31 +0000 | [diff] [blame] | 51 | |
Benjamin Peterson | b8401c7 | 2008-09-23 03:14:49 +0000 | [diff] [blame] | 52 | 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 Montanaro | 599bd5e | 2004-11-04 04:31:30 +0000 | [diff] [blame] | 56 | def test_order(self): |
| 57 | # be sure handlers are executed in reverse order |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 58 | atexit.register(h1) |
| 59 | atexit.register(h2) |
| 60 | atexit.register(h3) |
| 61 | atexit._run_exitfuncs() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 62 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 63 | self.assertEqual(self.stream.getvalue(), "h3\nh2\nh1\n") |
Fred Drake | c19425d | 2000-06-28 15:07:31 +0000 | [diff] [blame] | 64 | |
Skip Montanaro | 599bd5e | 2004-11-04 04:31:30 +0000 | [diff] [blame] | 65 | def test_raise(self): |
| 66 | # be sure raises are handled properly |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 67 | atexit.register(raise1) |
| 68 | atexit.register(raise2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 69 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 70 | self.assertRaises(TypeError, atexit._run_exitfuncs) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 71 | |
Victor Stinner | 358e11d | 2011-01-05 03:54:25 +0000 | [diff] [blame] | 72 | 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 Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 80 | def test_stress(self): |
| 81 | a = [0] |
| 82 | def inc(): |
| 83 | a[0] += 1 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 84 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 85 | for i in range(128): |
| 86 | atexit.register(inc) |
| 87 | atexit._run_exitfuncs() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 88 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 89 | self.assertEqual(a[0], 128) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 90 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 91 | def test_clear(self): |
| 92 | a = [0] |
| 93 | def inc(): |
| 94 | a[0] += 1 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 95 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 96 | atexit.register(inc) |
| 97 | atexit._clear() |
| 98 | atexit._run_exitfuncs() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 99 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 100 | self.assertEqual(a[0], 0) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 101 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 102 | def test_unregister(self): |
| 103 | a = [0] |
| 104 | def inc(): |
| 105 | a[0] += 1 |
| 106 | def dec(): |
| 107 | a[0] -= 1 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 108 | |
| 109 | for i in range(4): |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 110 | atexit.register(inc) |
| 111 | atexit.register(dec) |
| 112 | atexit.unregister(inc) |
| 113 | atexit._run_exitfuncs() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 114 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 115 | self.assertEqual(a[0], -1) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 116 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 117 | def test_bound_methods(self): |
| 118 | l = [] |
| 119 | atexit.register(l.append, 5) |
| 120 | atexit._run_exitfuncs() |
| 121 | self.assertEqual(l, [5]) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 122 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 123 | atexit.unregister(l.append) |
| 124 | atexit._run_exitfuncs() |
| 125 | self.assertEqual(l, [5]) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 126 | |
Tim Peters | 012b69c | 2002-07-16 19:30:59 +0000 | [diff] [blame] | 127 | |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 128 | class 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 Montanaro | 599bd5e | 2004-11-04 04:31:30 +0000 | [diff] [blame] | 162 | def test_main(): |
Antoine Pitrou | 2d350fd | 2013-08-01 20:56:12 +0200 | [diff] [blame] | 163 | support.run_unittest(__name__) |
| 164 | |
Tim Peters | 012b69c | 2002-07-16 19:30:59 +0000 | [diff] [blame] | 165 | |
Skip Montanaro | 599bd5e | 2004-11-04 04:31:30 +0000 | [diff] [blame] | 166 | if __name__ == "__main__": |
| 167 | test_main() |