]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43146: fix regression in traceback.print_exception(None) (GH-24463)
authorIrit Katriel <iritkatriel@yahoo.com>
Tue, 23 Feb 2021 14:58:47 +0000 (14:58 +0000)
committerGitHub <noreply@github.com>
Tue, 23 Feb 2021 14:58:47 +0000 (06:58 -0800)
Lib/test/test_traceback.py
Lib/traceback.py
Misc/NEWS.d/next/Library/2021-02-06-21-21-35.bpo-43146.MHtb2v.rst [new file with mode: 0644]

index 33bdda026662cc03edbe330c68c8f1753d6c59db..2261ea994209ff4644eb88102b076b2cea8491f5 100644 (file)
@@ -232,6 +232,24 @@ class TracebackCases(unittest.TestCase):
         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):
 
index 090465a3584b77d27b59d9c1fffe31f98ff803ea..dfb296c5e7b177d6f17769bbd20a43d40d41558b 100644 (file)
@@ -528,7 +528,9 @@ class TracebackException:
                     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 (file)
index 0000000..8d213a4
--- /dev/null
@@ -0,0 +1 @@
+Fix recent regression in None argument handling in :mod:`~traceback` module functions.
\ No newline at end of file