[pypy-svn] r65211 - in pypy/branch/pyjitpl5/pypy/rpython: lltypesystem test

arigo at codespeak.net arigo at codespeak.net
Mon May 11 12:10:07 CEST 2009


Author: arigo
Date: Mon May 11 12:10:05 2009
New Revision: 65211

Modified:
   pypy/branch/pyjitpl5/pypy/rpython/lltypesystem/rclass.py
   pypy/branch/pyjitpl5/pypy/rpython/lltypesystem/rstr.py
   pypy/branch/pyjitpl5/pypy/rpython/test/test_rclass.py
   pypy/branch/pyjitpl5/pypy/rpython/test/test_rstr.py
   pypy/branch/pyjitpl5/pypy/rpython/test/test_runicode.py
Log:
Give a uniform way to hash strings, unicodes, and instances given
a low-level pointer to them -- via an ADT method, gethash().


Modified: pypy/branch/pyjitpl5/pypy/rpython/lltypesystem/rclass.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/rpython/lltypesystem/rclass.py	(original)
+++ pypy/branch/pyjitpl5/pypy/rpython/lltypesystem/rclass.py	Mon May 11 12:10:05 2009
@@ -350,6 +350,10 @@
             if '_immutable_' in self.classdef.classdesc.classdict:
                 hints = hints.copy()
                 hints['immutable'] = True
+            if ('_hash_cache_' in fields or
+                '_hash_cache_' in self.rbase.allinstancefields):
+                adtmeths = adtmeths.copy()
+                adtmeths['gethash'] = self.get_ll_hash_function()
             object_type = MkStruct(self.classdef.name,
                                    ('super', self.rbase.object_type),
                                    hints=hints,

Modified: pypy/branch/pyjitpl5/pypy/rpython/lltypesystem/rstr.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/rpython/lltypesystem/rstr.py	(original)
+++ pypy/branch/pyjitpl5/pypy/rpython/lltypesystem/rstr.py	Mon May 11 12:10:05 2009
@@ -70,17 +70,6 @@
 copy_string_contents = _new_copy_contents_fun(STR, Char, 'string')
 copy_unicode_contents = _new_copy_contents_fun(UNICODE, UniChar, 'unicode')
 
-STR.become(GcStruct('rpy_string', ('hash',  Signed),
-                    ('chars', Array(Char, hints={'immutable': True})),
-                    adtmeths={'malloc' : staticAdtMethod(mallocstr),
-                              'empty'  : staticAdtMethod(emptystrfun),
-                              'copy_contents' : staticAdtMethod(copy_string_contents)}))
-UNICODE.become(GcStruct('rpy_unicode', ('hash', Signed),
-                        ('chars', Array(UniChar, hints={'immutable': True})),
-                        adtmeths={'malloc' : staticAdtMethod(mallocunicode),
-                                  'empty'  : staticAdtMethod(emptyunicodefun),
-                                  'copy_contents' : staticAdtMethod(copy_unicode_contents)}
-                        ))
 SIGNED_ARRAY = GcArray(Signed)
 CONST_STR_CACHE = WeakValueDictionary()
 CONST_UNICODE_CACHE = WeakValueDictionary()
@@ -843,6 +832,22 @@
 
 TEMP = GcArray(Ptr(STR))
 
+# ____________________________________________________________
+
+STR.become(GcStruct('rpy_string', ('hash',  Signed),
+                    ('chars', Array(Char, hints={'immutable': True})),
+                    adtmeths={'malloc' : staticAdtMethod(mallocstr),
+                              'empty'  : staticAdtMethod(emptystrfun),
+                              'copy_contents' : staticAdtMethod(copy_string_contents),
+                              'gethash': LLHelpers.ll_strhash}))
+UNICODE.become(GcStruct('rpy_unicode', ('hash', Signed),
+                        ('chars', Array(UniChar, hints={'immutable': True})),
+                        adtmeths={'malloc' : staticAdtMethod(mallocunicode),
+                                  'empty'  : staticAdtMethod(emptyunicodefun),
+                                  'copy_contents' : staticAdtMethod(copy_unicode_contents),
+                                  'gethash': LLHelpers.ll_strhash}
+                        ))
+
 
 # TODO: make the public interface of the rstr module cleaner
 ll_strconcat = LLHelpers.ll_strconcat

Modified: pypy/branch/pyjitpl5/pypy/rpython/test/test_rclass.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/rpython/test/test_rclass.py	(original)
+++ pypy/branch/pyjitpl5/pypy/rpython/test/test_rclass.py	Mon May 11 12:10:05 2009
@@ -781,6 +781,41 @@
         expected = hex(r_uint(xid)).lower().replace('l', '')
         assert expected in xstr
 
+    def test_hash_via_type(self):
+        from pypy.annotation import model as annmodel
+        from pypy.rpython import extregistry
+        from pypy.rpython.annlowlevel import cast_object_to_ptr
+        class X(object): pass
+        class Y(X): pass
+        class Z(Y): pass
+
+        def my_gethash(z):
+            not_implemented
+
+        def ll_my_gethash(ptr):
+            return ptr.gethash()
+
+        class MyGetHashEntry(extregistry.ExtRegistryEntry):
+            _about_ = my_gethash
+            def compute_result_annotation(self, s_instance):
+                return annmodel.SomeInteger()
+            def specialize_call(self, hop):
+                [v_instance] = hop.inputargs(*hop.args_r)
+                return hop.gendirectcall(ll_my_gethash, v_instance)
+
+        def f(n):
+            if n > 10:
+                z = Y()
+                got = -1    # path never used
+            else:
+                z = Z()
+                got = my_gethash(z)
+            expected = hash(z)     # put the _hash_cache_ in the class Y
+            return got - expected
+
+        res = self.interpret(f, [5])
+        assert res == 0
+
 
 class TestOOtype(BaseTestRclass, OORtypeMixin):
 

Modified: pypy/branch/pyjitpl5/pypy/rpython/test/test_rstr.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/rpython/test/test_rstr.py	(original)
+++ pypy/branch/pyjitpl5/pypy/rpython/test/test_rstr.py	Mon May 11 12:10:05 2009
@@ -1,7 +1,7 @@
 import random
 from pypy.rpython.lltypesystem.lltype import *
 from pypy.rpython.rstr import AbstractLLHelpers
-from pypy.rpython.lltypesystem.rstr import LLHelpers, mallocstr
+from pypy.rpython.lltypesystem.rstr import LLHelpers, STR
 from pypy.rpython.ootypesystem.ootype import make_string
 from pypy.rpython.rtyper import RPythonTyper, TyperError
 from pypy.rpython.test.tool import BaseRtypingTest, LLRtypeMixin, OORtypeMixin
@@ -861,6 +861,17 @@
             res = LLHelpers.ll_rfind(llstr(s1), llstr(s2), 0, n1)
             assert res == s1.rfind(s2)
 
+    def test_hash_via_type(self):
+        def f(n):
+            s = malloc(STR, n)
+            s.hash = 0
+            for i in range(n):
+                s.chars[i] = chr(i)
+            return s.gethash() - hash('\x00\x01\x02\x03\x04')
+
+        res = self.interpret(f, [5])
+        assert res == 0
+
 
 class TestOOtype(BaseTestRstr, OORtypeMixin):
 

Modified: pypy/branch/pyjitpl5/pypy/rpython/test/test_runicode.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/rpython/test/test_runicode.py	(original)
+++ pypy/branch/pyjitpl5/pypy/rpython/test/test_runicode.py	Mon May 11 12:10:05 2009
@@ -1,5 +1,6 @@
 
-
+from pypy.rpython.lltypesystem.lltype import malloc
+from pypy.rpython.lltypesystem.rstr import LLHelpers, UNICODE
 from pypy.rpython.test.tool import LLRtypeMixin, OORtypeMixin
 from pypy.rpython.test.test_rstr import AbstractTestRstr
 import py
@@ -214,5 +215,16 @@
 class TestLLtype(BaseTestRUnicode, LLRtypeMixin):
     EMPTY_STRING_HASH = -1
 
+    def test_hash_via_type(self):
+        def f(n):
+            s = malloc(UNICODE, n)
+            s.hash = 0
+            for i in range(n):
+                s.chars[i] = unichr(ord('A') + i)
+            return s.gethash() - hash(u'ABCDE')
+
+        res = self.interpret(f, [5])
+        assert res == 0
+
 class TestOOtype(BaseTestRUnicode, OORtypeMixin):
     EMPTY_STRING_HASH = 0



More information about the Pypy-commit mailing list