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

agaynor at codespeak.net agaynor at codespeak.net
Mon May 17 17:35:40 CEST 2010


Author: agaynor
Date: Mon May 17 17:35:39 2010
New Revision: 74531

Added:
   pypy/trunk/pypy/module/cpyext/test/test_iterator.py
Modified:
   pypy/trunk/pypy/module/cpyext/iterator.py
   pypy/trunk/pypy/module/cpyext/stubs.py
Log:
Implemented PyIter_Check for cpyext.

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 17:35:39 2010
@@ -1,6 +1,8 @@
-from pypy.module.cpyext.api import generic_cpy_call, cpython_api, PyObject
 from pypy.interpreter.error import OperationError
+from pypy.module.cpyext.api import (generic_cpy_call, cpython_api, PyObject,
+    CANNOT_FAIL)
 import pypy.module.__builtin__.operation as operation
+from pypy.rpython.lltypesystem import rffi
 
 
 @cpython_api([PyObject, PyObject], PyObject)
@@ -12,3 +14,12 @@
     """
     return operation.iter_sentinel(space, w_callable, w_sentinel)
 
+ at 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."""
+    try:
+        w_attr = space.getattr(space.type(w_obj), space.wrap("next"))
+    except:
+        return False
+    else:
+        return space.is_true(space.callable(w_attr))

Modified: pypy/trunk/pypy/module/cpyext/stubs.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/stubs.py	(original)
+++ pypy/trunk/pypy/module/cpyext/stubs.py	Mon May 17 17:35:39 2010
@@ -2084,11 +2084,6 @@
     raise NotImplementedError
 
 @cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
-def PyIter_Check(space, o):
-    """Return true if the object o supports the iterator protocol."""
-    raise NotImplementedError
-
- at cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
 def PySeqIter_Check(space, seq):
     """Return true if the type of op is PySeqIter_Type.
     """

Added: pypy/trunk/pypy/module/cpyext/test/test_iterator.py
==============================================================================
--- (empty file)
+++ pypy/trunk/pypy/module/cpyext/test/test_iterator.py	Mon May 17 17:35:39 2010
@@ -0,0 +1,9 @@
+from pypy.module.cpyext.test.test_api import BaseApiTest
+
+
+class TestIterator(BaseApiTest):
+    def test_check_iter(self, space, api):
+        assert api.PyIter_Check(space.wrap(iter("a")))
+        assert api.PyIter_Check(space.wrap(iter([])))
+        assert not api.PyIter_Check(space.wrap(type))
+        assert not api.PyIter_Check(space.wrap(2))



More information about the Pypy-commit mailing list