Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r72231:8f38ba1f23f0
Date: 2014-06-25 20:19 +0200
http://bitbucket.org/pypy/pypy/changeset/8f38ba1f23f0/

Log:    backout cc99ff68804a: any built-in lookup first misses a global
        ModuleDict! We need to be more subtle...

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
@@ -16,7 +16,7 @@
 
 
 class ModuleCell(W_Root):
-    def __init__(self, w_value):
+    def __init__(self, w_value=None):
         self.w_value = w_value
 
     def __repr__(self):
@@ -49,24 +49,17 @@
         return self.erase({})
 
     def mutated(self):
-        # A mutation means changing an existing key to point to a new value.
-        # A value is either a regular wrapped object, or a ModuleCell if we
-        # detect mutations.  It means that each existing key can only trigger
-        # a mutation at most once.
         self.version = VersionTag()
 
-    def dictvalue_no_unwrapping(self, w_dict, key):
+    def getdictvalue_no_unwrapping(self, w_dict, key):
         # NB: it's important to promote self here, so that self.version is a
         # no-op due to the quasi-immutable field
         self = jit.promote(self)
-        return self._dictvalue_no_unwrapping_pure(self.version, w_dict, key)
+        return self._getdictvalue_no_unwrapping_pure(self.version, w_dict, key)
 
     @jit.elidable_promote('0,1,2')
-    def _dictvalue_no_unwrapping_pure(self, version, w_dict, key):
-        # may raise KeyError.  If it does, then the JIT is prevented from
-        # considering this function as elidable.  This is what lets us add
-        # new keys to the dictionary without changing the version.
-        return self.unerase(w_dict.dstorage)[key]
+    def _getdictvalue_no_unwrapping_pure(self, version, w_dict, key):
+        return self.unerase(w_dict.dstorage).get(key, None)
 
     def setitem(self, w_dict, w_key, w_value):
         space = self.space
@@ -77,20 +70,17 @@
             w_dict.setitem(w_key, w_value)
 
     def setitem_str(self, w_dict, key, w_value):
-        try:
-            cell = self.dictvalue_no_unwrapping(w_dict, key)
-        except KeyError:
-            pass
-        else:
-            if isinstance(cell, ModuleCell):
-                cell.w_value = w_value
-                return
+        cell = self.getdictvalue_no_unwrapping(w_dict, key)
+        if isinstance(cell, ModuleCell):
+            cell.w_value = w_value
+            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.mutated()
         self.unerase(w_dict.dstorage)[key] = w_value
 
     def setdefault(self, w_dict, w_key, w_default):
@@ -140,10 +130,7 @@
             return w_dict.getitem(w_key)
 
     def getitem_str(self, w_dict, key):
-        try:
-            cell = self.dictvalue_no_unwrapping(w_dict, key)
-        except KeyError:
-            return None
+        cell = self.getdictvalue_no_unwrapping(w_dict, key)
         return unwrap_cell(cell)
 
     def w_keys(self, w_dict):
diff --git a/pypy/objspace/std/test/test_celldict.py 
b/pypy/objspace/std/test/test_celldict.py
--- a/pypy/objspace/std/test/test_celldict.py
+++ b/pypy/objspace/std/test/test_celldict.py
@@ -21,27 +21,27 @@
         w_key = self.FakeString(key)
         d.setitem(w_key, 1)
         v2 = strategy.version
-        assert v1 is v2          # doesn't change when adding new keys
+        assert v1 is not v2
         assert d.getitem(w_key) == 1
-        assert d.strategy.dictvalue_no_unwrapping(d, key) == 1
+        assert d.strategy.getdictvalue_no_unwrapping(d, key) == 1
 
         d.setitem(w_key, 2)
         v3 = strategy.version
         assert v2 is not v3
         assert d.getitem(w_key) == 2
-        assert d.strategy.dictvalue_no_unwrapping(d, key).w_value == 2
+        assert d.strategy.getdictvalue_no_unwrapping(d, key).w_value == 2
 
         d.setitem(w_key, 3)
         v4 = strategy.version
         assert v3 is v4
         assert d.getitem(w_key) == 3
-        assert d.strategy.dictvalue_no_unwrapping(d, key).w_value == 3
+        assert d.strategy.getdictvalue_no_unwrapping(d, key).w_value == 3
 
         d.delitem(w_key)
         v5 = strategy.version
         assert v5 is not v4
         assert d.getitem(w_key) is None
-        py.test.raises(KeyError, d.strategy.dictvalue_no_unwrapping, d, key)
+        assert d.strategy.getdictvalue_no_unwrapping(d, key) is None
 
     def test_same_key_set_twice(self):
         strategy = ModuleDictStrategy(space)
@@ -52,7 +52,7 @@
         x = object()
         d.setitem("a", x)
         v2 = strategy.version
-        assert v1 is v2
+        assert v1 is not v2
         d.setitem("a", x)
         v3 = strategy.version
         assert v2 is v3
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to