[pypy-svn] r39803 - in pypy/branch/rope-branch/pypy/objspace/std: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Sat Mar 3 14:49:26 CET 2007


Author: cfbolz
Date: Sat Mar  3 14:49:24 2007
New Revision: 39803

Modified:
   pypy/branch/rope-branch/pypy/objspace/std/ropeobject.py
   pypy/branch/rope-branch/pypy/objspace/std/test/test_ropeobject.py
Log:
add hash cache to rope objects


Modified: pypy/branch/rope-branch/pypy/objspace/std/ropeobject.py
==============================================================================
--- pypy/branch/rope-branch/pypy/objspace/std/ropeobject.py	(original)
+++ pypy/branch/rope-branch/pypy/objspace/std/ropeobject.py	Sat Mar  3 14:49:24 2007
@@ -16,6 +16,7 @@
 
     def __init__(w_self, node):
         w_self._node = node
+        w_self._w_hash = None
 
     def __repr__(w_self):
         """ representation for debugging purposes """
@@ -787,9 +788,12 @@
     return w_str._node.flatten()
 
 def hash__Rope(space, w_str):
-    node = w_str._node
-    x = rope.hash_rope(node)
-    return wrapint(space, x)
+    w_hash = w_str._w_hash
+    if w_hash is None:
+        node = w_str._node
+        x = rope.hash_rope(node)
+        w_hash = node._w_hash = wrapint(space, x)
+    return w_hash
 
 def lt__Rope_Rope(space, w_str1, w_str2):
     n1 = w_str1._node

Modified: pypy/branch/rope-branch/pypy/objspace/std/test/test_ropeobject.py
==============================================================================
--- pypy/branch/rope-branch/pypy/objspace/std/test/test_ropeobject.py	(original)
+++ pypy/branch/rope-branch/pypy/objspace/std/test/test_ropeobject.py	Sat Mar  3 14:49:24 2007
@@ -27,6 +27,18 @@
         s += '3'
         raises(TypeError, ord, s)
 
+    def test_hash_twice(self):
+        # check that we have the same hash as CPython for at least 31 bits
+        # (but don't go checking CPython's special case -1)
+        # check twice to catch hash cache problems`
+        s1 = 'hello'
+        s2 = 'hello world!'
+        assert hash(s1) & 0x7fffffff == 0x347697fd
+        assert hash(s1) & 0x7fffffff == 0x347697fd
+        assert hash(s2) & 0x7fffffff == 0x2f0bb411
+        assert hash(s2) & 0x7fffffff == 0x2f0bb411
+
+
 class AppTestRopeUnicode(object):
 
     def setup_class(cls):



More information about the Pypy-commit mailing list