Skip to content

Commit 919ad53

Browse files
authored
bpo-43950: make BinOp specializations more reliable (GH-27126)
1 parent 074e765 commit 919ad53

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

Lib/test/test_traceback.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,44 @@ def test_traceback_specialization_with_syntax_error(self):
485485
)
486486
self.assertEqual(result_lines, expected_error.splitlines())
487487

488+
def assertSpecialized(self, func, expected_specialization):
489+
result_lines = self.get_exception(func)
490+
specialization_line = result_lines[-1]
491+
self.assertEqual(specialization_line.lstrip(), expected_specialization)
492+
493+
def test_specialization_variations(self):
494+
self.assertSpecialized(lambda: 1/0,
495+
"~^~")
496+
self.assertSpecialized(lambda: 1/0/3,
497+
"~^~")
498+
self.assertSpecialized(lambda: 1 / 0,
499+
"~~^~~")
500+
self.assertSpecialized(lambda: 1 / 0 / 3,
501+
"~~^~~")
502+
self.assertSpecialized(lambda: 1/ 0,
503+
"~^~~")
504+
self.assertSpecialized(lambda: 1/ 0/3,
505+
"~^~~")
506+
self.assertSpecialized(lambda: 1 / 0,
507+
"~~~~~^~~~")
508+
self.assertSpecialized(lambda: 1 / 0 / 5,
509+
"~~~~~^~~~")
510+
self.assertSpecialized(lambda: 1 /0,
511+
"~~^~")
512+
self.assertSpecialized(lambda: 1//0,
513+
"~^^~")
514+
self.assertSpecialized(lambda: 1//0//4,
515+
"~^^~")
516+
self.assertSpecialized(lambda: 1 // 0,
517+
"~~^^~~")
518+
self.assertSpecialized(lambda: 1 // 0 // 4,
519+
"~~^^~~")
520+
self.assertSpecialized(lambda: 1 //0,
521+
"~~^^~")
522+
self.assertSpecialized(lambda: 1// 0,
523+
"~^^~~")
524+
525+
488526
@cpython_only
489527
@requires_debug_ranges()
490528
class CPythonTracebackErrorCaretTests(TracebackErrorLocationCaretTests):

Lib/traceback.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ def format(self):
496496

497497
try:
498498
anchors = _extract_caret_anchors_from_line_segment(
499-
frame._original_line[colno - 1:end_colno]
499+
frame._original_line[colno - 1:end_colno - 1]
500500
)
501501
except Exception:
502502
anchors = None

Python/traceback.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ extract_anchors_from_expr(const char *segment_str, expr_ty expr, Py_ssize_t *lef
543543
case BinOp_kind: {
544544
expr_ty left = expr->v.BinOp.left;
545545
expr_ty right = expr->v.BinOp.right;
546-
for (int i = left->end_col_offset + 1; i < right->col_offset; i++) {
546+
for (int i = left->end_col_offset; i < right->col_offset; i++) {
547547
if (IS_WHITESPACE(segment_str[i])) {
548548
continue;
549549
}

0 commit comments

Comments
 (0)