Skip to content

Commit 9be05df

Browse files
authored
gh-98398: Fix source locations for 'assert' bytecode (GH-98405)
1 parent e4ec8de commit 9be05df

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

Lib/test/test_compile.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -1215,7 +1215,6 @@ def test_multiline_boolean_expression(self):
12151215
d > 0)):
12161216
x = 42
12171217
"""
1218-
12191218
compiled_code, _ = self.check_positions_against_ast(snippet)
12201219
# jump if a is true:
12211220
self.assertOpcodeSourcePositionIs(compiled_code, 'POP_JUMP_IF_TRUE',
@@ -1233,6 +1232,23 @@ def test_multiline_boolean_expression(self):
12331232
self.assertOpcodeSourcePositionIs(compiled_code, 'POP_JUMP_IF_TRUE',
12341233
line=4, end_line=4, column=8, end_column=13, occurrence=2)
12351234

1235+
def test_multiline_assert(self):
1236+
snippet = """\
1237+
assert (a > 0 and
1238+
bb > 0 and
1239+
ccc == 4), "error msg"
1240+
"""
1241+
compiled_code, _ = self.check_positions_against_ast(snippet)
1242+
self.assertOpcodeSourcePositionIs(compiled_code, 'LOAD_ASSERTION_ERROR',
1243+
line=1, end_line=3, column=0, end_column=30, occurrence=1)
1244+
# The "error msg":
1245+
self.assertOpcodeSourcePositionIs(compiled_code, 'LOAD_CONST',
1246+
line=3, end_line=3, column=19, end_column=30, occurrence=4)
1247+
self.assertOpcodeSourcePositionIs(compiled_code, 'CALL',
1248+
line=1, end_line=3, column=0, end_column=30, occurrence=1)
1249+
self.assertOpcodeSourcePositionIs(compiled_code, 'RAISE_VARARGS',
1250+
line=1, end_line=3, column=0, end_column=30, occurrence=1)
1251+
12361252
def test_very_long_line_end_offset(self):
12371253
# Make sure we get the correct column offset for offsets
12381254
# too large to store in a byte.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix source location of 'assert' bytecodes.

Python/compile.c

+10-8
Original file line numberDiff line numberDiff line change
@@ -3960,31 +3960,33 @@ compiler_from_import(struct compiler *c, stmt_ty s)
39603960
static int
39613961
compiler_assert(struct compiler *c, stmt_ty s)
39623962
{
3963-
location loc = LOC(s);
39643963
/* Always emit a warning if the test is a non-zero length tuple */
39653964
if ((s->v.Assert.test->kind == Tuple_kind &&
39663965
asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) ||
39673966
(s->v.Assert.test->kind == Constant_kind &&
39683967
PyTuple_Check(s->v.Assert.test->v.Constant.value) &&
39693968
PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0))
39703969
{
3971-
if (!compiler_warn(c, loc, "assertion is always true, "
3972-
"perhaps remove parentheses?"))
3970+
if (!compiler_warn(c, LOC(s), "assertion is always true, "
3971+
"perhaps remove parentheses?"))
39733972
{
39743973
return 0;
39753974
}
39763975
}
3977-
if (c->c_optimize)
3976+
if (c->c_optimize) {
39783977
return 1;
3978+
}
39793979
NEW_JUMP_TARGET_LABEL(c, end);
3980-
if (!compiler_jump_if(c, &loc, s->v.Assert.test, end, 1))
3980+
location loc = LOC(s);
3981+
if (!compiler_jump_if(c, &loc, s->v.Assert.test, end, 1)) {
39813982
return 0;
3982-
ADDOP(c, loc, LOAD_ASSERTION_ERROR);
3983+
}
3984+
ADDOP(c, LOC(s), LOAD_ASSERTION_ERROR);
39833985
if (s->v.Assert.msg) {
39843986
VISIT(c, expr, s->v.Assert.msg);
3985-
ADDOP_I(c, loc, CALL, 0);
3987+
ADDOP_I(c, LOC(s), CALL, 0);
39863988
}
3987-
ADDOP_I(c, loc, RAISE_VARARGS, 1);
3989+
ADDOP_I(c, LOC(s), RAISE_VARARGS, 1);
39883990

39893991
USE_LABEL(c, end);
39903992
return 1;

0 commit comments

Comments
 (0)