[Python-checkins] bpo-34463: Make python tracebacks identical to C tracebacks for SyntaxErrors without a lineno (GH-23427)

miss-islington webhook-mailer at python.org
Tue Dec 22 17:12:39 EST 2020


https://github.com/python/cpython/commit/8e5c61a075f3a60272a7dcdc48db200090d76309
commit: 8e5c61a075f3a60272a7dcdc48db200090d76309
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2020-12-22T14:12:30-08:00
summary:

bpo-34463: Make python tracebacks identical to C tracebacks for SyntaxErrors without a lineno (GH-23427)

(cherry picked from commit 069560b1171eb6385121ff3b6331e8814a4e7454)

Co-authored-by: Irit Katriel <iritkatriel at yahoo.com>

files:
A Misc/NEWS.d/next/Library/2020-11-20-19-00-27.bpo-34463.aJcm56.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 8549ba2b75ad0..5bb3a58b2a103 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -667,6 +667,31 @@ def e():
         msg = self.get_report(e).splitlines()
         self.assertEqual(msg[-2], '               ^')
 
+    def test_syntax_error_no_lineno(self):
+        # See #34463.
+
+        # Without filename
+        e = SyntaxError('bad syntax')
+        msg = self.get_report(e).splitlines()
+        self.assertEqual(msg,
+            ['SyntaxError: bad syntax'])
+        e.lineno = 100
+        msg = self.get_report(e).splitlines()
+        self.assertEqual(msg,
+            ['  File "<string>", line 100', 'SyntaxError: bad syntax'])
+
+        # With filename
+        e = SyntaxError('bad syntax')
+        e.filename = 'myfile.py'
+
+        msg = self.get_report(e).splitlines()
+        self.assertEqual(msg,
+            ['SyntaxError: bad syntax (myfile.py)'])
+        e.lineno = 100
+        msg = self.get_report(e).splitlines()
+        self.assertEqual(msg,
+            ['  File "myfile.py", line 100', 'SyntaxError: bad syntax'])
+
     def test_message_none(self):
         # A message that looks like "None" should not be treated specially
         err = self.get_report(Exception(None))
diff --git a/Lib/traceback.py b/Lib/traceback.py
index fb34de9489300..d7fbdae680be6 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -515,7 +515,8 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
         if exc_type and issubclass(exc_type, SyntaxError):
             # Handle SyntaxError's specially
             self.filename = exc_value.filename
-            self.lineno = str(exc_value.lineno)
+            lno = exc_value.lineno
+            self.lineno = str(lno) if lno is not None else None
             self.text = exc_value.text
             self.offset = exc_value.offset
             self.msg = exc_value.msg
@@ -574,9 +575,12 @@ def format_exception_only(self):
     def _format_syntax_error(self, stype):
         """Format SyntaxError exceptions (internal helper)."""
         # Show exactly where the problem was found.
-        filename = self.filename or "<string>"
-        lineno = str(self.lineno) or '?'
-        yield '  File "{}", line {}\n'.format(filename, lineno)
+        filename_suffix = ''
+        if self.lineno is not None:
+            yield '  File "{}", line {}\n'.format(
+                self.filename or "<string>", self.lineno)
+        elif self.filename is not None:
+            filename_suffix = ' ({})'.format(self.filename)
 
         text = self.text
         if text is not None:
@@ -594,7 +598,7 @@ def _format_syntax_error(self, stype):
                 caretspace = ((c if c.isspace() else ' ') for c in ltext[:caret])
                 yield '    {}^\n'.format(''.join(caretspace))
         msg = self.msg or "<no detail available>"
-        yield "{}: {}\n".format(stype, msg)
+        yield "{}: {}{}\n".format(stype, msg, filename_suffix)
 
     def format(self, *, chain=True):
         """Format the exception.
diff --git a/Misc/NEWS.d/next/Library/2020-11-20-19-00-27.bpo-34463.aJcm56.rst b/Misc/NEWS.d/next/Library/2020-11-20-19-00-27.bpo-34463.aJcm56.rst
new file mode 100644
index 0000000000000..df183548236af
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-11-20-19-00-27.bpo-34463.aJcm56.rst
@@ -0,0 +1 @@
+Fixed discrepancy between :mod:`traceback` and the interpreter in formatting of SyntaxError with lineno not set (:mod:`traceback` was changed to match interpreter).



More information about the Python-checkins mailing list