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

@ -191,7 +191,7 @@ static void compiler_pop_fblock(struct compiler *, enum fblocktype,
static int compiler_in_loop(struct compiler *);
static int inplace_binop(struct compiler *, operator_ty);
static int expr_constant(struct compiler *, expr_ty);
static int expr_constant(expr_ty);
static int compiler_with(struct compiler *, stmt_ty, int);
static int compiler_async_with(struct compiler *, stmt_ty, int);
@ -331,7 +331,7 @@ PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
c.c_optimize = (optimize == -1) ? Py_OptimizeFlag : optimize;
c.c_nestlevel = 0;
if (!_PyAST_Optimize(mod, arena)) {
if (!_PyAST_Optimize(mod, arena, c.c_optimize)) {
goto finally;
}
@ -1332,15 +1332,13 @@ is_const(expr_ty e)
case Ellipsis_kind:
case NameConstant_kind:
return 1;
case Name_kind:
return _PyUnicode_EqualToASCIIString(e->v.Name.id, "__debug__");
default:
return 0;
}
}
static PyObject *
get_const_value(struct compiler *c, expr_ty e)
get_const_value(expr_ty e)
{
switch (e->kind) {
case Constant_kind:
@ -1355,9 +1353,6 @@ get_const_value(struct compiler *c, expr_ty e)
return Py_Ellipsis;
case NameConstant_kind:
return e->v.NameConstant.value;
case Name_kind:
assert(_PyUnicode_EqualToASCIIString(e->v.Name.id, "__debug__"));
return c->c_optimize ? Py_False : Py_True;
default:
Py_UNREACHABLE();
}
@ -2217,7 +2212,7 @@ compiler_if(struct compiler *c, stmt_ty s)
if (end == NULL)
return 0;
constant = expr_constant(c, s->v.If.test);
constant = expr_constant(s->v.If.test);
/* constant = 0: "if 0"
* constant = 1: "if 1", "if 2", ...
* constant = -1: rest */
@ -2363,7 +2358,7 @@ static int
compiler_while(struct compiler *c, stmt_ty s)
{
basicblock *loop, *orelse, *end, *anchor = NULL;
int constant = expr_constant(c, s->v.While.test);
int constant = expr_constant(s->v.While.test);
if (constant == 0) {
if (s->v.While.orelse)
@ -3098,11 +3093,6 @@ compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
!_PyUnicode_EqualToASCIIString(name, "True") &&
!_PyUnicode_EqualToASCIIString(name, "False"));
if (ctx == Load && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
ADDOP_O(c, LOAD_CONST, c->c_optimize ? Py_False : Py_True, consts);
return 1;
}
mangled = _Py_Mangle(c->u->u_private, name);
if (!mangled)
return 0;
@ -3370,7 +3360,7 @@ compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end
return 0;
}
for (i = begin; i < end; i++) {
key = get_const_value(c, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
key = get_const_value((expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Py_INCREF(key);
PyTuple_SET_ITEM(keys, i - begin, key);
}
@ -4140,10 +4130,10 @@ compiler_visit_keyword(struct compiler *c, keyword_ty k)
*/
static int
expr_constant(struct compiler *c, expr_ty e)
expr_constant(expr_ty e)
{
if (is_const(e)) {
return PyObject_IsTrue(get_const_value(c, e));
return PyObject_IsTrue(get_const_value(e));
}
return -1;
}