[pypy-svn] r66579 - in pypy/branch/parser-compiler/pypy/interpreter: astcompiler test

benjamin at codespeak.net benjamin at codespeak.net
Fri Jul 24 14:45:24 CEST 2009


Author: benjamin
Date: Fri Jul 24 14:45:24 2009
New Revision: 66579

Modified:
   pypy/branch/parser-compiler/pypy/interpreter/astcompiler/assemble.py
   pypy/branch/parser-compiler/pypy/interpreter/astcompiler/codegen.py
   pypy/branch/parser-compiler/pypy/interpreter/test/test_compiler.py
Log:
store the type of the contents of the tuple in the constant array

Modified: pypy/branch/parser-compiler/pypy/interpreter/astcompiler/assemble.py
==============================================================================
--- pypy/branch/parser-compiler/pypy/interpreter/astcompiler/assemble.py	(original)
+++ pypy/branch/parser-compiler/pypy/interpreter/astcompiler/assemble.py	Fri Jul 24 14:45:24 2009
@@ -180,17 +180,18 @@
             container[name] = index
         return index
 
-    def add_const(self, obj):
+    def add_const(self, obj, w_key=None):
         space = self.space
-        w_key = space.newtuple([obj, space.type(obj)])
+        if w_key is None:
+            w_key = space.newtuple([obj, space.type(obj)])
         w_len = space.finditem(self.w_consts, w_key)
         if w_len is None:
             w_len = space.len(self.w_consts)
             space.setitem(self.w_consts, w_key, w_len)
         return space.int_w(w_len)
 
-    def load_const(self, obj):
-        index = self.add_const(obj)
+    def load_const(self, obj, w_key=None):
+        index = self.add_const(obj, w_key)
         self.emit_op_arg(ops.LOAD_CONST, index)
 
     def update_position(self, lineno, force=False):

Modified: pypy/branch/parser-compiler/pypy/interpreter/astcompiler/codegen.py
==============================================================================
--- pypy/branch/parser-compiler/pypy/interpreter/astcompiler/codegen.py	(original)
+++ pypy/branch/parser-compiler/pypy/interpreter/astcompiler/codegen.py	Fri Jul 24 14:45:24 2009
@@ -767,7 +767,22 @@
 
     def visit_Const(self, const):
         self.update_position(const.lineno)
-        self.load_const(const.value)
+        space = self.space
+        value = const.value
+        # Constant tuples are tricky because we have to avoid letting equal
+        # values of the tuple that are not the same types being held together in
+        # the constant dict.  So we make the key include the type of each item
+        # in the sequence.
+        if space.is_true(space.isinstance(value, space.w_tuple)):
+            length = space.int_w(space.len(value))
+            key_w = [None]*(length + 2)
+            key_w[0] = value
+            for i in range(1, length + 1):
+                key_w[i] = space.type(space.getitem(value, space.wrap(i - 1)))
+            key_w[-1] = space.w_tuple
+            self.load_const(value, space.newtuple(key_w))
+        else:
+            self.load_const(value)
 
     def visit_UnaryOp(self, op):
         self.update_position(op.lineno)

Modified: pypy/branch/parser-compiler/pypy/interpreter/test/test_compiler.py
==============================================================================
--- pypy/branch/parser-compiler/pypy/interpreter/test/test_compiler.py	(original)
+++ pypy/branch/parser-compiler/pypy/interpreter/test/test_compiler.py	Fri Jul 24 14:45:24 2009
@@ -748,6 +748,12 @@
         op = ord(co[0]) + (ord(co[1]) << 8)
         assert op == opcode.opmap["LOAD_CONST"]
 
+    def test_tuple_constants(self):
+        ns = {}
+        exec "x = (1, 0); y = (1L, 0L)" in ns
+        assert isinstance(ns["x"][0], int)
+        assert isinstance(ns["y"][0], long)
+
     def test_division_folding(self):
         def code(source):
             return compile(source, "<test>", "exec")



More information about the Pypy-commit mailing list