[3.11] gh-106922: Fix error location for constructs with spaces and parentheses (GH-108959) by miss-islington · Pull Request #109148 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Lib/test/test_traceback.py
16 changes: 13 additions & 3 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,11 +603,21 @@ def _extract_caret_anchors_from_line_segment(segment):
and not operator_str[operator_offset + 1].isspace()
):
right_anchor += 1

while left_anchor < len(segment) and ((ch := segment[left_anchor]).isspace() or ch in ")#"):
left_anchor += 1
right_anchor += 1
return _Anchors(normalize(left_anchor), normalize(right_anchor))
case ast.Subscript():
subscript_start = normalize(expr.value.end_col_offset)
subscript_end = normalize(expr.slice.end_col_offset + 1)
return _Anchors(subscript_start, subscript_end)
left_anchor = normalize(expr.value.end_col_offset)
right_anchor = normalize(expr.slice.end_col_offset + 1)
while left_anchor < len(segment) and ((ch := segment[left_anchor]).isspace() or ch != "["):
left_anchor += 1
while right_anchor < len(segment) and ((ch := segment[right_anchor]).isspace() or ch != "]"):
right_anchor += 1
if right_anchor < len(segment):
right_anchor += 1
return _Anchors(left_anchor, right_anchor)

return None

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix caret placement for error locations for subscript and binary operations
that involve non-semantic parentheses and spaces. Patch by Pablo Galindo
17 changes: 17 additions & 0 deletions Python/traceback.c