]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-116034: fix location info on the error of a failed assertion (#116054)
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>
Thu, 29 Feb 2024 12:36:54 +0000 (12:36 +0000)
committerGitHub <noreply@github.com>
Thu, 29 Feb 2024 12:36:54 +0000 (12:36 +0000)
Lib/test/test_compile.py
Lib/test/test_traceback.py
Misc/NEWS.d/next/Core and Builtins/2024-02-28-16-42-17.gh-issue-116034.-Uu9tf.rst [new file with mode: 0644]
Python/compile.c

index 815800f4e2f530aa94803c68196171b59e9da8ef..6ed7fe2b06597cd3c81702db02cf53c4c8c91d9e 100644 (file)
@@ -1412,7 +1412,7 @@ class TestSourcePositions(unittest.TestCase):
         self.assertOpcodeSourcePositionIs(compiled_code, 'CALL',
             line=1, end_line=3, column=0, end_column=30, occurrence=1)
         self.assertOpcodeSourcePositionIs(compiled_code, 'RAISE_VARARGS',
-            line=1, end_line=3, column=0, end_column=30, occurrence=1)
+            line=1, end_line=3, column=8, end_column=16, occurrence=1)
 
     def test_multiline_generator_expression(self):
         snippet = textwrap.dedent("""\
index 61eb0a7ef93d7f5d4c8b9f8b949e48153260cd3a..d12b559cf076d6b0b20873ccdd05771ec53dd5fb 100644 (file)
@@ -666,6 +666,23 @@ class TracebackErrorLocationCaretTestBase:
         result_lines = self.get_exception(f_with_binary_operator)
         self.assertEqual(result_lines, expected_error.splitlines())
 
+    def test_caret_for_failed_assertion(self):
+        def f_assert():
+            test = 3
+            assert test == 1 and test == 2, "Bug found?"
+
+        lineno_f = f_assert.__code__.co_firstlineno
+        expected_error = (
+            'Traceback (most recent call last):\n'
+            f'  File "{__file__}", line {self.callable_line}, in get_exception\n'
+            '    callable()\n'
+            f'  File "{__file__}", line {lineno_f+2}, in f_assert\n'
+            '    assert test == 1 and test == 2, "Bug found?"\n'
+            '           ^^^^^^^^^^^^^^^^^^^^^^^\n'
+        )
+        result_lines = self.get_exception(f_assert)
+        self.assertEqual(result_lines, expected_error.splitlines())
+
     def test_traceback_specialization_with_syntax_error(self):
         bytecode = compile("1 / 0 / 1 / 2\n", TESTFN, "exec")
 
diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-02-28-16-42-17.gh-issue-116034.-Uu9tf.rst b/Misc/NEWS.d/next/Core and Builtins/2024-02-28-16-42-17.gh-issue-116034.-Uu9tf.rst
new file mode 100644 (file)
index 0000000..c711b63
--- /dev/null
@@ -0,0 +1 @@
+Fix location of the error on a failed assertion.
index ddd7b5c795b9ef486a07e9ec839ea949957a6525..a871e9c417517f766abb92e3dac4bd67d7b4a63b 100644 (file)
@@ -3883,7 +3883,7 @@ compiler_assert(struct compiler *c, stmt_ty s)
         VISIT(c, expr, s->v.Assert.msg);
         ADDOP_I(c, LOC(s), CALL, 0);
     }
-    ADDOP_I(c, LOC(s), RAISE_VARARGS, 1);
+    ADDOP_I(c, LOC(s->v.Assert.test), RAISE_VARARGS, 1);
 
     USE_LABEL(c, end);
     return SUCCESS;