https://github.com/python/cpython/commit/e75bc5ca2a5cb770ecc8a0fccc52962d5b44ad90
commit: e75bc5ca2a5cb770ecc8a0fccc52962d5b44ad90
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: terryjreedy <[email protected]>
date: 2026-10-21T00:51:03-04:00
summary:

[3.14] gh-152490: Fix TypeError when traceback.py formats 
SyntaxError(NotImplemented) (GH-152495) (#160010)

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 5503a5e0997740573b68d924535aa87ee4d36d79)

Co-authored-by: Aniket <[email protected]>
Co-authored-by: Terry Jan Reedy <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-06-28-14-37-01.gh-issue-152490.A8qLbF.rst
M Lib/test/test_traceback.py
M Lib/traceback.py

diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py
index e4203c8637585a..bc93006271bf87 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -564,6 +564,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 f87a6774d6ac50..0fdda197aca429 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -1453,7 +1453,11 @@ def _format_syntax_error(self, stype, **kwargs):
                     )
                 else:
                     yield '    {}\n'.format(ltext)
-        msg = self.msg or "<no detail available>"
+        try:
+            if not (msg := self.msg):
+                msg = "<no detail available>"
+        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.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to