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

afa at codespeak.net afa at codespeak.net
Mon Apr 26 19:11:43 CEST 2010


Author: afa
Date: Mon Apr 26 19:11:41 2010
New Revision: 74089

Modified:
   pypy/branch/cpython-extension/pypy/module/cpyext/mapping.py
   pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py
   pypy/branch/cpython-extension/pypy/module/cpyext/test/test_mapping.py
Log:
PyMapping_Size, PyMapping_Values, PyMapping_HasKey, PyMapping_HasKeyString


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	Mon Apr 26 19:11:41 2010
@@ -1,5 +1,6 @@
 from pypy.rpython.lltypesystem import lltype, rffi
-from pypy.module.cpyext.api import cpython_api, CANNOT_FAIL, CONST_STRING
+from pypy.module.cpyext.api import (
+    cpython_api, CANNOT_FAIL, CONST_STRING, Py_ssize_t)
 from pypy.module.cpyext.pyobject import PyObject
 
 
@@ -9,6 +10,14 @@
     function always succeeds."""
     return int(space.findattr(w_obj, space.wrap("items")) is not None)
 
+ at cpython_api([PyObject], Py_ssize_t, error=-1)
+def PyMapping_Size(space, w_obj):
+    return space.int_w(space.len(w_obj))
+
+ at cpython_api([PyObject], Py_ssize_t, error=-1)
+def PyMapping_Length(space, w_obj):
+    return space.int_w(space.len(w_obj))
+
 @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.
@@ -16,6 +25,12 @@
     return space.call_method(w_obj, "keys")
 
 @cpython_api([PyObject], PyObject)
+def PyMapping_Values(space, w_obj):
+    """On success, return a list of the values in object o.  On failure, return
+    NULL. This is equivalent to the Python expression o.values()."""
+    return space.call_method(w_obj, "values")
+
+ 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
@@ -37,3 +52,26 @@
     space.setitem(w_obj, w_key, w_value)
     return 0
 
+ at cpython_api([PyObject, PyObject], rffi.INT_real, error=CANNOT_FAIL)
+def PyMapping_HasKey(space, w_obj, w_key):
+    """Return 1 if the mapping object has the key key and 0 otherwise.
+    This is equivalent to o[key], returning True on success and False
+    on an exception.  This function always succeeds."""
+    try:
+        space.getitem(w_obj, w_key)
+        return 1
+    except:
+        return 0
+
+ at cpython_api([PyObject, CONST_STRING], rffi.INT_real, error=CANNOT_FAIL)
+def PyMapping_HasKeyString(space, w_obj, key):
+    """Return 1 if the mapping object has the key key and 0 otherwise.
+    This is equivalent to o[key], returning True on success and False
+    on an exception.  This function always succeeds."""
+    try:
+        w_key = space.wrap(rffi.charp2str(key))
+        space.getitem(w_obj, w_key)
+        return 1
+    except:
+        return 0
+

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py	Mon Apr 26 19:11:41 2010
@@ -2770,20 +2770,6 @@
     """
     raise NotImplementedError
 
- at cpython_api([PyObject], Py_ssize_t)
-def PyMapping_Size(space, o):
-    """
-    
-    
-    
-    Returns the number of keys in object o on success, and -1 on failure.  For
-    objects that do not provide mapping protocol, this is equivalent to the Python
-    expression len(o).
-    
-    These functions returned an int type. This might require
-    changes in your code for properly supporting 64-bit systems."""
-    raise NotImplementedError
-
 @cpython_api([PyObject, rffi.CCHARP], rffi.INT_real, error=-1)
 def PyMapping_DelItemString(space, o, key):
     """Remove the mapping for object key from the object o. Return -1 on
@@ -2796,26 +2782,6 @@
     failure.  This is equivalent to the Python statement del o[key]."""
     raise NotImplementedError
 
- at cpython_api([PyObject, rffi.CCHARP], rffi.INT_real, error=CANNOT_FAIL)
-def PyMapping_HasKeyString(space, o, key):
-    """On success, return 1 if the mapping object has the key key and 0
-    otherwise.  This is equivalent to o[key], returning True on success
-    and False on an exception.  This function always succeeds."""
-    raise NotImplementedError
-
- at cpython_api([PyObject, PyObject], rffi.INT_real, error=CANNOT_FAIL)
-def PyMapping_HasKey(space, o, key):
-    """Return 1 if the mapping object has the key key and 0 otherwise.
-    This is equivalent to o[key], returning True on success and False
-    on an exception.  This function always succeeds."""
-    raise NotImplementedError
-
- at cpython_api([PyObject], PyObject)
-def PyMapping_Values(space, o):
-    """On success, return a list of the values in object o.  On failure, return
-    NULL. This is equivalent to the Python expression o.values()."""
-    raise NotImplementedError
-
 @cpython_api([lltype.Signed, FILE, rffi.INT_real], lltype.Void)
 def PyMarshal_WriteLongToFile(space, value, file, version):
     """Marshal a long integer, value, to file.  This will only write

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	Mon Apr 26 19:11:41 2010
@@ -6,14 +6,19 @@
         assert api.PyMapping_Check(space.newdict())
         assert not api.PyMapping_Check(space.newlist([]))
 
-    def test_keys(self, space, api):
+    def test_size(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"]))
+        space.setitem(w_d, space.wrap("a"), space.wrap("b"))
 
-    def test_items(self, space, api):
+        assert api.PyMapping_Size(w_d) == 1
+        assert api.PyMapping_Length(w_d) == 1
+
+    def test_keys(self, space, api):
         w_d = space.newdict()
         space.setitem(w_d, space.wrap("a"), space.wrap("b"))
+
+        assert space.eq_w(api.PyMapping_Keys(w_d), space.wrap(["a"]))
+        assert space.eq_w(api.PyMapping_Values(w_d), space.wrap(["b"]))
         assert space.eq_w(api.PyMapping_Items(w_d), space.wrap([("a", "b")]))
 
     def test_setitemstring(self, space, api):
@@ -23,3 +28,13 @@
         assert 42 == space.unwrap(
             api.PyMapping_GetItemString(w_d, key, space.wrap(42)))
         rffi.free_charp(key)
+
+    def test_haskey(self, space, api):
+        w_d = space.newdict()
+        space.setitem(w_d, space.wrap("a"), space.wrap("b"))
+
+        assert api.PyMapping_HasKey(w_d, space.wrap("a"))
+        assert not api.PyMapping_HasKey(w_d, space.wrap("b"))
+
+        assert api.PyMapping_HasKey(w_d, w_d) == 0
+        # and no error is set



More information about the Pypy-commit mailing list