Skip to content

Commit 224b223

Browse files
committed
gh-99103: Normalize positions of specialized traceback anchors against the current line
1 parent 47ab848 commit 224b223

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

Lib/test/test_traceback.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,23 @@ def f_with_binary_operator():
559559
result_lines = self.get_exception(f_with_binary_operator)
560560
self.assertEqual(result_lines, expected_error.splitlines())
561561

562+
def test_caret_for_binary_operators_with_unicode(self):
563+
def f_with_binary_operator():
564+
áóí = 20
565+
return 10 + áóí / 0 + 30
566+
567+
lineno_f = f_with_binary_operator.__code__.co_firstlineno
568+
expected_error = (
569+
'Traceback (most recent call last):\n'
570+
f' File "{__file__}", line {self.callable_line}, in get_exception\n'
571+
' callable()\n'
572+
f' File "{__file__}", line {lineno_f+2}, in f_with_binary_operator\n'
573+
' return 10 + áóí / 0 + 30\n'
574+
' ~~~~^~~\n'
575+
)
576+
result_lines = self.get_exception(f_with_binary_operator)
577+
self.assertEqual(result_lines, expected_error.splitlines())
578+
562579
def test_caret_for_binary_operators_two_char(self):
563580
def f_with_binary_operator():
564581
divisor = 20
@@ -593,6 +610,23 @@ def f_with_subscript():
593610
result_lines = self.get_exception(f_with_subscript)
594611
self.assertEqual(result_lines, expected_error.splitlines())
595612

613+
def test_caret_for_subscript_unicode(self):
614+
def f_with_subscript():
615+
some_dict = {'ó': {'á': {'í': {'theta': 1}}}}
616+
return some_dict['ó']['á']['í']['beta']
617+
618+
lineno_f = f_with_subscript.__code__.co_firstlineno
619+
expected_error = (
620+
'Traceback (most recent call last):\n'
621+
f' File "{__file__}", line {self.callable_line}, in get_exception\n'
622+
' callable()\n'
623+
f' File "{__file__}", line {lineno_f+2}, in f_with_subscript\n'
624+
" return some_dict['ó']['á']['í']['beta']\n"
625+
' ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^\n'
626+
)
627+
result_lines = self.get_exception(f_with_subscript)
628+
self.assertEqual(result_lines, expected_error.splitlines())
629+
596630
def test_traceback_specialization_with_syntax_error(self):
597631
bytecode = compile("1 / 0 / 1 / 2\n", TESTFN, "exec")
598632

Lib/traceback.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -586,12 +586,13 @@ def _extract_caret_anchors_from_line_segment(segment):
586586
if len(tree.body) != 1:
587587
return None
588588

589+
normalize = lambda offset: _byte_offset_to_character_offset(segment, offset)
589590
statement = tree.body[0]
590591
match statement:
591592
case ast.Expr(expr):
592593
match expr:
593594
case ast.BinOp():
594-
operator_str = segment[expr.left.end_col_offset:expr.right.col_offset]
595+
operator_str = segment[normalize(expr.left.end_col_offset):normalize(expr.right.col_offset)]
595596
operator_offset = len(operator_str) - len(operator_str.lstrip())
596597

597598
left_anchor = expr.left.end_col_offset + operator_offset
@@ -601,9 +602,9 @@ def _extract_caret_anchors_from_line_segment(segment):
601602
and not operator_str[operator_offset + 1].isspace()
602603
):
603604
right_anchor += 1
604-
return _Anchors(left_anchor, right_anchor)
605+
return _Anchors(normalize(left_anchor), normalize(right_anchor))
605606
case ast.Subscript():
606-
return _Anchors(expr.value.end_col_offset, expr.slice.end_col_offset + 1)
607+
return _Anchors(normalize(expr.value.end_col_offset), normalize(expr.slice.end_col_offset + 1))
607608

608609
return None
609610

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix the error reporting positions of specialized traceback anchors when the
2+
source line contains unicode characters.

Python/traceback.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,8 +705,11 @@ extract_anchors_from_line(PyObject *filename, PyObject *line,
705705

706706
done:
707707
if (res > 0) {
708-
*left_anchor += start_offset;
709-
*right_anchor += start_offset;
708+
// Normalize the AST offsets to byte offsets and adjust it with the
709+
// start of the actual line (instead of the source code segment).
710+
assert(segment != NULL);
711+
*left_anchor = _PyPegen_byte_offset_to_character_offset(segment, *left_anchor) + start_offset;
712+
*right_anchor = _PyPegen_byte_offset_to_character_offset(segment, *right_anchor) + start_offset;
710713
}
711714
Py_XDECREF(segment);
712715
if (arena) {

0 commit comments

Comments
 (0)