From: Serhiy Storchaka Date: Mon, 12 Jun 2017 06:02:33 +0000 (+0300) Subject: [3.5] bpo-28994: Fixed errors handling in atexit._run_exitfuncs(). (GH-2034) (#2122) X-Git-Tag: v3.5.4rc1~84 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7d8c1ebd86ce27b28736c5e97fef58ec60b8ef31;p=python [3.5] bpo-28994: Fixed errors handling in atexit._run_exitfuncs(). (GH-2034) (#2122) The traceback no longer displayed for SystemExit raised in a callback registered by atexit. (cherry picked from commit 3fd54d4a7e604067e2bc0f8cfd58bdbdc09fa7f4) --- diff --git a/Lib/test/test_atexit.py b/Lib/test/test_atexit.py index c761076c4a..1d0b018aaf 100644 --- a/Lib/test/test_atexit.py +++ b/Lib/test/test_atexit.py @@ -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(): diff --git a/Misc/NEWS b/Misc/NEWS index 255f1467a8..c4c21e5662 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -56,6 +56,9 @@ Extension Modules 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. diff --git a/Modules/atexitmodule.c b/Modules/atexitmodule.c index 3cdf2d7e56..35ebf08ecd 100644 --- a/Modules/atexitmodule.c +++ b/Modules/atexitmodule.c @@ -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);