Skip to content

Commit b8c7ed8

Browse files
pablogsalmiss-islington
authored andcommitted
bpo-45249: Ensure the traceback module prints correctly syntax errors with ranges (pythonGH-28575)
(cherry picked from commit 20f439b) Co-authored-by: Pablo Galindo Salgado <[email protected]>
1 parent 9e209d4 commit b8c7ed8

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
@@ -39,6 +39,9 @@ def syntax_error_with_caret(self):
3939
def syntax_error_with_caret_2(self):
4040
compile("1 +\n", "?", "exec")
4141

42+
def syntax_error_with_caret_range(self):
43+
compile("f(x, y for y in range(30), z)", "?", "exec")
44+
4245
def syntax_error_bad_indentation(self):
4346
compile("def spam():\n print(1)\n print(2)", "?", "exec")
4447

@@ -55,18 +58,28 @@ def test_caret(self):
5558
self.assertTrue(err[1].strip() == "return x!")
5659
self.assertIn("^", err[2]) # third line has caret
5760
self.assertEqual(err[1].find("!"), err[2].find("^")) # in the right place
61+
self.assertEqual(err[2].count("^"), 1)
5862

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

6570
err = self.get_exception_format(self.syntax_error_with_caret_non_ascii,
6671
SyntaxError)
6772
self.assertIn("^", err[2]) # third line has caret
6873
self.assertEqual(err[2].count('\n'), 1) # and no additional newline
6974
self.assertEqual(err[1].find("+") + 1, err[2].find("^")) # in the right place
75+
self.assertEqual(err[2].count("^"), 1)
76+
77+
err = self.get_exception_format(self.syntax_error_with_caret_range,
78+
SyntaxError)
79+
self.assertIn("^", err[2]) # third line has caret
80+
self.assertEqual(err[2].count('\n'), 1) # and no additional newline
81+
self.assertEqual(err[1].find("y"), err[2].find("^")) # in the right place
82+
self.assertEqual(err[2].count("^"), len("y for y in range(30)"))
7083

7184
def test_nocaret(self):
7285
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
@@ -475,10 +475,14 @@ class TracebackException:
475475
occurred.
476476
- :attr:`lineno` For syntax errors - the linenumber where the error
477477
occurred.
478+
- :attr:`end_lineno` For syntax errors - the end linenumber where the error
479+
occurred. Can be `None` if not present.
478480
- :attr:`text` For syntax errors - the text where the error
479481
occurred.
480482
- :attr:`offset` For syntax errors - the offset into the text where the
481483
error occurred.
484+
- :attr:`end_offset` For syntax errors - the offset into the text where the
485+
error occurred. Can be `None` if not present.
482486
- :attr:`msg` For syntax errors - the compiler error message.
483487
"""
484488

@@ -507,8 +511,11 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
507511
self.filename = exc_value.filename
508512
lno = exc_value.lineno
509513
self.lineno = str(lno) if lno is not None else None
514+
end_lno = exc_value.end_lineno
515+
self.end_lineno = str(end_lno) if end_lno is not None else None
510516
self.text = exc_value.text
511517
self.offset = exc_value.offset
518+
self.end_offset = exc_value.end_offset
512519
self.msg = exc_value.msg
513520
if lookup_lines:
514521
self._load_lines()
@@ -623,12 +630,20 @@ def _format_syntax_error(self, stype):
623630
ltext = rtext.lstrip(' \n\f')
624631
spaces = len(rtext) - len(ltext)
625632
yield ' {}\n'.format(ltext)
626-
# Convert 1-based column offset to 0-based index into stripped text
627-
caret = (self.offset or 0) - 1 - spaces
628-
if caret >= 0:
629-
# non-space whitespace (likes tabs) must be kept for alignment
630-
caretspace = ((c if c.isspace() else ' ') for c in ltext[:caret])
631-
yield ' {}^\n'.format(''.join(caretspace))
633+
634+
if self.offset is not None:
635+
offset = self.offset
636+
end_offset = self.end_offset if self.end_offset is not None else offset
637+
if offset == end_offset or end_offset == -1:
638+
end_offset = offset + 1
639+
640+
# Convert 1-based column offset to 0-based index into stripped text
641+
colno = offset - 1 - spaces
642+
end_colno = end_offset - 1 - spaces
643+
if colno >= 0:
644+
# non-space whitespace (likes tabs) must be kept for alignment
645+
caretspace = ((c if c.isspace() else ' ') for c in ltext[:colno])
646+
yield ' {}{}'.format("".join(caretspace), ('^' * (end_colno - colno) + "\n"))
632647
msg = self.msg or "<no detail available>"
633648
yield "{}: {}{}\n".format(stype, msg, filename_suffix)
634649

0 commit comments

Comments
 (0)