https://github.com/python/cpython/commit/99ab0d49e205cc3977205cc3b98d0b0224c1e642
commit: 99ab0d49e205cc3977205cc3b98d0b0224c1e642
branch: 3.11
author: Irit Katriel <1055913+iritkatr...@users.noreply.github.com>
committer: iritkatriel <1055913+iritkatr...@users.noreply.github.com>
date: 2024-02-28T19:12:16Z
summary:

[3.11] gh-116034: fix location info on the error of a failed assertion (#116052)

files:
A Misc/NEWS.d/next/Core and 
Builtins/2024-02-28-17-25-19.gh-issue-116034.-Uu9tf.rst
M Lib/test/test_traceback.py
M Python/compile.c

diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py
index a857d61e9b4a5f..16971a555af1a4 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -636,6 +636,23 @@ def f_with_binary_operator():
         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-17-25-19.gh-issue-116034.-Uu9tf.rst b/Misc/NEWS.d/next/Core 
and Builtins/2024-02-28-17-25-19.gh-issue-116034.-Uu9tf.rst
new file mode 100644
index 00000000000000..c711b63605d1a1
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and 
Builtins/2024-02-28-17-25-19.gh-issue-116034.-Uu9tf.rst 
@@ -0,0 +1 @@
+Fix location of the error on a failed assertion.
diff --git a/Python/compile.c b/Python/compile.c
index 558df3fca653ea..16bc0ed013c9f1 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -4050,6 +4050,7 @@ compiler_assert(struct compiler *c, stmt_ty s)
         ADDOP_I(c, PRECALL, 0);
         ADDOP_I(c, CALL, 0);
     }
+    SET_LOC(c, s->v.Assert.test);
     ADDOP_I(c, RAISE_VARARGS, 1);
     compiler_use_next_block(c, end);
     return 1;

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to