aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/traceback.py
diff options
context:
space:
mode:
authorBatuhan Taskaya <isidentical@gmail.com>2022-11-13 02:37:25 +0300
committerGitHub <noreply@github.com>2022-11-12 15:37:25 -0800
commit57be5459593bbd09583317ebdafc4d58ae51dbf4 (patch)
treeb730c61cc81558a94898b1ffb603a5b42210fd26 /Lib/traceback.py
parentc95f554a408f76f96c14c006ebe8a0d3d3b40765 (diff)
downloadcpython-57be5459593bbd09583317ebdafc4d58ae51dbf4.tar.gz
cpython-57be5459593bbd09583317ebdafc4d58ae51dbf4.zip
gh-99103: Normalize specialized traceback anchors against the current line (GH-99145)
Automerge-Triggered-By: GH:isidentical
Diffstat (limited to 'Lib/traceback.py')
-rw-r--r--Lib/traceback.py13
1 files changed, 9 insertions, 4 deletions
diff --git a/Lib/traceback.py b/Lib/traceback.py
index 8d518728fa1..c43c4720ae5 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -586,12 +586,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
@@ -601,9 +604,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
@@ -1044,7 +1049,7 @@ def _compute_suggestion_error(exc_value, tb, wrong_name):
self = frame.f_locals['self']
if hasattr(self, wrong_name):
return f"self.{wrong_name}"
-
+
# Compute closest match
if len(d) > _MAX_CANDIDATE_ITEMS: