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

cfbolz at codespeak.net cfbolz at codespeak.net
Sat Aug 21 10:34:12 CEST 2010


Author: cfbolz
Date: Sat Aug 21 10:34:10 2010
New Revision: 76696

Modified:
   pypy/branch/better-map-instances/pypy/objspace/std/mapdict.py
   pypy/branch/better-map-instances/pypy/objspace/std/test/test_dictmultiobject.py
   pypy/branch/better-map-instances/pypy/objspace/std/test/test_mapdict.py
Log:
better support for devolding dicts


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	Sat Aug 21 10:34:10 2010
@@ -83,17 +83,51 @@
         result._init_empty(terminator)
         return result
 
+
+class DictTerminator(Terminator):
+    def __init__(self, w_cls=None):
+        Terminator.__init__(self, w_cls)
+        self.devolved_dict_terminator = DevolvedDictTerminator(w_cls)
+
     def materialize_r_dict(self, space, obj, w_d):
         result = Object()
-        result._init_empty(self) # XXX
+        result._init_empty(self.devolved_dict_terminator)
         return result
 
+
 class NoDictTerminator(Terminator):
     def write(self, obj, selector, w_value):
         if selector[1] == DICT:
             return False
         return Terminator.write(self, obj, selector, w_value)
 
+
+class DevolvedDictTerminator(Terminator):
+    def read(self, obj, selector):
+        w_dict = obj.getdict()
+        space = obj.space # XXX
+        return space.finditem_str(w_dict, selector[0])
+
+    def write(self, obj, selector, w_value):
+        if selector[1] == DICT:
+            w_dict = obj.getdict()
+            space = obj.space # XXX
+            space.setitem_str(w_dict, selector[0], w_value)
+            return True
+        Terminator.write(self, obj, selector, w_value)
+
+    def delete(self, obj, selector):
+        if selector[1] == DICT:
+            w_dict = obj.getdict()
+            space = obj.space # XXX
+            try:
+                space.delitem(w_dict, w_name)
+            except OperationError, ex:
+                if not ex.match(space, space.w_KeyError):
+                    raise
+        return Terminator.delete(self, obj, selector)
+
+
 class PlainAttribute(AbstractAttribute):
     def __init__(self, selector, back):
         self.selector = selector
@@ -175,7 +209,7 @@
     def deldictvalue(self, space, w_name):
         attrname = space.str_w(w_name)
         new_obj = self.map.delete(self, (attrname, DICT))
-        # XXX too slow?
+        # XXX too slow? XXX wrong for devolved dicts
         if new_obj.map is self.map and new_obj.storage == self.storage:
             return False
         self._become(new_obj)

Modified: pypy/branch/better-map-instances/pypy/objspace/std/test/test_dictmultiobject.py
==============================================================================
--- pypy/branch/better-map-instances/pypy/objspace/std/test/test_dictmultiobject.py	(original)
+++ pypy/branch/better-map-instances/pypy/objspace/std/test/test_dictmultiobject.py	Sat Aug 21 10:34:10 2010
@@ -602,6 +602,12 @@
                 classofinstance=classofinstance,
                 from_strdict_shared=from_strdict_shared)
 
+    def finditem_str(self, w_dict, s):
+        return w_dict.getitem_str(s) # assume it's a multidict
+
+    def setitem_str(self, w_dict, s, w_value):
+        return w_dict.setitem_str(s, w_value) # assume it's a multidict
+
     def allocate_instance(self, cls, type):
         return object.__new__(cls)
 

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	Sat Aug 21 10:34:10 2010
@@ -11,7 +11,7 @@
     def __init__(self, hasdict=True):
         self.hasdict = True
         if hasdict:
-            self.terminator = Terminator(self)
+            self.terminator = DictTerminator(self)
         else:
             self.terminator = NoDictTerminator(self)
 
@@ -269,3 +269,44 @@
 class TestDevolvedMapDictImplementation(BaseTestDevolvedDictImplementation):
     get_impl = get_impl
     ImplementionClass = MapDictImplementation
+
+# ___________________________________________________________
+# tests that check the obj interface after the dict has devolved
+
+def devolve_dict(obj):
+    w_d = obj.getdict()
+    w_d._as_rdict()
+
+def test_get_setdictvalue_after_devolve():
+    cls = Class()
+    obj = cls.instantiate()
+    a =  FakeMember("a")
+    b =  FakeMember("b")
+    c =  FakeMember("c")
+    obj.setslotvalue(a, 50)
+    obj.setslotvalue(b, 60)
+    obj.setslotvalue(c, 70)
+    obj.setdictvalue(space, "a", 5)
+    obj.setdictvalue(space, "b", 6)
+    obj.setdictvalue(space, "c", 7)
+    devolve_dict(obj)
+    assert obj.getdictvalue(space, "a") == 5
+    assert obj.getdictvalue(space, "b") == 6
+    assert obj.getdictvalue(space, "c") == 7
+    assert obj.getslotvalue(a) == 50
+    assert obj.getslotvalue(b) == 60
+    assert obj.getslotvalue(c) == 70
+
+    obj.setslotvalue(a, 501)
+    obj.setslotvalue(b, 601)
+    obj.setslotvalue(c, 701)
+    obj.setdictvalue(space, "a", 51)
+    obj.setdictvalue(space, "b", 61)
+    obj.setdictvalue(space, "c", 71)
+    assert obj.getdictvalue(space, "a") == 51
+    assert obj.getdictvalue(space, "b") == 61
+    assert obj.getdictvalue(space, "c") == 71
+    assert obj.getslotvalue(a) == 501
+    assert obj.getslotvalue(b) == 601
+    assert obj.getslotvalue(c) == 701
+



More information about the Pypy-commit mailing list