]> granicus.if.org Git - python/commitdiff
Test atexit shutdown mechanism in a subprocess (#4828)
authorAntoine Pitrou <pitrou@free.fr>
Wed, 13 Dec 2017 01:29:07 +0000 (02:29 +0100)
committerGitHub <noreply@github.com>
Wed, 13 Dec 2017 01:29:07 +0000 (02:29 +0100)
* Test atexit shutdown mechanism in a subprocess

Lib/test/test_atexit.py
Python/pylifecycle.c

index 1d0b018aafaf934349eafe57d652b5345e545cf9..aa56388ef608817ca6b283bf32ca912a5f645f36 100644 (file)
@@ -3,6 +3,7 @@ import unittest
 import io
 import atexit
 from test import support
+from test.support import script_helper
 
 ### helpers
 def h1():
@@ -152,6 +153,21 @@ class GeneralTest(unittest.TestCase):
         atexit._run_exitfuncs()
         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
 class SubinterpreterTest(unittest.TestCase):
index fdb09d910dab9765304b1731ca0f46325ec61100..f284855f342f69ba059e6cfe1c03122702e16add 100644 (file)
@@ -2086,6 +2086,8 @@ _Py_FatalInitError(_PyInitError err)
 /* For the atexit module. */
 void _Py_PyAtExit(void (*func)(void))
 {
+    /* Guard against API misuse (see bpo-17852) */
+    assert(_PyRuntime.pyexitfunc == NULL || _PyRuntime.pyexitfunc == func);
     _PyRuntime.pyexitfunc = func;
 }