[Python-checkins] Test atexit shutdown mechanism in a subprocess (#4828)

Antoine Pitrou webhook-mailer at python.org
Tue Dec 12 20:29:13 EST 2017


https://github.com/python/cpython/commit/fc5db95e0063eafa2bfb7f487fcaad5a7c4b65a1
commit: fc5db95e0063eafa2bfb7f487fcaad5a7c4b65a1
branch: master
author: Antoine Pitrou <pitrou at free.fr>
committer: GitHub <noreply at github.com>
date: 2017-12-13T02:29:07+01:00
summary:

Test atexit shutdown mechanism in a subprocess (#4828)

* Test atexit shutdown mechanism in a subprocess

files:
M Lib/test/test_atexit.py
M Python/pylifecycle.c

diff --git a/Lib/test/test_atexit.py b/Lib/test/test_atexit.py
index 1d0b018aafa..aa56388ef60 100644
--- a/Lib/test/test_atexit.py
+++ b/Lib/test/test_atexit.py
@@ -3,6 +3,7 @@
 import io
 import atexit
 from test import support
+from test.support import script_helper
 
 ### helpers
 def h1():
@@ -152,6 +153,21 @@ def test_bound_methods(self):
         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):
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index fdb09d910da..f284855f342 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -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;
 }
 



More information about the Python-checkins mailing list