[Python-checkins] [3.6] bpo-27169: The __debug__ constant is now optimized out at compile time. (GH-4880) (#4882)

Serhiy Storchaka webhook-mailer at python.org
Fri Dec 15 06:26:29 EST 2017


https://github.com/python/cpython/commit/b82da9ebb20053637e731fd40589e1e52f9f8f6e
commit: b82da9ebb20053637e731fd40589e1e52f9f8f6e
branch: 3.6
author: Serhiy Storchaka <storchaka at gmail.com>
committer: GitHub <noreply at github.com>
date: 2017-12-15T13:26:26+02:00
summary:

[3.6] bpo-27169: The __debug__ constant is now optimized out at compile time. (GH-4880) (#4882)

This fixes also bpo-22091..
(cherry picked from commit 3325a6780c81f1ea51190370b5454879c4862a37)

files:
A Misc/NEWS.d/next/Core and Builtins/2017-12-15-11-50-06.bpo-27169.VO84fQ.rst
M Lib/test/test_builtin.py
M Lib/test/test_compile.py
M Python/compile.c

diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index 49c4a53c052..e0dbe784980 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -331,16 +331,16 @@ def test_compile(self):
         try:
             assert False
         except AssertionError:
-            return (True, f.__doc__)
+            return (True, f.__doc__, __debug__)
         else:
-            return (False, f.__doc__)
+            return (False, f.__doc__, __debug__)
         '''
         def f(): """doc"""
-        values = [(-1, __debug__, f.__doc__),
-                  (0, True, 'doc'),
-                  (1, False, 'doc'),
-                  (2, False, None)]
-        for optval, debugval, docstring in values:
+        values = [(-1, __debug__, f.__doc__, __debug__),
+                  (0, True, 'doc', True),
+                  (1, False, 'doc', False),
+                  (2, False, None, False)]
+        for optval, *expected in values:
             # test both direct compilation and compilation via AST
             codeobjs = []
             codeobjs.append(compile(codestr, "<test>", "exec", optimize=optval))
@@ -350,7 +350,7 @@ def f(): """doc"""
                 ns = {}
                 exec(code, ns)
                 rv = ns['f']()
-                self.assertEqual(rv, (debugval, docstring))
+                self.assertEqual(rv, tuple(expected))
 
     def test_delattr(self):
         sys.spam = 1
diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py
index da1db1567b4..7fe34658e04 100644
--- a/Lib/test/test_compile.py
+++ b/Lib/test/test_compile.py
@@ -35,6 +35,7 @@ def test_debug_assignment(self):
         import builtins
         prev = builtins.__debug__
         setattr(builtins, '__debug__', 'sure')
+        self.assertEqual(__debug__, prev)
         setattr(builtins, '__debug__', prev)
 
     def test_argument_handling(self):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-12-15-11-50-06.bpo-27169.VO84fQ.rst b/Misc/NEWS.d/next/Core and Builtins/2017-12-15-11-50-06.bpo-27169.VO84fQ.rst
new file mode 100644
index 00000000000..81d1d15cbbd
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2017-12-15-11-50-06.bpo-27169.VO84fQ.rst	
@@ -0,0 +1,2 @@
+The ``__debug__`` constant is now optimized out at compile time. This fixes also
+bpo-22091.
diff --git a/Python/compile.c b/Python/compile.c
index 4d525a02c81..9984d55858a 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -1343,13 +1343,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:
@@ -1364,6 +1366,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:
         assert(!is_const(e));
         return NULL;
@@ -3035,6 +3040,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);
@@ -3298,7 +3308,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);
         }
@@ -4044,35 +4054,10 @@ compiler_visit_keyword(struct compiler *c, keyword_ty k)
 static int
 expr_constant(struct compiler *c, expr_ty e)
 {
-    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;
 }
 
 



More information about the Python-checkins mailing list