Skip to content

Commit e75bc5c

Browse files
miss-islingtonAniketsyterryjreedypicnixz
authored
[3.14] gh-152490: Fix TypeError when traceback.py formats SyntaxError(NotImplemented) (GH-152495) (#160010)
gh-152490: Fix TypeError when traceback.py formats SyntaxError(NotImplemented) (GH-152495) Method traceback.TracebackException._format_syntax_error contains line `msg = self.msg or "<no detail available>"`. Since 3.14, bool(NotImplemented) raises TypeError and hence so does `self.msg or ...`. Catch it in a try statement. --------- (cherry picked from commit 5503a5e) Co-authored-by: Aniket <148300120+Aniketsy@users.noreply.github.com> Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent 172c998 commit e75bc5c

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

‎Lib/test/test_traceback.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,13 @@ def test_format_exception_only_exc(self):
564564
output = traceback.format_exception_only(Exception("projector"))
565565
self.assertEqual(output, ["Exception: projector\n"])
566566

567+
@support.subTests("arg", [NotImplemented, "invalid syntax"])
568+
def test_format_syntax_error_message(self, arg):
569+
exc = SyntaxError(arg)
570+
lines = traceback.format_exception_only(type(exc), exc)
571+
result = "".join(lines)
572+
self.assertIn(str(arg), result)
573+
567574
def test_exception_is_None(self):
568575
NONE_EXC_STRING = 'NoneType: None\n'
569576
excfile = StringIO()

‎Lib/traceback.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1453,7 +1453,11 @@ def _format_syntax_error(self, stype, **kwargs):
14531453
)
14541454
else:
14551455
yield ' {}\n'.format(ltext)
1456-
msg = self.msg or "<no detail available>"
1456+
try:
1457+
if not (msg := self.msg):
1458+
msg = "<no detail available>"
1459+
except TypeError:
1460+
pass
14571461
yield "{}{}{}: {}{}{}{}\n".format(
14581462
theme.type,
14591463
stype,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :exc:`TypeError` when formatting a :exc:`SyntaxError` raised with :data:`NotImplemented` as its argument, which caused unexpected REPL exit and IDLE Shell restart.

0 commit comments

Comments
 (0)