From e6e3958aae33b7022fdd6e0ff856eb5d747d81fa Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Fri, 20 Nov 2020 18:22:27 +0000 Subject: [PATCH 1/4] bpo-34463: Match python traceback format to the C traceback for a SyntaxError without lineno. --- Lib/test/test_traceback.py | 25 +++++++++++++++++++++++++ Lib/traceback.py | 12 ++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 91688ff72bbea1..6e8f189719d627 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -687,6 +687,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 "", 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 d2d93c8a32ac29..eda59023797b02 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -526,7 +526,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 @@ -586,8 +587,11 @@ def _format_syntax_error(self, stype): """Format SyntaxError exceptions (internal helper).""" # 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(filename, self.lineno) + elif self.filename is not None: + filename_suffix = ' ({})'.format(self.filename) text = self.text if text is not None: @@ -605,7 +609,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 "" - yield "{}: {}\n".format(stype, msg) + yield "{}: {}{}\n".format(stype, msg, filename_suffix) def format(self, *, chain=True): """Format the exception. From 738926e384770ec0ae77cc569e1f6b7e5de7ff07 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 20 Nov 2020 19:00:29 +0000 Subject: [PATCH 2/4] =?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-11-20-19-00-27.bpo-34463.aJcm56.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2020-11-20-19-00-27.bpo-34463.aJcm56.rst 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 00000000000000..c36ce9a596610f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-11-20-19-00-27.bpo-34463.aJcm56.rst @@ -0,0 +1 @@ +Fixed discrepancy between traceback.py and the interpreter in formatting of SyntaxError with lineno not set (traceback.py was changed to match interpreter). \ No newline at end of file From 6b4f10f4184abf9945935051522ed3e157ab2e1f Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Fri, 20 Nov 2020 19:03:42 +0000 Subject: [PATCH 3/4] Update 2020-11-20-19-00-27.bpo-34463.aJcm56.rst --- .../next/Library/2020-11-20-19-00-27.bpo-34463.aJcm56.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index c36ce9a596610f..df183548236afb 100644 --- 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 @@ -1 +1 @@ -Fixed discrepancy between traceback.py and the interpreter in formatting of SyntaxError with lineno not set (traceback.py was changed to match interpreter). \ No newline at end of file +Fixed discrepancy between :mod:`traceback` and the interpreter in formatting of SyntaxError with lineno not set (:mod:`traceback` was changed to match interpreter). From 9e9c4cd258f7439572d859424aa9010304cfb375 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Tue, 22 Dec 2020 09:23:59 +0000 Subject: [PATCH 4/4] inline the filename calculation --- Lib/traceback.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/traceback.py b/Lib/traceback.py index eda59023797b02..66069f11356367 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -586,10 +586,10 @@ 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 "" filename_suffix = '' if self.lineno is not None: - yield ' File "{}", line {}\n'.format(filename, self.lineno) + yield ' File "{}", line {}\n'.format( + self.filename or "", self.lineno) elif self.filename is not None: filename_suffix = ' ({})'.format(self.filename)