Skip to content

[3.8] bpo-34463: Make python tracebacks identical to C tracebacks for #23899

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +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 "<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):
#
Expand Down
14 changes: 9 additions & 5 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 "<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
Expand All @@ -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 "<no detail available>"
yield "{}: {}\n".format(stype, msg)
yield "{}: {}{}\n".format(stype, msg, filename_suffix)

def format(self, *, chain=True):
"""Format the exception.
Expand Down
Original file line number Diff line number Diff line change
@@ -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).