mirror of
https://github.com/python/cpython.git
synced 2024-11-27 11:55:13 +08:00
gh-98762: Fix locations of match sub-patterns (GH-98775)
This commit is contained in:
parent
73a921b070
commit
fb844e1931
@ -1164,7 +1164,7 @@ class TestSourcePositions(unittest.TestCase):
|
||||
class SourceOffsetVisitor(ast.NodeVisitor):
|
||||
def generic_visit(self, node):
|
||||
super().generic_visit(node)
|
||||
if not isinstance(node, ast.expr) and not isinstance(node, ast.stmt):
|
||||
if not isinstance(node, (ast.expr, ast.stmt, ast.pattern)):
|
||||
return
|
||||
lines.add(node.lineno)
|
||||
end_lines.add(node.end_lineno)
|
||||
@ -1442,6 +1442,100 @@ async def f():
|
||||
self.assertOpcodeSourcePositionIs(compiled_code, 'RETURN_VALUE',
|
||||
line=2, end_line=7, column=4, end_column=36, occurrence=1)
|
||||
|
||||
def test_matchcase_sequence(self):
|
||||
snippet = """\
|
||||
match x:
|
||||
case a, b:
|
||||
pass
|
||||
"""
|
||||
compiled_code, _ = self.check_positions_against_ast(snippet)
|
||||
self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_SEQUENCE',
|
||||
line=2, end_line=2, column=9, end_column=13, occurrence=1)
|
||||
self.assertOpcodeSourcePositionIs(compiled_code, 'UNPACK_SEQUENCE',
|
||||
line=2, end_line=2, column=9, end_column=13, occurrence=1)
|
||||
self.assertOpcodeSourcePositionIs(compiled_code, 'STORE_NAME',
|
||||
line=2, end_line=2, column=9, end_column=13, occurrence=1)
|
||||
self.assertOpcodeSourcePositionIs(compiled_code, 'STORE_NAME',
|
||||
line=2, end_line=2, column=9, end_column=13, occurrence=2)
|
||||
|
||||
def test_matchcase_sequence_wildcard(self):
|
||||
snippet = """\
|
||||
match x:
|
||||
case a, *b, c:
|
||||
pass
|
||||
"""
|
||||
compiled_code, _ = self.check_positions_against_ast(snippet)
|
||||
self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_SEQUENCE',
|
||||
line=2, end_line=2, column=9, end_column=17, occurrence=1)
|
||||
self.assertOpcodeSourcePositionIs(compiled_code, 'UNPACK_EX',
|
||||
line=2, end_line=2, column=9, end_column=17, occurrence=1)
|
||||
self.assertOpcodeSourcePositionIs(compiled_code, 'STORE_NAME',
|
||||
line=2, end_line=2, column=9, end_column=17, occurrence=1)
|
||||
self.assertOpcodeSourcePositionIs(compiled_code, 'STORE_NAME',
|
||||
line=2, end_line=2, column=9, end_column=17, occurrence=2)
|
||||
self.assertOpcodeSourcePositionIs(compiled_code, 'STORE_NAME',
|
||||
line=2, end_line=2, column=9, end_column=17, occurrence=3)
|
||||
|
||||
def test_matchcase_mapping(self):
|
||||
snippet = """\
|
||||
match x:
|
||||
case {"a" : a, "b": b}:
|
||||
pass
|
||||
"""
|
||||
compiled_code, _ = self.check_positions_against_ast(snippet)
|
||||
self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_MAPPING',
|
||||
line=2, end_line=2, column=9, end_column=26, occurrence=1)
|
||||
self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_KEYS',
|
||||
line=2, end_line=2, column=9, end_column=26, occurrence=1)
|
||||
self.assertOpcodeSourcePositionIs(compiled_code, 'STORE_NAME',
|
||||
line=2, end_line=2, column=9, end_column=26, occurrence=1)
|
||||
self.assertOpcodeSourcePositionIs(compiled_code, 'STORE_NAME',
|
||||
line=2, end_line=2, column=9, end_column=26, occurrence=2)
|
||||
|
||||
def test_matchcase_mapping_wildcard(self):
|
||||
snippet = """\
|
||||
match x:
|
||||
case {"a" : a, "b": b, **c}:
|
||||
pass
|
||||
"""
|
||||
compiled_code, _ = self.check_positions_against_ast(snippet)
|
||||
self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_MAPPING',
|
||||
line=2, end_line=2, column=9, end_column=31, occurrence=1)
|
||||
self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_KEYS',
|
||||
line=2, end_line=2, column=9, end_column=31, occurrence=1)
|
||||
self.assertOpcodeSourcePositionIs(compiled_code, 'STORE_NAME',
|
||||
line=2, end_line=2, column=9, end_column=31, occurrence=1)
|
||||
self.assertOpcodeSourcePositionIs(compiled_code, 'STORE_NAME',
|
||||
line=2, end_line=2, column=9, end_column=31, occurrence=2)
|
||||
|
||||
def test_matchcase_class(self):
|
||||
snippet = """\
|
||||
match x:
|
||||
case C(a, b):
|
||||
pass
|
||||
"""
|
||||
compiled_code, _ = self.check_positions_against_ast(snippet)
|
||||
self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_CLASS',
|
||||
line=2, end_line=2, column=9, end_column=16, occurrence=1)
|
||||
self.assertOpcodeSourcePositionIs(compiled_code, 'UNPACK_SEQUENCE',
|
||||
line=2, end_line=2, column=9, end_column=16, occurrence=1)
|
||||
self.assertOpcodeSourcePositionIs(compiled_code, 'STORE_NAME',
|
||||
line=2, end_line=2, column=9, end_column=16, occurrence=1)
|
||||
self.assertOpcodeSourcePositionIs(compiled_code, 'STORE_NAME',
|
||||
line=2, end_line=2, column=9, end_column=16, occurrence=2)
|
||||
|
||||
def test_matchcase_or(self):
|
||||
snippet = """\
|
||||
match x:
|
||||
case C(1) | C(2):
|
||||
pass
|
||||
"""
|
||||
compiled_code, _ = self.check_positions_against_ast(snippet)
|
||||
self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_CLASS',
|
||||
line=2, end_line=2, column=9, end_column=13, occurrence=1)
|
||||
self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_CLASS',
|
||||
line=2, end_line=2, column=16, end_column=20, occurrence=2)
|
||||
|
||||
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.
|
||||
|
@ -0,0 +1 @@
|
||||
Fix source locations of :keyword:`match` sub-patterns.
|
245
Python/compile.c
245
Python/compile.c
@ -507,10 +507,9 @@ static int compiler_async_comprehension_generator(
|
||||
int depth,
|
||||
expr_ty elt, expr_ty val, int type);
|
||||
|
||||
static int compiler_pattern(struct compiler *, location *,
|
||||
pattern_ty, pattern_context *);
|
||||
static int compiler_pattern(struct compiler *, pattern_ty, pattern_context *);
|
||||
static int compiler_match(struct compiler *, stmt_ty);
|
||||
static int compiler_pattern_subpattern(struct compiler *, location *,
|
||||
static int compiler_pattern_subpattern(struct compiler *,
|
||||
pattern_ty, pattern_context *);
|
||||
|
||||
static void remove_redundant_nops(basicblock *bb);
|
||||
@ -6428,11 +6427,11 @@ pattern_unpack_helper(struct compiler *c, location loc,
|
||||
}
|
||||
|
||||
static int
|
||||
pattern_helper_sequence_unpack(struct compiler *c, location *ploc,
|
||||
pattern_helper_sequence_unpack(struct compiler *c, location loc,
|
||||
asdl_pattern_seq *patterns, Py_ssize_t star,
|
||||
pattern_context *pc)
|
||||
{
|
||||
RETURN_IF_FALSE(pattern_unpack_helper(c, *ploc, patterns));
|
||||
RETURN_IF_FALSE(pattern_unpack_helper(c, loc, patterns));
|
||||
Py_ssize_t size = asdl_seq_LEN(patterns);
|
||||
// We've now got a bunch of new subjects on the stack. They need to remain
|
||||
// there after each subpattern match:
|
||||
@ -6441,7 +6440,7 @@ pattern_helper_sequence_unpack(struct compiler *c, location *ploc,
|
||||
// One less item to keep track of each time we loop through:
|
||||
pc->on_top--;
|
||||
pattern_ty pattern = asdl_seq_GET(patterns, i);
|
||||
RETURN_IF_FALSE(compiler_pattern_subpattern(c, ploc, pattern, pc));
|
||||
RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
@ -6450,7 +6449,7 @@ pattern_helper_sequence_unpack(struct compiler *c, location *ploc,
|
||||
// UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a
|
||||
// starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc.
|
||||
static int
|
||||
pattern_helper_sequence_subscr(struct compiler *c, location *ploc,
|
||||
pattern_helper_sequence_subscr(struct compiler *c, location loc,
|
||||
asdl_pattern_seq *patterns, Py_ssize_t star,
|
||||
pattern_context *pc)
|
||||
{
|
||||
@ -6466,41 +6465,40 @@ pattern_helper_sequence_subscr(struct compiler *c, location *ploc,
|
||||
assert(WILDCARD_STAR_CHECK(pattern));
|
||||
continue;
|
||||
}
|
||||
ADDOP_I(c, *ploc, COPY, 1);
|
||||
ADDOP_I(c, loc, COPY, 1);
|
||||
if (i < star) {
|
||||
ADDOP_LOAD_CONST_NEW(c, *ploc, PyLong_FromSsize_t(i));
|
||||
ADDOP_LOAD_CONST_NEW(c, loc, PyLong_FromSsize_t(i));
|
||||
}
|
||||
else {
|
||||
// The subject may not support negative indexing! Compute a
|
||||
// nonnegative index:
|
||||
ADDOP(c, *ploc, GET_LEN);
|
||||
ADDOP_LOAD_CONST_NEW(c, *ploc, PyLong_FromSsize_t(size - i));
|
||||
ADDOP_BINARY(c, *ploc, Sub);
|
||||
ADDOP(c, loc, GET_LEN);
|
||||
ADDOP_LOAD_CONST_NEW(c, loc, PyLong_FromSsize_t(size - i));
|
||||
ADDOP_BINARY(c, loc, Sub);
|
||||
}
|
||||
ADDOP(c, *ploc, BINARY_SUBSCR);
|
||||
RETURN_IF_FALSE(compiler_pattern_subpattern(c, ploc, pattern, pc));
|
||||
ADDOP(c, loc, BINARY_SUBSCR);
|
||||
RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
|
||||
}
|
||||
// Pop the subject, we're done with it:
|
||||
pc->on_top--;
|
||||
ADDOP(c, *ploc, POP_TOP);
|
||||
ADDOP(c, loc, POP_TOP);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Like compiler_pattern, but turn off checks for irrefutability.
|
||||
static int
|
||||
compiler_pattern_subpattern(struct compiler *c, location *ploc,
|
||||
compiler_pattern_subpattern(struct compiler *c,
|
||||
pattern_ty p, pattern_context *pc)
|
||||
{
|
||||
int allow_irrefutable = pc->allow_irrefutable;
|
||||
pc->allow_irrefutable = 1;
|
||||
RETURN_IF_FALSE(compiler_pattern(c, ploc, p, pc));
|
||||
RETURN_IF_FALSE(compiler_pattern(c, p, pc));
|
||||
pc->allow_irrefutable = allow_irrefutable;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
compiler_pattern_as(struct compiler *c, location *ploc,
|
||||
pattern_ty p, pattern_context *pc)
|
||||
compiler_pattern_as(struct compiler *c, pattern_ty p, pattern_context *pc)
|
||||
{
|
||||
assert(p->kind == MatchAs_kind);
|
||||
if (p->v.MatchAs.pattern == NULL) {
|
||||
@ -6508,20 +6506,20 @@ compiler_pattern_as(struct compiler *c, location *ploc,
|
||||
if (!pc->allow_irrefutable) {
|
||||
if (p->v.MatchAs.name) {
|
||||
const char *e = "name capture %R makes remaining patterns unreachable";
|
||||
return compiler_error(c, *ploc, e, p->v.MatchAs.name);
|
||||
return compiler_error(c, LOC(p), e, p->v.MatchAs.name);
|
||||
}
|
||||
const char *e = "wildcard makes remaining patterns unreachable";
|
||||
return compiler_error(c, *ploc, e);
|
||||
return compiler_error(c, LOC(p), e);
|
||||
}
|
||||
return pattern_helper_store_name(c, *ploc, p->v.MatchAs.name, pc);
|
||||
return pattern_helper_store_name(c, LOC(p), p->v.MatchAs.name, pc);
|
||||
}
|
||||
// Need to make a copy for (possibly) storing later:
|
||||
pc->on_top++;
|
||||
ADDOP_I(c, *ploc, COPY, 1);
|
||||
RETURN_IF_FALSE(compiler_pattern(c, ploc, p->v.MatchAs.pattern, pc));
|
||||
ADDOP_I(c, LOC(p), COPY, 1);
|
||||
RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc));
|
||||
// Success! Store it:
|
||||
pc->on_top--;
|
||||
RETURN_IF_FALSE(pattern_helper_store_name(c, *ploc, p->v.MatchAs.name, pc));
|
||||
RETURN_IF_FALSE(pattern_helper_store_name(c, LOC(p), p->v.MatchAs.name, pc));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -6529,8 +6527,7 @@ static int
|
||||
compiler_pattern_star(struct compiler *c, pattern_ty p, pattern_context *pc)
|
||||
{
|
||||
assert(p->kind == MatchStar_kind);
|
||||
location loc = LOC(p);
|
||||
RETURN_IF_FALSE(pattern_helper_store_name(c, loc, p->v.MatchStar.name, pc));
|
||||
RETURN_IF_FALSE(pattern_helper_store_name(c, LOC(p), p->v.MatchStar.name, pc));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -6559,8 +6556,7 @@ validate_kwd_attrs(struct compiler *c, asdl_identifier_seq *attrs, asdl_pattern_
|
||||
}
|
||||
|
||||
static int
|
||||
compiler_pattern_class(struct compiler *c, location *ploc,
|
||||
pattern_ty p, pattern_context *pc)
|
||||
compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc)
|
||||
{
|
||||
assert(p->kind == MatchClass_kind);
|
||||
asdl_pattern_seq *patterns = p->v.MatchClass.patterns;
|
||||
@ -6573,11 +6569,11 @@ compiler_pattern_class(struct compiler *c, location *ploc,
|
||||
// AST validator shouldn't let this happen, but if it does,
|
||||
// just fail, don't crash out of the interpreter
|
||||
const char * e = "kwd_attrs (%d) / kwd_patterns (%d) length mismatch in class pattern";
|
||||
return compiler_error(c, *ploc, e, nattrs, nkwd_patterns);
|
||||
return compiler_error(c, LOC(p), e, nattrs, nkwd_patterns);
|
||||
}
|
||||
if (INT_MAX < nargs || INT_MAX < nargs + nattrs - 1) {
|
||||
const char *e = "too many sub-patterns in class pattern %R";
|
||||
return compiler_error(c, *ploc, e, p->v.MatchClass.cls);
|
||||
return compiler_error(c, LOC(p), e, p->v.MatchClass.cls);
|
||||
}
|
||||
if (nattrs) {
|
||||
RETURN_IF_FALSE(!validate_kwd_attrs(c, kwd_attrs, kwd_patterns));
|
||||
@ -6590,15 +6586,15 @@ compiler_pattern_class(struct compiler *c, location *ploc,
|
||||
PyObject *name = asdl_seq_GET(kwd_attrs, i);
|
||||
PyTuple_SET_ITEM(attr_names, i, Py_NewRef(name));
|
||||
}
|
||||
ADDOP_LOAD_CONST_NEW(c, *ploc, attr_names);
|
||||
ADDOP_I(c, *ploc, MATCH_CLASS, nargs);
|
||||
ADDOP_I(c, *ploc, COPY, 1);
|
||||
ADDOP_LOAD_CONST(c, *ploc, Py_None);
|
||||
ADDOP_I(c, *ploc, IS_OP, 1);
|
||||
ADDOP_LOAD_CONST_NEW(c, LOC(p), attr_names);
|
||||
ADDOP_I(c, LOC(p), MATCH_CLASS, nargs);
|
||||
ADDOP_I(c, LOC(p), COPY, 1);
|
||||
ADDOP_LOAD_CONST(c, LOC(p), Py_None);
|
||||
ADDOP_I(c, LOC(p), IS_OP, 1);
|
||||
// TOS is now a tuple of (nargs + nattrs) attributes (or None):
|
||||
pc->on_top++;
|
||||
RETURN_IF_FALSE(jump_to_fail_pop(c, *ploc, pc, POP_JUMP_IF_FALSE));
|
||||
ADDOP_I(c, *ploc, UNPACK_SEQUENCE, nargs + nattrs);
|
||||
RETURN_IF_FALSE(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE));
|
||||
ADDOP_I(c, LOC(p), UNPACK_SEQUENCE, nargs + nattrs);
|
||||
pc->on_top += nargs + nattrs - 1;
|
||||
for (i = 0; i < nargs + nattrs; i++) {
|
||||
pc->on_top--;
|
||||
@ -6612,19 +6608,18 @@ compiler_pattern_class(struct compiler *c, location *ploc,
|
||||
pattern = asdl_seq_GET(kwd_patterns, i - nargs);
|
||||
}
|
||||
if (WILDCARD_CHECK(pattern)) {
|
||||
ADDOP(c, *ploc, POP_TOP);
|
||||
ADDOP(c, LOC(p), POP_TOP);
|
||||
continue;
|
||||
}
|
||||
*ploc = LOC(pattern);
|
||||
RETURN_IF_FALSE(compiler_pattern_subpattern(c, ploc, pattern, pc));
|
||||
RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
|
||||
}
|
||||
// Success! Pop the tuple of attributes:
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
compiler_pattern_mapping(struct compiler *c, location *ploc,
|
||||
pattern_ty p, pattern_context *pc)
|
||||
compiler_pattern_mapping(struct compiler *c, pattern_ty p,
|
||||
pattern_context *pc)
|
||||
{
|
||||
assert(p->kind == MatchMapping_kind);
|
||||
asdl_expr_seq *keys = p->v.MatchMapping.keys;
|
||||
@ -6635,29 +6630,29 @@ compiler_pattern_mapping(struct compiler *c, location *ploc,
|
||||
// AST validator shouldn't let this happen, but if it does,
|
||||
// just fail, don't crash out of the interpreter
|
||||
const char * e = "keys (%d) / patterns (%d) length mismatch in mapping pattern";
|
||||
return compiler_error(c, *ploc, e, size, npatterns);
|
||||
return compiler_error(c, LOC(p), e, size, npatterns);
|
||||
}
|
||||
// We have a double-star target if "rest" is set
|
||||
PyObject *star_target = p->v.MatchMapping.rest;
|
||||
// We need to keep the subject on top during the mapping and length checks:
|
||||
pc->on_top++;
|
||||
ADDOP(c, *ploc, MATCH_MAPPING);
|
||||
RETURN_IF_FALSE(jump_to_fail_pop(c, *ploc, pc, POP_JUMP_IF_FALSE));
|
||||
ADDOP(c, LOC(p), MATCH_MAPPING);
|
||||
RETURN_IF_FALSE(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE));
|
||||
if (!size && !star_target) {
|
||||
// If the pattern is just "{}", we're done! Pop the subject:
|
||||
pc->on_top--;
|
||||
ADDOP(c, *ploc, POP_TOP);
|
||||
ADDOP(c, LOC(p), POP_TOP);
|
||||
return 1;
|
||||
}
|
||||
if (size) {
|
||||
// If the pattern has any keys in it, perform a length check:
|
||||
ADDOP(c, *ploc, GET_LEN);
|
||||
ADDOP_LOAD_CONST_NEW(c, *ploc, PyLong_FromSsize_t(size));
|
||||
ADDOP_COMPARE(c, *ploc, GtE);
|
||||
RETURN_IF_FALSE(jump_to_fail_pop(c, *ploc, pc, POP_JUMP_IF_FALSE));
|
||||
ADDOP(c, LOC(p), GET_LEN);
|
||||
ADDOP_LOAD_CONST_NEW(c, LOC(p), PyLong_FromSsize_t(size));
|
||||
ADDOP_COMPARE(c, LOC(p), GtE);
|
||||
RETURN_IF_FALSE(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE));
|
||||
}
|
||||
if (INT_MAX < size - 1) {
|
||||
return compiler_error(c, *ploc, "too many sub-patterns in mapping pattern");
|
||||
return compiler_error(c, LOC(p), "too many sub-patterns in mapping pattern");
|
||||
}
|
||||
// Collect all of the keys into a tuple for MATCH_KEYS and
|
||||
// **rest. They can either be dotted names or literals:
|
||||
@ -6687,7 +6682,7 @@ compiler_pattern_mapping(struct compiler *c, location *ploc,
|
||||
}
|
||||
if (in_seen) {
|
||||
const char *e = "mapping pattern checks duplicate key (%R)";
|
||||
compiler_error(c, *ploc, e, key->v.Constant.value);
|
||||
compiler_error(c, LOC(p), e, key->v.Constant.value);
|
||||
goto error;
|
||||
}
|
||||
if (PySet_Add(seen, key->v.Constant.value)) {
|
||||
@ -6697,7 +6692,7 @@ compiler_pattern_mapping(struct compiler *c, location *ploc,
|
||||
|
||||
else if (key->kind != Attribute_kind) {
|
||||
const char *e = "mapping pattern keys may only match literals and attribute lookups";
|
||||
compiler_error(c, *ploc, e);
|
||||
compiler_error(c, LOC(p), e);
|
||||
goto error;
|
||||
}
|
||||
if (!compiler_visit_expr(c, key)) {
|
||||
@ -6708,22 +6703,22 @@ compiler_pattern_mapping(struct compiler *c, location *ploc,
|
||||
// all keys have been checked; there are no duplicates
|
||||
Py_DECREF(seen);
|
||||
|
||||
ADDOP_I(c, *ploc, BUILD_TUPLE, size);
|
||||
ADDOP(c, *ploc, MATCH_KEYS);
|
||||
ADDOP_I(c, LOC(p), BUILD_TUPLE, size);
|
||||
ADDOP(c, LOC(p), MATCH_KEYS);
|
||||
// There's now a tuple of keys and a tuple of values on top of the subject:
|
||||
pc->on_top += 2;
|
||||
ADDOP_I(c, *ploc, COPY, 1);
|
||||
ADDOP_LOAD_CONST(c, *ploc, Py_None);
|
||||
ADDOP_I(c, *ploc, IS_OP, 1);
|
||||
RETURN_IF_FALSE(jump_to_fail_pop(c, *ploc, pc, POP_JUMP_IF_FALSE));
|
||||
ADDOP_I(c, LOC(p), COPY, 1);
|
||||
ADDOP_LOAD_CONST(c, LOC(p), Py_None);
|
||||
ADDOP_I(c, LOC(p), IS_OP, 1);
|
||||
RETURN_IF_FALSE(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE));
|
||||
// So far so good. Use that tuple of values on the stack to match
|
||||
// sub-patterns against:
|
||||
ADDOP_I(c, *ploc, UNPACK_SEQUENCE, size);
|
||||
ADDOP_I(c, LOC(p), UNPACK_SEQUENCE, size);
|
||||
pc->on_top += size - 1;
|
||||
for (Py_ssize_t i = 0; i < size; i++) {
|
||||
pc->on_top--;
|
||||
pattern_ty pattern = asdl_seq_GET(patterns, i);
|
||||
RETURN_IF_FALSE(compiler_pattern_subpattern(c, ploc, pattern, pc));
|
||||
RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
|
||||
}
|
||||
// If we get this far, it's a match! Whatever happens next should consume
|
||||
// the tuple of keys and the subject:
|
||||
@ -6735,20 +6730,20 @@ compiler_pattern_mapping(struct compiler *c, location *ploc,
|
||||
// rest = dict(TOS1)
|
||||
// for key in TOS:
|
||||
// del rest[key]
|
||||
ADDOP_I(c, *ploc, BUILD_MAP, 0); // [subject, keys, empty]
|
||||
ADDOP_I(c, *ploc, SWAP, 3); // [empty, keys, subject]
|
||||
ADDOP_I(c, *ploc, DICT_UPDATE, 2); // [copy, keys]
|
||||
ADDOP_I(c, *ploc, UNPACK_SEQUENCE, size); // [copy, keys...]
|
||||
ADDOP_I(c, LOC(p), BUILD_MAP, 0); // [subject, keys, empty]
|
||||
ADDOP_I(c, LOC(p), SWAP, 3); // [empty, keys, subject]
|
||||
ADDOP_I(c, LOC(p), DICT_UPDATE, 2); // [copy, keys]
|
||||
ADDOP_I(c, LOC(p), UNPACK_SEQUENCE, size); // [copy, keys...]
|
||||
while (size) {
|
||||
ADDOP_I(c, *ploc, COPY, 1 + size--); // [copy, keys..., copy]
|
||||
ADDOP_I(c, *ploc, SWAP, 2); // [copy, keys..., copy, key]
|
||||
ADDOP(c, *ploc, DELETE_SUBSCR); // [copy, keys...]
|
||||
ADDOP_I(c, LOC(p), COPY, 1 + size--); // [copy, keys..., copy]
|
||||
ADDOP_I(c, LOC(p), SWAP, 2); // [copy, keys..., copy, key]
|
||||
ADDOP(c, LOC(p), DELETE_SUBSCR); // [copy, keys...]
|
||||
}
|
||||
RETURN_IF_FALSE(pattern_helper_store_name(c, *ploc, star_target, pc));
|
||||
RETURN_IF_FALSE(pattern_helper_store_name(c, LOC(p), star_target, pc));
|
||||
}
|
||||
else {
|
||||
ADDOP(c, *ploc, POP_TOP); // Tuple of keys.
|
||||
ADDOP(c, *ploc, POP_TOP); // Subject.
|
||||
ADDOP(c, LOC(p), POP_TOP); // Tuple of keys.
|
||||
ADDOP(c, LOC(p), POP_TOP); // Subject.
|
||||
}
|
||||
return 1;
|
||||
|
||||
@ -6758,8 +6753,7 @@ error:
|
||||
}
|
||||
|
||||
static int
|
||||
compiler_pattern_or(struct compiler *c, location *ploc,
|
||||
pattern_ty p, pattern_context *pc)
|
||||
compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
|
||||
{
|
||||
assert(p->kind == MatchOr_kind);
|
||||
NEW_JUMP_TARGET_LABEL(c, end);
|
||||
@ -6785,9 +6779,8 @@ compiler_pattern_or(struct compiler *c, location *ploc,
|
||||
pc->fail_pop = NULL;
|
||||
pc->fail_pop_size = 0;
|
||||
pc->on_top = 0;
|
||||
*ploc = LOC(alt);
|
||||
if (!cfg_builder_addop_i(CFG_BUILDER(c), COPY, 1, *ploc) ||
|
||||
!compiler_pattern(c, ploc, alt, pc)) {
|
||||
if (!cfg_builder_addop_i(CFG_BUILDER(c), COPY, 1, LOC(alt)) ||
|
||||
!compiler_pattern(c, alt, pc)) {
|
||||
goto error;
|
||||
}
|
||||
// Success!
|
||||
@ -6841,7 +6834,7 @@ compiler_pattern_or(struct compiler *c, location *ploc,
|
||||
// Do the same thing to the stack, using several
|
||||
// rotations:
|
||||
while (rotations--) {
|
||||
if (!pattern_helper_rotate(c, *ploc, icontrol + 1)){
|
||||
if (!pattern_helper_rotate(c, LOC(alt), icontrol + 1)){
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
@ -6849,8 +6842,8 @@ compiler_pattern_or(struct compiler *c, location *ploc,
|
||||
}
|
||||
}
|
||||
assert(control);
|
||||
if (!cfg_builder_addop_j(CFG_BUILDER(c), *ploc, JUMP, end) ||
|
||||
!emit_and_reset_fail_pop(c, *ploc, pc))
|
||||
if (!cfg_builder_addop_j(CFG_BUILDER(c), LOC(alt), JUMP, end) ||
|
||||
!emit_and_reset_fail_pop(c, LOC(alt), pc))
|
||||
{
|
||||
goto error;
|
||||
}
|
||||
@ -6861,8 +6854,8 @@ compiler_pattern_or(struct compiler *c, location *ploc,
|
||||
// Need to NULL this for the PyObject_Free call in the error block.
|
||||
old_pc.fail_pop = NULL;
|
||||
// No match. Pop the remaining copy of the subject and fail:
|
||||
if (!cfg_builder_addop_noarg(CFG_BUILDER(c), POP_TOP, *ploc) ||
|
||||
!jump_to_fail_pop(c, *ploc, pc, JUMP)) {
|
||||
if (!cfg_builder_addop_noarg(CFG_BUILDER(c), POP_TOP, LOC(p)) ||
|
||||
!jump_to_fail_pop(c, LOC(p), pc, JUMP)) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
@ -6877,7 +6870,7 @@ compiler_pattern_or(struct compiler *c, location *ploc,
|
||||
Py_ssize_t nrots = nstores + 1 + pc->on_top + PyList_GET_SIZE(pc->stores);
|
||||
for (Py_ssize_t i = 0; i < nstores; i++) {
|
||||
// Rotate this capture to its proper place on the stack:
|
||||
if (!pattern_helper_rotate(c, *ploc, nrots)) {
|
||||
if (!pattern_helper_rotate(c, LOC(p), nrots)) {
|
||||
goto error;
|
||||
}
|
||||
// Update the list of previous stores with this new name, checking for
|
||||
@ -6888,7 +6881,7 @@ compiler_pattern_or(struct compiler *c, location *ploc,
|
||||
goto error;
|
||||
}
|
||||
if (dupe) {
|
||||
compiler_error_duplicate_store(c, *ploc, name);
|
||||
compiler_error_duplicate_store(c, LOC(p), name);
|
||||
goto error;
|
||||
}
|
||||
if (PyList_Append(pc->stores, name)) {
|
||||
@ -6899,10 +6892,10 @@ compiler_pattern_or(struct compiler *c, location *ploc,
|
||||
Py_DECREF(control);
|
||||
// NOTE: Returning macros are safe again.
|
||||
// Pop the copy of the subject:
|
||||
ADDOP(c, *ploc, POP_TOP);
|
||||
ADDOP(c, LOC(p), POP_TOP);
|
||||
return 1;
|
||||
diff:
|
||||
compiler_error(c, *ploc, "alternative patterns bind different names");
|
||||
compiler_error(c, LOC(p), "alternative patterns bind different names");
|
||||
error:
|
||||
PyObject_Free(old_pc.fail_pop);
|
||||
Py_DECREF(old_pc.stores);
|
||||
@ -6912,8 +6905,8 @@ error:
|
||||
|
||||
|
||||
static int
|
||||
compiler_pattern_sequence(struct compiler *c, location *ploc,
|
||||
pattern_ty p, pattern_context *pc)
|
||||
compiler_pattern_sequence(struct compiler *c, pattern_ty p,
|
||||
pattern_context *pc)
|
||||
{
|
||||
assert(p->kind == MatchSequence_kind);
|
||||
asdl_pattern_seq *patterns = p->v.MatchSequence.patterns;
|
||||
@ -6927,7 +6920,7 @@ compiler_pattern_sequence(struct compiler *c, location *ploc,
|
||||
if (pattern->kind == MatchStar_kind) {
|
||||
if (star >= 0) {
|
||||
const char *e = "multiple starred names in sequence pattern";
|
||||
return compiler_error(c, *ploc, e);
|
||||
return compiler_error(c, LOC(p), e);
|
||||
}
|
||||
star_wildcard = WILDCARD_STAR_CHECK(pattern);
|
||||
only_wildcard &= star_wildcard;
|
||||
@ -6938,33 +6931,33 @@ compiler_pattern_sequence(struct compiler *c, location *ploc,
|
||||
}
|
||||
// We need to keep the subject on top during the sequence and length checks:
|
||||
pc->on_top++;
|
||||
ADDOP(c, *ploc, MATCH_SEQUENCE);
|
||||
RETURN_IF_FALSE(jump_to_fail_pop(c, *ploc, pc, POP_JUMP_IF_FALSE));
|
||||
ADDOP(c, LOC(p), MATCH_SEQUENCE);
|
||||
RETURN_IF_FALSE(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE));
|
||||
if (star < 0) {
|
||||
// No star: len(subject) == size
|
||||
ADDOP(c, *ploc, GET_LEN);
|
||||
ADDOP_LOAD_CONST_NEW(c, *ploc, PyLong_FromSsize_t(size));
|
||||
ADDOP_COMPARE(c, *ploc, Eq);
|
||||
RETURN_IF_FALSE(jump_to_fail_pop(c, *ploc, pc, POP_JUMP_IF_FALSE));
|
||||
ADDOP(c, LOC(p), GET_LEN);
|
||||
ADDOP_LOAD_CONST_NEW(c, LOC(p), PyLong_FromSsize_t(size));
|
||||
ADDOP_COMPARE(c, LOC(p), Eq);
|
||||
RETURN_IF_FALSE(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE));
|
||||
}
|
||||
else if (size > 1) {
|
||||
// Star: len(subject) >= size - 1
|
||||
ADDOP(c, *ploc, GET_LEN);
|
||||
ADDOP_LOAD_CONST_NEW(c, *ploc, PyLong_FromSsize_t(size - 1));
|
||||
ADDOP_COMPARE(c, *ploc, GtE);
|
||||
RETURN_IF_FALSE(jump_to_fail_pop(c, *ploc, pc, POP_JUMP_IF_FALSE));
|
||||
ADDOP(c, LOC(p), GET_LEN);
|
||||
ADDOP_LOAD_CONST_NEW(c, LOC(p), PyLong_FromSsize_t(size - 1));
|
||||
ADDOP_COMPARE(c, LOC(p), GtE);
|
||||
RETURN_IF_FALSE(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE));
|
||||
}
|
||||
// Whatever comes next should consume the subject:
|
||||
pc->on_top--;
|
||||
if (only_wildcard) {
|
||||
// Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc.
|
||||
ADDOP(c, *ploc, POP_TOP);
|
||||
ADDOP(c, LOC(p), POP_TOP);
|
||||
}
|
||||
else if (star_wildcard) {
|
||||
RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, ploc, patterns, star, pc));
|
||||
RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, LOC(p), patterns, star, pc));
|
||||
}
|
||||
else {
|
||||
RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, ploc, patterns, star, pc));
|
||||
RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, LOC(p), patterns, star, pc));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
@ -6973,15 +6966,14 @@ static int
|
||||
compiler_pattern_value(struct compiler *c, pattern_ty p, pattern_context *pc)
|
||||
{
|
||||
assert(p->kind == MatchValue_kind);
|
||||
location loc = LOC(p);
|
||||
expr_ty value = p->v.MatchValue.value;
|
||||
if (!MATCH_VALUE_EXPR(value)) {
|
||||
const char *e = "patterns may only match literals and attribute lookups";
|
||||
return compiler_error(c, loc, e);
|
||||
return compiler_error(c, LOC(p), e);
|
||||
}
|
||||
VISIT(c, expr, value);
|
||||
ADDOP_COMPARE(c, loc, Eq);
|
||||
RETURN_IF_FALSE(jump_to_fail_pop(c, loc, pc, POP_JUMP_IF_FALSE));
|
||||
ADDOP_COMPARE(c, LOC(p), Eq);
|
||||
RETURN_IF_FALSE(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -6989,40 +6981,37 @@ static int
|
||||
compiler_pattern_singleton(struct compiler *c, pattern_ty p, pattern_context *pc)
|
||||
{
|
||||
assert(p->kind == MatchSingleton_kind);
|
||||
location loc = LOC(p);
|
||||
ADDOP_LOAD_CONST(c, loc, p->v.MatchSingleton.value);
|
||||
ADDOP_COMPARE(c, loc, Is);
|
||||
RETURN_IF_FALSE(jump_to_fail_pop(c, loc, pc, POP_JUMP_IF_FALSE));
|
||||
ADDOP_LOAD_CONST(c, LOC(p), p->v.MatchSingleton.value);
|
||||
ADDOP_COMPARE(c, LOC(p), Is);
|
||||
RETURN_IF_FALSE(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
compiler_pattern(struct compiler *c, location *ploc,
|
||||
pattern_ty p, pattern_context *pc)
|
||||
compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc)
|
||||
{
|
||||
*ploc = LOC(p);
|
||||
switch (p->kind) {
|
||||
case MatchValue_kind:
|
||||
return compiler_pattern_value(c, p, pc);
|
||||
case MatchSingleton_kind:
|
||||
return compiler_pattern_singleton(c, p, pc);
|
||||
case MatchSequence_kind:
|
||||
return compiler_pattern_sequence(c, ploc, p, pc);
|
||||
return compiler_pattern_sequence(c, p, pc);
|
||||
case MatchMapping_kind:
|
||||
return compiler_pattern_mapping(c, ploc, p, pc);
|
||||
return compiler_pattern_mapping(c, p, pc);
|
||||
case MatchClass_kind:
|
||||
return compiler_pattern_class(c, ploc, p, pc);
|
||||
return compiler_pattern_class(c, p, pc);
|
||||
case MatchStar_kind:
|
||||
return compiler_pattern_star(c, p, pc);
|
||||
case MatchAs_kind:
|
||||
return compiler_pattern_as(c, ploc, p, pc);
|
||||
return compiler_pattern_as(c, p, pc);
|
||||
case MatchOr_kind:
|
||||
return compiler_pattern_or(c, ploc, p, pc);
|
||||
return compiler_pattern_or(c, p, pc);
|
||||
}
|
||||
// AST validator shouldn't let this happen, but if it does,
|
||||
// just fail, don't crash out of the interpreter
|
||||
const char *e = "invalid match pattern node in AST (kind=%d)";
|
||||
return compiler_error(c, *ploc, e, p->kind);
|
||||
return compiler_error(c, LOC(p), e, p->kind);
|
||||
}
|
||||
|
||||
static int
|
||||
@ -7037,9 +7026,8 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc)
|
||||
for (Py_ssize_t i = 0; i < cases - has_default; i++) {
|
||||
m = asdl_seq_GET(s->v.Match.cases, i);
|
||||
// Only copy the subject if we're *not* on the last case:
|
||||
location loc = LOC(m->pattern);
|
||||
if (i != cases - has_default - 1) {
|
||||
ADDOP_I(c, loc, COPY, 1);
|
||||
ADDOP_I(c, LOC(m->pattern), COPY, 1);
|
||||
}
|
||||
RETURN_IF_FALSE(pc->stores = PyList_New(0));
|
||||
// Irrefutable cases must be either guarded, last, or both:
|
||||
@ -7048,7 +7036,7 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc)
|
||||
pc->fail_pop_size = 0;
|
||||
pc->on_top = 0;
|
||||
// NOTE: Can't use returning macros here (they'll leak pc->stores)!
|
||||
if (!compiler_pattern(c, &loc, m->pattern, pc)) {
|
||||
if (!compiler_pattern(c, m->pattern, pc)) {
|
||||
Py_DECREF(pc->stores);
|
||||
return 0;
|
||||
}
|
||||
@ -7057,7 +7045,7 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc)
|
||||
Py_ssize_t nstores = PyList_GET_SIZE(pc->stores);
|
||||
for (Py_ssize_t n = 0; n < nstores; n++) {
|
||||
PyObject *name = PyList_GET_ITEM(pc->stores, n);
|
||||
if (!compiler_nameop(c, loc, name, Store)) {
|
||||
if (!compiler_nameop(c, LOC(m->pattern), name, Store)) {
|
||||
Py_DECREF(pc->stores);
|
||||
return 0;
|
||||
}
|
||||
@ -7066,11 +7054,11 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc)
|
||||
// NOTE: Returning macros are safe again.
|
||||
if (m->guard) {
|
||||
RETURN_IF_FALSE(ensure_fail_pop(c, pc, 0));
|
||||
RETURN_IF_FALSE(compiler_jump_if(c, loc, m->guard, pc->fail_pop[0], 0));
|
||||
RETURN_IF_FALSE(compiler_jump_if(c, LOC(m->pattern), m->guard, pc->fail_pop[0], 0));
|
||||
}
|
||||
// Success! Pop the subject off, we're done with it:
|
||||
if (i != cases - has_default - 1) {
|
||||
ADDOP(c, loc, POP_TOP);
|
||||
ADDOP(c, LOC(m->pattern), POP_TOP);
|
||||
}
|
||||
VISIT_SEQ(c, stmt, m->body);
|
||||
ADDOP_JUMP(c, NO_LOCATION, JUMP, end);
|
||||
@ -7083,17 +7071,16 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc)
|
||||
// A trailing "case _" is common, and lets us save a bit of redundant
|
||||
// pushing and popping in the loop above:
|
||||
m = asdl_seq_GET(s->v.Match.cases, cases - 1);
|
||||
location loc = LOC(m->pattern);
|
||||
if (cases == 1) {
|
||||
// No matches. Done with the subject:
|
||||
ADDOP(c, loc, POP_TOP);
|
||||
ADDOP(c, LOC(m->pattern), POP_TOP);
|
||||
}
|
||||
else {
|
||||
// Show line coverage for default case (it doesn't create bytecode)
|
||||
ADDOP(c, loc, NOP);
|
||||
ADDOP(c, LOC(m->pattern), NOP);
|
||||
}
|
||||
if (m->guard) {
|
||||
RETURN_IF_FALSE(compiler_jump_if(c, loc, m->guard, end, 0));
|
||||
RETURN_IF_FALSE(compiler_jump_if(c, LOC(m->pattern), m->guard, end, 0));
|
||||
}
|
||||
VISIT_SEQ(c, stmt, m->body);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user