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

afa at codespeak.net afa at codespeak.net
Sat Jun 5 00:26:51 CEST 2010


Author: afa
Date: Sat Jun  5 00:26:50 2010
New Revision: 75127

Modified:
   pypy/trunk/pypy/module/cpyext/test/test_typeobject.py
   pypy/trunk/pypy/module/cpyext/typeobject.py
Log:
Small support for Py_TYPE(x)->tp_dict:
it is a "dictproxy" of the (rpython) w_type.dict_w.


Modified: pypy/trunk/pypy/module/cpyext/test/test_typeobject.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/test_typeobject.py	(original)
+++ pypy/trunk/pypy/module/cpyext/test/test_typeobject.py	Sat Jun  5 00:26:50 2010
@@ -206,6 +206,27 @@
         del obj.x
         assert obj.z == prop
 
+    def test_tp_dict(self):
+        foo = self.import_module("foo")
+        module = self.import_extension('test', [
+           ("read_tp_dict", "METH_O",
+            '''
+                 PyObject *method;
+                 if (!args->ob_type->tp_dict)
+                 {
+                     PyErr_SetNone(PyExc_ValueError);
+                     return NULL;
+                 }
+                 method = PyDict_GetItemString(
+                     args->ob_type->tp_dict, "copy");
+                 Py_INCREF(method);
+                 return method;
+             '''
+             )
+            ])
+        obj = foo.new()
+        assert module.read_tp_dict(obj) == foo.fooType.copy
+
 
 class TestTypes(BaseApiTest):
     def test_type_attributes(self, space, api):
@@ -304,7 +325,7 @@
                 return args
         assert module.tp_call(C(), ('x', 2)) == ('x', 2)
 
-    def test_tp_str(self): 
+    def test_tp_str(self):
         module = self.import_extension('foo', [
            ("tp_str", "METH_O",
             '''
@@ -321,4 +342,3 @@
             def __str__(self):
                 return "text"
         assert module.tp_str(C()) == "text"
-            

Modified: pypy/trunk/pypy/module/cpyext/typeobject.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/typeobject.py	(original)
+++ pypy/trunk/pypy/module/cpyext/typeobject.py	Sat Jun  5 00:26:50 2010
@@ -382,6 +382,7 @@
     Py_DecRef(space, obj_pto.c_tp_bases)
     Py_DecRef(space, obj_pto.c_tp_mro)
     Py_DecRef(space, obj_pto.c_tp_cache) # let's do it like cpython
+    Py_DecRef(space, obj_pto.c_tp_dict)
     if obj_pto.c_tp_flags & Py_TPFLAGS_HEAPTYPE:
         if obj_pto.c_tp_as_buffer:
             lltype.free(obj_pto.c_tp_as_buffer, flavor='raw')
@@ -543,8 +544,6 @@
     """
     Sets up tp_bases, necessary before creating the interpreter type.
     """
-    pto.c_tp_dict = lltype.nullptr(PyObject.TO) # not supported
-
     base = pto.c_tp_base
     base_pyo = rffi.cast(PyObject, pto.c_tp_base)
     if base and not base.c_tp_flags & Py_TPFLAGS_READY:
@@ -575,6 +574,10 @@
             PyObject_GenericSetAttr.api_func.functype,
             PyObject_GenericSetAttr.api_func.get_wrapper(space))
 
+    if w_obj.is_cpytype():
+        Py_DecRef(space, pto.c_tp_dict)
+        pto.c_tp_dict = make_ref(space, w_obj.getdict())
+
 @cpython_api([PyTypeObjectPtr, PyTypeObjectPtr], rffi.INT_real, error=CANNOT_FAIL)
 def PyType_IsSubtype(space, a, b):
     """Return true if a is a subtype of b.



More information about the Pypy-commit mailing list