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

fijal at codespeak.net fijal at codespeak.net
Sun Mar 28 01:07:36 CET 2010


Author: fijal
Date: Sun Mar 28 01:07:34 2010
New Revision: 73007

Modified:
   pypy/branch/cpython-extension/pypy/module/cpyext/dictobject.py
   pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py
   pypy/branch/cpython-extension/pypy/module/cpyext/test/test_dictobject.py
Log:
Implement PyDict_GetItemString


Modified: pypy/branch/cpython-extension/pypy/module/cpyext/dictobject.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/dictobject.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/dictobject.py	Sun Mar 28 01:07:34 2010
@@ -1,7 +1,8 @@
 from pypy.rpython.lltypesystem import rffi, lltype
 from pypy.module.cpyext.api import cpython_api, PyObject, CANNOT_FAIL, \
-    general_check
+    general_check, register_container
 from pypy.module.cpyext.pyerrors import PyErr_BadInternalCall
+from pypy.interpreter.error import OperationError
 
 @cpython_api([], PyObject)
 def PyDict_New(space):
@@ -37,3 +38,15 @@
         return 0
     else:
         PyErr_BadInternalCall(space)
+
+ at cpython_api([PyObject, rffi.CCHARP], PyObject, borrowed=True)
+def PyDict_GetItemString(space, w_dict, key):
+    """This is the same as PyDict_GetItem(), but key is specified as a
+    char*, rather than a PyObject*."""
+    if not PyDict_Check(space, w_dict):
+        PyErr_BadInternalCall(space)
+    w_res = space.finditem_str(w_dict, rffi.charp2str(key))
+    if w_res is None:
+        raise OperationError(space.w_KeyError, space.wrap("Key not found"))
+    register_container(space, w_dict)
+    return w_res

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	Sun Mar 28 01:07:34 2010
@@ -1388,12 +1388,6 @@
     if the key key is not present, but without setting an exception."""
     raise NotImplementedError
 
- at cpython_api([PyObject, rffi.CCHARP], PyObject, borrowed=True)
-def PyDict_GetItemString(space, p, key):
-    """This is the same as PyDict_GetItem(), but key is specified as a
-    char*, rather than a PyObject*."""
-    raise NotImplementedError
-
 @cpython_api([PyObject], PyObject)
 def PyDict_Items(space, p):
     """Return a PyListObject containing all the items from the

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/test/test_dictobject.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/test/test_dictobject.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/test/test_dictobject.py	Sun Mar 28 01:07:34 2010
@@ -22,6 +22,11 @@
             );
             Py_RETURN_NONE;
         """),
+        ("dict_getitem_str", "METH_VARARGS",
+         """
+         return PyDict_GetItemString(PyTuple_GetItem(args, 0), "name");
+         """
+         ),
         ])
         
         assert module.test_dict_create() == {}
@@ -29,3 +34,7 @@
         d = {}
         module.test_dict_setitem(d, "c", 72)
         assert d["c"] == 72
+        d["name"] = 3
+        assert module.dict_getitem_str(d) == 3
+        del d["name"]
+        raises(KeyError, module.dict_getitem_str, d)



More information about the Pypy-commit mailing list