[pypy-svn] r74534 - in pypy/trunk/pypy/module/cpyext: . test

afa at codespeak.net afa at codespeak.net
Mon May 17 19:13:44 CEST 2010


Author: afa
Date: Mon May 17 19:13:43 2010
New Revision: 74534

Modified:
   pypy/trunk/pypy/module/cpyext/iterator.py
   pypy/trunk/pypy/module/cpyext/stubsactive.py
   pypy/trunk/pypy/module/cpyext/test/test_iterator.py
Log:
Provide real implementations of PyObject_GetIter() and PyIter_Next()


Modified: pypy/trunk/pypy/module/cpyext/iterator.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/iterator.py	(original)
+++ pypy/trunk/pypy/module/cpyext/iterator.py	Mon May 17 19:13:43 2010
@@ -14,6 +14,28 @@
     """
     return operation.iter_sentinel(space, w_callable, w_sentinel)
 
+ at cpython_api([PyObject], PyObject)
+def PyObject_GetIter(space, w_obj):
+    """This is equivalent to the Python expression iter(o). It returns a new
+    iterator for the object argument, or the object itself if the object is
+    already an iterator.  Raises TypeError and returns NULL if the object
+    cannot be iterated."""
+    return space.iter(w_obj)
+
+ at cpython_api([PyObject], PyObject, error=CANNOT_FAIL)
+def PyIter_Next(space, w_obj):
+    """Return the next value from the iteration o.  If the object is an
+    iterator, this retrieves the next value from the iteration, and returns
+    NULL with no exception set if there are no remaining items.  If the object
+    is not an iterator, TypeError is raised, or if there is an error in
+    retrieving the item, returns NULL and passes along the exception."""
+    try:
+        return space.next(w_obj)
+    except OperationError, e:
+        if not e.match(space, space.w_StopIteration):
+            raise
+    return None
+
 @cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
 def PyIter_Check(space, w_obj):
     """Return true if the object o supports the iterator protocol."""

Modified: pypy/trunk/pypy/module/cpyext/stubsactive.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/stubsactive.py	(original)
+++ pypy/trunk/pypy/module/cpyext/stubsactive.py	Mon May 17 19:13:43 2010
@@ -13,23 +13,6 @@
     PyFile_DecUseCount() functions described below as appropriate."""
     raise NotImplementedError
 
- at cpython_api([PyObject], PyObject)
-def PyObject_GetIter(space, o):
-    """This is equivalent to the Python expression iter(o). It returns a new
-    iterator for the object argument, or the object  itself if the object is already
-    an iterator.  Raises TypeError and returns NULL if the object cannot be
-    iterated."""
-    raise NotImplementedError
-
- at cpython_api([PyObject], PyObject)
-def PyIter_Next(space, o):
-    """Return the next value from the iteration o.  If the object is an iterator,
-    this retrieves the next value from the iteration, and returns NULL with no
-    exception set if there are no remaining items.  If the object is not an
-    iterator, TypeError is raised, or if there is an error in retrieving the
-    item, returns NULL and passes along the exception."""
-    raise NotImplementedError
-
 @cpython_api([rffi.ULONG], PyObject)
 def PyLong_FromUnsignedLong(space, v):
     """Return a new PyLongObject object from a C unsigned long, or

Modified: pypy/trunk/pypy/module/cpyext/test/test_iterator.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/test_iterator.py	(original)
+++ pypy/trunk/pypy/module/cpyext/test/test_iterator.py	Mon May 17 19:13:43 2010
@@ -7,3 +7,11 @@
         assert api.PyIter_Check(space.wrap(iter([])))
         assert not api.PyIter_Check(space.wrap(type))
         assert not api.PyIter_Check(space.wrap(2))
+
+    def test_getIter(self, space, api):
+        w_iter = api.PyObject_GetIter(space.wrap([1, 2, 3]))
+        assert space.unwrap(api.PyIter_Next(w_iter)) == 1
+        assert space.unwrap(api.PyIter_Next(w_iter)) == 2
+        assert space.unwrap(api.PyIter_Next(w_iter)) == 3
+        assert api.PyIter_Next(w_iter) is None
+        assert not api.PyErr_Occurred()



More information about the Pypy-commit mailing list