]> granicus.if.org Git - python/commitdiff
[3.6] bpo-28994: Fixed errors handling in atexit._run_exitfuncs(). (GH-2034) (#2121)
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 12 Jun 2017 06:02:13 +0000 (09:02 +0300)
committerGitHub <noreply@github.com>
Mon, 12 Jun 2017 06:02:13 +0000 (09:02 +0300)
The traceback no longer displayed for SystemExit raised in a callback registered by atexit..
(cherry picked from commit 3fd54d4a7e604067e2bc0f8cfd58bdbdc09fa7f4)

Lib/test/test_atexit.py
Misc/NEWS
Modules/atexitmodule.c

index c761076c4a0225af8c69afede54096a001ddb384..1d0b018aafaf934349eafe57d652b5345e545cf9 100644 (file)
@@ -23,6 +23,9 @@ def raise1():
 def raise2():
     raise SystemError
 
+def exit():
+    raise SystemExit
+
 
 class GeneralTest(unittest.TestCase):
 
@@ -76,6 +79,13 @@ class GeneralTest(unittest.TestCase):
         self.assertRaises(ZeroDivisionError, atexit._run_exitfuncs)
         self.assertIn("ZeroDivisionError", self.stream.getvalue())
 
+    def test_exit(self):
+        # be sure a SystemExit is handled properly
+        atexit.register(exit)
+
+        self.assertRaises(SystemExit, atexit._run_exitfuncs)
+        self.assertEqual(self.stream.getvalue(), '')
+
     def test_print_tracebacks(self):
         # Issue #18776: the tracebacks should be printed when errors occur.
         def f():
index be2ec4f9547b2c0c9fe6932ad6781cbf26105391..e56c5a300feb3dfed230c27b5d262e5af65146b0 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -49,6 +49,9 @@ Core and Builtins
 Library
 -------
 
+- bpo-28994: The traceback no longer displayed for SystemExit raised in
+  a callback registered by atexit.
+
 - bpo-30508: Don't log exceptions if Task/Future "cancel()" method was
   called.
 
index 3cdf2d7e56348a75d99748272dc8bf7bcf59ded6..35ebf08ecd3c665b3f59ac752bee2781548b75b7 100644 (file)
@@ -97,7 +97,7 @@ atexit_callfuncs(void)
                 Py_XDECREF(exc_tb);
             }
             PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
-            if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
+            if (!PyErr_GivenExceptionMatches(exc_type, PyExc_SystemExit)) {
                 PySys_WriteStderr("Error in atexit._run_exitfuncs:\n");
                 PyErr_NormalizeException(&exc_type, &exc_value, &exc_tb);
                 PyErr_Display(exc_type, exc_value, exc_tb);