]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43950: make BinOp specializations more reliable (GH-27126)
authorBatuhan Taskaya <batuhan@python.org>
Thu, 15 Jul 2021 23:38:11 +0000 (02:38 +0300)
committerGitHub <noreply@github.com>
Thu, 15 Jul 2021 23:38:11 +0000 (00:38 +0100)
Lib/test/test_traceback.py
Lib/traceback.py
Python/traceback.c

index 8baf38c1afd5d62fc2a878ed73f1dcd257682ec1..402f773814ec43ead22f4714a3a2d1df286f62c8 100644 (file)
@@ -485,6 +485,44 @@ class TracebackErrorLocationCaretTests(unittest.TestCase):
         )
         self.assertEqual(result_lines, expected_error.splitlines())
 
+    def assertSpecialized(self, func, expected_specialization):
+        result_lines = self.get_exception(func)
+        specialization_line = result_lines[-1]
+        self.assertEqual(specialization_line.lstrip(), expected_specialization)
+
+    def test_specialization_variations(self):
+        self.assertSpecialized(lambda: 1/0,
+                                      "~^~")
+        self.assertSpecialized(lambda: 1/0/3,
+                                      "~^~")
+        self.assertSpecialized(lambda: 1 / 0,
+                                      "~~^~~")
+        self.assertSpecialized(lambda: 1 / 0 / 3,
+                                      "~~^~~")
+        self.assertSpecialized(lambda: 1/ 0,
+                                      "~^~~")
+        self.assertSpecialized(lambda: 1/ 0/3,
+                                      "~^~~")
+        self.assertSpecialized(lambda: 1    /  0,
+                                      "~~~~~^~~~")
+        self.assertSpecialized(lambda: 1    /  0   / 5,
+                                      "~~~~~^~~~")
+        self.assertSpecialized(lambda: 1 /0,
+                                      "~~^~")
+        self.assertSpecialized(lambda: 1//0,
+                                      "~^^~")
+        self.assertSpecialized(lambda: 1//0//4,
+                                      "~^^~")
+        self.assertSpecialized(lambda: 1 // 0,
+                                      "~~^^~~")
+        self.assertSpecialized(lambda: 1 // 0 // 4,
+                                      "~~^^~~")
+        self.assertSpecialized(lambda: 1 //0,
+                                      "~~^^~")
+        self.assertSpecialized(lambda: 1// 0,
+                                      "~^^~~")
+
+
 @cpython_only
 @requires_debug_ranges()
 class CPythonTracebackErrorCaretTests(TracebackErrorLocationCaretTests):
index ec5e20d431feb8900053f59ea4fad368e6d08984..40d736af56dd71c4ae4b963e36e35ed18db85c55 100644 (file)
@@ -496,7 +496,7 @@ class StackSummary(list):
 
                     try:
                         anchors = _extract_caret_anchors_from_line_segment(
-                            frame._original_line[colno - 1:end_colno]
+                            frame._original_line[colno - 1:end_colno - 1]
                         )
                     except Exception:
                         anchors = None
index 199d3ea7596bf81161c93d05cfa95a1393d3ccde..643096c81fc8f50861cc1b594706adc1fde68288 100644 (file)
@@ -543,7 +543,7 @@ extract_anchors_from_expr(const char *segment_str, expr_ty expr, Py_ssize_t *lef
         case BinOp_kind: {
             expr_ty left = expr->v.BinOp.left;
             expr_ty right = expr->v.BinOp.right;
-            for (int i = left->end_col_offset + 1; i < right->col_offset; i++) {
+            for (int i = left->end_col_offset; i < right->col_offset; i++) {
                 if (IS_WHITESPACE(segment_str[i])) {
                     continue;
                 }