7
7
static int
8
8
make_const (expr_ty node , PyObject * val , PyArena * arena )
9
9
{
10
+ // Even if no new value was calculated, make_const may still
11
+ // need to clear an error (e.g. for division by zero)
10
12
if (val == NULL ) {
11
13
if (PyErr_ExceptionMatches (PyExc_KeyboardInterrupt )) {
12
14
return 0 ;
@@ -49,7 +51,7 @@ fold_unaryop(expr_ty node, PyArena *arena, _PyASTOptimizeState *state)
49
51
of !=. Detecting such cases doesn't seem worthwhile.
50
52
Python uses </> for 'is subset'/'is superset' operations on sets.
51
53
They don't satisfy not folding laws. */
52
- int op = asdl_seq_GET (arg -> v .Compare .ops , 0 );
54
+ cmpop_ty op = asdl_seq_GET (arg -> v .Compare .ops , 0 );
53
55
switch (op ) {
54
56
case Is :
55
57
op = IsNot ;
@@ -63,8 +65,17 @@ fold_unaryop(expr_ty node, PyArena *arena, _PyASTOptimizeState *state)
63
65
case NotIn :
64
66
op = In ;
65
67
break ;
66
- default :
67
- op = 0 ;
68
+ // The remaining comparison operators can't be safely inverted
69
+ case Eq :
70
+ case NotEq :
71
+ case Lt :
72
+ case LtE :
73
+ case Gt :
74
+ case GtE :
75
+ op = 0 ; // The AST enums leave "0" free as an "unused" marker
76
+ break ;
77
+ // No default case, so the compiler will emit a warning if new
78
+ // comparison operators are added without being handled here
68
79
}
69
80
if (op ) {
70
81
asdl_seq_SET (arg -> v .Compare .ops , 0 , op );
@@ -224,7 +235,7 @@ fold_binop(expr_ty node, PyArena *arena, _PyASTOptimizeState *state)
224
235
225
236
PyObject * lv = lhs -> v .Constant .value ;
226
237
PyObject * rv = rhs -> v .Constant .value ;
227
- PyObject * newval ;
238
+ PyObject * newval = NULL ;
228
239
229
240
switch (node -> v .BinOp .op ) {
230
241
case Add :
@@ -263,8 +274,11 @@ fold_binop(expr_ty node, PyArena *arena, _PyASTOptimizeState *state)
263
274
case BitAnd :
264
275
newval = PyNumber_And (lv , rv );
265
276
break ;
266
- default : // Unknown operator
277
+ // No builtin constants implement the following operators
278
+ case MatMult :
267
279
return 1 ;
280
+ // No default case, so the compiler will emit a warning if new binary
281
+ // operators are added without being handled here
268
282
}
269
283
270
284
return make_const (node , newval , arena );
@@ -457,8 +471,11 @@ astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
457
471
case Expression_kind :
458
472
CALL (astfold_expr , expr_ty , node_ -> v .Expression .body );
459
473
break ;
460
- default :
474
+ // The following top level nodes don't participate in constant folding
475
+ case FunctionType_kind :
461
476
break ;
477
+ // No default case, so the compiler will emit a warning if new top level
478
+ // compilation nodes are added without being handled here
462
479
}
463
480
return 1 ;
464
481
}
@@ -567,8 +584,14 @@ astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
567
584
return make_const (node_ , PyBool_FromLong (!state -> optimize ), ctx_ );
568
585
}
569
586
break ;
570
- default :
587
+ case NamedExpr_kind :
588
+ CALL (astfold_expr , expr_ty , node_ -> v .NamedExpr .value );
589
+ break ;
590
+ case Constant_kind :
591
+ // Already a constant, nothing further to do
571
592
break ;
593
+ // No default case, so the compiler will emit a warning if new expression
594
+ // kinds are added without being handled here
572
595
}
573
596
return 1 ;
574
597
}
@@ -686,8 +709,17 @@ astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
686
709
case Expr_kind :
687
710
CALL (astfold_expr , expr_ty , node_ -> v .Expr .value );
688
711
break ;
689
- default :
690
- break ;
712
+ // The following statements don't contain any subexpressions to be folded
713
+ case Import_kind :
714
+ case ImportFrom_kind :
715
+ case Global_kind :
716
+ case Nonlocal_kind :
717
+ case Pass_kind :
718
+ case Break_kind :
719
+ case Continue_kind :
720
+ break ;
721
+ // No default case, so the compiler will emit a warning if new statement
722
+ // kinds are added without being handled here
691
723
}
692
724
return 1 ;
693
725
}
@@ -700,8 +732,8 @@ astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTOptimizeState
700
732
CALL_OPT (astfold_expr , expr_ty , node_ -> v .ExceptHandler .type );
701
733
CALL_SEQ (astfold_stmt , stmt , node_ -> v .ExceptHandler .body );
702
734
break ;
703
- default :
704
- break ;
735
+ // No default case, so the compiler will emit a warning if new handler
736
+ // kinds are added without being handled here
705
737
}
706
738
return 1 ;
707
739
}
0 commit comments