Skip to content

Commit fc5db95

Browse files
authored
Test atexit shutdown mechanism in a subprocess (#4828)
* Test atexit shutdown mechanism in a subprocess
1 parent 317def9 commit fc5db95

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

Lib/test/test_atexit.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io
44
import atexit
55
from test import support
6+
from test.support import script_helper
67

78
### helpers
89
def h1():
@@ -152,6 +153,21 @@ def test_bound_methods(self):
152153
atexit._run_exitfuncs()
153154
self.assertEqual(l, [5])
154155

156+
def test_shutdown(self):
157+
# Actually test the shutdown mechanism in a subprocess
158+
code = """if 1:
159+
import atexit
160+
161+
def f(msg):
162+
print(msg)
163+
164+
atexit.register(f, "one")
165+
atexit.register(f, "two")
166+
"""
167+
res = script_helper.assert_python_ok("-c", code)
168+
self.assertEqual(res.out.decode().splitlines(), ["two", "one"])
169+
self.assertFalse(res.err)
170+
155171

156172
@support.cpython_only
157173
class SubinterpreterTest(unittest.TestCase):

Python/pylifecycle.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,6 +2086,8 @@ _Py_FatalInitError(_PyInitError err)
20862086
/* For the atexit module. */
20872087
void _Py_PyAtExit(void (*func)(void))
20882088
{
2089+
/* Guard against API misuse (see bpo-17852) */
2090+
assert(_PyRuntime.pyexitfunc == NULL || _PyRuntime.pyexitfunc == func);
20892091
_PyRuntime.pyexitfunc = func;
20902092
}
20912093

0 commit comments

Comments
 (0)