[pypy-commit] pypy py2-mappingproxy: Allow attribute deletion on C-defined types

rlamy pypy.commits at gmail.com
Tue Aug 9 10:52:45 EDT 2016


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: py2-mappingproxy
Changeset: r86114:5cb67984053b
Date: 2016-08-05 16:47 +0100
http://bitbucket.org/pypy/pypy/changeset/5cb67984053b/

Log:	Allow attribute deletion on C-defined types

diff --git a/pypy/module/cpyext/test/test_typeobject.py b/pypy/module/cpyext/test/test_typeobject.py
--- a/pypy/module/cpyext/test/test_typeobject.py
+++ b/pypy/module/cpyext/test/test_typeobject.py
@@ -299,6 +299,15 @@
         assert type(obj)._some_attribute == 1
         del d["_some_attribute"]
 
+        class A(object):
+            pass
+        obj = A()
+        d = module.get_type_dict(obj)
+        assert type(d) is dict
+        d["_some_attribute"] = 1
+        assert type(obj)._some_attribute == 1
+        del d["_some_attribute"]
+
         d = module.get_type_dict(1)
         assert type(d) is dict
         try:
@@ -376,6 +385,21 @@
 
         api.Py_DecRef(ref)
 
+    def test_type_dict(self, space, api):
+        w_class = space.appexec([], """():
+            class A(object):
+                pass
+            return A
+            """)
+        ref = make_ref(space, w_class)
+
+        py_type = rffi.cast(PyTypeObjectPtr, ref)
+        w_dict = from_ref(space, py_type.c_tp_dict)
+        w_name = space.wrap('a')
+        space.setitem(w_dict, w_name, space.wrap(1))
+        assert space.int_w(space.getattr(w_class, w_name)) == 1
+        space.delitem(w_dict, w_name)
+
     def test_multiple_inheritance(self, space, api):
         w_class = space.appexec([], """():
             class A(object):
diff --git a/pypy/objspace/std/typeobject.py b/pypy/objspace/std/typeobject.py
--- a/pypy/objspace/std/typeobject.py
+++ b/pypy/objspace/std/typeobject.py
@@ -346,7 +346,7 @@
     def deldictvalue(self, space, key):
         if self.lazyloaders:
             self._cleanup_()    # force un-lazification
-        if not self.is_heaptype():
+        if not (self.is_heaptype() or self.is_cpytype()):
             raise oefmt(space.w_TypeError,
                         "can't delete attributes on type object '%N'", self)
         try:


More information about the pypy-commit mailing list