diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index e38d0942e463e9..e72c2e53c5aba0 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -587,6 +587,18 @@ def test_format_exception_only_exc(self): output = traceback.format_exception_only(Exception("projector")) self.assertEqual(output, ["Exception: projector\n"]) + def test_format_syntax_error_msg(self): + exc = SyntaxError(NotImplemented) + lines = traceback.format_exception_only(type(exc), exc) + result = "".join(lines) + self.assertIn("NotImplemented", result) + + exc = SyntaxError("invalid syntax") + lines = traceback.format_exception_only(type(exc), exc) + result = "".join(lines) + self.assertIn("SyntaxError", result) + self.assertIn("invalid syntax", 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..e9f284660a6139 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -1611,7 +1611,10 @@ def _format_syntax_error(self, stype, **kwargs): ) else: yield ' {}\n'.format(ltext) - msg = self.msg or "" + if self.msg is None or self.msg == '': + msg = "" + else: + msg = str(self.msg) 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..ddf205cb11a9b2 --- /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.