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

agaynor at codespeak.net agaynor at codespeak.net
Sat Mar 27 22:21:18 CET 2010


Author: agaynor
Date: Sat Mar 27 22:21:16 2010
New Revision: 72983

Added:
   pypy/branch/cpython-extension/pypy/module/cpyext/test/test_dictobject.py
Modified:
   pypy/branch/cpython-extension/pypy/module/cpyext/dictobject.py
   pypy/branch/cpython-extension/pypy/module/cpyext/include/object.h
Log:
Implement PyDict_{Get, Set}Item, and added dict tests.

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	Sat Mar 27 22:21:16 2010
@@ -12,6 +12,20 @@
     w_type = space.w_dict
     return general_check(space, w_obj, w_type)
 
+ at cpython_api([PyObject, PyObject], PyObject)
+def PyDict_GetItem(space, w_dict, w_key):
+    if PyDict_Check(space, w_dict):
+        return space.getitem(w_dict, w_key)
+    else:
+        PyErr_BadInternalCall(space)
+
+ at cpython_api([PyObject, PyObject, PyObject], rffi.INT_real, error=-1)
+def PyDict_SetItem(space, w_dict, w_key, w_obj):
+    if PyDict_Check(space, w_dict):
+        space.setitem(w_dict, w_key, w_obj)
+        return 0
+    else:
+        PyErr_BadInternalCall(space)
 
 @cpython_api([PyObject, rffi.CCHARP, PyObject], rffi.INT_real, error=-1)
 def PyDict_SetItemString(space, w_dict, key_ptr, w_obj):

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/include/object.h
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/include/object.h	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/include/object.h	Sat Mar 27 22:21:16 2010
@@ -3,6 +3,7 @@
 
 #include <stdio.h>
 
+#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
 
 typedef void* Py_buffer;
 

Added: pypy/branch/cpython-extension/pypy/module/cpyext/test/test_dictobject.py
==============================================================================
--- (empty file)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/test/test_dictobject.py	Sat Mar 27 22:21:16 2010
@@ -0,0 +1,31 @@
+from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
+
+
+class AppTestDictObject(AppTestCpythonExtensionBase):
+    def test_dict(self):
+        module = self.import_extension("foo", [
+        ("test_dict_create", "METH_NOARGS",
+        """
+            PyObject *p = PyDict_New();
+            return p;
+        """),
+        ("test_dict_getitem", "METH_VARARGS",
+        """
+            return PyDict_GetItem(PyTuple_GetItem(args, 0), PyTuple_GetItem(args, 1));
+        """),
+        ("test_dict_setitem", "METH_VARARGS",
+        """
+            PyDict_SetItem(
+                PyTuple_GetItem(args, 0),
+                PyTuple_GetItem(args, 1),
+                PyTuple_GetItem(args, 2)
+            );
+            Py_RETURN_NONE;
+        """),
+        ])
+        
+        assert module.test_dict_create() == {}
+        assert module.test_dict_getitem({"a": 72}, "a") == 72
+        d = {}
+        module.test_dict_setitem(d, "c", 72)
+        assert d["c"] == 72



More information about the Pypy-commit mailing list