[3.11] gh-106922: Fix error location for constructs with spaces and parentheses (GH-108959) (#109148)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
This commit is contained in:
Miss Islington (bot) 2023-09-08 09:56:10 -07:00 committed by GitHub
parent b55cf2c2d8
commit c1a2ef5efc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 68 additions and 3 deletions

View file

@ -566,6 +566,24 @@ class TracebackErrorLocationCaretTests(unittest.TestCase):
result_lines = self.get_exception(f_with_binary_operator)
self.assertEqual(result_lines, expected_error.splitlines())
def test_caret_for_binary_operators_with_spaces_and_parenthesis(self):
def f_with_binary_operator():
a = 1
b = ""
return ( a ) + b
lineno_f = f_with_binary_operator.__code__.co_firstlineno
expected_error = (
'Traceback (most recent call last):\n'
f' File "{__file__}", line {self.callable_line}, in get_exception\n'
' callable()\n'
f' File "{__file__}", line {lineno_f+3}, in f_with_binary_operator\n'
' return ( a ) + b\n'
' ~~~~~~~~~~^~~\n'
)
result_lines = self.get_exception(f_with_binary_operator)
self.assertEqual(result_lines, expected_error.splitlines())
def test_caret_for_subscript(self):
def f_with_subscript():
some_dict = {'x': {'y': None}}
@ -600,6 +618,24 @@ class TracebackErrorLocationCaretTests(unittest.TestCase):
result_lines = self.get_exception(f_with_subscript)
self.assertEqual(result_lines, expected_error.splitlines())
def test_caret_for_subscript_with_spaces_and_parenthesis(self):
def f_with_binary_operator():
a = []
b = c = 1
return b [ a ] + c
lineno_f = f_with_binary_operator.__code__.co_firstlineno
expected_error = (
'Traceback (most recent call last):\n'
f' File "{__file__}", line {self.callable_line}, in get_exception\n'
' callable()\n'
f' File "{__file__}", line {lineno_f+3}, in f_with_binary_operator\n'
' return b [ a ] + c\n'
' ~~~~~~^^^^^^^^^\n'
)
result_lines = self.get_exception(f_with_binary_operator)
self.assertEqual(result_lines, expected_error.splitlines())
def test_traceback_specialization_with_syntax_error(self):
bytecode = compile("1 / 0 / 1 / 2\n", TESTFN, "exec")