static int
make_const(expr_ty node, PyObject *val, PyArena *arena)
{
+ // Even if no new value was calculated, make_const may still
+ // need to clear an error (e.g. for division by zero)
if (val == NULL) {
if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt)) {
return 0;
of !=. Detecting such cases doesn't seem worthwhile.
Python uses </> for 'is subset'/'is superset' operations on sets.
They don't satisfy not folding laws. */
- int op = asdl_seq_GET(arg->v.Compare.ops, 0);
+ cmpop_ty op = asdl_seq_GET(arg->v.Compare.ops, 0);
switch (op) {
case Is:
op = IsNot;
case NotIn:
op = In;
break;
- default:
- op = 0;
+ // The remaining comparison operators can't be safely inverted
+ case Eq:
+ case NotEq:
+ case Lt:
+ case LtE:
+ case Gt:
+ case GtE:
+ op = 0; // The AST enums leave "0" free as an "unused" marker
+ break;
+ // No default case, so the compiler will emit a warning if new
+ // comparison operators are added without being handled here
}
if (op) {
asdl_seq_SET(arg->v.Compare.ops, 0, op);
PyObject *lv = lhs->v.Constant.value;
PyObject *rv = rhs->v.Constant.value;
- PyObject *newval;
+ PyObject *newval = NULL;
switch (node->v.BinOp.op) {
case Add:
case BitAnd:
newval = PyNumber_And(lv, rv);
break;
- default: // Unknown operator
+ // No builtin constants implement the following operators
+ case MatMult:
return 1;
+ // No default case, so the compiler will emit a warning if new binary
+ // operators are added without being handled here
}
return make_const(node, newval, arena);
case Expression_kind:
CALL(astfold_expr, expr_ty, node_->v.Expression.body);
break;
- default:
+ // The following top level nodes don't participate in constant folding
+ case FunctionType_kind:
break;
+ // No default case, so the compiler will emit a warning if new top level
+ // compilation nodes are added without being handled here
}
return 1;
}
return make_const(node_, PyBool_FromLong(!state->optimize), ctx_);
}
break;
- default:
+ case NamedExpr_kind:
+ CALL(astfold_expr, expr_ty, node_->v.NamedExpr.value);
+ break;
+ case Constant_kind:
+ // Already a constant, nothing further to do
break;
+ // No default case, so the compiler will emit a warning if new expression
+ // kinds are added without being handled here
}
return 1;
}
case Expr_kind:
CALL(astfold_expr, expr_ty, node_->v.Expr.value);
break;
- default:
- break;
+ // The following statements don't contain any subexpressions to be folded
+ case Import_kind:
+ case ImportFrom_kind:
+ case Global_kind:
+ case Nonlocal_kind:
+ case Pass_kind:
+ case Break_kind:
+ case Continue_kind:
+ break;
+ // No default case, so the compiler will emit a warning if new statement
+ // kinds are added without being handled here
}
return 1;
}
CALL_OPT(astfold_expr, expr_ty, node_->v.ExceptHandler.type);
CALL_SEQ(astfold_stmt, stmt, node_->v.ExceptHandler.body);
break;
- default:
- break;
+ // No default case, so the compiler will emit a warning if new handler
+ // kinds are added without being handled here
}
return 1;
}