mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
bpo-45249: Ensure the traceback module prints correctly syntax errors with ranges (GH-28575)
This commit is contained in:
parent
adc5d32f47
commit
20f439b6b9
2 changed files with 34 additions and 6 deletions
|
@ -43,6 +43,9 @@ class TracebackCases(unittest.TestCase):
|
||||||
def syntax_error_with_caret_2(self):
|
def syntax_error_with_caret_2(self):
|
||||||
compile("1 +\n", "?", "exec")
|
compile("1 +\n", "?", "exec")
|
||||||
|
|
||||||
|
def syntax_error_with_caret_range(self):
|
||||||
|
compile("f(x, y for y in range(30), z)", "?", "exec")
|
||||||
|
|
||||||
def syntax_error_bad_indentation(self):
|
def syntax_error_bad_indentation(self):
|
||||||
compile("def spam():\n print(1)\n print(2)", "?", "exec")
|
compile("def spam():\n print(1)\n print(2)", "?", "exec")
|
||||||
|
|
||||||
|
@ -59,18 +62,28 @@ class TracebackCases(unittest.TestCase):
|
||||||
self.assertTrue(err[1].strip() == "return x!")
|
self.assertTrue(err[1].strip() == "return x!")
|
||||||
self.assertIn("^", err[2]) # third line has caret
|
self.assertIn("^", err[2]) # third line has caret
|
||||||
self.assertEqual(err[1].find("!"), err[2].find("^")) # in the right place
|
self.assertEqual(err[1].find("!"), err[2].find("^")) # in the right place
|
||||||
|
self.assertEqual(err[2].count("^"), 1)
|
||||||
|
|
||||||
err = self.get_exception_format(self.syntax_error_with_caret_2,
|
err = self.get_exception_format(self.syntax_error_with_caret_2,
|
||||||
SyntaxError)
|
SyntaxError)
|
||||||
self.assertIn("^", err[2]) # third line has caret
|
self.assertIn("^", err[2]) # third line has caret
|
||||||
self.assertEqual(err[2].count('\n'), 1) # and no additional newline
|
self.assertEqual(err[2].count('\n'), 1) # and no additional newline
|
||||||
self.assertEqual(err[1].find("+") + 1, err[2].find("^")) # in the right place
|
self.assertEqual(err[1].find("+") + 1, err[2].find("^")) # in the right place
|
||||||
|
self.assertEqual(err[2].count("^"), 1)
|
||||||
|
|
||||||
err = self.get_exception_format(self.syntax_error_with_caret_non_ascii,
|
err = self.get_exception_format(self.syntax_error_with_caret_non_ascii,
|
||||||
SyntaxError)
|
SyntaxError)
|
||||||
self.assertIn("^", err[2]) # third line has caret
|
self.assertIn("^", err[2]) # third line has caret
|
||||||
self.assertEqual(err[2].count('\n'), 1) # and no additional newline
|
self.assertEqual(err[2].count('\n'), 1) # and no additional newline
|
||||||
self.assertEqual(err[1].find("+") + 1, err[2].find("^")) # in the right place
|
self.assertEqual(err[1].find("+") + 1, err[2].find("^")) # in the right place
|
||||||
|
self.assertEqual(err[2].count("^"), 1)
|
||||||
|
|
||||||
|
err = self.get_exception_format(self.syntax_error_with_caret_range,
|
||||||
|
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("y"), err[2].find("^")) # in the right place
|
||||||
|
self.assertEqual(err[2].count("^"), len("y for y in range(30)"))
|
||||||
|
|
||||||
def test_nocaret(self):
|
def test_nocaret(self):
|
||||||
exc = SyntaxError("error", ("x.py", 23, None, "bad syntax"))
|
exc = SyntaxError("error", ("x.py", 23, None, "bad syntax"))
|
||||||
|
|
|
@ -622,10 +622,14 @@ class TracebackException:
|
||||||
occurred.
|
occurred.
|
||||||
- :attr:`lineno` For syntax errors - the linenumber where the error
|
- :attr:`lineno` For syntax errors - the linenumber where the error
|
||||||
occurred.
|
occurred.
|
||||||
|
- :attr:`end_lineno` For syntax errors - the end linenumber where the error
|
||||||
|
occurred. Can be `None` if not present.
|
||||||
- :attr:`text` For syntax errors - the text where the error
|
- :attr:`text` For syntax errors - the text where the error
|
||||||
occurred.
|
occurred.
|
||||||
- :attr:`offset` For syntax errors - the offset into the text where the
|
- :attr:`offset` For syntax errors - the offset into the text where the
|
||||||
error occurred.
|
error occurred.
|
||||||
|
- :attr:`end_offset` For syntax errors - the offset into the text where the
|
||||||
|
error occurred. Can be `None` if not present.
|
||||||
- :attr:`msg` For syntax errors - the compiler error message.
|
- :attr:`msg` For syntax errors - the compiler error message.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@ -655,8 +659,11 @@ class TracebackException:
|
||||||
self.filename = exc_value.filename
|
self.filename = exc_value.filename
|
||||||
lno = exc_value.lineno
|
lno = exc_value.lineno
|
||||||
self.lineno = str(lno) if lno is not None else None
|
self.lineno = str(lno) if lno is not None else None
|
||||||
|
end_lno = exc_value.end_lineno
|
||||||
|
self.end_lineno = str(end_lno) if end_lno is not None else None
|
||||||
self.text = exc_value.text
|
self.text = exc_value.text
|
||||||
self.offset = exc_value.offset
|
self.offset = exc_value.offset
|
||||||
|
self.end_offset = exc_value.end_offset
|
||||||
self.msg = exc_value.msg
|
self.msg = exc_value.msg
|
||||||
if lookup_lines:
|
if lookup_lines:
|
||||||
self._load_lines()
|
self._load_lines()
|
||||||
|
@ -771,12 +778,20 @@ class TracebackException:
|
||||||
ltext = rtext.lstrip(' \n\f')
|
ltext = rtext.lstrip(' \n\f')
|
||||||
spaces = len(rtext) - len(ltext)
|
spaces = len(rtext) - len(ltext)
|
||||||
yield ' {}\n'.format(ltext)
|
yield ' {}\n'.format(ltext)
|
||||||
# Convert 1-based column offset to 0-based index into stripped text
|
|
||||||
caret = (self.offset or 0) - 1 - spaces
|
if self.offset is not None:
|
||||||
if caret >= 0:
|
offset = self.offset
|
||||||
# non-space whitespace (likes tabs) must be kept for alignment
|
end_offset = self.end_offset if self.end_offset is not None else offset
|
||||||
caretspace = ((c if c.isspace() else ' ') for c in ltext[:caret])
|
if offset == end_offset or end_offset == -1:
|
||||||
yield ' {}^\n'.format(''.join(caretspace))
|
end_offset = offset + 1
|
||||||
|
|
||||||
|
# Convert 1-based column offset to 0-based index into stripped text
|
||||||
|
colno = offset - 1 - spaces
|
||||||
|
end_colno = end_offset - 1 - spaces
|
||||||
|
if colno >= 0:
|
||||||
|
# non-space whitespace (likes tabs) must be kept for alignment
|
||||||
|
caretspace = ((c if c.isspace() else ' ') for c in ltext[:colno])
|
||||||
|
yield ' {}{}'.format("".join(caretspace), ('^' * (end_colno - colno) + "\n"))
|
||||||
msg = self.msg or "<no detail available>"
|
msg = self.msg or "<no detail available>"
|
||||||
yield "{}: {}{}\n".format(stype, msg, filename_suffix)
|
yield "{}: {}{}\n".format(stype, msg, filename_suffix)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue