[pypy-commit] pypy typed-cells: also use mutable int cells for celldict by sharing the code

cfbolz noreply at buildbot.pypy.org
Wed Jan 21 18:54:53 CET 2015


Author: Carl Friedrich Bolz <cfbolz at gmx.de>
Branch: typed-cells
Changeset: r75470:190ff2945d6d
Date: 2015-01-21 18:54 +0100
http://bitbucket.org/pypy/pypy/changeset/190ff2945d6d/

Log:	also use mutable int cells for celldict by sharing the code

diff --git a/pypy/objspace/std/celldict.py b/pypy/objspace/std/celldict.py
--- a/pypy/objspace/std/celldict.py
+++ b/pypy/objspace/std/celldict.py
@@ -9,23 +9,16 @@
 from pypy.objspace.std.dictmultiobject import (
     DictStrategy, ObjectDictStrategy, _never_equal_to_string,
     create_iterator_classes)
+from pypy.objspace.std.typeobject import (
+    MutableCell, IntMutableCell, ObjectMutableCell, write_cell)
 
 
 class VersionTag(object):
     pass
 
-
-class ModuleCell(W_Root):
-    def __init__(self, w_value=None):
-        self.w_value = w_value
-
-    def __repr__(self):
-        return "<ModuleCell: %s>" % (self.w_value, )
-
-
-def unwrap_cell(w_value):
-    if isinstance(w_value, ModuleCell):
-        return w_value.w_value
+def unwrap_cell(space, w_value):
+    if isinstance(w_value, MutableCell):
+        return w_value.unwrap_cell(space)
     return w_value
 
 
@@ -71,15 +64,9 @@
 
     def setitem_str(self, w_dict, key, w_value):
         cell = self.getdictvalue_no_unwrapping(w_dict, key)
-        if isinstance(cell, ModuleCell):
-            cell.w_value = w_value
+        w_value = write_cell(self.space, cell, w_value)
+        if w_value is None:
             return
-        if cell is not None:
-            # If the new value and the current value are the same, don't
-            # create a level of indirection, or mutate the version.
-            if self.space.is_w(w_value, cell):
-                return
-            w_value = ModuleCell(w_value)
         self.mutated()
         self.unerase(w_dict.dstorage)[key] = w_value
 
@@ -131,7 +118,7 @@
 
     def getitem_str(self, w_dict, key):
         cell = self.getdictvalue_no_unwrapping(w_dict, key)
-        return unwrap_cell(cell)
+        return unwrap_cell(self.space, cell)
 
     def w_keys(self, w_dict):
         space = self.space
@@ -140,12 +127,12 @@
 
     def values(self, w_dict):
         iterator = self.unerase(w_dict.dstorage).itervalues
-        return [unwrap_cell(cell) for cell in iterator()]
+        return [unwrap_cell(self.space, cell) for cell in iterator()]
 
     def items(self, w_dict):
         space = self.space
         iterator = self.unerase(w_dict.dstorage).iteritems
-        return [space.newtuple([_wrapkey(space, key), unwrap_cell(cell)])
+        return [space.newtuple([_wrapkey(space, key), unwrap_cell(self.space, cell)])
                 for key, cell in iterator()]
 
     def clear(self, w_dict):
@@ -157,7 +144,7 @@
         d = self.unerase(w_dict.dstorage)
         key, cell = d.popitem()
         self.mutated()
-        return _wrapkey(space, key), unwrap_cell(cell)
+        return _wrapkey(space, key), unwrap_cell(self.space, cell)
 
     def switch_to_object_strategy(self, w_dict):
         space = self.space
@@ -165,7 +152,7 @@
         strategy = space.fromcache(ObjectDictStrategy)
         d_new = strategy.unerase(strategy.get_empty_storage())
         for key, cell in d.iteritems():
-            d_new[_wrapkey(space, key)] = unwrap_cell(cell)
+            d_new[_wrapkey(space, key)] = unwrap_cell(self.space, cell)
         w_dict.strategy = strategy
         w_dict.dstorage = strategy.erase(d_new)
 
@@ -181,7 +168,7 @@
     wrapkey = _wrapkey
 
     def wrapvalue(space, value):
-        return unwrap_cell(value)
+        return unwrap_cell(space, value)
 
 
 create_iterator_classes(ModuleDictStrategy)
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
@@ -22,6 +22,10 @@
     def unwrap_cell(self, space):
         return self.w_value
 
+    def __repr__(self):
+        return "<ObjectMutableCell: %s>" % (self.w_value, )
+
+
 class IntMutableCell(MutableCell):
     def __init__(self, intvalue):
         self.intvalue = intvalue
@@ -29,6 +33,9 @@
     def unwrap_cell(self, space):
         return space.wrap(self.intvalue)
 
+    def __repr__(self):
+        return "<IntMutableCell: %s>" % (self.intvalue, )
+
 
 def unwrap_cell(space, w_value):
     if space.config.objspace.std.withtypeversion:
@@ -38,6 +45,9 @@
 
 def write_cell(space, w_cell, w_value):
     from pypy.objspace.std.intobject import W_IntObject
+    if w_cell is None:
+        # attribute does not exist at all, write it without a cell first
+        return w_value
     if isinstance(w_cell, ObjectMutableCell):
         w_cell.w_value = w_value
         return None
@@ -303,10 +313,9 @@
             if version_tag is not None:
                 w_curr = w_self._pure_getdictvalue_no_unwrapping(
                         space, version_tag, name)
-                if w_curr is not None:
-                    w_value = write_cell(space, w_curr, w_value)
-                    if w_value is None:
-                        return True
+                w_value = write_cell(space, w_curr, w_value)
+                if w_value is None:
+                    return True
         w_self.mutated(name)
         w_self.dict_w[name] = w_value
         return True


More information about the pypy-commit mailing list