Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 26f18b8

Browse files
authored
bpo-43146: fix regression in traceback.print_exception(None) (GH-24463)
1 parent 7bb1caf commit 26f18b8

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

Lib/test/test_traceback.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,24 @@ def test_format_exception_only_exc(self):
232232
output = traceback.format_exception_only(Exception("projector"))
233233
self.assertEqual(output, ["Exception: projector\n"])
234234

235+
def test_exception_is_None(self):
236+
NONE_EXC_STRING = 'NoneType: None\n'
237+
excfile = StringIO()
238+
traceback.print_exception(None, None, None, file=excfile)
239+
self.assertEqual(excfile.getvalue(), NONE_EXC_STRING)
240+
241+
excfile = StringIO()
242+
traceback.print_exc(None, file=excfile)
243+
self.assertEqual(excfile.getvalue(), NONE_EXC_STRING)
244+
245+
self.assertEqual(traceback.format_exc(None), NONE_EXC_STRING)
246+
self.assertEqual(
247+
traceback.format_exception(None, None, None), [NONE_EXC_STRING])
248+
self.assertEqual(
249+
traceback.format_exception_only(None), [NONE_EXC_STRING])
250+
self.assertEqual(
251+
traceback.format_exception_only(None, None), [NONE_EXC_STRING])
252+
235253

236254
class TracebackFormatTests(unittest.TestCase):
237255

Lib/traceback.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,9 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
528528
cause = None
529529

530530
if compact:
531-
need_context = cause is None and not e.__suppress_context__
531+
need_context = (cause is None and
532+
e is not None and
533+
not e.__suppress_context__)
532534
else:
533535
need_context = True
534536
if (e and e.__context__ is not None
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix recent regression in None argument handling in :mod:`~traceback` module functions.

0 commit comments

Comments
 (0)