[pypy-commit] pypy cpyext-ext: fix, test PyDictProxy object

mattip pypy.commits at gmail.com
Mon Feb 22 08:04:56 EST 2016


Author: mattip <matti.picus at gmail.com>
Branch: cpyext-ext
Changeset: r82388:b1fc26f1766b
Date: 2016-02-21 18:00 +0100
http://bitbucket.org/pypy/pypy/changeset/b1fc26f1766b/

Log:	fix, test PyDictProxy object

diff --git a/pypy/module/cpyext/api.py b/pypy/module/cpyext/api.py
--- a/pypy/module/cpyext/api.py
+++ b/pypy/module/cpyext/api.py
@@ -473,7 +473,7 @@
         "PyUnicode_Type": "space.w_unicode",
         "PyBaseString_Type": "space.w_basestring",
         "PyDict_Type": "space.w_dict",
-        #"PyDictProxy_Type": "space.type(space.w_NotImplemented)",
+        "PyDictProxy_Type": "cpyext.dictobject.make_frozendict(space)",
         "PyTuple_Type": "space.w_tuple",
         "PyList_Type": "space.w_list",
         "PySet_Type": "space.w_set",
diff --git a/pypy/module/cpyext/dictobject.py b/pypy/module/cpyext/dictobject.py
--- a/pypy/module/cpyext/dictobject.py
+++ b/pypy/module/cpyext/dictobject.py
@@ -248,3 +248,15 @@
     w_frozendict = make_frozendict(space)
     return space.call_function(w_frozendict, w_dict)
 
+ at cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
+def PyDictProxy_Check(space, w_obj):
+    w_typ = make_frozendict(space)
+    print 'check', w_typ, space.type(w_obj)
+    return space.isinstance_w(w_obj, w_typ)
+
+ at cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
+def PyDictProxy_CheckExact(space, w_obj):
+    w_typ = make_frozendict(space)
+    print 'exact', w_typ, w_obj
+    return space.is_w(space.type(w_obj), w_typ)
+
diff --git a/pypy/module/cpyext/test/test_dictobject.py b/pypy/module/cpyext/test/test_dictobject.py
--- a/pypy/module/cpyext/test/test_dictobject.py
+++ b/pypy/module/cpyext/test/test_dictobject.py
@@ -1,8 +1,10 @@
+import py
 from rpython.rtyper.lltypesystem import rffi, lltype
 from pypy.module.cpyext.test.test_api import BaseApiTest
 from pypy.module.cpyext.api import Py_ssize_tP, PyObjectP
 from pypy.module.cpyext.pyobject import make_ref, from_ref
 from pypy.interpreter.error import OperationError
+from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
 
 class TestDictObject(BaseApiTest):
     def test_dict(self, space, api):
@@ -159,7 +161,6 @@
 
     def test_dictproxy(self, space, api):
         w_dict = space.sys.get('modules')
-        w_proxy = api.PyDictProxy_New(w_dict)
         assert space.contains_w(w_proxy, space.wrap('sys'))
         raises(OperationError, space.setitem,
                w_proxy, space.wrap('sys'), space.w_None)
@@ -167,6 +168,40 @@
                w_proxy, space.wrap('sys'))
         raises(OperationError, space.call_method, w_proxy, 'clear')
 
-    def test_dictproxytype(self, space, api):
-        # XXX test PyDictProxy_Type, currently space.NotImplemented
-        assert False
+
+    @py.test.mark.xfail(reason='make_frozendict memoize only works translated')
+    def test_dictproxy(self, space, api):
+        w_proxy = api.PyDictProxy_New(w_dict)
+        assert api.PyDictProxy_Check(w_proxy)
+    
+class AppTestDictObject(AppTestCpythonExtensionBase):
+    #@py.test.mark.xfail(reason='make_frozendict memoize only works translated')
+    def test_dictproxytype(self):
+        module = self.import_extension('foo', [
+            ("dict_proxy", "METH_VARARGS",
+             """
+                 PyObject * dict;
+                 PyObject * proxydict;
+                 int i;
+                 if (!PyArg_ParseTuple(args, "O", &dict))
+                     return NULL;
+                 proxydict = PyDictProxy_New(dict);
+                 Py_DECREF(dict);
+                 /* when memoize works untranslated, add these tests
+                 if (!PyDictProxy_Check(proxydict)) {
+                    Py_DECREF(proxydict);
+                    PyErr_SetNone(PyExc_ValueError);
+                    return NULL;
+                 } 
+                 if (!PyDictProxy_CheckExact(proxydict)) {
+                    Py_DECREF(proxydict);
+                    PyErr_SetNone(PyExc_ValueError);
+                    return NULL;
+                 }
+                 */
+                 i = PyObject_Size(proxydict);
+                 Py_DECREF(proxydict);
+                 return PyLong_FromLong(i); 
+             """),
+            ])
+        assert module.dict_proxy({'a': 1, 'b': 2}) == 2


More information about the pypy-commit mailing list