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

afa at codespeak.net afa at codespeak.net
Fri May 21 17:53:20 CEST 2010


Author: afa
Date: Fri May 21 17:53:18 2010
New Revision: 74639

Modified:
   pypy/trunk/pypy/module/cpyext/object.py
   pypy/trunk/pypy/module/cpyext/test/test_object.py
Log:
Add PyObject_SetItem, PyObject_DelItem, PyObject_DelAttrString


Modified: pypy/trunk/pypy/module/cpyext/object.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/object.py	(original)
+++ pypy/trunk/pypy/module/cpyext/object.py	Fri May 21 17:53:18 2010
@@ -128,6 +128,14 @@
     space.delattr(w_obj, w_name)
     return 0
 
+ at cpython_api([PyObject, CONST_STRING], rffi.INT_real, error=-1)
+def PyObject_DelAttrString(space, w_obj, name_ptr):
+    """Delete attribute named attr_name, for object o. Returns -1 on failure.
+    This is the equivalent of the Python statement del o.attr_name."""
+    w_name = space.wrap(rffi.charp2str(name_ptr))
+    space.delattr(w_obj, w_name)
+    return 0
+
 @cpython_api([PyObject], lltype.Void)
 def PyObject_ClearWeakRefs(space, w_object):
     w_object.clear_all_weakrefs()
@@ -148,6 +156,20 @@
     This is the equivalent of the Python expression o[key]."""
     return space.getitem(w_obj, w_key)
 
+ at cpython_api([PyObject, PyObject, PyObject], rffi.INT_real, error=-1)
+def PyObject_SetItem(space, w_obj, w_key, w_value):
+    """Map the object key to the value v.  Returns -1 on failure.  This is the
+    equivalent of the Python statement o[key] = v."""
+    space.setitem(w_obj, w_key, w_value)
+    return 0
+
+ at cpython_api([PyObject, PyObject], rffi.INT_real, error=-1)
+def PyObject_DelItem(space, w_obj, w_key):
+    """Delete the mapping for key from o.  Returns -1 on failure. This is the
+    equivalent of the Python statement del o[key]."""
+    space.delitem(w_obj, w_key)
+    return 0
+
 @cpython_api([PyObject, PyTypeObjectPtr], PyObject)
 def PyObject_Init(space, op, type):
     """Initialize a newly-allocated object op with its type and initial

Modified: pypy/trunk/pypy/module/cpyext/test/test_object.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/test_object.py	(original)
+++ pypy/trunk/pypy/module/cpyext/test/test_object.py	Fri May 21 17:53:18 2010
@@ -63,6 +63,9 @@
         assert not api.PyObject_GetAttrString(space.wrap(""), charp2)
         assert api.PyErr_Occurred() is space.w_AttributeError
         api.PyErr_Clear()
+        assert api.PyObject_DelAttrString(space.wrap(""), charp1) == -1
+        assert api.PyErr_Occurred() is space.w_AttributeError
+        api.PyErr_Clear()
         rffi.free_charp(charp1)
         rffi.free_charp(charp2)
 
@@ -78,6 +81,14 @@
         space.setitem(w_d, space.wrap("a key!"), space.wrap(72))
         assert space.unwrap(api.PyObject_GetItem(w_d, space.wrap("a key!"))) == 72
 
+        assert api.PyObject_SetItem(w_d, space.wrap("key"), space.w_None) == 0
+        assert space.getitem(w_d, space.wrap("key")) is space.w_None
+
+        assert api.PyObject_DelItem(w_d, space.wrap("key")) == 0
+        assert api.PyObject_GetItem(w_d, space.wrap("key")) is None
+        assert api.PyErr_Occurred() is space.w_KeyError
+        api.PyErr_Clear()
+
     def test_size(self, space, api):
         assert api.PyObject_Size(space.newlist([space.w_None])) == 1
         



More information about the Pypy-commit mailing list