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 |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 5 | from test import support |
Fred Drake | c19425d | 2000-06-28 15:07:31 +0000 | [diff] [blame] | 6 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 7 | ### helpers |
| 8 | def h1(): |
| 9 | print("h1") |
| 10 | |
| 11 | def h2(): |
| 12 | print("h2") |
| 13 | |
| 14 | def h3(): |
| 15 | print("h3") |
| 16 | |
| 17 | def h4(*args, **kwargs): |
| 18 | print("h4", args, kwargs) |
| 19 | |
| 20 | def raise1(): |
| 21 | raise TypeError |
| 22 | |
| 23 | def raise2(): |
| 24 | raise SystemError |
| 25 | |
Skip Montanaro | 599bd5e | 2004-11-04 04:31:30 +0000 | [diff] [blame] | 26 | class TestCase(unittest.TestCase): |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 27 | def setUp(self): |
Victor Stinner | 457ab06 | 2011-01-05 23:47:00 +0000 | [diff] [blame] | 28 | self.save_stdout = sys.stdout |
| 29 | self.save_stderr = sys.stderr |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 30 | self.stream = io.StringIO() |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 31 | sys.stdout = sys.stderr = self.stream |
| 32 | atexit._clear() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 33 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 34 | def tearDown(self): |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 35 | sys.stdout = self.save_stdout |
| 36 | sys.stderr = self.save_stderr |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 37 | atexit._clear() |
| 38 | |
Skip Montanaro | 599bd5e | 2004-11-04 04:31:30 +0000 | [diff] [blame] | 39 | def test_args(self): |
| 40 | # be sure args are handled properly |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 41 | atexit.register(h1) |
| 42 | atexit.register(h4) |
| 43 | atexit.register(h4, 4, kw="abc") |
| 44 | atexit._run_exitfuncs() |
| 45 | |
| 46 | self.assertEqual(self.stream.getvalue(), |
| 47 | "h4 (4,) {'kw': 'abc'}\nh4 () {}\nh1\n") |
Fred Drake | c19425d | 2000-06-28 15:07:31 +0000 | [diff] [blame] | 48 | |
Benjamin Peterson | b8401c7 | 2008-09-23 03:14:49 +0000 | [diff] [blame] | 49 | def test_badargs(self): |
| 50 | atexit.register(lambda: 1, 0, 0, (x for x in (1,2)), 0, 0) |
| 51 | self.assertRaises(TypeError, atexit._run_exitfuncs) |
| 52 | |
Skip Montanaro | 599bd5e | 2004-11-04 04:31:30 +0000 | [diff] [blame] | 53 | def test_order(self): |
| 54 | # be sure handlers are executed in reverse order |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 55 | atexit.register(h1) |
| 56 | atexit.register(h2) |
| 57 | atexit.register(h3) |
| 58 | atexit._run_exitfuncs() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 59 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 60 | self.assertEqual(self.stream.getvalue(), "h3\nh2\nh1\n") |
Fred Drake | c19425d | 2000-06-28 15:07:31 +0000 | [diff] [blame] | 61 | |
Skip Montanaro | 599bd5e | 2004-11-04 04:31:30 +0000 | [diff] [blame] | 62 | def test_raise(self): |
| 63 | # be sure raises are handled properly |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 64 | atexit.register(raise1) |
| 65 | atexit.register(raise2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 66 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 67 | self.assertRaises(TypeError, atexit._run_exitfuncs) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 68 | |
Victor Stinner | 358e11d | 2011-01-05 03:54:25 +0000 | [diff] [blame] | 69 | def test_raise_unnormalized(self): |
| 70 | # Issue #10756: Make sure that an unnormalized exception is |
| 71 | # handled properly |
| 72 | atexit.register(lambda: 1 / 0) |
| 73 | |
| 74 | self.assertRaises(ZeroDivisionError, atexit._run_exitfuncs) |
| 75 | self.assertIn("ZeroDivisionError", self.stream.getvalue()) |
| 76 | |
Antoine Pitrou | 24201d4 | 2013-10-13 21:53:13 +0200 | [diff] [blame] | 77 | def test_print_tracebacks(self): |
| 78 | # Issue #18776: the tracebacks should be printed when errors occur. |
| 79 | def f(): |
| 80 | 1/0 # one |
| 81 | def g(): |
| 82 | 1/0 # two |
| 83 | def h(): |
| 84 | 1/0 # three |
| 85 | atexit.register(f) |
| 86 | atexit.register(g) |
| 87 | atexit.register(h) |
| 88 | |
| 89 | self.assertRaises(ZeroDivisionError, atexit._run_exitfuncs) |
| 90 | stderr = self.stream.getvalue() |
| 91 | self.assertEqual(stderr.count("ZeroDivisionError"), 3) |
| 92 | self.assertIn("# one", stderr) |
| 93 | self.assertIn("# two", stderr) |
| 94 | self.assertIn("# three", stderr) |
| 95 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 96 | def test_stress(self): |
| 97 | a = [0] |
| 98 | def inc(): |
| 99 | a[0] += 1 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 100 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 101 | for i in range(128): |
| 102 | atexit.register(inc) |
| 103 | atexit._run_exitfuncs() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 104 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 105 | self.assertEqual(a[0], 128) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 106 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 107 | def test_clear(self): |
| 108 | a = [0] |
| 109 | def inc(): |
| 110 | a[0] += 1 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 111 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 112 | atexit.register(inc) |
| 113 | atexit._clear() |
| 114 | atexit._run_exitfuncs() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 115 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 116 | self.assertEqual(a[0], 0) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 117 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 118 | def test_unregister(self): |
| 119 | a = [0] |
| 120 | def inc(): |
| 121 | a[0] += 1 |
| 122 | def dec(): |
| 123 | a[0] -= 1 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 124 | |
| 125 | for i in range(4): |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 126 | atexit.register(inc) |
| 127 | atexit.register(dec) |
| 128 | atexit.unregister(inc) |
| 129 | atexit._run_exitfuncs() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 130 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 131 | self.assertEqual(a[0], -1) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 132 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 133 | def test_bound_methods(self): |
| 134 | l = [] |
| 135 | atexit.register(l.append, 5) |
| 136 | atexit._run_exitfuncs() |
| 137 | self.assertEqual(l, [5]) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 138 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 139 | atexit.unregister(l.append) |
| 140 | atexit._run_exitfuncs() |
| 141 | self.assertEqual(l, [5]) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 142 | |
Tim Peters | 012b69c | 2002-07-16 19:30:59 +0000 | [diff] [blame] | 143 | |
Skip Montanaro | 599bd5e | 2004-11-04 04:31:30 +0000 | [diff] [blame] | 144 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 145 | support.run_unittest(TestCase) |
Tim Peters | 012b69c | 2002-07-16 19:30:59 +0000 | [diff] [blame] | 146 | |
Skip Montanaro | 599bd5e | 2004-11-04 04:31:30 +0000 | [diff] [blame] | 147 | if __name__ == "__main__": |
| 148 | test_main() |