]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43146: fix None-handling in single-arg traceback.print_exception(None) (GH-24629)
authorIrit Katriel <iritkatriel@yahoo.com>
Tue, 23 Feb 2021 17:43:04 +0000 (17:43 +0000)
committerGitHub <noreply@github.com>
Tue, 23 Feb 2021 17:43:04 +0000 (09:43 -0800)
(The previous commit fixed print_exception(None, None, None).)

Lib/test/test_traceback.py
Lib/traceback.py
Misc/NEWS.d/next/Library/2021-02-23-17-20-16.bpo-43146.JAFplg.rst [new file with mode: 0644]

index 2261ea994209ff4644eb88102b076b2cea8491f5..5bd969d62493a452f5777f53452598547deba91e 100644 (file)
@@ -234,6 +234,10 @@ class TracebackCases(unittest.TestCase):
 
     def test_exception_is_None(self):
         NONE_EXC_STRING = 'NoneType: None\n'
+        excfile = StringIO()
+        traceback.print_exception(None, file=excfile)
+        self.assertEqual(excfile.getvalue(), NONE_EXC_STRING)
+
         excfile = StringIO()
         traceback.print_exception(None, None, None, file=excfile)
         self.assertEqual(excfile.getvalue(), NONE_EXC_STRING)
@@ -243,6 +247,7 @@ class TracebackCases(unittest.TestCase):
         self.assertEqual(excfile.getvalue(), NONE_EXC_STRING)
 
         self.assertEqual(traceback.format_exc(None), NONE_EXC_STRING)
+        self.assertEqual(traceback.format_exception(None), [NONE_EXC_STRING])
         self.assertEqual(
             traceback.format_exception(None, None, None), [NONE_EXC_STRING])
         self.assertEqual(
index dfb296c5e7b177d6f17769bbd20a43d40d41558b..8f908dd2e09444a3ff2cd49fc875d2d82c835b18 100644 (file)
@@ -91,7 +91,10 @@ def _parse_value_tb(exc, value, tb):
     if (value is _sentinel) != (tb is _sentinel):
         raise ValueError("Both or neither of value and tb must be given")
     if value is tb is _sentinel:
-        return exc, exc.__traceback__
+        if exc is not None:
+            return exc, exc.__traceback__
+        else:
+            return None, None
     return value, tb
 
 
diff --git a/Misc/NEWS.d/next/Library/2021-02-23-17-20-16.bpo-43146.JAFplg.rst b/Misc/NEWS.d/next/Library/2021-02-23-17-20-16.bpo-43146.JAFplg.rst
new file mode 100644 (file)
index 0000000..151edbe
--- /dev/null
@@ -0,0 +1 @@
+Handle None in single-arg versions of :func:`~traceback.print_exception` and :func:`~traceback.format_exception`.
\ No newline at end of file