Skip to content

Commit 5503a5e

Browse files
Aniketsyterryjreedypicnixz
authored
gh-152490: Fix TypeError when traceback.py formats SyntaxError(NotImplemented) (#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. --------- Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent 1277a5e commit 5503a5e

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
@@ -1611,7 +1611,11 @@ def _format_syntax_error(self, stype, **kwargs):
16111611
)
16121612
else:
16131613
yield ' {}\n'.format(ltext)
1614-
msg = self.msg or "<no detail available>"
1614+
try:
1615+
if not (msg := self.msg):
1616+
msg = "<no detail available>"
1617+
except TypeError:
1618+
pass
16151619
yield "{}{}{}: {}{}{}{}\n".format(
16161620
theme.type,
16171621
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)