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

arigo at codespeak.net arigo at codespeak.net
Mon May 26 19:26:28 CEST 2003


Author: arigo
Date: Mon May 26 19:26:28 2003
New Revision: 478

Added:
   pypy/trunk/src/pypy/objspace/std/test/test_iterobject.py
   pypy/trunk/src/pypy/objspace/std/test/test_tupleobject.py
Modified:
   pypy/trunk/src/pypy/objspace/std/iterobject.py
   pypy/trunk/src/pypy/objspace/std/iterobject_app.py
   pypy/trunk/src/pypy/objspace/std/test/test_moduleobject.py
   pypy/trunk/src/pypy/objspace/std/tupleobject.py
Log:
sequence iterators

Modified: pypy/trunk/src/pypy/objspace/std/iterobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/iterobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/iterobject.py	Mon May 26 19:26:28 2003
@@ -1,10 +1,27 @@
-from pypy.objspace.std.objspace import *
+from objspace import *
 
-appfile = StdObjSpace.AppFile(__name__)
-W_SequenceIterator = pull_class_from_appfile(appfile, 'SequenceIterator')
 
-StdObjSpace.getiter.register(W_SequenceIterator.method('__iter__'),
-                             W_SequenceIterator....)
+class W_SeqIterObject:
+    delegate_once = {}
 
+    def __init__(self, w_seq, index=0):
+        self.w_seq = w_seq
+        self.index = index
 
-# XXX figure out some nice syntax to grab multimethods from the _app.py file
+
+def iter_seqiter(space, w_seqiter):
+    return w_seqiter
+
+StdObjSpace.iter.register(iter_seqiter, W_SeqIterObject)
+
+def next_seqiter(space, w_seqiter):
+    try:
+        w_item = space.getitem(w_seqiter.w_seq, space.wrap(w_seqiter.index))
+    except OperationError, e:
+        if e.match(space, space.w_IndexError):
+            raise NoValue
+        raise
+    w_seqiter.index += 1
+    return w_item
+
+StdObjSpace.next.register(next_seqiter, W_SeqIterObject)

Modified: pypy/trunk/src/pypy/objspace/std/iterobject_app.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/iterobject_app.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/iterobject_app.py	Mon May 26 19:26:28 2003
@@ -13,3 +13,5 @@
         self.it_index += 1
         return item
     # Yes, the implementation is complete, I think
+
+# XXX design a way to have this working and get rid of iterobject.py

Added: pypy/trunk/src/pypy/objspace/std/test/test_iterobject.py
==============================================================================
--- (empty file)
+++ pypy/trunk/src/pypy/objspace/std/test/test_iterobject.py	Mon May 26 19:26:28 2003
@@ -0,0 +1,33 @@
+import unittest, sys
+import testsupport
+from pypy.interpreter import unittest_w
+from pypy.objspace.std import iterobject as iobj
+from pypy.objspace.std.objspace import *
+
+
+class TestW_IterObject(unittest_w.TestCase_w):
+
+    def setUp(self):
+        self.space = StdObjSpace()
+
+    def tearDown(self):
+        pass
+
+    def test_iter(self):
+        w = self.space.wrap
+        w_tuple = self.space.newtuple([w(5), w(3), w(99)])
+        w_iter = iobj.W_SeqIterObject(w_tuple)
+        self.assertEqual_w(self.space.next(w_iter), w(5))
+        self.assertEqual_w(self.space.next(w_iter), w(3))
+        self.assertEqual_w(self.space.next(w_iter), w(99))
+        self.assertRaises(NoValue, self.space.next, w_iter)
+        self.assertRaises(NoValue, self.space.next, w_iter)
+
+    def test_emptyiter(self):
+        w_list = self.space.newlist([])
+        w_iter = iobj.W_SeqIterObject(w_list)
+        self.assertRaises(NoValue, self.space.next, w_iter)
+        self.assertRaises(NoValue, self.space.next, w_iter)
+
+if __name__ == '__main__':
+    unittest.main()

Modified: pypy/trunk/src/pypy/objspace/std/test/test_moduleobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/test/test_moduleobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/test/test_moduleobject.py	Mon May 26 19:26:28 2003
@@ -31,6 +31,7 @@
         space.delattr(w_m, space.wrap('x'))
         self.assertRaises_w(space.w_AttributeError, space.getattr,
                             w_m, space.wrap('x'))
+        self.assertEqual_w(space.getattr(w_m, space.wrap('yy')), w_yy)
 
 if __name__ == '__main__':
     unittest.main()

Added: pypy/trunk/src/pypy/objspace/std/test/test_tupleobject.py
==============================================================================
--- (empty file)
+++ pypy/trunk/src/pypy/objspace/std/test/test_tupleobject.py	Mon May 26 19:26:28 2003
@@ -0,0 +1,50 @@
+import unittest, sys
+import testsupport
+from pypy.interpreter import unittest_w
+from pypy.objspace.std import tupleobject as tobj
+from pypy.objspace.std.objspace import *
+
+
+class TestW_TupleObject(unittest_w.TestCase_w):
+
+    def setUp(self):
+        self.space = StdObjSpace()
+
+    def tearDown(self):
+        pass
+
+    def test_is_true(self):
+        w = self.space.wrap
+        w_tuple = tobj.W_TupleObject([])
+        self.assertEqual(self.space.is_true(w_tuple), False)
+        w_tuple = tobj.W_TupleObject([w(5)])
+        self.assertEqual(self.space.is_true(w_tuple), True)
+        w_tuple = tobj.W_TupleObject([w(5), w(3)])
+        self.assertEqual(self.space.is_true(w_tuple), True)
+
+    def test_getitem(self):
+        w = self.space.wrap
+        w_tuple = tobj.W_TupleObject([w(5), w(3)])
+        self.assertEqual_w(self.space.getitem(w_tuple, w(0)), w(5))
+        self.assertEqual_w(self.space.getitem(w_tuple, w(1)), w(3))
+        self.assertEqual_w(self.space.getitem(w_tuple, w(-2)), w(5))
+        self.assertEqual_w(self.space.getitem(w_tuple, w(-1)), w(3))
+        self.assertRaises_w(self.space.w_IndexError,
+                            self.space.getitem, w_tuple, w(2))
+        self.assertRaises_w(self.space.w_IndexError,
+                            self.space.getitem, w_tuple, w(42))
+        self.assertRaises_w(self.space.w_IndexError,
+                            self.space.getitem, w_tuple, w(-3))
+
+    def test_iter(self):
+        w = self.space.wrap
+        w_tuple = tobj.W_TupleObject([w(5), w(3), w(99)])
+        w_iter = self.space.iter(w_tuple)
+        self.assertEqual_w(self.space.next(w_iter), w(5))
+        self.assertEqual_w(self.space.next(w_iter), w(3))
+        self.assertEqual_w(self.space.next(w_iter), w(99))
+        self.assertRaises(NoValue, self.space.next, w_iter)
+        self.assertRaises(NoValue, self.space.next, w_iter)
+
+if __name__ == '__main__':
+    unittest.main()

Modified: pypy/trunk/src/pypy/objspace/std/tupleobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/tupleobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/tupleobject.py	Mon May 26 19:26:28 2003
@@ -32,7 +32,17 @@
 
 def tuple_getitem(space, w_tuple, w_index):
     items = w_tuple.wrappeditems
-    w_item = items[w_index.intval]
+    try:
+        w_item = items[w_index.intval]
+    except IndexError:
+        raise OperationError(space.w_IndexError,
+                             space.wrap("tuple index out of range"))
     return w_item
 
 StdObjSpace.getitem.register(tuple_getitem, W_TupleObject, W_IntObject)
+
+def tuple_iter(space, w_tuple):
+    import iterobject
+    return iterobject.W_SeqIterObject(w_tuple)
+
+StdObjSpace.iter.register(tuple_iter, W_TupleObject)


More information about the Pypy-commit mailing list