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

pedronis at codespeak.net pedronis at codespeak.net
Tue May 31 23:35:19 CEST 2005


Author: pedronis
Date: Tue May 31 23:35:18 2005
New Revision: 12955

Modified:
   pypy/dist/pypy/rpython/lltype.py
   pypy/dist/pypy/rpython/test/test_lltype.py
Log:
support recursive lltype



Modified: pypy/dist/pypy/rpython/lltype.py
==============================================================================
--- pypy/dist/pypy/rpython/lltype.py	(original)
+++ pypy/dist/pypy/rpython/lltype.py	Tue May 31 23:35:18 2005
@@ -2,6 +2,25 @@
 import py
 from pypy.rpython.rarithmetic import r_uint
 from pypy.tool.uid import Hashable
+from pypy.tool.tls import tlsobject
+
+TLS = tlsobject()
+
+def saferecursive(func, defl):
+    def safe(*args):
+        try:
+            seeing = TLS.seeing
+        except AttributeError:
+            seeing = TLS.seeing = {}
+        seeingkey = tuple([func] + [id(arg) for arg in args])
+        if seeingkey in seeing:
+            return defl
+        seeing[seeingkey] = True
+        try:
+            return func(*args)
+        finally:
+            del seeing[seeingkey]
+    return safe
 
 class frozendict(dict):
 
@@ -9,11 +28,14 @@
         items = self.items()
         items.sort()
         return hash(tuple(items))
+    __hash__ = saferecursive(__hash__, 0)
 
 
 class LowLevelType(object):
     def __eq__(self, other):
         return self.__class__ is other.__class__ and self.__dict__ == other.__dict__
+    __eq__ = saferecursive(__eq__, True)
+
     def __ne__(self, other):
         return not (self == other)
 
@@ -21,6 +43,7 @@
         items = self.__dict__.items()
         items.sort()
         return hash((self.__class__,) + tuple(items))
+    __hash__ = saferecursive(__hash__, 0)
 
     def __repr__(self):
         return '<%s>' % (self,)
@@ -91,6 +114,7 @@
     def _str_fields(self):
         return ', '.join(['%s: %s' % (name, self._flds[name])
                           for name in self._names])
+    _str_fields = saferecursive(_str_fields, '...')
 
     def __str__(self):
         return "%s %s { %s }" % (self.__class__.__name__,

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	Tue May 31 23:35:18 2005
@@ -258,7 +258,7 @@
     assert S.x == GcPtr(S)
     py.test.raises(TypeError, "GcForwardReference().become(Struct('abc'))")
     ForwardReference().become(Struct('abc'))
-
+    hash(S)
 
 def test_nullptr():
     S = Struct('s')



More information about the Pypy-commit mailing list