From b58f6ea0ec332fa643071bd05401924651a04da3 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Tue, 22 Dec 2020 22:26:40 +0000 Subject: [PATCH 1/3] [3.8] 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 --- Lib/test/test_traceback.py | 25 ++++++++++++++++++++++++- Lib/traceback.py | 14 +++++++++----- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index a5ba6090cf08d2..36f77398b57671 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -679,7 +679,30 @@ def test_message_none(self): err = self.get_report(Exception('')) self.assertIn('Exception\n', err) - + 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 "", 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']) class PyExcReportingTests(BaseExceptionReportingTests, unittest.TestCase): # # This checks reporting through the 'traceback' module, with both diff --git a/Lib/traceback.py b/Lib/traceback.py index 19de84b26bf358..f76bdcf0b7dd18 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 @@ -569,9 +570,12 @@ def format_exception_only(self): return # It was a syntax error; show exactly where the problem was found. - filename = self.filename or "" - 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 "", self.lineno) + elif self.filename is not None: + filename_suffix = ' ({})'.format(self.filename) badline = self.text offset = self.offset @@ -585,7 +589,7 @@ def format_exception_only(self): caretspace = ((c.isspace() and c or ' ') for c in caretspace) yield ' {}^\n'.format(''.join(caretspace)) msg = self.msg or "" - yield "{}: {}\n".format(stype, msg) + yield "{}: {}{}\n".format(stype, msg, filename_suffix) def format(self, *, chain=True): """Format the exception. From cc6dd89baf35ff4c558dc0798b3c2be53ec2569b Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 22 Dec 2020 22:51:49 +0000 Subject: [PATCH 2/3] =?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 --- .../NEWS.d/next/Library/2020-12-22-22-51-48.bpo-34463.TUD8V5.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2020-12-22-22-51-48.bpo-34463.TUD8V5.rst diff --git a/Misc/NEWS.d/next/Library/2020-12-22-22-51-48.bpo-34463.TUD8V5.rst b/Misc/NEWS.d/next/Library/2020-12-22-22-51-48.bpo-34463.TUD8V5.rst new file mode 100644 index 00000000000000..d045fb7dbabfc6 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-12-22-22-51-48.bpo-34463.TUD8V5.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). \ No newline at end of file From 711cd060cd5f1680a33d94df1e754ec9a1898107 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Tue, 22 Dec 2020 22:54:27 +0000 Subject: [PATCH 3/3] added missing newline in test --- Lib/test/test_traceback.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 36f77398b57671..2ffb6fa6a79ca3 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -703,6 +703,7 @@ def test_syntax_error_no_lineno(self): msg = self.get_report(e).splitlines() self.assertEqual(msg, [' File "myfile.py", line 100', 'SyntaxError: bad syntax']) + class PyExcReportingTests(BaseExceptionReportingTests, unittest.TestCase): # # This checks reporting through the 'traceback' module, with both