[pypy-svn] r24967 - in pypy/dist/pypy/rpython: . lltypesystem test

nik at codespeak.net nik at codespeak.net
Fri Mar 24 16:01:51 CET 2006


Author: nik
Date: Fri Mar 24 16:01:50 2006
New Revision: 24967

Modified:
   pypy/dist/pypy/rpython/lltypesystem/rtuple.py
   pypy/dist/pypy/rpython/rtuple.py
   pypy/dist/pypy/rpython/test/test_rtuple.py
Log:
support hashing, make more tuple tests pass on ootypesystem. add a test
about tuple -> list conversion (skipped for ootypes for now). only
iterators now left for full tuple support in ootypes.


Modified: pypy/dist/pypy/rpython/lltypesystem/rtuple.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/rtuple.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/rtuple.py	Fri Mar 24 16:01:50 2006
@@ -23,61 +23,6 @@
 #        ...
 #    }
 
-_gen_eq_function_cache = {}
-_gen_hash_function_cache = {}
-
-def gen_eq_function(items_r):
-    eq_funcs = [r_item.get_ll_eq_function() or operator.eq for r_item in items_r]
-    key = tuple(eq_funcs)
-    try:
-        return _gen_eq_function_cache[key]
-    except KeyError:
-        miniglobals = {}
-        source = """
-def ll_eq(t1, t2):
-    %s
-    return True
-"""
-        body = []
-        for i, eq_func in enumerate(eq_funcs):
-            miniglobals['eq%d' % i] = eq_func
-            body.append("if not eq%d(t1.item%d, t2.item%d): return False" % (i, i, i))
-        body = ('\n'+' '*4).join(body)
-        source = source % body
-        exec source in miniglobals
-        ll_eq = miniglobals['ll_eq']
-        _gen_eq_function_cache[key] = ll_eq
-        return ll_eq
-
-def gen_hash_function(items_r):
-    # based on CPython
-    hash_funcs = [r_item.get_ll_hash_function() for r_item in items_r]
-    key = tuple(hash_funcs)
-    try:
-        return _gen_hash_function_cache[key]
-    except KeyError:
-        miniglobals = {}
-        source = """
-def ll_hash(t):
-    retval = 0x345678
-    %s
-    return retval
-"""
-        body = []
-        mult = 1000003
-        for i, hash_func in enumerate(hash_funcs):
-            miniglobals['hash%d' % i] = hash_func
-            body.append("retval = (retval ^ hash%d(t.item%d)) * %d" %
-                        (i, i, mult))
-            mult = intmask(mult + 82520 + 2*len(items_r))
-        body = ('\n'+' '*4).join(body)
-        source = source % body
-        exec source in miniglobals
-        ll_hash = miniglobals['ll_hash']
-        _gen_hash_function_cache[key] = ll_hash
-        return ll_hash
-
-
 class TupleRepr(AbstractTupleRepr):
 
     def __init__(self, rtyper, items_r):
@@ -100,15 +45,6 @@
     def instantiate(self):
         return malloc(self.lowleveltype.TO)
 
-    #def get_eqfunc(self):
-    #    return inputconst(Void, self.item_repr.get_ll_eq_function())
-
-    def get_ll_eq_function(self):
-        return gen_eq_function(self.items_r)
-
-    def get_ll_hash_function(self):
-        return gen_hash_function(self.items_r)    
-
     def rtype_bltn_list(self, hop):
         from pypy.rpython import rlist
         nitems = len(self.items_r)

Modified: pypy/dist/pypy/rpython/rtuple.py
==============================================================================
--- pypy/dist/pypy/rpython/rtuple.py	(original)
+++ pypy/dist/pypy/rpython/rtuple.py	Fri Mar 24 16:01:50 2006
@@ -20,6 +20,62 @@
         keys = [rtyper.makekey(s_item) for s_item in self.items]
         return tuple([self.__class__]+keys)
 
+
+_gen_eq_function_cache = {}
+_gen_hash_function_cache = {}
+
+def gen_eq_function(items_r):
+    eq_funcs = [r_item.get_ll_eq_function() or operator.eq for r_item in items_r]
+    key = tuple(eq_funcs)
+    try:
+        return _gen_eq_function_cache[key]
+    except KeyError:
+        miniglobals = {}
+        source = """
+def ll_eq(t1, t2):
+    %s
+    return True
+"""
+        body = []
+        for i, eq_func in enumerate(eq_funcs):
+            miniglobals['eq%d' % i] = eq_func
+            body.append("if not eq%d(t1.item%d, t2.item%d): return False" % (i, i, i))
+        body = ('\n'+' '*4).join(body)
+        source = source % body
+        exec source in miniglobals
+        ll_eq = miniglobals['ll_eq']
+        _gen_eq_function_cache[key] = ll_eq
+        return ll_eq
+
+def gen_hash_function(items_r):
+    # based on CPython
+    hash_funcs = [r_item.get_ll_hash_function() for r_item in items_r]
+    key = tuple(hash_funcs)
+    try:
+        return _gen_hash_function_cache[key]
+    except KeyError:
+        miniglobals = {}
+        source = """
+def ll_hash(t):
+    retval = 0x345678
+    %s
+    return retval
+"""
+        body = []
+        mult = 1000003
+        for i, hash_func in enumerate(hash_funcs):
+            miniglobals['hash%d' % i] = hash_func
+            body.append("retval = (retval ^ hash%d(t.item%d)) * %d" %
+                        (i, i, mult))
+            mult = intmask(mult + 82520 + 2*len(items_r))
+        body = ('\n'+' '*4).join(body)
+        source = source % body
+        exec source in miniglobals
+        ll_hash = miniglobals['ll_hash']
+        _gen_hash_function_cache[key] = ll_hash
+        return ll_hash
+
+
 class AbstractTupleRepr(Repr):
 
     def __init__(self, rtyper, items_r):
@@ -66,6 +122,12 @@
     def rtype_len(self, hop):
         return hop.inputconst(Signed, len(self.items_r))
 
+    def get_ll_eq_function(self):
+        return gen_eq_function(self.items_r)
+
+    def get_ll_hash_function(self):
+        return gen_hash_function(self.items_r)    
+
 
 class __extend__(pairtype(AbstractTupleRepr, IntegerRepr)):
 

Modified: pypy/dist/pypy/rpython/test/test_rtuple.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rtuple.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rtuple.py	Fri Mar 24 16:01:50 2006
@@ -133,11 +133,70 @@
             return x[1]
         
         res = self.interpret(f, [0])
-        if self.type_system == "lltype":
-            assert ''.join(res.super.typeptr.name) == "B\00"
-        else:
-            assert ootype.dynamicType(res)._name.split(".")[-1] == "B"
+        assert self.class_name(res) == "B"
+        
+    def test_inst_tuple_add_getitem(self):
+        class A:
+            pass
+        class B(A):
+            pass
+
+        def f(i):
+            x = (1, A())
+            y = (2, B())
+            if i:
+                z = x + y
+            else:
+                z = y + x
+            return z[1]
+        
+        res = self.interpret(f, [1])
+        assert self.class_name(res) == "A"
+
+        res = self.interpret(f, [0])
+        assert self.class_name(res) == "B"
+        
+    def test_type_erase(self):
+        class A(object):
+            pass
+        class B(object):
+            pass
+
+        def f():
+            return (A(), B()), (B(), A())
+
+        t = TranslationContext()
+        s = t.buildannotator().build_types(f, [])
+        rtyper = t.buildrtyper(type_system=self.type_system)
+        rtyper.specialize()
+
+        s_AB_tup = s.items[0]
+        s_BA_tup = s.items[1]
+        
+        r_AB_tup = rtyper.getrepr(s_AB_tup)
+        r_BA_tup = rtyper.getrepr(s_AB_tup)
+
+        assert r_AB_tup.lowleveltype == r_BA_tup.lowleveltype
+
+    def test_tuple_hash(self):
+        def f(i, j):
+            return hash((i, j))
+
+        res1 = self.interpret(f, [12, 27])
+        res2 = self.interpret(f, [27, 12])
+        assert res1 != res2
+
+    def test_tuple_to_list(self):
+        if self.type_system == "ootype":
+            py.test.skip("XXX fix me if ootypes support lists")
         
+        def f(i, j):
+            return list((i, j))
+
+        res = self.interpret(f, [2, 3])
+        assert res._obj.items == [2, 3]
+
+
 def test_tuple_iterator_length1():
     def f(i):
         total = 0
@@ -166,66 +225,18 @@
     res = interpret(f, [0])
     assert ''.join(res.super.typeptr.name) == "B\00"
 
-    
-def test_inst_tuple_add_getitem():
-    class A:
-        pass
-    class B(A):
-        pass
-
-    def f(i):
-        x = (1, A())
-        y = (2, B())
-        if i:
-            z = x + y
-        else:
-            z = y + x
-        return z[1]
-    
-    res = interpret(f, [1])
-    assert ''.join(res.super.typeptr.name) == "A\00"
-
-    res = interpret(f, [0])
-    assert ''.join(res.super.typeptr.name) == "B\00"
-    
-
-def test_type_erase():
-    class A(object):
-        pass
-    class B(object):
-        pass
-
-    def f():
-        return (A(), B()), (B(), A())
-
-    t = TranslationContext()
-    s = t.buildannotator().build_types(f, [])
-    rtyper = t.buildrtyper()
-    rtyper.specialize()
-
-    s_AB_tup = s.items[0]
-    s_BA_tup = s.items[1]
-    
-    r_AB_tup = rtyper.getrepr(s_AB_tup)
-    r_BA_tup = rtyper.getrepr(s_AB_tup)
-
-    assert r_AB_tup.lowleveltype == r_BA_tup.lowleveltype
-
-
-def test_tuple_hash():
-    def f(i, j):
-        return hash((i, j))
-
-    res1 = interpret(f, [12, 27])
-    res2 = interpret(f, [27, 12])
-    assert res1 != res2
-
 
 class TestLLTuple(AbstractTestRTuple):
 
     type_system = "lltype"
 
+    def class_name(self, value):
+        return "".join(value.super.typeptr.name)[:-1]
+
 class TestOOTuple(AbstractTestRTuple):
 
     type_system = "ootype"
 
+    def class_name(self, value):
+        return ootype.dynamicType(value)._name.split(".")[-1] 
+



More information about the Pypy-commit mailing list