[pypy-svn] r65137 - in pypy/branch/io-lang/pypy/lang/io: . test

david at codespeak.net david at codespeak.net
Thu May 7 15:49:48 CEST 2009


Author: david
Date: Thu May  7 15:49:47 2009
New Revision: 65137

Modified:
   pypy/branch/io-lang/pypy/lang/io/model.py
   pypy/branch/io-lang/pypy/lang/io/test/test_model.py
Log:
(cfbolz, david) loop detection in lookup

Modified: pypy/branch/io-lang/pypy/lang/io/model.py
==============================================================================
--- pypy/branch/io-lang/pypy/lang/io/model.py	(original)
+++ pypy/branch/io-lang/pypy/lang/io/model.py	Thu May  7 15:49:47 2009
@@ -12,13 +12,20 @@
     def __ne__(self, other):
         return not self == other
 
-    def lookup(self, name):
+    def lookup(self, name, seen=None):
+        if seen is None:
+            seen = {}
+        else:
+            if self in seen:
+                return None
+        seen[self] = None
+
         try:
             return self.slots[name]
         except KeyError:
             pass
-        for x in self.protos:
-            t = x.lookup(name)
+        for x in self.protos:            
+            t = x.lookup(name, seen)
             if t is not None:
                 return t
 

Modified: pypy/branch/io-lang/pypy/lang/io/test/test_model.py
==============================================================================
--- pypy/branch/io-lang/pypy/lang/io/test/test_model.py	(original)
+++ pypy/branch/io-lang/pypy/lang/io/test/test_model.py	Thu May  7 15:49:47 2009
@@ -38,4 +38,27 @@
     x = W_Number(space, 2)
     xx = x.clone()
     assert xx.protos == [x]
-    assert isinstance(xx, W_Number)
\ No newline at end of file
+    assert isinstance(xx, W_Number)
+    
+def test_lookup_cycles():
+    obj = W_Object(None, )
+    obj.protos.append(obj)
+    a = obj.slots['not_fail'] = W_Object(None)
+    assert obj.lookup("not_fail") is a
+    assert obj.lookup('fail') is None
+    
+def test_lookup_cycling_complex():
+    space = ObjSpace()
+    a = W_Object(None, )
+    b = W_Object(None, )
+    c = W_Object(None, )
+    a.protos += [a, c]
+    c.protos += [b]
+    b.protos += [b]
+    a.slots['a'] = W_Number(space, 1)
+    b.slots['b'] = W_Number(space, 2)
+    c.slots['c'] = W_Number(space, 3)
+    assert a.lookup('fail') is None
+    assert a.lookup('a').value == 1
+    assert a.lookup('b').value == 2
+    assert a.lookup('c').value == 3
\ No newline at end of file



More information about the Pypy-commit mailing list