|
4 | 4 | import atexit |
5 | 5 | from test import test_support |
6 | 6 |
|
7 | | -class TestCase(unittest.TestCase): |
8 | | - def test_args(self): |
9 | | - # be sure args are handled properly |
10 | | - s = StringIO.StringIO() |
11 | | - sys.stdout = sys.stderr = s |
12 | | - save_handlers = atexit._exithandlers |
13 | | - atexit._exithandlers = [] |
14 | | - try: |
15 | | - atexit.register(self.h1) |
16 | | - atexit.register(self.h4) |
17 | | - atexit.register(self.h4, 4, kw="abc") |
18 | | - atexit._run_exitfuncs() |
19 | | - finally: |
20 | | - sys.stdout = sys.__stdout__ |
21 | | - sys.stderr = sys.__stderr__ |
22 | | - atexit._exithandlers = save_handlers |
23 | | - self.assertEqual(s.getvalue(), "h4 (4,) {'kw': 'abc'}\nh4 () {}\nh1\n") |
| 7 | +### helpers |
| 8 | +def h1(): |
| 9 | + print("h1") |
24 | 10 |
|
25 | | - def test_order(self): |
26 | | - # be sure handlers are executed in reverse order |
27 | | - s = StringIO.StringIO() |
28 | | - sys.stdout = sys.stderr = s |
29 | | - save_handlers = atexit._exithandlers |
30 | | - atexit._exithandlers = [] |
31 | | - try: |
32 | | - atexit.register(self.h1) |
33 | | - atexit.register(self.h2) |
34 | | - atexit.register(self.h3) |
35 | | - atexit._run_exitfuncs() |
36 | | - finally: |
37 | | - sys.stdout = sys.__stdout__ |
38 | | - sys.stderr = sys.__stderr__ |
39 | | - atexit._exithandlers = save_handlers |
40 | | - self.assertEqual(s.getvalue(), "h3\nh2\nh1\n") |
| 11 | +def h2(): |
| 12 | + print("h2") |
41 | 13 |
|
42 | | - def test_sys_override(self): |
43 | | - # be sure a preset sys.exitfunc is handled properly |
44 | | - s = StringIO.StringIO() |
45 | | - sys.stdout = sys.stderr = s |
46 | | - save_handlers = atexit._exithandlers |
47 | | - atexit._exithandlers = [] |
48 | | - exfunc = sys.exitfunc |
49 | | - sys.exitfunc = self.h1 |
50 | | - reload(atexit) |
51 | | - try: |
52 | | - atexit.register(self.h2) |
53 | | - atexit._run_exitfuncs() |
54 | | - finally: |
55 | | - sys.stdout = sys.__stdout__ |
56 | | - sys.stderr = sys.__stderr__ |
57 | | - atexit._exithandlers = save_handlers |
58 | | - sys.exitfunc = exfunc |
59 | | - self.assertEqual(s.getvalue(), "h2\nh1\n") |
| 14 | +def h3(): |
| 15 | + print("h3") |
60 | 16 |
|
61 | | - def test_raise(self): |
62 | | - # be sure raises are handled properly |
63 | | - s = StringIO.StringIO() |
64 | | - sys.stdout = sys.stderr = s |
65 | | - save_handlers = atexit._exithandlers |
66 | | - atexit._exithandlers = [] |
67 | | - try: |
68 | | - atexit.register(self.raise1) |
69 | | - atexit.register(self.raise2) |
70 | | - self.assertRaises(TypeError, atexit._run_exitfuncs) |
71 | | - finally: |
72 | | - sys.stdout = sys.__stdout__ |
73 | | - sys.stderr = sys.__stderr__ |
74 | | - atexit._exithandlers = save_handlers |
| 17 | +def h4(*args, **kwargs): |
| 18 | + print("h4", args, kwargs) |
| 19 | + |
| 20 | +def raise1(): |
| 21 | + raise TypeError |
75 | 22 |
|
76 | | - ### helpers |
77 | | - def h1(self): |
78 | | - print("h1") |
| 23 | +def raise2(): |
| 24 | + raise SystemError |
79 | 25 |
|
80 | | - def h2(self): |
81 | | - print("h2") |
| 26 | +class TestCase(unittest.TestCase): |
| 27 | + def setUp(self): |
| 28 | + self.stream = StringIO.StringIO() |
| 29 | + sys.stdout = sys.stderr = self.stream |
| 30 | + atexit._clear() |
| 31 | + |
| 32 | + def tearDown(self): |
| 33 | + sys.stdout = sys.__stdout__ |
| 34 | + sys.stderr = sys.__stderr__ |
| 35 | + atexit._clear() |
82 | 36 |
|
83 | | - def h3(self): |
84 | | - print("h3") |
| 37 | + def test_args(self): |
| 38 | + # be sure args are handled properly |
| 39 | + atexit.register(h1) |
| 40 | + atexit.register(h4) |
| 41 | + atexit.register(h4, 4, kw="abc") |
| 42 | + atexit._run_exitfuncs() |
85 | 43 |
|
86 | | - def h4(self, *args, **kwargs): |
87 | | - print("h4", args, kwargs) |
| 44 | + self.assertEqual(self.stream.getvalue(), |
| 45 | + "h4 (4,) {'kw': 'abc'}\nh4 () {}\nh1\n") |
88 | 46 |
|
89 | | - def raise1(self): |
90 | | - raise TypeError |
| 47 | + def test_order(self): |
| 48 | + # be sure handlers are executed in reverse order |
| 49 | + atexit.register(h1) |
| 50 | + atexit.register(h2) |
| 51 | + atexit.register(h3) |
| 52 | + atexit._run_exitfuncs() |
| 53 | + |
| 54 | + self.assertEqual(self.stream.getvalue(), "h3\nh2\nh1\n") |
91 | 55 |
|
92 | | - def raise2(self): |
93 | | - raise SystemError |
| 56 | + def test_raise(self): |
| 57 | + # be sure raises are handled properly |
| 58 | + atexit.register(raise1) |
| 59 | + atexit.register(raise2) |
| 60 | + |
| 61 | + self.assertRaises(TypeError, atexit._run_exitfuncs) |
| 62 | + |
| 63 | + def test_stress(self): |
| 64 | + a = [0] |
| 65 | + def inc(): |
| 66 | + a[0] += 1 |
| 67 | + |
| 68 | + for i in range(128): |
| 69 | + atexit.register(inc) |
| 70 | + atexit._run_exitfuncs() |
| 71 | + |
| 72 | + self.assertEqual(a[0], 128) |
| 73 | + |
| 74 | + def test_clear(self): |
| 75 | + a = [0] |
| 76 | + def inc(): |
| 77 | + a[0] += 1 |
| 78 | + |
| 79 | + atexit.register(inc) |
| 80 | + atexit._clear() |
| 81 | + atexit._run_exitfuncs() |
| 82 | + |
| 83 | + self.assertEqual(a[0], 0) |
| 84 | + |
| 85 | + def test_unregister(self): |
| 86 | + a = [0] |
| 87 | + def inc(): |
| 88 | + a[0] += 1 |
| 89 | + def dec(): |
| 90 | + a[0] -= 1 |
| 91 | + |
| 92 | + for i in range(4): |
| 93 | + atexit.register(inc) |
| 94 | + atexit.register(dec) |
| 95 | + atexit.unregister(inc) |
| 96 | + atexit._run_exitfuncs() |
| 97 | + |
| 98 | + self.assertEqual(a[0], -1) |
| 99 | + |
| 100 | + def test_bound_methods(self): |
| 101 | + l = [] |
| 102 | + atexit.register(l.append, 5) |
| 103 | + atexit._run_exitfuncs() |
| 104 | + self.assertEqual(l, [5]) |
| 105 | + |
| 106 | + atexit.unregister(l.append) |
| 107 | + atexit._run_exitfuncs() |
| 108 | + self.assertEqual(l, [5]) |
| 109 | + |
94 | 110 |
|
95 | 111 | def test_main(): |
96 | 112 | test_support.run_unittest(TestCase) |
97 | 113 |
|
98 | | - |
99 | 114 | if __name__ == "__main__": |
100 | 115 | test_main() |
0 commit comments