Skip to content

Commit 20f439b

Browse files
authored
bpo-45249: Ensure the traceback module prints correctly syntax errors with ranges (GH-28575)
1 parent adc5d32 commit 20f439b

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

Lib/test/test_traceback.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ def syntax_error_with_caret(self):
4343
def syntax_error_with_caret_2(self):
4444
compile("1 +\n", "?", "exec")
4545

46+
def syntax_error_with_caret_range(self):
47+
compile("f(x, y for y in range(30), z)", "?", "exec")
48+
4649
def syntax_error_bad_indentation(self):
4750
compile("def spam():\n print(1)\n print(2)", "?", "exec")
4851

@@ -59,18 +62,28 @@ def test_caret(self):
5962
self.assertTrue(err[1].strip() == "return x!")
6063
self.assertIn("^", err[2]) # third line has caret
6164
self.assertEqual(err[1].find("!"), err[2].find("^")) # in the right place
65+
self.assertEqual(err[2].count("^"), 1)
6266

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

6974
err = self.get_exception_format(self.syntax_error_with_caret_non_ascii,
7075
SyntaxError)
7176
self.assertIn("^", err[2]) # third line has caret
7277
self.assertEqual(err[2].count('\n'), 1) # and no additional newline
7378
self.assertEqual(err[1].find("+") + 1, err[2].find("^")) # in the right place
79+
self.assertEqual(err[2].count("^"), 1)
80+
81+
err = self.get_exception_format(self.syntax_error_with_caret_range,
82+
SyntaxError)
83+
self.assertIn("^", err[2]) # third line has caret
84+
self.assertEqual(err[2].count('\n'), 1) # and no additional newline
85+
self.assertEqual(err[1].find("y"), err[2].find("^")) # in the right place
86+
self.assertEqual(err[2].count("^"), len("y for y in range(30)"))
7487

7588
def test_nocaret(self):
7689
exc = SyntaxError("error", ("x.py", 23, None, "bad syntax"))

Lib/traceback.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -622,10 +622,14 @@ class TracebackException:
622622
occurred.
623623
- :attr:`lineno` For syntax errors - the linenumber where the error
624624
occurred.
625+
- :attr:`end_lineno` For syntax errors - the end linenumber where the error
626+
occurred. Can be `None` if not present.
625627
- :attr:`text` For syntax errors - the text where the error
626628
occurred.
627629
- :attr:`offset` For syntax errors - the offset into the text where the
628630
error occurred.
631+
- :attr:`end_offset` For syntax errors - the offset into the text where the
632+
error occurred. Can be `None` if not present.
629633
- :attr:`msg` For syntax errors - the compiler error message.
630634
"""
631635

@@ -655,8 +659,11 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
655659
self.filename = exc_value.filename
656660
lno = exc_value.lineno
657661
self.lineno = str(lno) if lno is not None else None
662+
end_lno = exc_value.end_lineno
663+
self.end_lineno = str(end_lno) if end_lno is not None else None
658664
self.text = exc_value.text
659665
self.offset = exc_value.offset
666+
self.end_offset = exc_value.end_offset
660667
self.msg = exc_value.msg
661668
if lookup_lines:
662669
self._load_lines()
@@ -771,12 +778,20 @@ def _format_syntax_error(self, stype):
771778
ltext = rtext.lstrip(' \n\f')
772779
spaces = len(rtext) - len(ltext)
773780
yield ' {}\n'.format(ltext)
774-
# Convert 1-based column offset to 0-based index into stripped text
775-
caret = (self.offset or 0) - 1 - spaces
776-
if caret >= 0:
777-
# non-space whitespace (likes tabs) must be kept for alignment
778-
caretspace = ((c if c.isspace() else ' ') for c in ltext[:caret])
779-
yield ' {}^\n'.format(''.join(caretspace))
781+
782+
if self.offset is not None:
783+
offset = self.offset
784+
end_offset = self.end_offset if self.end_offset is not None else offset
785+
if offset == end_offset or end_offset == -1:
786+
end_offset = offset + 1
787+
788+
# Convert 1-based column offset to 0-based index into stripped text
789+
colno = offset - 1 - spaces
790+
end_colno = end_offset - 1 - spaces
791+
if colno >= 0:
792+
# non-space whitespace (likes tabs) must be kept for alignment
793+
caretspace = ((c if c.isspace() else ' ') for c in ltext[:colno])
794+
yield ' {}{}'.format("".join(caretspace), ('^' * (end_colno - colno) + "\n"))
780795
msg = self.msg or "<no detail available>"
781796
yield "{}: {}{}\n".format(stype, msg, filename_suffix)
782797

0 commit comments

Comments
 (0)