[pypy-svn] r73964 - in pypy/branch/cpython-extension/pypy/module/cpyext: . test

afa at codespeak.net afa at codespeak.net
Thu Apr 22 13:06:15 CEST 2010


Author: afa
Date: Thu Apr 22 13:06:14 2010
New Revision: 73964

Modified:
   pypy/branch/cpython-extension/pypy/module/cpyext/mapping.py
   pypy/branch/cpython-extension/pypy/module/cpyext/test/test_mapping.py
Log:
Add PyMapping_Check and PyMapping_Items


Modified: pypy/branch/cpython-extension/pypy/module/cpyext/mapping.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/mapping.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/mapping.py	Thu Apr 22 13:06:14 2010
@@ -1,9 +1,24 @@
-from pypy.module.cpyext.api import cpython_api
+from pypy.rpython.lltypesystem import lltype, rffi
+from pypy.module.cpyext.api import cpython_api, CANNOT_FAIL
 from pypy.module.cpyext.pyobject import PyObject
 
 
+ at cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
+def PyMapping_Check(space, w_obj):
+    """Return 1 if the object provides mapping protocol, and 0 otherwise.  This
+    function always succeeds."""
+    return int(space.findattr(w_obj, space.wrap("items")) is not None)
+
 @cpython_api([PyObject], PyObject)
 def PyMapping_Keys(space, w_obj):
     """On success, return a list of the keys in object o.  On failure, return NULL.
     This is equivalent to the Python expression o.keys()."""
     return space.call_method(w_obj, "keys")
+
+ at cpython_api([PyObject], PyObject)
+def PyMapping_Items(space, w_obj):
+    """On success, return a list of the items in object o, where each item is a tuple
+    containing a key-value pair.  On failure, return NULL. This is equivalent to
+    the Python expression o.items()."""
+    return space.call_method(w_obj, "items")
+

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/test/test_mapping.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/test/test_mapping.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/test/test_mapping.py	Thu Apr 22 13:06:14 2010
@@ -2,7 +2,16 @@
 
 
 class TestMapping(BaseApiTest):
+    def test_check(self, space, api):
+        assert api.PyMapping_Check(space.newdict())
+        assert not api.PyMapping_Check(space.newlist([]))
+
     def test_keys(self, space, api):
         w_d = space.newdict()
         space.setitem(w_d, space.wrap("a"), space.wrap("a"))
         assert space.eq_w(api.PyMapping_Keys(w_d), space.wrap(["a"]))
+
+    def test_items(self, space, api):
+        w_d = space.newdict()
+        space.setitem(w_d, space.wrap("a"), space.wrap("b"))
+        assert space.eq_w(api.PyMapping_Items(w_d), space.wrap([("a", "b")]))



More information about the Pypy-commit mailing list