summaryrefslogtreecommitdiff
path: root/Python/compile.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-12-25 12:47:50 +0200
committerGitHub <noreply@github.com>2017-12-25 12:47:50 +0200
commit3dfbaf51f0d90646e0414ddbd3b513ee8e5ffe9b (patch)
tree03005e193284d3da6590aa9e4740d2dcfa0e189b /Python/compile.c
parent1b3029ac8370ac76fa9571aef540e10aed0667ff (diff)
downloadcpython-git-3dfbaf51f0d90646e0414ddbd3b513ee8e5ffe9b.tar.gz
bpo-32372: Move __debug__ optimization to the AST level. (#4925)
Diffstat (limited to 'Python/compile.c')
-rw-r--r--Python/compile.c26
1 files changed, 8 insertions, 18 deletions
diff --git a/Python/compile.c b/Python/compile.c
index 392de40f20..e5745d16e3 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -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;
}