From f022b2803003a48eed7a68b3a305c37193e42ef9 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Wed, 28 Feb 2024 15:39:18 +0000 Subject: [PATCH 1/4] gh-116034: fix location info on the error of a failed assertion --- Lib/test/test_traceback.py | 17 +++++++++++++++++ .../2024-02-28-15-38-49.bpo-116034.-Uu9tf.rst | 1 + Python/compile.c | 1 + 3 files changed, 19 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-02-28-15-38-49.bpo-116034.-Uu9tf.rst 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-15-38-49.bpo-116034.-Uu9tf.rst b/Misc/NEWS.d/next/Core and Builtins/2024-02-28-15-38-49.bpo-116034.-Uu9tf.rst new file mode 100644 index 00000000000000..c711b63605d1a1 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-02-28-15-38-49.bpo-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; From d00e3b79ebcf6fc70dd3a4a0bb52eb2801c5c9de Mon Sep 17 00:00:00 2001 From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Date: Wed, 28 Feb 2024 17:24:07 +0000 Subject: [PATCH 2/4] Rename 2024-02-28-15-38-49.bpo-116034.-Uu9tf.rst to 2024-02-28-15-38-49.gh-116034.-Uu9tf.rst --- ...116034.-Uu9tf.rst => 2024-02-28-15-38-49.gh-116034.-Uu9tf.rst} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Misc/NEWS.d/next/Core and Builtins/{2024-02-28-15-38-49.bpo-116034.-Uu9tf.rst => 2024-02-28-15-38-49.gh-116034.-Uu9tf.rst} (100%) diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-02-28-15-38-49.bpo-116034.-Uu9tf.rst b/Misc/NEWS.d/next/Core and Builtins/2024-02-28-15-38-49.gh-116034.-Uu9tf.rst similarity index 100% rename from Misc/NEWS.d/next/Core and Builtins/2024-02-28-15-38-49.bpo-116034.-Uu9tf.rst rename to Misc/NEWS.d/next/Core and Builtins/2024-02-28-15-38-49.gh-116034.-Uu9tf.rst From eb3a92ea6715401d8477f1a00061966c8a803ecb Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 28 Feb 2024 17:25:23 +0000 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2024-02-28-17-25-19.gh-issue-116034.-Uu9tf.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-02-28-17-25-19.gh-issue-116034.-Uu9tf.rst 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. From d1126ed6fbdf728274c6046c9f3d416288a4cbcd Mon Sep 17 00:00:00 2001 From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Date: Wed, 28 Feb 2024 17:25:58 +0000 Subject: [PATCH 4/4] Delete Misc/NEWS.d/next/Core and Builtins/2024-02-28-15-38-49.gh-116034.-Uu9tf.rst --- .../Core and Builtins/2024-02-28-15-38-49.gh-116034.-Uu9tf.rst | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-02-28-15-38-49.gh-116034.-Uu9tf.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-02-28-15-38-49.gh-116034.-Uu9tf.rst b/Misc/NEWS.d/next/Core and Builtins/2024-02-28-15-38-49.gh-116034.-Uu9tf.rst deleted file mode 100644 index c711b63605d1a1..00000000000000 --- a/Misc/NEWS.d/next/Core and Builtins/2024-02-28-15-38-49.gh-116034.-Uu9tf.rst +++ /dev/null @@ -1 +0,0 @@ -Fix location of the error on a failed assertion.