summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-12-15 10:35:48 (GMT)
committerGitHub <noreply@github.com>2017-12-15 10:35:48 (GMT)
commit3325a6780c81f1ea51190370b5454879c4862a37 (patch)
tree3100f972b635137528b105c410ab88e6bff64521 /Python
parent297fd876aad8ef443d8992618de22c46dbda258b (diff)
downloadcpython-3325a6780c81f1ea51190370b5454879c4862a37.zip
cpython-3325a6780c81f1ea51190370b5454879c4862a37.tar.gz
cpython-3325a6780c81f1ea51190370b5454879c4862a37.tar.bz2
bpo-27169: The __debug__ constant is now optimized out at compile time. (#4880)
This fixes also bpo-22091.
Diffstat (limited to 'Python')
-rw-r--r--Python/compile.c45
1 files changed, 15 insertions, 30 deletions
diff --git a/Python/compile.c b/Python/compile.c
index 3fd8491..51c9a08 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -1332,13 +1332,15 @@ 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(expr_ty e)
+get_const_value(struct compiler *c, expr_ty e)
{
switch (e->kind) {
case Constant_kind:
@@ -1353,6 +1355,9 @@ get_const_value(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();
}
@@ -3097,6 +3102,11 @@ 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;
+ }
+
op = 0;
optype = OP_NAME;
scope = PyST_GetScope(c->u->u_ste, mangled);
@@ -3360,7 +3370,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((expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
+ key = get_const_value(c, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Py_INCREF(key);
PyTuple_SET_ITEM(keys, i - begin, key);
}
@@ -4132,35 +4142,10 @@ compiler_visit_keyword(struct compiler *c, keyword_ty k)
static int
expr_constant(struct compiler *c, expr_ty e)
{
- const char *id;
- switch (e->kind) {
- case Ellipsis_kind:
- return 1;
- case Constant_kind:
- return PyObject_IsTrue(e->v.Constant.value);
- case Num_kind:
- return PyObject_IsTrue(e->v.Num.n);
- case Str_kind:
- return PyObject_IsTrue(e->v.Str.s);
- case Name_kind:
- /* optimize away names that can't be reassigned */
- id = PyUnicode_AsUTF8(e->v.Name.id);
- if (id && strcmp(id, "__debug__") == 0)
- return !c->c_optimize;
- return -1;
- case NameConstant_kind: {
- PyObject *o = e->v.NameConstant.value;
- if (o == Py_None)
- return 0;
- else if (o == Py_True)
- return 1;
- else if (o == Py_False)
- return 0;
- }
- /* fall through */
- default:
- return -1;
+ if (is_const(e)) {
+ return PyObject_IsTrue(get_const_value(c, e));
}
+ return -1;
}