@@ -5352,9 +5352,26 @@ codegen_slice(compiler *c, expr_ty s)
5352
5352
#define WILDCARD_STAR_CHECK (N ) \
5353
5353
((N)->kind == MatchStar_kind && !(N)->v.MatchStar.name)
5354
5354
5355
+ // Expressions such as '1+2j' or '1-2j'
5356
+ static inline bool is_complex_literal (expr_ty e ) {
5357
+ return e -> kind == BinOp_kind
5358
+ && (e -> v .BinOp .op == Add || e -> v .BinOp .op == Sub )
5359
+ && e -> v .BinOp .left -> kind == Constant_kind
5360
+ && e -> v .BinOp .right -> kind == Constant_kind
5361
+ && (PyLong_CheckExact (e -> v .BinOp .left -> v .Constant .value )
5362
+ || PyFloat_CheckExact (e -> v .BinOp .left -> v .Constant .value ))
5363
+ && PyComplex_CheckExact (e -> v .BinOp .right -> v .Constant .value );
5364
+ }
5365
+
5355
5366
// Limit permitted subexpressions, even if the parser & AST validator let them through
5356
- #define MATCH_VALUE_EXPR (N ) \
5357
- ((N)->kind == Constant_kind || (N)->kind == Attribute_kind)
5367
+ static inline bool is_match_value_expr (expr_ty e ) {
5368
+ // The permitted expressions in a case pattern value are constants,
5369
+ // attribute lookups, and complex literals. However,
5370
+ // complex literals are represented as a binary add or sub in
5371
+ // the AST rather than a constant, so we need to check for them
5372
+ // manually here.
5373
+ return e -> kind == Constant_kind || e -> kind == Attribute_kind || is_complex_literal (e );
5374
+ }
5358
5375
5359
5376
// Allocate or resize pc->fail_pop to allow for n items to be popped on failure.
5360
5377
static int
@@ -6019,7 +6036,7 @@ codegen_pattern_value(compiler *c, pattern_ty p, pattern_context *pc)
6019
6036
{
6020
6037
assert (p -> kind == MatchValue_kind );
6021
6038
expr_ty value = p -> v .MatchValue .value ;
6022
- if (!MATCH_VALUE_EXPR (value )) {
6039
+ if (!is_match_value_expr (value )) {
6023
6040
const char * e = "patterns may only match literals and attribute lookups" ;
6024
6041
return _PyCompile_Error (c , LOC (p ), e );
6025
6042
}
0 commit comments