[Python-checkins] bpo-43146: fix regression in traceback.print_exception(None) (GH-24463)

gvanrossum webhook-mailer at python.org
Tue Feb 23 09:59:13 EST 2021


https://github.com/python/cpython/commit/26f18b8540b49d592af66361f8df1a03953d1768
commit: 26f18b8540b49d592af66361f8df1a03953d1768
branch: master
author: Irit Katriel <iritkatriel at yahoo.com>
committer: gvanrossum <gvanrossum at gmail.com>
date: 2021-02-23T06:58:47-08:00
summary:

bpo-43146: fix regression in traceback.print_exception(None) (GH-24463)

files:
A Misc/NEWS.d/next/Library/2021-02-06-21-21-35.bpo-43146.MHtb2v.rst
M Lib/test/test_traceback.py
M Lib/traceback.py

diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py
index 33bdda026662c..2261ea994209f 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -232,6 +232,24 @@ def test_format_exception_only_exc(self):
         output = traceback.format_exception_only(Exception("projector"))
         self.assertEqual(output, ["Exception: projector\n"])
 
+    def test_exception_is_None(self):
+        NONE_EXC_STRING = 'NoneType: None\n'
+        excfile = StringIO()
+        traceback.print_exception(None, None, None, file=excfile)
+        self.assertEqual(excfile.getvalue(), NONE_EXC_STRING)
+
+        excfile = StringIO()
+        traceback.print_exc(None, file=excfile)
+        self.assertEqual(excfile.getvalue(), NONE_EXC_STRING)
+
+        self.assertEqual(traceback.format_exc(None), NONE_EXC_STRING)
+        self.assertEqual(
+            traceback.format_exception(None, None, None), [NONE_EXC_STRING])
+        self.assertEqual(
+            traceback.format_exception_only(None), [NONE_EXC_STRING])
+        self.assertEqual(
+            traceback.format_exception_only(None, None), [NONE_EXC_STRING])
+
 
 class TracebackFormatTests(unittest.TestCase):
 
diff --git a/Lib/traceback.py b/Lib/traceback.py
index 090465a3584b7..dfb296c5e7b17 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -528,7 +528,9 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
                     cause = None
 
                 if compact:
-                    need_context = cause is None and not e.__suppress_context__
+                    need_context = (cause is None and
+                                    e is not None and
+                                    not e.__suppress_context__)
                 else:
                     need_context = True
                 if (e and e.__context__ is not None
diff --git a/Misc/NEWS.d/next/Library/2021-02-06-21-21-35.bpo-43146.MHtb2v.rst b/Misc/NEWS.d/next/Library/2021-02-06-21-21-35.bpo-43146.MHtb2v.rst
new file mode 100644
index 0000000000000..8d213a4138ef0
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-02-06-21-21-35.bpo-43146.MHtb2v.rst
@@ -0,0 +1 @@
+Fix recent regression in None argument handling in :mod:`~traceback` module functions.
\ No newline at end of file



More information about the Python-checkins mailing list