mirror of
https://github.com/python/cpython.git
synced 2025-08-30 13:38:43 +00:00
gh-98398: Fix source locations for 'assert' bytecode (GH-98405)
This commit is contained in:
parent
e4ec8de6fa
commit
9be05df399
3 changed files with 28 additions and 9 deletions
|
@ -1215,7 +1215,6 @@ if (a or
|
||||||
d > 0)):
|
d > 0)):
|
||||||
x = 42
|
x = 42
|
||||||
"""
|
"""
|
||||||
|
|
||||||
compiled_code, _ = self.check_positions_against_ast(snippet)
|
compiled_code, _ = self.check_positions_against_ast(snippet)
|
||||||
# jump if a is true:
|
# jump if a is true:
|
||||||
self.assertOpcodeSourcePositionIs(compiled_code, 'POP_JUMP_IF_TRUE',
|
self.assertOpcodeSourcePositionIs(compiled_code, 'POP_JUMP_IF_TRUE',
|
||||||
|
@ -1233,6 +1232,23 @@ if (a or
|
||||||
self.assertOpcodeSourcePositionIs(compiled_code, 'POP_JUMP_IF_TRUE',
|
self.assertOpcodeSourcePositionIs(compiled_code, 'POP_JUMP_IF_TRUE',
|
||||||
line=4, end_line=4, column=8, end_column=13, occurrence=2)
|
line=4, end_line=4, column=8, end_column=13, occurrence=2)
|
||||||
|
|
||||||
|
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):
|
def test_very_long_line_end_offset(self):
|
||||||
# Make sure we get the correct column offset for offsets
|
# Make sure we get the correct column offset for offsets
|
||||||
# too large to store in a byte.
|
# too large to store in a byte.
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix source location of 'assert' bytecodes.
|
|
@ -3960,7 +3960,6 @@ compiler_from_import(struct compiler *c, stmt_ty s)
|
||||||
static int
|
static int
|
||||||
compiler_assert(struct compiler *c, stmt_ty s)
|
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 */
|
/* Always emit a warning if the test is a non-zero length tuple */
|
||||||
if ((s->v.Assert.test->kind == Tuple_kind &&
|
if ((s->v.Assert.test->kind == Tuple_kind &&
|
||||||
asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) ||
|
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_Check(s->v.Assert.test->v.Constant.value) &&
|
||||||
PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0))
|
PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0))
|
||||||
{
|
{
|
||||||
if (!compiler_warn(c, loc, "assertion is always true, "
|
if (!compiler_warn(c, LOC(s), "assertion is always true, "
|
||||||
"perhaps remove parentheses?"))
|
"perhaps remove parentheses?"))
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (c->c_optimize)
|
if (c->c_optimize) {
|
||||||
return 1;
|
return 1;
|
||||||
|
}
|
||||||
NEW_JUMP_TARGET_LABEL(c, end);
|
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;
|
return 0;
|
||||||
ADDOP(c, loc, LOAD_ASSERTION_ERROR);
|
}
|
||||||
|
ADDOP(c, LOC(s), LOAD_ASSERTION_ERROR);
|
||||||
if (s->v.Assert.msg) {
|
if (s->v.Assert.msg) {
|
||||||
VISIT(c, expr, 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);
|
USE_LABEL(c, end);
|
||||||
return 1;
|
return 1;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue