aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBatuhan Taskaya <isidentical@gmail.com>2022-11-22 01:16:12 +0300
committerGitHub <noreply@github.com>2022-11-21 22:16:12 +0000
commita3480ec79512c6988fd9fcd6bf8959b45c3675d9 (patch)
tree9f91c382074f544aa483c0657e6016a7753fb451
parentgh-98629: Fixes sys._git and sys.version creation on Windows (GH-99664) (diff)
downloadcpython-a3480ec79512c6988fd9fcd6bf8959b45c3675d9.tar.gz
cpython-a3480ec79512c6988fd9fcd6bf8959b45c3675d9.tar.bz2
cpython-a3480ec79512c6988fd9fcd6bf8959b45c3675d9.zip
[3.11] gh-99103: Normalize specialized traceback anchors against the current line (#99423)
[3.11] gh-99103: Normalize specialized traceback anchors against the current line (GH-99145) Automerge-Triggered-By: GH:isidentical. (cherry picked from commit 57be5459593bbd09583317ebdafc4d58ae51dbf4) Co-authored-by: Batuhan Taskaya <isidentical@gmail.com>
-rw-r--r--Lib/test/test_traceback.py34
-rw-r--r--Lib/traceback.py11
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2022-11-06-00-17-58.gh-issue-99103.bFA9BX.rst2
-rw-r--r--Python/traceback.c9
4 files changed, 51 insertions, 5 deletions
diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py
index 6d15500656..eadc9c440e 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -532,6 +532,23 @@ 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_unicode(self):
+ def f_with_binary_operator():
+ áóí = 20
+ return 10 + áóí / 0 + 30
+
+ 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+2}, in f_with_binary_operator\n'
+ ' return 10 + áóí / 0 + 30\n'
+ ' ~~~~^~~\n'
+ )
+ result_lines = self.get_exception(f_with_binary_operator)
+ self.assertEqual(result_lines, expected_error.splitlines())
+
def test_caret_for_binary_operators_two_char(self):
def f_with_binary_operator():
divisor = 20
@@ -566,6 +583,23 @@ class TracebackErrorLocationCaretTests(unittest.TestCase):
result_lines = self.get_exception(f_with_subscript)
self.assertEqual(result_lines, expected_error.splitlines())
+ def test_caret_for_subscript_unicode(self):
+ def f_with_subscript():
+ some_dict = {'ó': {'á': {'í': {'theta': 1}}}}
+ return some_dict['ó']['á']['í']['beta']
+
+ lineno_f = f_with_subscript.__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+2}, in f_with_subscript\n'
+ " return some_dict['ó']['á']['í']['beta']\n"
+ ' ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^\n'
+ )
+ result_lines = self.get_exception(f_with_subscript)
+ self.assertEqual(result_lines, expected_error.splitlines())
+
def test_traceback_specialization_with_syntax_error(self):
bytecode = compile("1 / 0 / 1 / 2\n", TESTFN, "exec")
diff --git a/Lib/traceback.py b/Lib/traceback.py
index fa2cc341af..0182bb7525 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -585,12 +585,15 @@ def _extract_caret_anchors_from_line_segment(segment):
if len(tree.body) != 1:
return None
+ normalize = lambda offset: _byte_offset_to_character_offset(segment, offset)
statement = tree.body[0]
match statement:
case ast.Expr(expr):
match expr:
case ast.BinOp():
- operator_str = segment[expr.left.end_col_offset:expr.right.col_offset]
+ operator_start = normalize(expr.left.end_col_offset)
+ operator_end = normalize(expr.right.col_offset)
+ operator_str = segment[operator_start:operator_end]
operator_offset = len(operator_str) - len(operator_str.lstrip())
left_anchor = expr.left.end_col_offset + operator_offset
@@ -600,9 +603,11 @@ def _extract_caret_anchors_from_line_segment(segment):
and not operator_str[operator_offset + 1].isspace()
):
right_anchor += 1
- return _Anchors(left_anchor, right_anchor)
+ return _Anchors(normalize(left_anchor), normalize(right_anchor))
case ast.Subscript():
- return _Anchors(expr.value.end_col_offset, expr.slice.end_col_offset + 1)
+ subscript_start = normalize(expr.value.end_col_offset)
+ subscript_end = normalize(expr.slice.end_col_offset + 1)
+ return _Anchors(subscript_start, subscript_end)
return None
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-11-06-00-17-58.gh-issue-99103.bFA9BX.rst b/Misc/NEWS.d/next/Core and Builtins/2022-11-06-00-17-58.gh-issue-99103.bFA9BX.rst
new file mode 100644
index 0000000000..f5378eb837
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-11-06-00-17-58.gh-issue-99103.bFA9BX.rst
@@ -0,0 +1,2 @@
+Fix the error reporting positions of specialized traceback anchors when the
+source line contains Unicode characters.
diff --git a/Python/traceback.c b/Python/traceback.c
index 20348c06fd..7f47349a27 100644
--- a/Python/traceback.c
+++ b/Python/traceback.c
@@ -705,8 +705,13 @@ extract_anchors_from_line(PyObject *filename, PyObject *line,
done:
if (res > 0) {
- *left_anchor += start_offset;
- *right_anchor += start_offset;
+ // Normalize the AST offsets to byte offsets and adjust them with the
+ // start of the actual line (instead of the source code segment).
+ assert(segment != NULL);
+ assert(*left_anchor >= 0);
+ assert(*right_anchor >= 0);
+ *left_anchor = _PyPegen_byte_offset_to_character_offset(segment, *left_anchor) + start_offset;
+ *right_anchor = _PyPegen_byte_offset_to_character_offset(segment, *right_anchor) + start_offset;
}
Py_XDECREF(segment);
if (arena) {