[pypy-svn] r9929 - in pypy/dist/pypy/objspace/std: . test

arigo at codespeak.net arigo at codespeak.net
Sun Mar 20 16:51:17 CET 2005


Author: arigo
Date: Sun Mar 20 16:51:16 2005
New Revision: 9929

Modified:
   pypy/dist/pypy/objspace/std/test/test_tupleobject.py
   pypy/dist/pypy/objspace/std/tupleobject.py
Log:
tuple.__hash__().

Modified: pypy/dist/pypy/objspace/std/test/test_tupleobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_tupleobject.py	(original)
+++ pypy/dist/pypy/objspace/std/test/test_tupleobject.py	Sun Mar 20 16:51:16 2005
@@ -233,3 +233,12 @@
                            self.space.w_True)
         assert self.space.eq_w(self.space.le(w_tuple4, w_tuple3),
                            self.space.w_True)
+
+
+class AppTestW_TupleObject:
+
+    def test_hash(self):
+        # check that hash behaves as in 2.4 for at least 31 bits
+        assert hash(()) & 0x7fffffff == 0x35d373
+        assert hash((12,)) & 0x7fffffff == 0x1cca0557
+        assert hash((12,34)) & 0x7fffffff == 0x153e2a41

Modified: pypy/dist/pypy/objspace/std/tupleobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/tupleobject.py	(original)
+++ pypy/dist/pypy/objspace/std/tupleobject.py	Sun Mar 20 16:51:16 2005
@@ -1,5 +1,6 @@
 from pypy.objspace.std.objspace import *
 from pypy.objspace.std.intobject import W_IntObject
+from pypy.objspace.std.restricted_int import intmask
 from pypy.objspace.std.sliceobject import W_SliceObject
 from pypy.objspace.std import slicetype
 from pypy.interpreter import gateway
@@ -122,7 +123,16 @@
 repr__Tuple = app.interphook('repr__Tuple') 
 
 def hash__Tuple(space, w_tuple):
-    # silly-ish, but _correct_, while lacking it would be WRONG
-    return space.len(w_tuple)
+    # this is the CPython 2.4 algorithm (changed from 2.3)
+    mult = 1000003
+    x = 0x345678
+    z = len(w_tuple.wrappeditems)
+    for w_item in w_tuple.wrappeditems:
+        y = space.int_w(space.hash(w_item))
+        x = (x ^ y) * mult
+        z -= 1
+        mult += 82520 + z + z
+    x += 97531
+    return space.wrap(intmask(x))
 
 register_all(vars())



More information about the Pypy-commit mailing list