bpo-32372: Move __debug__ optimization to the AST level. (#4925)

This commit is contained in:
Serhiy Storchaka 2017-12-25 12:47:50 +02:00 committed by GitHub
parent 1b3029ac83
commit 3dfbaf51f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 52 deletions

View file

@ -75,7 +75,7 @@ unary_not(PyObject *v)
}
static int
fold_unaryop(expr_ty node, PyArena *arena)
fold_unaryop(expr_ty node, PyArena *arena, int optimize)
{
expr_ty arg = node->v.UnaryOp.operand;
@ -252,7 +252,7 @@ safe_mod(PyObject *v, PyObject *w)
}
static int
fold_binop(expr_ty node, PyArena *arena)
fold_binop(expr_ty node, PyArena *arena, int optimize)
{
expr_ty lhs, rhs;
lhs = node->v.BinOp.left;
@ -334,7 +334,7 @@ make_const_tuple(asdl_seq *elts)
}
static int
fold_tuple(expr_ty node, PyArena *arena)
fold_tuple(expr_ty node, PyArena *arena, int optimize)
{
PyObject *newval;
@ -346,7 +346,7 @@ fold_tuple(expr_ty node, PyArena *arena)
}
static int
fold_subscr(expr_ty node, PyArena *arena)
fold_subscr(expr_ty node, PyArena *arena, int optimize)
{
PyObject *newval;
expr_ty arg, idx;
@ -374,7 +374,7 @@ fold_subscr(expr_ty node, PyArena *arena)
in "for" loop and comprehensions.
*/
static int
fold_iter(expr_ty arg, PyArena *arena)
fold_iter(expr_ty arg, PyArena *arena, int optimize)
{
PyObject *newval;
if (arg->kind == List_kind) {
@ -393,7 +393,7 @@ fold_iter(expr_ty arg, PyArena *arena)
}
static int
fold_compare(expr_ty node, PyArena *arena)
fold_compare(expr_ty node, PyArena *arena, int optimize)
{
asdl_int_seq *ops;
asdl_seq *args;
@ -407,29 +407,29 @@ fold_compare(expr_ty node, PyArena *arena)
i = asdl_seq_LEN(ops) - 1;
int op = asdl_seq_GET(ops, i);
if (op == In || op == NotIn) {
if (!fold_iter((expr_ty)asdl_seq_GET(args, i), arena)) {
if (!fold_iter((expr_ty)asdl_seq_GET(args, i), arena, optimize)) {
return 0;
}
}
return 1;
}
static int astfold_mod(mod_ty node_, PyArena* ctx_);
static int astfold_stmt(stmt_ty node_, PyArena* ctx_);
static int astfold_expr(expr_ty node_, PyArena* ctx_);
static int astfold_arguments(arguments_ty node_, PyArena* ctx_);
static int astfold_comprehension(comprehension_ty node_, PyArena* ctx_);
static int astfold_keyword(keyword_ty node_, PyArena* ctx_);
static int astfold_slice(slice_ty node_, PyArena* ctx_);
static int astfold_arg(arg_ty node_, PyArena* ctx_);
static int astfold_withitem(withitem_ty node_, PyArena* ctx_);
static int astfold_excepthandler(excepthandler_ty node_, PyArena* ctx_);
static int astfold_mod(mod_ty node_, PyArena *ctx_, int optimize_);
static int astfold_stmt(stmt_ty node_, PyArena *ctx_, int optimize_);
static int astfold_expr(expr_ty node_, PyArena *ctx_, int optimize_);
static int astfold_arguments(arguments_ty node_, PyArena *ctx_, int optimize_);
static int astfold_comprehension(comprehension_ty node_, PyArena *ctx_, int optimize_);
static int astfold_keyword(keyword_ty node_, PyArena *ctx_, int optimize_);
static int astfold_slice(slice_ty node_, PyArena *ctx_, int optimize_);
static int astfold_arg(arg_ty node_, PyArena *ctx_, int optimize_);
static int astfold_withitem(withitem_ty node_, PyArena *ctx_, int optimize_);
static int astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, int optimize_);
#define CALL(FUNC, TYPE, ARG) \
if (!FUNC((ARG), ctx_)) \
if (!FUNC((ARG), ctx_, optimize_)) \
return 0;
#define CALL_OPT(FUNC, TYPE, ARG) \
if ((ARG) != NULL && !FUNC((ARG), ctx_)) \
if ((ARG) != NULL && !FUNC((ARG), ctx_, optimize_)) \
return 0;
#define CALL_SEQ(FUNC, TYPE, ARG) { \
@ -437,7 +437,7 @@ static int astfold_excepthandler(excepthandler_ty node_, PyArena* ctx_);
asdl_seq *seq = (ARG); /* avoid variable capture */ \
for (i = 0; i < asdl_seq_LEN(seq); i++) { \
TYPE elt = (TYPE)asdl_seq_GET(seq, i); \
if (elt != NULL && !FUNC(elt, ctx_)) \
if (elt != NULL && !FUNC(elt, ctx_, optimize_)) \
return 0; \
} \
}
@ -447,13 +447,13 @@ static int astfold_excepthandler(excepthandler_ty node_, PyArena* ctx_);
asdl_int_seq *seq = (ARG); /* avoid variable capture */ \
for (i = 0; i < asdl_seq_LEN(seq); i++) { \
TYPE elt = (TYPE)asdl_seq_GET(seq, i); \
if (!FUNC(elt, ctx_)) \
if (!FUNC(elt, ctx_, optimize_)) \
return 0; \
} \
}
static int
astfold_mod(mod_ty node_, PyArena* ctx_)
astfold_mod(mod_ty node_, PyArena *ctx_, int optimize_)
{
switch (node_->kind) {
case Module_kind:
@ -475,7 +475,7 @@ astfold_mod(mod_ty node_, PyArena* ctx_)
}
static int
astfold_expr(expr_ty node_, PyArena* ctx_)
astfold_expr(expr_ty node_, PyArena *ctx_, int optimize_)
{
switch (node_->kind) {
case BoolOp_kind:
@ -567,6 +567,11 @@ astfold_expr(expr_ty node_, PyArena* ctx_)
CALL_SEQ(astfold_expr, expr_ty, node_->v.Tuple.elts);
CALL(fold_tuple, expr_ty, node_);
break;
case Name_kind:
if (_PyUnicode_EqualToASCIIString(node_->v.Name.id, "__debug__")) {
return make_const(node_, PyBool_FromLong(!optimize_), ctx_);
}
break;
default:
break;
}
@ -574,7 +579,7 @@ astfold_expr(expr_ty node_, PyArena* ctx_)
}
static int
astfold_slice(slice_ty node_, PyArena* ctx_)
astfold_slice(slice_ty node_, PyArena *ctx_, int optimize_)
{
switch (node_->kind) {
case Slice_kind:
@ -595,14 +600,14 @@ astfold_slice(slice_ty node_, PyArena* ctx_)
}
static int
astfold_keyword(keyword_ty node_, PyArena* ctx_)
astfold_keyword(keyword_ty node_, PyArena *ctx_, int optimize_)
{
CALL(astfold_expr, expr_ty, node_->value);
return 1;
}
static int
astfold_comprehension(comprehension_ty node_, PyArena* ctx_)
astfold_comprehension(comprehension_ty node_, PyArena *ctx_, int optimize_)
{
CALL(astfold_expr, expr_ty, node_->target);
CALL(astfold_expr, expr_ty, node_->iter);
@ -613,7 +618,7 @@ astfold_comprehension(comprehension_ty node_, PyArena* ctx_)
}
static int
astfold_arguments(arguments_ty node_, PyArena* ctx_)
astfold_arguments(arguments_ty node_, PyArena *ctx_, int optimize_)
{
CALL_SEQ(astfold_arg, arg_ty, node_->args);
CALL_OPT(astfold_arg, arg_ty, node_->vararg);
@ -625,14 +630,14 @@ astfold_arguments(arguments_ty node_, PyArena* ctx_)
}
static int
astfold_arg(arg_ty node_, PyArena* ctx_)
astfold_arg(arg_ty node_, PyArena *ctx_, int optimize_)
{
CALL_OPT(astfold_expr, expr_ty, node_->annotation);
return 1;
}
static int
astfold_stmt(stmt_ty node_, PyArena* ctx_)
astfold_stmt(stmt_ty node_, PyArena *ctx_, int optimize_)
{
switch (node_->kind) {
case FunctionDef_kind:
@ -728,7 +733,7 @@ astfold_stmt(stmt_ty node_, PyArena* ctx_)
}
static int
astfold_excepthandler(excepthandler_ty node_, PyArena* ctx_)
astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, int optimize_)
{
switch (node_->kind) {
case ExceptHandler_kind:
@ -742,7 +747,7 @@ astfold_excepthandler(excepthandler_ty node_, PyArena* ctx_)
}
static int
astfold_withitem(withitem_ty node_, PyArena* ctx_)
astfold_withitem(withitem_ty node_, PyArena *ctx_, int optimize_)
{
CALL(astfold_expr, expr_ty, node_->context_expr);
CALL_OPT(astfold_expr, expr_ty, node_->optional_vars);
@ -755,9 +760,9 @@ astfold_withitem(withitem_ty node_, PyArena* ctx_)
#undef CALL_INT_SEQ
int
_PyAST_Optimize(mod_ty mod, PyArena *arena)
_PyAST_Optimize(mod_ty mod, PyArena *arena, int optimize)
{
int ret = astfold_mod(mod, arena);
int ret = astfold_mod(mod, arena, optimize);
assert(ret || PyErr_Occurred());
return ret;
}