diff options
author | Brandt Bucher <brandt@python.org> | 2021-04-29 17:19:28 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-29 17:19:28 -0700 |
commit | dbe60ee09dc5a624cfb78dff61ecf050a5b3f105 (patch) | |
tree | cb0850e50916b666879770be4fb42b473db357e8 /Python | |
parent | 87655e2cf58c543914ea05ebe5a0377441da1ef2 (diff) | |
download | cpython3-dbe60ee09dc5a624cfb78dff61ecf050a5b3f105.tar.gz |
bpo-43892: Validate the first term of complex literal value patterns (GH-25735)
Diffstat (limited to 'Python')
-rw-r--r-- | Python/ast_opt.c | 2 | ||||
-rw-r--r-- | Python/compile.c | 56 |
2 files changed, 20 insertions, 38 deletions
diff --git a/Python/ast_opt.c b/Python/ast_opt.c index 254dd646c4..c1fdea3a88 100644 --- a/Python/ast_opt.c +++ b/Python/ast_opt.c @@ -825,7 +825,7 @@ astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTOptimizeState *state) break; case MatchAs_kind: if (node_->v.MatchAs.pattern) { - CALL(astfold_pattern, expr_ty, node_->v.MatchAs.pattern); + CALL(astfold_pattern, pattern_ty, node_->v.MatchAs.pattern); } break; case MatchOr_kind: diff --git a/Python/compile.c b/Python/compile.c index 1349a852a2..6560fd426a 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -5609,6 +5609,10 @@ compiler_slice(struct compiler *c, expr_ty s) static int pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc) { + if (n == NULL) { + ADDOP(c, POP_TOP); + return 1; + } if (forbidden_name(c, n, Store)) { return 0; } @@ -5783,41 +5787,22 @@ compiler_pattern_subpattern(struct compiler *c, pattern_ty p, pattern_context *p } static int -compiler_pattern_capture(struct compiler *c, identifier n, pattern_context *pc) -{ - RETURN_IF_FALSE(pattern_helper_store_name(c, n, pc)); - ADDOP_LOAD_CONST(c, Py_True); - return 1; -} - -static int -compiler_pattern_wildcard(struct compiler *c, pattern_ty p, pattern_context *pc) -{ - assert(p->kind == MatchAs_kind); - if (!pc->allow_irrefutable) { - // Whoops, can't have a wildcard here! - const char *e = "wildcard makes remaining patterns unreachable"; - return compiler_error(c, e); - } - ADDOP(c, POP_TOP); - ADDOP_LOAD_CONST(c, Py_True); - return 1; -} - -static int compiler_pattern_as(struct compiler *c, pattern_ty p, pattern_context *pc) { assert(p->kind == MatchAs_kind); - if (p->v.MatchAs.name == NULL) { - return compiler_pattern_wildcard(c, p, pc); - } if (p->v.MatchAs.pattern == NULL) { + // An irrefutable match: if (!pc->allow_irrefutable) { - // Whoops, can't have a name capture here! - const char *e = "name capture %R makes remaining patterns unreachable"; - return compiler_error(c, e, p->v.MatchAs.name); + if (p->v.MatchAs.name) { + const char *e = "name capture %R makes remaining patterns unreachable"; + return compiler_error(c, e, p->v.MatchAs.name); + } + const char *e = "wildcard makes remaining patterns unreachable"; + return compiler_error(c, e); } - return compiler_pattern_capture(c, p->v.MatchAs.name, pc); + RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc)); + ADDOP_LOAD_CONST(c, Py_True); + return 1; } basicblock *end, *fail_pop_1; RETURN_IF_FALSE(end = compiler_new_block(c)); @@ -5842,12 +5827,9 @@ static int compiler_pattern_star(struct compiler *c, pattern_ty p, pattern_context *pc) { assert(p->kind == MatchStar_kind); - if (!pc->allow_irrefutable) { - // Whoops, can't have a star capture here! - const char *e = "star captures are only allowed as part of sequence patterns"; - return compiler_error(c, e); - } - return compiler_pattern_capture(c, p->v.MatchStar.name, pc); + RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchStar.name, pc)); + ADDOP_LOAD_CONST(c, Py_True); + return 1; } static int @@ -6206,7 +6188,7 @@ compiler_pattern_value(struct compiler *c, pattern_ty p, pattern_context *pc) } static int -compiler_pattern_constant(struct compiler *c, pattern_ty p, pattern_context *pc) +compiler_pattern_singleton(struct compiler *c, pattern_ty p, pattern_context *pc) { assert(p->kind == MatchSingleton_kind); ADDOP_LOAD_CONST(c, p->v.MatchSingleton.value); @@ -6222,7 +6204,7 @@ compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc) case MatchValue_kind: return compiler_pattern_value(c, p, pc); case MatchSingleton_kind: - return compiler_pattern_constant(c, p, pc); + return compiler_pattern_singleton(c, p, pc); case MatchSequence_kind: return compiler_pattern_sequence(c, p, pc); case MatchMapping_kind: |