From 2c5b6e4980db3812a0604c740ea456253fe8a855 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Tue, 18 Oct 2022 16:57:43 +0100 Subject: [PATCH 1/2] gh-98398: Fix source locations for 'assert' bytecode --- Lib/test/test_compile.py | 18 ++++++++++++++++++ Python/compile.c | 18 ++++++++++-------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index 8bf8470ff16feb..05dc5ae4a28642 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -1208,6 +1208,24 @@ def test_multiline_expression(self): self.assertOpcodeSourcePositionIs(compiled_code, 'CALL', line=1, end_line=3, column=0, end_column=1) + def test_multiline_assert(self): + snippet = """\ +assert (a > 0 and + bb > 0 and + ccc == 4), "error msg" +""" + + compiled_code, _ = self.check_positions_against_ast(snippet) + self.assertOpcodeSourcePositionIs(compiled_code, 'LOAD_ASSERTION_ERROR', + line=1, end_line=3, column=0, end_column=30, occurrence=1) + # The "error msg": + self.assertOpcodeSourcePositionIs(compiled_code, 'LOAD_CONST', + line=3, end_line=3, column=19, end_column=30, occurrence=4) + 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) + def test_very_long_line_end_offset(self): # Make sure we get the correct column offset for offsets # too large to store in a byte. diff --git a/Python/compile.c b/Python/compile.c index 5fbf6fe10d132e..cbdf487821b6f3 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -3960,7 +3960,6 @@ compiler_from_import(struct compiler *c, stmt_ty s) static int compiler_assert(struct compiler *c, stmt_ty s) { - location loc = LOC(s); /* Always emit a warning if the test is a non-zero length tuple */ if ((s->v.Assert.test->kind == Tuple_kind && asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) || @@ -3968,23 +3967,26 @@ compiler_assert(struct compiler *c, stmt_ty s) PyTuple_Check(s->v.Assert.test->v.Constant.value) && PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0)) { - if (!compiler_warn(c, loc, "assertion is always true, " - "perhaps remove parentheses?")) + if (!compiler_warn(c, LOC(s), "assertion is always true, " + "perhaps remove parentheses?")) { return 0; } } - if (c->c_optimize) + if (c->c_optimize) { return 1; + } NEW_JUMP_TARGET_LABEL(c, end); - if (!compiler_jump_if(c, &loc, s->v.Assert.test, end, 1)) + location loc = LOC(s); + if (!compiler_jump_if(c, &loc, s->v.Assert.test, end, 1)) { return 0; - ADDOP(c, loc, LOAD_ASSERTION_ERROR); + } + ADDOP(c, LOC(s), LOAD_ASSERTION_ERROR); if (s->v.Assert.msg) { VISIT(c, expr, s->v.Assert.msg); - ADDOP_I(c, loc, CALL, 0); + ADDOP_I(c, LOC(s), CALL, 0); } - ADDOP_I(c, loc, RAISE_VARARGS, 1); + ADDOP_I(c, LOC(s), RAISE_VARARGS, 1); USE_LABEL(c, end); return 1; From a93d9372431e38cf4d4e2eee4753c0afa0f940e6 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 18 Oct 2022 16:17:47 +0000 Subject: [PATCH 2/2] =?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 --- .../2022-10-18-16-17-44.gh-issue-98398.x4rYK_.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-10-18-16-17-44.gh-issue-98398.x4rYK_.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-10-18-16-17-44.gh-issue-98398.x4rYK_.rst b/Misc/NEWS.d/next/Core and Builtins/2022-10-18-16-17-44.gh-issue-98398.x4rYK_.rst new file mode 100644 index 00000000000000..35d33c90a6902b --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-10-18-16-17-44.gh-issue-98398.x4rYK_.rst @@ -0,0 +1 @@ +Fix source location of 'assert' bytecodes.