summaryrefslogtreecommitdiff
path: root/Lib/traceback.py
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2020-12-22 14:12:30 -0800
committerGitHub <noreply@github.com>2020-12-22 14:12:30 -0800
commit8e5c61a075f3a60272a7dcdc48db200090d76309 (patch)
tree71cebac3f47f312acb7fe1556e99d881dbc56060 /Lib/traceback.py
parent4ec2149708c7c06ebeccc27e28e2bbc51c4abeca (diff)
downloadcpython-git-8e5c61a075f3a60272a7dcdc48db200090d76309.tar.gz
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>
Diffstat (limited to 'Lib/traceback.py')
-rw-r--r--Lib/traceback.py14
1 files changed, 9 insertions, 5 deletions
diff --git a/Lib/traceback.py b/Lib/traceback.py
index fb34de9489..d7fbdae680 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
@@ -574,9 +575,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:
@@ -594,7 +598,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.