Skip to content

bpo-40612: Fix SyntaxError edge cases in traceback formatting #20072

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 8 commits into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
bpo-40612: Fix SyntaxError edge cases in traceback module
  • Loading branch information
gvanrossum committed May 13, 2020
commit 87e78b517b22bd5a7e2d55313a67585924c1d80b
10 changes: 5 additions & 5 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ def test_caret(self):
SyntaxError)
self.assertIn("^", err[2]) # third line has caret
self.assertEqual(err[2].count('\n'), 1) # and no additional newline
self.assertEqual(err[1].find("+"), err[2].find("^")) # in the right place
self.assertEqual(err[1].find("+") + 1, err[2].find("^")) # in the right place

err = self.get_exception_format(self.syntax_error_with_caret_non_ascii,
SyntaxError)
self.assertIn("^", err[2]) # third line has caret
self.assertEqual(err[2].count('\n'), 1) # and no additional newline
self.assertEqual(err[1].find("+"), err[2].find("^")) # in the right place
self.assertEqual(err[1].find("+") + 1, err[2].find("^")) # in the right place

def test_nocaret(self):
exc = SyntaxError("error", ("x.py", 23, None, "bad syntax"))
Expand All @@ -78,7 +78,7 @@ def test_bad_indentation(self):
self.assertEqual(len(err), 4)
self.assertEqual(err[1].strip(), "print(2)")
self.assertIn("^", err[2])
self.assertEqual(err[1].find(")"), err[2].find("^"))
self.assertEqual(err[1].find(")") + 1, err[2].find("^"))

err = self.get_exception_format(self.syntax_error_bad_indentation2,
IndentationError)
Expand Down Expand Up @@ -656,7 +656,7 @@ def outer_raise():
self.assertIn('inner_raise() # Marker', blocks[2])
self.check_zero_div(blocks[2])

@support.skip_if_new_parser("Pegen is arguably better here, so no need to fix this")
@unittest.skipIf(support.use_old_parser(), "Pegen is arguably better here, so no need to fix this")
def test_syntax_error_offset_at_eol(self):
# See #10186.
def e():
Expand All @@ -666,7 +666,7 @@ def e():
def e():
exec("x = 5 | 4 |")
msg = self.get_report(e).splitlines()
self.assertEqual(msg[-2], ' ^')
self.assertEqual(msg[-2], ' ^')

def test_message_none(self):
# A message that looks like "None" should not be treated specially
Expand Down
14 changes: 9 additions & 5 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,9 +569,12 @@ def format_exception_only(self):

if not issubclass(self.exc_type, SyntaxError):
yield _format_final_exc_line(stype, self._str)
return
else:
yield from self._format_syntax_error(stype)

# It was a syntax error; show exactly where the problem was found.
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)
Expand All @@ -580,12 +583,13 @@ def format_exception_only(self):
offset = self.offset
if badline is not None:
yield ' {}\n'.format(badline.strip())
if offset is not None:
if offset is not None and offset >= 1:
caretspace = badline.rstrip('\n')
offset = min(len(caretspace), offset) - 1
# Convert to 0-based offset, and clip to text length
offset = min(len(caretspace), offset - 1)
caretspace = caretspace[:offset].lstrip()
# non-space whitespace (likes tabs) must be kept for alignment
caretspace = ((c.isspace() and c or ' ') for c in caretspace)
caretspace = ((c if c.isspace() else ' ') for c in caretspace)
yield ' {}^\n'.format(''.join(caretspace))
msg = self.msg or "<no detail available>"
yield "{}: {}\n".format(stype, msg)
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy