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

cfbolz at codespeak.net cfbolz at codespeak.net
Tue Nov 3 15:20:42 CET 2009


Author: cfbolz
Date: Tue Nov  3 15:20:41 2009
New Revision: 68937

Modified:
   pypy/trunk/pypy/objspace/std/sharingdict.py
   pypy/trunk/pypy/objspace/std/test/test_sharingdict.py
Log:
don't devolve sharing dictionaries when a key is deleted


Modified: pypy/trunk/pypy/objspace/std/sharingdict.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/sharingdict.py	(original)
+++ pypy/trunk/pypy/objspace/std/sharingdict.py	Tue Nov  3 15:20:41 2009
@@ -126,12 +126,24 @@
         w_key_type = space.type(w_key)
         if space.is_w(w_key_type, space.w_str):
             key = space.str_w(w_key)
-            if (self.structure.last_key is not None and
-                key == self.structure.last_key):
-                self.entries[self.structure.length - 1] = None
-                self.structure = self.structure.back_struct
-                return
-            self._as_rdict().delitem(w_key)
+            pos = self.structure.lookup_position(key)
+            if pos == -1:
+                raise KeyError
+            for i in range(pos, len(self.entries)):
+                self.entries[pos] = self.entries[pos + 1]
+            # don't make the entries list shorter, new keys might be added soon
+            self.entries[-1] = None
+            structure = self.structure
+            num_back = len(self.entries) - pos - 1
+            keys = [None] * num_back
+            for i in range(num_back):
+                keys[i] = structure.last_key
+                structure = structure.back_struct
+            # go back the structure that contains the deleted key
+            structure = structure.back_struct
+            for i in range(num_back - 1, -1, -1):
+                structure = structure.get_next_structure(keys[i])
+            self.structure = structure
         elif _is_sane_hash(space, w_key_type):
             raise KeyError
         else:

Modified: pypy/trunk/pypy/objspace/std/test/test_sharingdict.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/test/test_sharingdict.py	(original)
+++ pypy/trunk/pypy/objspace/std/test/test_sharingdict.py	Tue Nov  3 15:20:41 2009
@@ -1,6 +1,8 @@
+import py
 from pypy.conftest import gettestobjspace
-from pypy.objspace.std.sharingdict import SharedStructure, NUM_DIGITS
+from pypy.objspace.std.sharingdict import SharedStructure, NUM_DIGITS, SharedDictImplementation
 from pypy.interpreter import gateway
+from pypy.objspace.std.test.test_dictmultiobject import FakeSpace
 
 def instance_with_keys(structure, *keys):
     for key in keys:
@@ -27,3 +29,19 @@
     assert empty_structure.size_estimate() == 3
     assert empty_structure.other_structs.get("a").size_estimate() == 6
     assert empty_structure.other_structs.get("x").size_estimate() == 2
+
+def test_delete():
+    space = FakeSpace()
+    d = SharedDictImplementation(space)
+    d.setitem_str("a", 1)
+    d.setitem_str("b", 2)
+    d.setitem_str("c", 3)
+    d.delitem("b")
+    assert d.r_dict_content is None
+    assert d.entries == [1, 3, None]
+    assert d.structure.keys == {"a": 0, "c": 1}
+    assert d.getitem("a") == 1
+    assert d.getitem("c") == 3
+    assert d.getitem("b") is None
+    py.test.raises(KeyError, d.delitem, "b")
+



More information about the Pypy-commit mailing list