diff options
author | Pablo Galindo Salgado <Pablogsal@gmail.com> | 2024-08-19 15:09:03 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-19 15:09:03 (GMT) |
commit | 48856ead6ae023b2819ee63cb6ff97a0976a2cc3 (patch) | |
tree | ea753ff7bc6661b7858c0e8b7e309c2648c34fb0 /Lib/test | |
parent | b6d0a40a42fa17d087e502245b29cde46368ac07 (diff) | |
download | cpython-48856ead6ae023b2819ee63cb6ff97a0976a2cc3.zip cpython-48856ead6ae023b2819ee63cb6ff97a0976a2cc3.tar.gz cpython-48856ead6ae023b2819ee63cb6ff97a0976a2cc3.tar.bz2 |
gh-123123: Fix display of syntax errors covering multiple lines (#123131)
Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_traceback.py | 40 |
1 files changed, 36 insertions, 4 deletions
diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 1895c88..b568221 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -699,6 +699,35 @@ class TracebackErrorLocationCaretTestBase: 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. @@ -2312,19 +2341,22 @@ class BaseExceptionReportingTests: 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) |