From 9f2f946829fc62088b20682fafb9ef76efbcd33d Mon Sep 17 00:00:00 2001
From: Serhiy Storchaka <storchaka@gmail.com>
Date: Fri, 9 Jun 2017 22:27:55 +0300
Subject: [PATCH 1/2] bpo-28994: Fixed errors handling in
 atexit._run_exitfuncs().

---
 Modules/atexitmodule.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Modules/atexitmodule.c b/Modules/atexitmodule.c
index 3cdf2d7e56348a..35ebf08ecd3c66 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);

From 7963df25f2ae2fd8cfa6f0198a63925384112385 Mon Sep 17 00:00:00 2001
From: Serhiy Storchaka <storchaka@gmail.com>
Date: Sun, 11 Jun 2017 14:47:53 +0300
Subject: [PATCH 2/2] Add test and Misc/NEWS entry.

---
 Lib/test/test_atexit.py | 10 ++++++++++
 Misc/NEWS               |  3 +++
 2 files changed, 13 insertions(+)

diff --git a/Lib/test/test_atexit.py b/Lib/test/test_atexit.py
index c761076c4a0225..1d0b018aafaf93 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 @@ def test_raise_unnormalized(self):
         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 b436524754388c..f0cc231955e8cb 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -355,6 +355,9 @@ Extension Modules
 Library
 -------
 
+- bpo-28994: The traceback no longer displayed for SystemExit raised in
+  a callback registered by atexit.
+
 - bpo-11822: The dis.dis() function now is able to disassemble nested
   code objects.