[pypy-svn] r74017 - in pypy/branch/cpython-extension/pypy/module/cpyext: . test
afa at codespeak.net
afa at codespeak.net
Fri Apr 23 14:11:59 CEST 2010
Author: afa
Date: Fri Apr 23 14:11:57 2010
New Revision: 74017
Modified:
pypy/branch/cpython-extension/pypy/module/cpyext/mapping.py
pypy/branch/cpython-extension/pypy/module/cpyext/test/test_mapping.py
Log:
PyMapping_SetItemString, PyMapping_GetItemString
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 Fri Apr 23 14:11:57 2010
@@ -1,5 +1,5 @@
from pypy.rpython.lltypesystem import lltype, rffi
-from pypy.module.cpyext.api import cpython_api, CANNOT_FAIL
+from pypy.module.cpyext.api import cpython_api, CANNOT_FAIL, CONST_STRING
from pypy.module.cpyext.pyobject import PyObject
@@ -22,3 +22,18 @@
the Python expression o.items()."""
return space.call_method(w_obj, "items")
+ at cpython_api([PyObject, CONST_STRING], PyObject)
+def PyMapping_GetItemString(space, w_obj, key):
+ """Return element of o corresponding to the object key or NULL on failure.
+ This is the equivalent of the Python expression o[key]."""
+ w_key = space.wrap(rffi.charp2str(key))
+ return space.getitem(w_obj, w_key)
+
+ at cpython_api([PyObject, CONST_STRING, PyObject], rffi.INT_real, error=-1)
+def PyMapping_SetItemString(space, w_obj, key, w_value):
+ """Map the object key to the value v in object o. Returns -1 on failure.
+ This is the equivalent of the Python statement o[key] = v."""
+ w_key = space.wrap(rffi.charp2str(key))
+ space.setitem(w_obj, w_key, w_value)
+ return 0
+
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 Fri Apr 23 14:11:57 2010
@@ -1,5 +1,5 @@
from pypy.module.cpyext.test.test_api import BaseApiTest
-
+from pypy.rpython.lltypesystem import lltype, rffi
class TestMapping(BaseApiTest):
def test_check(self, space, api):
@@ -15,3 +15,11 @@
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")]))
+
+ def test_setitemstring(self, space, api):
+ w_d = space.newdict()
+ key = rffi.str2charp("key")
+ api.PyMapping_SetItemString(w_d, key, space.wrap(42))
+ assert 42 == space.unwrap(
+ api.PyMapping_GetItemString(w_d, key, space.wrap(42)))
+ rffi.free_charp(key)
More information about the Pypy-commit
mailing list