diff options
author | Irit Katriel <iritkatriel@yahoo.com> | 2021-04-26 19:42:40 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-26 19:42:40 (GMT) |
commit | 727bed675f1a47d12492efba0ca118707502d988 (patch) | |
tree | 901c722d64813cdc57a2528f562010412b2880b3 | |
parent | e1203e8001432a08b87b54867490beb19a694069 (diff) | |
download | cpython-727bed675f1a47d12492efba0ca118707502d988.zip cpython-727bed675f1a47d12492efba0ca118707502d988.tar.gz cpython-727bed675f1a47d12492efba0ca118707502d988.tar.bz2 |
[3.8] bpo-34463: Make python tracebacks identical to C tracebacks for (#23899)
* [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 <iritkatriel@yahoo.com>
* 📜🤖 Added by blurb_it.
* added missing newline in test
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
-rw-r--r-- | Lib/test/test_traceback.py | 24 | ||||
-rw-r--r-- | Lib/traceback.py | 14 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2020-12-22-22-51-48.bpo-34463.TUD8V5.rst | 1 |
3 files changed, 34 insertions, 5 deletions
diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index a5ba609..2ffb6fa 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -679,6 +679,30 @@ class BaseExceptionReportingTests: 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 "<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']) class PyExcReportingTests(BaseExceptionReportingTests, unittest.TestCase): # diff --git a/Lib/traceback.py b/Lib/traceback.py index 19de84b..f76bdcf 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -515,7 +515,8 @@ class TracebackException: 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 @@ class TracebackException: return # It was a syntax error; 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) badline = self.text offset = self.offset @@ -585,7 +589,7 @@ class TracebackException: caretspace = ((c.isspace() and c or ' ') for c in caretspace) 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-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 0000000..d045fb7 --- /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 |