[pypy-svn] r66097 - in pypy/trunk/pypy/objspace/std: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Fri Jul 3 13:22:28 CEST 2009


Author: cfbolz
Date: Fri Jul  3 13:22:27 2009
New Revision: 66097

Modified:
   pypy/trunk/pypy/objspace/std/dictmultiobject.py
   pypy/trunk/pypy/objspace/std/dictobject.py
   pypy/trunk/pypy/objspace/std/test/test_dictmultiobject.py
   pypy/trunk/pypy/objspace/std/test/test_dictobject.py
   pypy/trunk/pypy/objspace/std/test/test_typeobject.py
   pypy/trunk/pypy/objspace/std/typeobject.py
Log:
(pedronis, cfbolz): Fix the fact that dict proxies are not updated properly. The
fix works only when using multidicts, as otherwise it would be very annoying to
fix.


Modified: pypy/trunk/pypy/objspace/std/dictmultiobject.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/dictmultiobject.py	(original)
+++ pypy/trunk/pypy/objspace/std/dictmultiobject.py	Fri Jul  3 13:22:27 2009
@@ -1049,6 +1049,7 @@
             w_self.implementation = SharedDictImplementation(space)
         else:
             w_self.implementation = space.emptydictimpl
+        w_self.space = space
 
     def initialize_content(w_self, list_pairs_w):
         impl = w_self.implementation
@@ -1056,6 +1057,11 @@
             impl = impl.setitem(w_k, w_v)
         w_self.implementation = impl
 
+    def initialize_from_strdict_shared(w_self, strdict):
+        impl = StrDictImplementation(w_self.space)
+        impl.content = strdict
+        w_self.implementation = impl
+
     def __repr__(w_self):
         """ representation for debugging purposes """
         return "%s(%s)" % (w_self.__class__.__name__, w_self.implementation)

Modified: pypy/trunk/pypy/objspace/std/dictobject.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/dictobject.py	(original)
+++ pypy/trunk/pypy/objspace/std/dictobject.py	Fri Jul  3 13:22:27 2009
@@ -13,11 +13,18 @@
             w_self.content = r_dict(space.eq_w, space.hash_w)
         else:
             w_self.content = w_otherdict.content.copy()
+        w_self.space = space
 
     def initialize_content(w_self, list_pairs_w):
         for w_k, w_v in list_pairs_w:
             w_self.content[w_k] = w_v
 
+    def initialize_from_strdict_shared(w_self, strdict):
+        # XXX the stuff below is slightly broken, as the dict is not really shared
+        # this would be very very annoying to fix with non-multidicts
+        for key, w_value in strdict.items():
+            w_self.content[w_self.space.wrap(key)] = w_value
+        
     def __repr__(w_self):
         """ representation for debugging purposes """
         return "%s(%s)" % (w_self.__class__.__name__, w_self.content)

Modified: pypy/trunk/pypy/objspace/std/test/test_dictmultiobject.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/test/test_dictmultiobject.py	(original)
+++ pypy/trunk/pypy/objspace/std/test/test_dictmultiobject.py	Fri Jul  3 13:22:27 2009
@@ -12,6 +12,17 @@
     def setup_class(cls):
         cls.space = gettestobjspace(**{"objspace.std.withmultidict": True})
 
+    def test_initialize_from_strdict_really_shared(self):
+        space = self.space
+        w = space.wrap
+        d = {"a": w(1), "b": w(2)}
+        w_d = space.DictObjectCls(space)
+        w_d.initialize_from_strdict_shared(d)
+        assert self.space.eq_w(space.getitem(w_d, w("a")), w(1))
+        assert self.space.eq_w(space.getitem(w_d, w("b")), w(2))
+        d["c"] = w(41)
+        assert self.space.eq_w(space.getitem(w_d, w("c")), w(41))
+
 class AppTest_DictMultiObject(test_dictobject.AppTest_DictObject):
     def setup_class(cls):
         cls.space = gettestobjspace(**{"objspace.std.withmultidict": True})

Modified: pypy/trunk/pypy/objspace/std/test/test_dictobject.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/test/test_dictobject.py	(original)
+++ pypy/trunk/pypy/objspace/std/test/test_dictobject.py	Fri Jul  3 13:22:27 2009
@@ -123,6 +123,16 @@
         assert self.space.eq_w(space.call_function(get, w("33")), w(None))
         assert self.space.eq_w(space.call_function(get, w("33"), w(44)), w(44))
 
+    def test_initialize_from_strdict_shared(self):
+        space = self.space
+        w = space.wrap
+        d = {"a": w(1), "b": w(2)}
+        w_d = space.DictObjectCls(space)
+        w_d.initialize_from_strdict_shared(d)
+        assert self.space.eq_w(space.getitem(w_d, w("a")), w(1))
+        assert self.space.eq_w(space.getitem(w_d, w("b")), w(2))
+
+        
 
 
 class AppTest_DictObject:

Modified: pypy/trunk/pypy/objspace/std/test/test_typeobject.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/test/test_typeobject.py	(original)
+++ pypy/trunk/pypy/objspace/std/test/test_typeobject.py	Fri Jul  3 13:22:27 2009
@@ -915,14 +915,19 @@
                 return 0
         raises(TypeError, X)
 
+class AppTestWithMultidictTypes:
+    def setup_class(cls):
+        cls.space = gettestobjspace(**{"objspace.std.withmultidict": True})
+
     def test_dictproxy_is_updated(self):
-        skip("fix me")
         class A(object):
             x = 1
         d = A.__dict__
         assert d["x"] == 1
         A.y = 2
         assert d["y"] == 2
+        assert ("x", 1) in d.items()
+        assert ("y", 2) in d.items()
 
 
 class AppTestMutableBuiltintypes:

Modified: pypy/trunk/pypy/objspace/std/typeobject.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/typeobject.py	(original)
+++ pypy/trunk/pypy/objspace/std/typeobject.py	Fri Jul  3 13:22:27 2009
@@ -246,14 +246,8 @@
         if w_self.lazyloaders:
             w_self._freeze_()    # force un-lazification
         space = w_self.space
-        dictspec = []
-        for key, w_value in w_self.dict_w.items():
-            dictspec.append((space.wrap(key), w_value))
-        # speed hack: instantiate a dict object cls directly
-        # NB: cannot use newdict, because that could return something else
-        # than an instance of DictObjectCls
         newdic = space.DictObjectCls(space)
-        newdic.initialize_content(dictspec)
+        newdic.initialize_from_strdict_shared(w_self.dict_w)
         return W_DictProxyObject(newdic)
 
     def unwrap(w_self, space):



More information about the Pypy-commit mailing list