From fe4b1b4dc1a3e267733cf1523ba41852c1577f49 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Tue, 23 Feb 2021 17:13:56 +0000 Subject: [PATCH 1/2] bpo-43146: fix None-handling in single-arg version of traceback.print_exception() and traceback.format_exception() --- Lib/test/test_traceback.py | 5 +++++ Lib/traceback.py | 5 ++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 2261ea994209ff..5bd969d62493a4 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 dfb296c5e7b177..8f908dd2e09444 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 From 1095d2f539ff50acc7d42ec70e13f257c3cd3026 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 23 Feb 2021 17:20:17 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2021-02-23-17-20-16.bpo-43146.JAFplg.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2021-02-23-17-20-16.bpo-43146.JAFplg.rst 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 00000000000000..151edbe28d246f --- /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