[pypy-svn] r13195 - in pypy/dist/pypy/rpython: . test
arigo at codespeak.net
arigo at codespeak.net
Wed Jun 8 19:40:43 CEST 2005
Author: arigo
Date: Wed Jun 8 19:40:41 2005
New Revision: 13195
Modified:
pypy/dist/pypy/rpython/lltype.py
pypy/dist/pypy/rpython/test/test_lltype.py
Log:
Problem with hash() of recursive structures.
Modified: pypy/dist/pypy/rpython/lltype.py
==============================================================================
--- pypy/dist/pypy/rpython/lltype.py (original)
+++ pypy/dist/pypy/rpython/lltype.py Wed Jun 8 19:40:41 2005
@@ -28,7 +28,6 @@
items = self.items()
items.sort()
return hash(tuple(items))
- __hash__ = saferecursive(__hash__, 0)
class LowLevelType(object):
@@ -40,10 +39,21 @@
return not (self == other)
def __hash__(self):
+ # cannot use saferecursive() -- see test_lltype.test_hash().
+ # this version uses a compromize between computation time and
+ # collision-avoidance that can be customized if needed.
+ try:
+ if TLS.nested_hash_level >= 3:
+ return 0
+ except AttributeError:
+ TLS.nested_hash_level = 0
items = self.__dict__.items()
items.sort()
- return hash((self.__class__,) + tuple(items))
- __hash__ = saferecursive(__hash__, 0)
+ TLS.nested_hash_level += 1
+ try:
+ return hash((self.__class__,) + tuple(items))
+ finally:
+ TLS.nested_hash_level -= 1
def __repr__(self):
return '<%s>' % (self,)
Modified: pypy/dist/pypy/rpython/test/test_lltype.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_lltype.py (original)
+++ pypy/dist/pypy/rpython/test/test_lltype.py Wed Jun 8 19:40:41 2005
@@ -241,3 +241,12 @@
S = Struct('s')
p0 = nullptr(S)
assert not p0
+
+def test_hash():
+ S = ForwardReference()
+ S.become(Struct('S', ('p', Ptr(S))))
+ assert S == S
+ S1 = Struct('S', ('p', Ptr(S)))
+ assert S1 == S
+ assert S == S1
+ assert hash(S1) == hash(S)
More information about the Pypy-commit
mailing list