Skip to content

Commit

Permalink
bpo-43146: fix None-handling in single-arg traceback.print_exception(…
Browse files Browse the repository at this point in the history
…None) (pythonGH-24629)

(The previous commit fixed print_exception(None, None, None).)
  • Loading branch information
iritkatriel authored Feb 23, 2021
1 parent 26f18b8 commit b798ab0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
Expand Down
5 changes: 4 additions & 1 deletion Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Handle None in single-arg versions of :func:`~traceback.print_exception` and :func:`~traceback.format_exception`.

0 comments on commit b798ab0

Please sign in to comment.