Skip to content

[3.13] gh-123123: Fix display of syntax errors covering multiple lines (GH-123131) #123147

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 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
40 changes: 36 additions & 4 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,35 @@ def f_with_multiline():
result_lines = self.get_exception(f_with_multiline)
self.assertEqual(result_lines, expected_f.splitlines())

# Check custom error messages covering multiple lines
code = textwrap.dedent("""
dummy_call(
"dummy value"
foo="bar",
)
""")

def f_with_multiline():
# Need to defer the compilation until in self.get_exception(..)
return compile(code, "?", "exec")

lineno_f = f_with_multiline.__code__.co_firstlineno

expected_f = (
'Traceback (most recent call last):\n'
f' File "{__file__}", line {self.callable_line}, in get_exception\n'
' callable()\n'
' ~~~~~~~~^^\n'
f' File "{__file__}", line {lineno_f+2}, in f_with_multiline\n'
' return compile(code, "?", "exec")\n'
' File "?", line 3\n'
' "dummy value"\n'
' ^^^^^^^^^^^^^'
)

result_lines = self.get_exception(f_with_multiline)
self.assertEqual(result_lines, expected_f.splitlines())

def test_caret_multiline_expression_bin_op(self):
# Make sure no carets are printed for expressions spanning multiple
# lines.
Expand Down Expand Up @@ -2309,19 +2338,22 @@ def test_message_none(self):
def test_syntax_error_various_offsets(self):
for offset in range(-5, 10):
for add in [0, 2]:
text = " "*add + "text%d" % offset
text = " " * add + "text%d" % offset
expected = [' File "file.py", line 1']
if offset < 1:
expected.append(" %s" % text.lstrip())
elif offset <= 6:
expected.append(" %s" % text.lstrip())
expected.append(" %s^" % (" "*(offset-1)))
# Set the caret length to match the length of the text minus the offset.
caret_length = max(1, len(text.lstrip()) - offset + 1)
expected.append(" %s%s" % (" " * (offset - 1), "^" * caret_length))
else:
caret_length = max(1, len(text.lstrip()) - 4)
expected.append(" %s" % text.lstrip())
expected.append(" %s^" % (" "*5))
expected.append(" %s%s" % (" " * 5, "^" * caret_length))
expected.append("SyntaxError: msg")
expected.append("")
err = self.get_report(SyntaxError("msg", ("file.py", 1, offset+add, text)))
err = self.get_report(SyntaxError("msg", ("file.py", 1, offset + add, text)))
exp = "\n".join(expected)
self.assertEqual(exp, err)

Expand Down
10 changes: 7 additions & 3 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -1292,11 +1292,15 @@ def _format_syntax_error(self, stype, **kwargs):
yield ' {}\n'.format(ltext)
else:
offset = self.offset
end_offset = self.end_offset if self.end_offset not in {None, 0} else offset
if self.lineno == self.end_lineno:
end_offset = self.end_offset if self.end_offset not in {None, 0} else offset
else:
end_offset = len(rtext) + 1

if self.text and offset > len(self.text):
offset = len(self.text) + 1
offset = len(rtext) + 1
if self.text and end_offset > len(self.text):
end_offset = len(self.text) + 1
end_offset = len(rtext) + 1
if offset >= end_offset or end_offset < 0:
end_offset = offset + 1

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix displaying :exc:`SyntaxError` exceptions covering multiple lines. Patch
by Pablo Galindo
Loading