Test atexit shutdown mechanism in a subprocess (#4828)

* Test atexit shutdown mechanism in a subprocess
This commit is contained in:
Antoine Pitrou 2017-12-13 02:29:07 +01:00 committed by GitHub
parent 317def9fdb
commit fc5db95e00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 0 deletions

View File

@ -3,6 +3,7 @@ import unittest
import io import io
import atexit import atexit
from test import support from test import support
from test.support import script_helper
### helpers ### helpers
def h1(): def h1():
@ -152,6 +153,21 @@ class GeneralTest(unittest.TestCase):
atexit._run_exitfuncs() atexit._run_exitfuncs()
self.assertEqual(l, [5]) self.assertEqual(l, [5])
def test_shutdown(self):
# Actually test the shutdown mechanism in a subprocess
code = """if 1:
import atexit
def f(msg):
print(msg)
atexit.register(f, "one")
atexit.register(f, "two")
"""
res = script_helper.assert_python_ok("-c", code)
self.assertEqual(res.out.decode().splitlines(), ["two", "one"])
self.assertFalse(res.err)
@support.cpython_only @support.cpython_only
class SubinterpreterTest(unittest.TestCase): class SubinterpreterTest(unittest.TestCase):

View File

@ -2086,6 +2086,8 @@ _Py_FatalInitError(_PyInitError err)
/* For the atexit module. */ /* For the atexit module. */
void _Py_PyAtExit(void (*func)(void)) void _Py_PyAtExit(void (*func)(void))
{ {
/* Guard against API misuse (see bpo-17852) */
assert(_PyRuntime.pyexitfunc == NULL || _PyRuntime.pyexitfunc == func);
_PyRuntime.pyexitfunc = func; _PyRuntime.pyexitfunc = func;
} }