mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
bpo-34463: Make python tracebacks identical to C tracebacks for SyntaxErrors without a lineno (GH-23427)
This commit is contained in:
parent
6afb730e2a
commit
069560b117
3 changed files with 35 additions and 5 deletions
|
@ -525,7 +525,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
|
||||
|
@ -584,9 +585,12 @@ class TracebackException:
|
|||
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:
|
||||
|
@ -604,7 +608,7 @@ class TracebackException:
|
|||
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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue