[3.6] bpo-27169: The debug constant is now optimized out at compi… · python/cpython@b82da9e (original) (raw)
`@@ -1343,13 +1343,15 @@ is_const(expr_ty e)
`
1343
1343
`case Ellipsis_kind:
`
1344
1344
`case NameConstant_kind:
`
1345
1345
`return 1;
`
``
1346
`+
case Name_kind:
`
``
1347
`+
return _PyUnicode_EqualToASCIIString(e->v.Name.id, "debug");
`
1346
1348
`default:
`
1347
1349
`return 0;
`
1348
1350
` }
`
1349
1351
`}
`
1350
1352
``
1351
1353
`static PyObject *
`
1352
``
`-
get_const_value(expr_ty e)
`
``
1354
`+
get_const_value(struct compiler *c, expr_ty e)
`
1353
1355
`{
`
1354
1356
`switch (e->kind) {
`
1355
1357
`case Constant_kind:
`
`@@ -1364,6 +1366,9 @@ get_const_value(expr_ty e)
`
1364
1366
`return Py_Ellipsis;
`
1365
1367
`case NameConstant_kind:
`
1366
1368
`return e->v.NameConstant.value;
`
``
1369
`+
case Name_kind:
`
``
1370
`+
assert(_PyUnicode_EqualToASCIIString(e->v.Name.id, "debug"));
`
``
1371
`+
return c->c_optimize ? Py_False : Py_True;
`
1367
1372
`default:
`
1368
1373
`assert(!is_const(e));
`
1369
1374
`return NULL;
`
`@@ -3035,6 +3040,11 @@ compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
`
3035
3040
` !_PyUnicode_EqualToASCIIString(name, "True") &&
`
3036
3041
` !_PyUnicode_EqualToASCIIString(name, "False"));
`
3037
3042
``
``
3043
`+
if (ctx == Load && _PyUnicode_EqualToASCIIString(name, "debug")) {
`
``
3044
`+
ADDOP_O(c, LOAD_CONST, c->c_optimize ? Py_False : Py_True, consts);
`
``
3045
`+
return 1;
`
``
3046
`+
}
`
``
3047
+
3038
3048
`op = 0;
`
3039
3049
`optype = OP_NAME;
`
3040
3050
`scope = PyST_GetScope(c->u->u_ste, mangled);
`
`@@ -3298,7 +3308,7 @@ compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end
`
3298
3308
`return 0;
`
3299
3309
` }
`
3300
3310
`for (i = begin; i < end; i++) {
`
3301
``
`-
key = get_const_value((expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
`
``
3311
`+
key = get_const_value(c, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
`
3302
3312
`Py_INCREF(key);
`
3303
3313
`PyTuple_SET_ITEM(keys, i - begin, key);
`
3304
3314
` }
`
`@@ -4044,35 +4054,10 @@ compiler_visit_keyword(struct compiler *c, keyword_ty k)
`
4044
4054
`static int
`
4045
4055
`expr_constant(struct compiler *c, expr_ty e)
`
4046
4056
`{
`
4047
``
`-
char *id;
`
4048
``
`-
switch (e->kind) {
`
4049
``
`-
case Ellipsis_kind:
`
4050
``
`-
return 1;
`
4051
``
`-
case Constant_kind:
`
4052
``
`-
return PyObject_IsTrue(e->v.Constant.value);
`
4053
``
`-
case Num_kind:
`
4054
``
`-
return PyObject_IsTrue(e->v.Num.n);
`
4055
``
`-
case Str_kind:
`
4056
``
`-
return PyObject_IsTrue(e->v.Str.s);
`
4057
``
`-
case Name_kind:
`
4058
``
`-
/* optimize away names that can't be reassigned */
`
4059
``
`-
id = PyUnicode_AsUTF8(e->v.Name.id);
`
4060
``
`-
if (id && strcmp(id, "debug") == 0)
`
4061
``
`-
return !c->c_optimize;
`
4062
``
`-
return -1;
`
4063
``
`-
case NameConstant_kind: {
`
4064
``
`-
PyObject *o = e->v.NameConstant.value;
`
4065
``
`-
if (o == Py_None)
`
4066
``
`-
return 0;
`
4067
``
`-
else if (o == Py_True)
`
4068
``
`-
return 1;
`
4069
``
`-
else if (o == Py_False)
`
4070
``
`-
return 0;
`
4071
``
`-
}
`
4072
``
`-
/* fall through */
`
4073
``
`-
default:
`
4074
``
`-
return -1;
`
``
4057
`+
if (is_const(e)) {
`
``
4058
`+
return PyObject_IsTrue(get_const_value(c, e));
`
4075
4059
` }
`
``
4060
`+
return -1;
`
4076
4061
`}
`
4077
4062
``
4078
4063
``