Author: Carl Friedrich Bolz <[email protected]>
Branch: share-mapdict-methods-2
Changeset: r84025:1cb2c3897dbb
Date: 2016-04-28 23:24 +0300
http://bitbucket.org/pypy/pypy/changeset/1cb2c3897dbb/

Log:    fix problems with __del__: only make a single RPython subclass if
        the base class already has a del

diff --git a/pypy/interpreter/test/test_typedef.py 
b/pypy/interpreter/test/test_typedef.py
--- a/pypy/interpreter/test/test_typedef.py
+++ b/pypy/interpreter/test/test_typedef.py
@@ -383,6 +383,25 @@
         assert not hasattr(b, "storage")
         assert hasattr(c, "storage")
 
+    def test_del(self):
+        space = self.space
+        a, b, c, d = space.unpackiterable(space.appexec([], """():
+            class A(object):
+                pass
+            class B(object):
+                def __del__(self):
+                    pass
+            class F(file):
+                pass
+            class G(file):
+                def __del__(self):
+                    pass
+            return A(), B(), F("xyz", "w"), G("ghi", "w")
+        """))
+        assert type(b).__base__ is type(a)
+        assert hasattr(c, "__del__")
+        assert type(d) is type(c)
+
 class AppTestTypeDef:
 
     def setup_class(cls):
diff --git a/pypy/interpreter/typedef.py b/pypy/interpreter/typedef.py
--- a/pypy/interpreter/typedef.py
+++ b/pypy/interpreter/typedef.py
@@ -113,11 +113,18 @@
         return _subclass_cache[key]
     except KeyError:
         # XXX can save a class if cls already has a __del__
-        if needsdel:
+        keys = [key]
+        base_has_del = hasattr(cls, '__del__')
+        if base_has_del:
+            # if the base has a __del__, we only need one class
+            keys = [(space, cls, True), (space, cls, False)]
+            needsdel = True
+        elif needsdel:
             cls = get_unique_interplevel_subclass(space, cls, False)
         subcls = _getusercls(space, cls, needsdel)
         assert key not in _subclass_cache
-        _subclass_cache[key] = subcls
+        for key in keys:
+            _subclass_cache[key] = subcls
         return subcls
 get_unique_interplevel_subclass._annspecialcase_ = "specialize:memo"
 _subclass_cache = {}
@@ -133,20 +140,24 @@
     name = cls.__name__ + "User"
 
     mixins_needed = []
-    if cls is W_ObjectObject or cls is W_InstanceObject:
-        mixins_needed.append(_make_storage_mixin_size_n())
-    else:
-        mixins_needed.append(MapdictStorageMixin)
-    copy_methods = [BaseUserClassMapdict]
-    if reallywantdict or not typedef.hasdict:
-        # the type has no dict, mapdict to provide the dict
-        copy_methods.append(MapdictDictSupport)
-        name += "Dict"
-    if not typedef.weakrefable:
-        # the type does not support weakrefs yet, mapdict to provide weakref
-        # support
-        copy_methods.append(MapdictWeakrefSupport)
-        name += "Weakrefable"
+    copy_methods = []
+    mixins_needed = []
+    name = cls.__name__
+    if not cls.user_overridden_class:
+        if cls is W_ObjectObject or cls is W_InstanceObject:
+            mixins_needed.append(_make_storage_mixin_size_n())
+        else:
+            mixins_needed.append(MapdictStorageMixin)
+        copy_methods = [BaseUserClassMapdict]
+        if reallywantdict or not typedef.hasdict:
+            # the type has no dict, mapdict to provide the dict
+            copy_methods.append(MapdictDictSupport)
+            name += "Dict"
+        if not typedef.weakrefable:
+            # the type does not support weakrefs yet, mapdict to provide 
weakref
+            # support
+            copy_methods.append(MapdictWeakrefSupport)
+            name += "Weakrefable"
     if wants_del:
         name += "Del"
         parent_destructor = getattr(cls, '__del__', None)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to