Skip to content

Commit b798ab0

Browse files
authored
bpo-43146: fix None-handling in single-arg traceback.print_exception(None) (pythonGH-24629)
(The previous commit fixed print_exception(None, None, None).)
1 parent 26f18b8 commit b798ab0

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

Lib/test/test_traceback.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ def test_format_exception_only_exc(self):
234234

235235
def test_exception_is_None(self):
236236
NONE_EXC_STRING = 'NoneType: None\n'
237+
excfile = StringIO()
238+
traceback.print_exception(None, file=excfile)
239+
self.assertEqual(excfile.getvalue(), NONE_EXC_STRING)
240+
237241
excfile = StringIO()
238242
traceback.print_exception(None, None, None, file=excfile)
239243
self.assertEqual(excfile.getvalue(), NONE_EXC_STRING)
@@ -243,6 +247,7 @@ def test_exception_is_None(self):
243247
self.assertEqual(excfile.getvalue(), NONE_EXC_STRING)
244248

245249
self.assertEqual(traceback.format_exc(None), NONE_EXC_STRING)
250+
self.assertEqual(traceback.format_exception(None), [NONE_EXC_STRING])
246251
self.assertEqual(
247252
traceback.format_exception(None, None, None), [NONE_EXC_STRING])
248253
self.assertEqual(

Lib/traceback.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ def _parse_value_tb(exc, value, tb):
9191
if (value is _sentinel) != (tb is _sentinel):
9292
raise ValueError("Both or neither of value and tb must be given")
9393
if value is tb is _sentinel:
94-
return exc, exc.__traceback__
94+
if exc is not None:
95+
return exc, exc.__traceback__
96+
else:
97+
return None, None
9598
return value, tb
9699

97100

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Handle None in single-arg versions of :func:`~traceback.print_exception` and :func:`~traceback.format_exception`.

0 commit comments

Comments
 (0)