diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index d5b00a3bb628cb..d1127c77ab8a81 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -587,6 +587,13 @@ def test_format_exception_only_exc(self): output = traceback.format_exception_only(Exception("projector")) self.assertEqual(output, ["Exception: projector\n"]) + @support.subTests("arg", [NotImplemented, "invalid syntax"]) + def test_format_syntax_error_message(self, arg): + exc = SyntaxError(arg) + lines = traceback.format_exception_only(type(exc), exc) + result = "".join(lines) + self.assertIn(str(arg), result) + def test_exception_is_None(self): NONE_EXC_STRING = 'NoneType: None\n' excfile = StringIO() diff --git a/Lib/traceback.py b/Lib/traceback.py index dcdab1f12e9a16..b81b326ab1b23b 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -1611,7 +1611,11 @@ def _format_syntax_error(self, stype, **kwargs): ) else: yield ' {}\n'.format(ltext) - msg = self.msg or "" + try: + if not (msg := self.msg): + msg = "" + except TypeError: + pass yield "{}{}{}: {}{}{}{}\n".format( theme.type, stype, diff --git a/Misc/NEWS.d/next/Library/2026-06-28-14-37-01.gh-issue-152490.A8qLbF.rst b/Misc/NEWS.d/next/Library/2026-06-28-14-37-01.gh-issue-152490.A8qLbF.rst new file mode 100644 index 00000000000000..70429574990997 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-06-28-14-37-01.gh-issue-152490.A8qLbF.rst @@ -0,0 +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.