From 4663913ee0e05e0fb8e9459aa66fe2a8fe3122de Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Sun, 28 Jun 2026 14:32:24 +0000 Subject: [PATCH 1/7] Fix TypeError when formatting SyntaxError with msg=NotImplemented in traceback.py --- Lib/test/test_traceback.py | 5 +++++ Lib/traceback.py | 5 ++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index e38d0942e463e9c..26eada434204af7 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -625,6 +625,11 @@ def test_signatures(self): str(inspect.signature(traceback.format_exception_only)), '(exc, /, value=, *, show_group=False, **kwargs)') + def test_syntax_error_with_not_implemented_msg(self): + exc = SyntaxError(NotImplemented) + lines = traceback.format_exception_only(type(exc), exc) + self.assertIsInstance("".join(lines), str) + class PurePythonExceptionFormattingMixin: def get_exception(self, callable, slice_start=0, slice_end=-1): diff --git a/Lib/traceback.py b/Lib/traceback.py index dcdab1f12e9a168..b9585db021b1fa1 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: + msg = "" + else: + msg = str(self.msg) yield "{}{}{}: {}{}{}{}\n".format( theme.type, stype, From 03a2e53b021db9d785dd3e851aa866a825517fc7 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 28 Jun 2026 14:37:03 +0000 Subject: [PATCH 2/7] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2026-06-28-14-37-01.gh-issue-152490.A8qLbF.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-06-28-14-37-01.gh-issue-152490.A8qLbF.rst 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 000000000000000..5252f0b12fe03b9 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-06-28-14-37-01.gh-issue-152490.A8qLbF.rst @@ -0,0 +1,2 @@ +Fix :exc:`TypeError` when formatting a :exc:`SyntaxError` raised with :data:`NotImplemented` as its argument, which caused IDLE Shell to restart +unexpectedly. From 83fc00eb9481d2602ac2c0c8e99e616f3420592b Mon Sep 17 00:00:00 2001 From: Aniket <148300120+Aniketsy@users.noreply.github.com> Date: Sun, 28 Jun 2026 20:10:15 +0530 Subject: [PATCH 3/7] fix format --- .../Library/2026-06-28-14-37-01.gh-issue-152490.A8qLbF.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 index 5252f0b12fe03b9..2d99b0799b40245 100644 --- 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 @@ -1,2 +1 @@ -Fix :exc:`TypeError` when formatting a :exc:`SyntaxError` raised with :data:`NotImplemented` as its argument, which caused IDLE Shell to restart -unexpectedly. +Fix :exc:`TypeError` when formatting a :exc:`SyntaxError` raised with :data:`NotImplemented` as its argument, which caused IDLE Shell to restart unexpectedly. From e6c02184d740b29de4a57d21d3a1f782665fda41 Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Sun, 28 Jun 2026 18:39:27 +0000 Subject: [PATCH 4/7] rename and update test --- Lib/test/test_traceback.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 26eada434204af7..04b865e14d5d4e6 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -583,10 +583,20 @@ def test_format_exception_exc(self): with self.assertRaisesRegex(TypeError, 'required positional argument'): traceback.format_exception(exc=e) - def test_format_exception_only_exc(self): + def test_format_syntax_error(self): output = traceback.format_exception_only(Exception("projector")) self.assertEqual(output, ["Exception: projector\n"]) + exc = SyntaxError(NotImplemented) + lines = traceback.format_exception_only(type(exc), exc) + self.assertIsInstance("".join(lines), str) + + 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() @@ -625,11 +635,6 @@ def test_signatures(self): str(inspect.signature(traceback.format_exception_only)), '(exc, /, value=, *, show_group=False, **kwargs)') - def test_syntax_error_with_not_implemented_msg(self): - exc = SyntaxError(NotImplemented) - lines = traceback.format_exception_only(type(exc), exc) - self.assertIsInstance("".join(lines), str) - class PurePythonExceptionFormattingMixin: def get_exception(self, callable, slice_start=0, slice_end=-1): From e68c5216389c81d66337d72f768d6c050400ddde Mon Sep 17 00:00:00 2001 From: Aniket <148300120+Aniketsy@users.noreply.github.com> Date: Mon, 29 Jun 2026 06:37:23 +0530 Subject: [PATCH 5/7] Update Misc/NEWS.d/next/Library/2026-06-28-14-37-01.gh-issue-152490.A8qLbF.rst Co-authored-by: Terry Jan Reedy --- .../next/Library/2026-06-28-14-37-01.gh-issue-152490.A8qLbF.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 2d99b0799b40245..ddf205cb11a9b29 100644 --- 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 @@ -1 +1 @@ -Fix :exc:`TypeError` when formatting a :exc:`SyntaxError` raised with :data:`NotImplemented` as its argument, which caused IDLE Shell to restart unexpectedly. +Fix :exc:`TypeError` when formatting a :exc:`SyntaxError` raised with :data:`NotImplemented` as its argument, which caused unexpected REPL exit and IDLE Shell restart. From 16bfcd2392407de19c491916c23d3be9af299e0a Mon Sep 17 00:00:00 2001 From: Aniket <148300120+Aniketsy@users.noreply.github.com> Date: Mon, 29 Jun 2026 06:57:14 +0530 Subject: [PATCH 6/7] Update Lib/traceback.py Co-authored-by: Terry Jan Reedy --- Lib/traceback.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/traceback.py b/Lib/traceback.py index b9585db021b1fa1..e9f284660a61395 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -1611,7 +1611,7 @@ def _format_syntax_error(self, stype, **kwargs): ) else: yield ' {}\n'.format(ltext) - if self.msg is None: + if self.msg is None or self.msg == '': msg = "" else: msg = str(self.msg) From 0c5b7114db7ab6e4b4b54c0f8019f6f8e3b45d15 Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Mon, 29 Jun 2026 12:54:17 +0000 Subject: [PATCH 7/7] update tests --- Lib/test/test_traceback.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 04b865e14d5d4e6..e72c2e53c5aba07 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -583,13 +583,15 @@ def test_format_exception_exc(self): with self.assertRaisesRegex(TypeError, 'required positional argument'): traceback.format_exception(exc=e) - def test_format_syntax_error(self): + 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) - self.assertIsInstance("".join(lines), str) + result = "".join(lines) + self.assertIn("NotImplemented", result) exc = SyntaxError("invalid syntax") lines = traceback.format_exception_only(type(exc), exc)