[Python-checkins] bpo-43146: fix None-handling in single-arg traceback.print_exception(None) (GH-24629)

gvanrossum webhook-mailer at python.org
Tue Feb 23 12:43:28 EST 2021


https://github.com/python/cpython/commit/b798ab06937f8bb24b444a49dd42e11fff15e654
commit: b798ab06937f8bb24b444a49dd42e11fff15e654
branch: master
author: Irit Katriel <iritkatriel at yahoo.com>
committer: gvanrossum <gvanrossum at gmail.com>
date: 2021-02-23T09:43:04-08:00
summary:

bpo-43146: fix None-handling in single-arg traceback.print_exception(None) (GH-24629)

(The previous commit fixed print_exception(None, None, None).)

files:
A Misc/NEWS.d/next/Library/2021-02-23-17-20-16.bpo-43146.JAFplg.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 2261ea994209f..5bd969d62493a 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -234,6 +234,10 @@ def test_format_exception_only_exc(self):
 
     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 @@ def test_exception_is_None(self):
         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(
diff --git a/Lib/traceback.py b/Lib/traceback.py
index dfb296c5e7b17..8f908dd2e0944 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -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
index 0000000000000..151edbe28d246
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-02-23-17-20-16.bpo-43146.JAFplg.rst
@@ -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



More information about the Python-checkins mailing list