[pypy-svn] r76785 - in pypy/branch/better-map-instances/pypy/objspace/std: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Sun Aug 29 14:28:26 CEST 2010


Author: cfbolz
Date: Sun Aug 29 14:28:24 2010
New Revision: 76785

Modified:
   pypy/branch/better-map-instances/pypy/objspace/std/mapdict.py
   pypy/branch/better-map-instances/pypy/objspace/std/test/test_mapdict.py
Log:
some tests about changing the class of an instance. revealed a bug.


Modified: pypy/branch/better-map-instances/pypy/objspace/std/mapdict.py
==============================================================================
--- pypy/branch/better-map-instances/pypy/objspace/std/mapdict.py	(original)
+++ pypy/branch/better-map-instances/pypy/objspace/std/mapdict.py	Sun Aug 29 14:28:24 2010
@@ -158,6 +158,12 @@
     def remove_dict_entries(self, obj):
         assert 0, "should be unreachable"
 
+    def set_terminator(self, obj, terminator):
+        if not isinstance(terminator, DevolvedDictTerminator):
+            assert isinstance(terminator, DictTerminator)
+            terminator = terminator.devolved_dict_terminator
+        return Terminator.set_terminator(self, obj, terminator)
+
 class PlainAttribute(AbstractAttribute):
     _immutable_ = True
     def __init__(self, selector, back):

Modified: pypy/branch/better-map-instances/pypy/objspace/std/test/test_mapdict.py
==============================================================================
--- pypy/branch/better-map-instances/pypy/objspace/std/test/test_mapdict.py	(original)
+++ pypy/branch/better-map-instances/pypy/objspace/std/test/test_mapdict.py	Sun Aug 29 14:28:24 2010
@@ -399,3 +399,48 @@
         A.slot1.__set__(x, 'parent')  # using A.slot1
         assert x.slot1 == 'child'     # B.slot1 should still have its old value
         assert A.slot1.__get__(x) == 'parent'
+
+    def test_change_class(self):
+        class A(object):
+            pass
+        class B(object):
+            pass
+        a = A()
+        a.x = 1
+        a.y = 2
+        assert a.x == 1
+        assert a.y == 2
+        a.__class__ = B
+        assert a.x == 1
+        assert a.y == 2
+        assert isinstance(a, B)
+
+        # dict accessed:
+        a = A()
+        a.x = 1
+        a.y = 2
+        assert a.x == 1
+        assert a.y == 2
+        d = a.__dict__
+        assert d == {"x": 1, "y": 2}
+        a.__class__ = B
+        assert a.x == 1
+        assert a.y == 2
+        assert a.__dict__ is d
+        assert d == {"x": 1, "y": 2}
+        assert isinstance(a, B)
+
+        # dict devolved:
+        a = A()
+        a.x = 1
+        a.y = 2
+        assert a.x == 1
+        assert a.y == 2
+        d = a.__dict__
+        d[1] = 3
+        assert d == {"x": 1, "y": 2, 1:3}
+        a.__class__ = B
+        assert a.x == 1
+        assert a.y == 2
+        assert a.__dict__ is d
+        assert isinstance(a, B)



More information about the Pypy-commit mailing list