Skip to content

Commit da61baf

Browse files
miss-islingtonAniketsyterryjreedypicnixz
authored
[3.15] gh-152490: Fix TypeError when traceback.py formats SyntaxError(NotImplemented) (GH-152495) (#160009)
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 96005d3 commit da61baf

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
@@ -587,6 +587,13 @@ def test_format_exception_only_exc(self):
587587
output = traceback.format_exception_only(Exception("projector"))
588588
self.assertEqual(output, ["Exception: projector\n"])
589589

590+
@support.subTests("arg", [NotImplemented, "invalid syntax"])
591+
def test_format_syntax_error_message(self, arg):
592+
exc = SyntaxError(arg)
593+
lines = traceback.format_exception_only(type(exc), exc)
594+
result = "".join(lines)
595+
self.assertIn(str(arg), result)
596+
590597
def test_exception_is_None(self):
591598
NONE_EXC_STRING = 'NoneType: None\n'
592599
excfile = StringIO()

‎Lib/traceback.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1600,7 +1600,11 @@ def _format_syntax_error(self, stype, **kwargs):
16001600
)
16011601
else:
16021602
yield ' {}\n'.format(ltext)
1603-
msg = self.msg or "<no detail available>"
1603+
try:
1604+
if not (msg := self.msg):
1605+
msg = "<no detail available>"
1606+
except TypeError:
1607+
pass
16041608
yield "{}{}{}: {}{}{}{}\n".format(
16051609
theme.type,
16061610
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)