[pypy-svn] rev 1117 - in pypy/trunk/src/pypy/objspace/std: . test

mwh at codespeak.net mwh at codespeak.net
Tue Jul 8 13:51:26 CEST 2003


Author: mwh
Date: Tue Jul  8 13:51:25 2003
New Revision: 1117

Modified:
   pypy/trunk/src/pypy/objspace/std/test/test_iterobject.py
   pypy/trunk/src/pypy/objspace/std/userobject.py
Log:
support falling back from __iter__ methods to __getitem__
methods


Modified: pypy/trunk/src/pypy/objspace/std/test/test_iterobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/test/test_iterobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/test/test_iterobject.py	Tue Jul  8 13:51:25 2003
@@ -56,5 +56,19 @@
                 return self
         self.assertEquals(list(C()), [])
 
+    def test_iter_getitem(self):
+        class C:
+            def __getitem__(self, i):
+                return range(2)[i]
+        self.assertEquals(list(C()), range(2))
+
+    def test_iter_fail_noseq(self):
+        class C:
+            pass
+        self.assertRaises(TypeError,
+                          iter,
+                          C())
+        
+
 if __name__ == '__main__':
     test.main()

Modified: pypy/trunk/src/pypy/objspace/std/userobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/userobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/userobject.py	Tue Jul  8 13:51:25 2003
@@ -163,6 +163,19 @@
         "For __init__()."
         return self.internal_do_call(space, w_userobj, w_args, w_kwds)
 
+    def iter_call(self, space, *args_w):
+        try:
+            return self.do_call(space, args_w)
+        except FailedToImplement:
+            w_userobj = args_w[0]
+            try:
+                space.type(w_userobj).lookup(space.wrap('__getitem__'))
+            except KeyError:
+                raise FailedToImplement
+            else:
+                from iterobject import W_SeqIterObject
+                return W_SeqIterObject(space, args_w[0])
+
 
 import new
 for multimethod in typeobject.hack_out_multimethods(StdObjSpace):
@@ -175,5 +188,6 @@
 next__User    = SpecialMethod('next').next_call
 is_true__User = SpecialMethod('nonzero').nonzero_call
 object_init__User_ANY_ANY = SpecialMethod('__init__').argskwds_call
+iter__User = SpecialMethod('__iter__').iter_call
 
 register_all(vars())


More information about the Pypy-commit mailing list