]> granicus.if.org Git - python/commitdiff
Fix constantification of None.
authorRaymond Hettinger <python@rcn.com>
Fri, 2 Mar 2007 19:19:05 +0000 (19:19 +0000)
committerRaymond Hettinger <python@rcn.com>
Fri, 2 Mar 2007 19:19:05 +0000 (19:19 +0000)
Lib/test/test_peepholer.py
Misc/NEWS
Python/compile.c

index 4385a84c1fbf255f4c21d36f50e6acabf6c81f1d..16d0268b1cf310f13676ddd52cc01b87a3fe9b09 100644 (file)
@@ -49,6 +49,11 @@ class TestTranforms(unittest.TestCase):
             self.assert_(elem not in asm)
         for elem in ('LOAD_CONST', '(None)'):
             self.assert_(elem in asm)
+        def f():
+            'Adding a docstring made this test fail in Py2.5.0'
+            return None
+        self.assert_('LOAD_CONST' in disassemble(f))
+        self.assert_('LOAD_GLOBAL' not in disassemble(f))
 
     def test_while_one(self):
         # Skip over:  LOAD_CONST trueconst  JUMP_IF_FALSE xx  POP_TOP
index 8b0e7e21dba9d6a101a64b69ce43ae5a07a97414..c3fabfeaeafa755ce2c19cf46be2326d0043d3e2 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -15,6 +15,9 @@ Core and builtins
 - Bug #1669182: prevent crash when trying to print an unraisable error
   from a string exception.
 
+- The peephole optimizer left None as a global in functions with a docstring
+  and an explicit return value.
+
 - Bug #1653736: Properly discard third argument to slot_nb_inplace_power.
 
 - SF #151204:  enumerate() now raises an Overflow error at sys.maxint items.
index 8be3d79be9f69e21a12e2267a3a3dcd8318b2b4c..e493beb6c2fe9be9e14f4356a8f294a7d12ff5dd 100644 (file)
@@ -773,13 +773,17 @@ optimize_code(PyObject *code, PyObject* consts, PyObject *names,
                                if (name == NULL  ||  strcmp(name, "None") != 0)
                                        continue;
                                for (j=0 ; j < PyList_GET_SIZE(consts) ; j++) {
-                                       if (PyList_GET_ITEM(consts, j) == Py_None) {
-                                               codestr[i] = LOAD_CONST;
-                                               SETARG(codestr, i, j);
-                                               cumlc = lastlc + 1;
+                                       if (PyList_GET_ITEM(consts, j) == Py_None)
                                                break;
-                                       }
                                }
+                               if (j == PyList_GET_SIZE(consts)) {
+                                       if (PyList_Append(consts, Py_None) == -1)
+                                               goto exitUnchanged;                                        
+                               }
+                               assert(PyList_GET_ITEM(consts, j) == Py_None);
+                               codestr[i] = LOAD_CONST;
+                               SETARG(codestr, i, j);
+                               cumlc = lastlc + 1;
                                break;
 
                                /* Skip over LOAD_CONST trueconst