| 1 |
""" |
| 2 |
Tests run by test_atexit in a subprocess since it clears atexit callbacks. |
| 3 |
""" |
| 4 |
import atexit |
| 5 |
import sys |
| 6 |
import unittest |
| 7 |
from test import support |
| 8 |
|
| 9 |
|
| 10 |
class GeneralTest(unittest.TestCase): |
| 11 |
def setUp(self): |
| 12 |
atexit._clear() |
| 13 |
|
| 14 |
def tearDown(self): |
| 15 |
atexit._clear() |
| 16 |
|
| 17 |
def assert_raises_unraisable(self, exc_type, func, *args): |
| 18 |
with support.catch_unraisable_exception() as cm: |
| 19 |
atexit.register(func, *args) |
| 20 |
atexit._run_exitfuncs() |
| 21 |
|
| 22 |
self.assertIsNone(cm.unraisable.object) |
| 23 |
self.assertEqual(cm.unraisable.err_msg, |
| 24 |
f'Exception ignored in atexit callback {func!r}') |
| 25 |
self.assertEqual(cm.unraisable.exc_type, exc_type) |
| 26 |
self.assertEqual(type(cm.unraisable.exc_value), exc_type) |
| 27 |
|
| 28 |
def test_order(self): |
| 29 |
# Check that callbacks are called in reverse order with the expected |
| 30 |
# positional and keyword arguments. |
| 31 |
calls = [] |
| 32 |
|
| 33 |
def func1(*args, **kwargs): |
| 34 |
calls.append(('func1', args, kwargs)) |
| 35 |
|
| 36 |
def func2(*args, **kwargs): |
| 37 |
calls.append(('func2', args, kwargs)) |
| 38 |
|
| 39 |
# be sure args are handled properly |
| 40 |
atexit.register(func1, 1, 2) |
| 41 |
atexit.register(func2) |
| 42 |
atexit.register(func2, 3, key="value") |
| 43 |
atexit._run_exitfuncs() |
| 44 |
|
| 45 |
self.assertEqual(calls, |
| 46 |
[('func2', (3,), {'key': 'value'}), |
| 47 |
('func2', (), {}), |
| 48 |
('func1', (1, 2), {})]) |
| 49 |
|
| 50 |
def test_badargs(self): |
| 51 |
def func(): |
| 52 |
pass |
| 53 |
|
| 54 |
# func() has no parameter, but it's called with 2 parameters |
| 55 |
self.assert_raises_unraisable(TypeError, func, 1 ,2) |
| 56 |
|
| 57 |
def test_raise(self): |
| 58 |
def raise_type_error(): |
| 59 |
raise TypeError |
| 60 |
|
| 61 |
self.assert_raises_unraisable(TypeError, raise_type_error) |
| 62 |
|
| 63 |
def test_raise_unnormalized(self): |
| 64 |
# bpo-10756: Make sure that an unnormalized exception is handled |
| 65 |
# properly. |
| 66 |
def div_zero(): |
| 67 |
1 / 0 |
| 68 |
|
| 69 |
self.assert_raises_unraisable(ZeroDivisionError, div_zero) |
| 70 |
|
| 71 |
def test_exit(self): |
| 72 |
self.assert_raises_unraisable(SystemExit, sys.exit) |
| 73 |
|
| 74 |
def test_stress(self): |
| 75 |
a = [0] |
| 76 |
def inc(): |
| 77 |
a[0] += 1 |
| 78 |
|
| 79 |
for i in range(128): |
| 80 |
atexit.register(inc) |
| 81 |
atexit._run_exitfuncs() |
| 82 |
|
| 83 |
self.assertEqual(a[0], 128) |
| 84 |
|
| 85 |
def test_clear(self): |
| 86 |
a = [0] |
| 87 |
def inc(): |
| 88 |
a[0] += 1 |
| 89 |
|
| 90 |
atexit.register(inc) |
| 91 |
atexit._clear() |
| 92 |
atexit._run_exitfuncs() |
| 93 |
|
| 94 |
self.assertEqual(a[0], 0) |
| 95 |
|
| 96 |
def test_unregister(self): |
| 97 |
a = [0] |
| 98 |
def inc(): |
| 99 |
a[0] += 1 |
| 100 |
def dec(): |
| 101 |
a[0] -= 1 |
| 102 |
|
| 103 |
for i in range(4): |
| 104 |
atexit.register(inc) |
| 105 |
atexit.register(dec) |
| 106 |
atexit.unregister(inc) |
| 107 |
atexit._run_exitfuncs() |
| 108 |
|
| 109 |
self.assertEqual(a[0], -1) |
| 110 |
|
| 111 |
def test_bound_methods(self): |
| 112 |
l = [] |
| 113 |
atexit.register(l.append, 5) |
| 114 |
atexit._run_exitfuncs() |
| 115 |
self.assertEqual(l, [5]) |
| 116 |
|
| 117 |
atexit.unregister(l.append) |
| 118 |
atexit._run_exitfuncs() |
| 119 |
self.assertEqual(l, [5]) |
| 120 |
|
| 121 |
def test_atexit_with_unregistered_function(self): |
| 122 |
# See bpo-46025 for more info |
| 123 |
def func(): |
| 124 |
atexit.unregister(func) |
| 125 |
1/0 |
| 126 |
atexit.register(func) |
| 127 |
try: |
| 128 |
with support.catch_unraisable_exception() as cm: |
| 129 |
atexit._run_exitfuncs() |
| 130 |
self.assertIsNone(cm.unraisable.object) |
| 131 |
self.assertEqual(cm.unraisable.err_msg, |
| 132 |
f'Exception ignored in atexit callback {func!r}') |
| 133 |
self.assertEqual(cm.unraisable.exc_type, ZeroDivisionError) |
| 134 |
self.assertEqual(type(cm.unraisable.exc_value), ZeroDivisionError) |
| 135 |
finally: |
| 136 |
atexit.unregister(func) |
| 137 |
|
| 138 |
def test_eq_unregister_clear(self): |
| 139 |
# Issue #112127: callback's __eq__ may call unregister or _clear |
| 140 |
class Evil: |
| 141 |
def __eq__(self, other): |
| 142 |
action(other) |
| 143 |
return NotImplemented |
| 144 |
|
| 145 |
for action in atexit.unregister, lambda o: atexit._clear(): |
| 146 |
with self.subTest(action=action): |
| 147 |
atexit.register(lambda: None) |
| 148 |
atexit.unregister(Evil()) |
| 149 |
atexit._clear() |
| 150 |
|
| 151 |
def test_eq_unregister(self): |
| 152 |
# Issue #112127: callback's __eq__ may call unregister |
| 153 |
def f1(): |
| 154 |
log.append(1) |
| 155 |
def f2(): |
| 156 |
log.append(2) |
| 157 |
def f3(): |
| 158 |
log.append(3) |
| 159 |
|
| 160 |
class Pred: |
| 161 |
def __eq__(self, other): |
| 162 |
nonlocal cnt |
| 163 |
cnt += 1 |
| 164 |
if cnt == when: |
| 165 |
atexit.unregister(what) |
| 166 |
if other is f2: |
| 167 |
return True |
| 168 |
return False |
| 169 |
|
| 170 |
for what, expected in ( |
| 171 |
(f1, [3]), |
| 172 |
(f2, [3, 1]), |
| 173 |
(f3, [1]), |
| 174 |
): |
| 175 |
for when in range(1, 4): |
| 176 |
with self.subTest(what=what.__name__, when=when): |
| 177 |
cnt = 0 |
| 178 |
log = [] |
| 179 |
for f in (f1, f2, f3): |
| 180 |
atexit.register(f) |
| 181 |
atexit.unregister(Pred()) |
| 182 |
atexit._run_exitfuncs() |
| 183 |
self.assertEqual(log, expected) |
| 184 |
|
| 185 |
|
| 186 |
if __name__ == "__main__": |
| 187 |
unittest.main() |