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

cfbolz at codespeak.net cfbolz at codespeak.net
Thu Aug 26 00:03:03 CEST 2010


Author: cfbolz
Date: Thu Aug 26 00:02:59 2010
New Revision: 76735

Modified:
   pypy/branch/better-map-instances/pypy/objspace/std/celldict.py
   pypy/branch/better-map-instances/pypy/objspace/std/dictmultiobject.py
   pypy/branch/better-map-instances/pypy/objspace/std/mapdict.py
   pypy/branch/better-map-instances/pypy/objspace/std/sharingdict.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:
fix some XXXs:
   - a better clear
   - fix test about slots with the same names
   - when calling _as_rdict, always call the correct impl_fallback_* method


Modified: pypy/branch/better-map-instances/pypy/objspace/std/celldict.py
==============================================================================
--- pypy/branch/better-map-instances/pypy/objspace/std/celldict.py	(original)
+++ pypy/branch/better-map-instances/pypy/objspace/std/celldict.py	Thu Aug 26 00:02:59 2010
@@ -45,7 +45,7 @@
         if space.is_w(space.type(w_key), space.w_str):
             self.impl_setitem_str(self.space.str_w(w_key), w_value)
         else:
-            self._as_rdict().setitem(w_key, w_value)
+            self._as_rdict().impl_fallback_setitem(w_key, w_value)
 
     def impl_setitem_str(self, name, w_value, shadows_type=True):
         self.getcell(name, True).w_value = w_value
@@ -66,7 +66,7 @@
         elif _is_sane_hash(space, w_key_type):
             raise KeyError
         else:
-            self._as_rdict().delitem(w_key)
+            self._as_rdict().impl_fallback_delitem(w_key)
         
     def impl_length(self):
         # inefficient, but do we care?
@@ -85,7 +85,7 @@
         elif _is_sane_hash(space, w_lookup_type):
             return None
         else:
-            return self._as_rdict().getitem(w_lookup)
+            return self._as_rdict().impl_fallback_getitem(w_lookup)
 
     def impl_getitem_str(self, lookup):
         res = self.getcell(lookup, False)

Modified: pypy/branch/better-map-instances/pypy/objspace/std/dictmultiobject.py
==============================================================================
--- pypy/branch/better-map-instances/pypy/objspace/std/dictmultiobject.py	(original)
+++ pypy/branch/better-map-instances/pypy/objspace/std/dictmultiobject.py	Thu Aug 26 00:02:59 2010
@@ -310,7 +310,7 @@
         if space.is_w(space.type(w_key), space.w_str):
             self.impl_setitem_str(self.space.str_w(w_key), w_value)
         else:
-            self._as_rdict().setitem(w_key, w_value)
+            self._as_rdict().impl_fallback_setitem(w_key, w_value)
 
     def impl_setitem_str(self, key, w_value, shadows_type=True):
         self.content[key] = w_value
@@ -324,7 +324,7 @@
         elif _is_sane_hash(space, w_key_type):
             raise KeyError
         else:
-            self._as_rdict().delitem(w_key)
+            self._as_rdict().impl_fallback_delitem(w_key)
         
     def impl_length(self):
         return len(self.content)
@@ -344,7 +344,7 @@
         elif _is_sane_hash(space, w_lookup_type):
             return None
         else:
-            return self._as_rdict().getitem(w_key)
+            return self._as_rdict().impl_fallback_getitem(w_key)
 
     def impl_iter(self):
         return StrIteratorImplementation(self.space, self)
@@ -414,7 +414,7 @@
             StrDictImplementation.impl_setitem_str(
                 self, self.space.str_w(w_key), w_value, False)
         else:
-            self._as_rdict().setitem(w_key, w_value)
+            self._as_rdict().impl_fallback_setitem(w_key, w_value)
 
     def impl_shadows_anything(self):
         return (self._shadows_anything or 
@@ -446,7 +446,7 @@
         elif _is_sane_hash(space, w_key_type):
             raise KeyError
         else:
-            self._as_rdict().delitem(w_key)
+            self._as_rdict().impl_fallback_delitem(w_key)
 
     def impl_get_builtin_indexed(self, i):
         return self.shadowed[i]

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	Thu Aug 26 00:02:59 2010
@@ -57,6 +57,9 @@
     def materialize_r_dict(self, space, obj, w_d):
         raise NotImplementedError("abstract base class")
 
+    def remove_dict_entries(self, obj):
+        raise NotImplementedError("abstract base class")
+
 
 class Terminator(AbstractAttribute):
     def __init__(self, w_cls, space):
@@ -88,6 +91,8 @@
         result._init_empty(terminator)
         return result
 
+    def remove_dict_entries(self, obj):
+        return self.copy(obj)
 
 class DictTerminator(Terminator):
     def __init__(self, w_cls, space):
@@ -135,6 +140,8 @@
             return Terminator.copy(self, obj)
         return Terminator.delete(self, obj, selector)
 
+    def remove_dict_entries(self, obj):
+        assert 0, "should be unreachable"
 
 class PlainAttribute(AbstractAttribute):
     def __init__(self, selector, back):
@@ -197,13 +204,23 @@
             self._copy_attr(obj, new_obj)
         return new_obj
 
+    def remove_dict_entries(self, obj):
+        new_obj = self.back.remove_dict_entries(obj)
+        if self.selector[1] != DICT:
+            self._copy_attr(obj, new_obj)
+        return new_obj
 
+def _become(w_obj, new_obj):
+    # this is like the _become method, really, but we cannot use that due to
+    # RPython reasons
+    w_obj._set_mapdict_map(new_obj.map)
+    w_obj._set_mapdict_storage(new_obj.storage)
 # ____________________________________________________________
 # object implementation
 
-DICT = 6     # XXX meant '0'?
-SLOT = 1
-SPECIAL = 2
+DICT = 0
+SPECIAL = 1
+SLOTS_STARTING_FROM = 2
 
 from pypy.interpreter.baseobjspace import W_Root
 
@@ -273,10 +290,12 @@
         self._init_empty(w_subtype.terminator)
 
     def getslotvalue(self, member):
-        return self.map.read(self, (member.name, SLOT))
+        key = (member.name, SLOTS_STARTING_FROM + member.index)
+        return self.map.read(self, key)
 
     def setslotvalue(self, member, w_value):
-        self.map.write(self, (member.name, SLOT), w_value)
+        key = (member.name, SLOTS_STARTING_FROM + member.index)
+        self.map.write(self, key, w_value)
 
     # used by _weakref implemenation
 
@@ -314,7 +333,7 @@
         elif _is_sane_hash(space, w_lookup_type):
             return None
         else:
-            return self._as_rdict().getitem(w_lookup)
+            return self._as_rdict().impl_fallback_getitem(w_lookup)
 
     def impl_getitem_str(self, key):
         return self.w_obj.getdictvalue(self.space, key)
@@ -328,7 +347,7 @@
         if space.is_w(space.type(w_key), space.w_str):
             self.impl_setitem_str(self.space.str_w(w_key), w_value)
         else:
-            self._as_rdict().setitem(w_key, w_value)
+            self._as_rdict().impl_fallback_setitem(w_key, w_value)
 
     def impl_delitem(self, w_key):
         space = self.space
@@ -340,7 +359,7 @@
         elif _is_sane_hash(space, w_key_type):
             raise KeyError
         else:
-            self._as_rdict().delitem(w_key)
+            self._as_rdict().impl_fallback_delitem(w_key)
 
     def impl_length(self):
         res = 0
@@ -355,9 +374,9 @@
         return MapDictIteratorImplementation(self.space, self)
 
     def impl_clear(self):
-        # XXX implement me better, or provide a reasonable default
-        # XXX implementation in W_DictMultiObject
-        self._as_rdict().clear()
+        w_obj = self.w_obj
+        new_obj = w_obj._get_mapdict_map().remove_dict_entries(w_obj)
+        _become(w_obj, new_obj)
 
     def _clear_fields(self):
         self.w_obj = None
@@ -369,19 +388,13 @@
         materialize_r_dict(space, w_obj, self)
         self._clear_fields()
         return self
-        # XXX then the calls self._as_rdict().method() from above look like
-        # recursive calls, and a stack check is inserted, which is pointless.
-        # It would be better to return self.r_dict_content, I think
 
 
 def materialize_r_dict(space, obj, w_d):
     map = obj._get_mapdict_map()
     assert obj.getdict() is w_d
     new_obj = map.materialize_r_dict(space, obj, w_d)
-    # XXX this is like _become, really, but we cannot use that due to RPython
-    # reasons
-    obj._set_mapdict_map(new_obj.map)
-    obj._set_mapdict_storage(new_obj.storage)
+    _become(obj, new_obj)
 
 class MapDictIteratorImplementation(IteratorImplementation):
     def __init__(self, space, dictimplementation):

Modified: pypy/branch/better-map-instances/pypy/objspace/std/sharingdict.py
==============================================================================
--- pypy/branch/better-map-instances/pypy/objspace/std/sharingdict.py	(original)
+++ pypy/branch/better-map-instances/pypy/objspace/std/sharingdict.py	Thu Aug 26 00:02:59 2010
@@ -71,7 +71,7 @@
         elif _is_sane_hash(space, w_lookup_type):
             return None
         else:
-            return self._as_rdict().getitem(w_lookup)
+            return self._as_rdict().impl_fallback_getitem(w_lookup)
 
     def impl_getitem_str(self, lookup):
         i = self.structure.lookup_position(lookup)
@@ -84,7 +84,7 @@
         if space.is_w(space.type(w_key), space.w_str):
             self.impl_setitem_str(self.space.str_w(w_key), w_value)
         else:
-            self._as_rdict().setitem(w_key, w_value)
+            self._as_rdict().impl_fallback_setitem(w_key, w_value)
 
     @unroll_safe
     def impl_setitem_str(self, key, w_value, shadows_type=True):
@@ -132,7 +132,7 @@
         elif _is_sane_hash(space, w_key_type):
             raise KeyError
         else:
-            self._as_rdict().delitem(w_key)
+            self._as_rdict().impl_fallback_delitem(w_key)
         
     def impl_length(self):
         return self.structure.length

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	Thu Aug 26 00:02:59 2010
@@ -696,6 +696,14 @@
         assert self.impl.length() == 0
         self.check_not_devolved()
 
+    def test_clear(self):
+        self.fill_impl()
+        assert self.impl.length() == 2
+        self.impl.clear()
+        assert self.impl.length() == 0
+        self.check_not_devolved()
+
+
     def test_keys(self):
         self.fill_impl()
         keys = self.impl.keys()

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	Thu Aug 26 00:02:59 2010
@@ -5,8 +5,9 @@
 space = FakeSpace()
 
 class FakeMember(object):
-    def __init__(self, name):
+    def __init__(self, name, index=0):
         self.name = name
+        self.index = index
 
 class Class(object):
     def __init__(self, hasdict=True):
@@ -51,11 +52,11 @@
 def test_search():
     aa = PlainAttribute(("b", DICT), PlainAttribute(("a", DICT), Terminator(None, None)))
     assert aa.search(DICT) is aa
-    assert aa.search(SLOT) is None
+    assert aa.search(SLOTS_STARTING_FROM) is None
     assert aa.search(SPECIAL) is None
-    bb = PlainAttribute(("C", SPECIAL), PlainAttribute(("A", SLOT), aa))
+    bb = PlainAttribute(("C", SPECIAL), PlainAttribute(("A", SLOTS_STARTING_FROM), aa))
     assert bb.search(DICT) is aa
-    assert bb.search(SLOT) is bb.back
+    assert bb.search(SLOTS_STARTING_FROM) is bb.back
     assert bb.search(SPECIAL) is bb
 
 def test_add_attribute():
@@ -193,6 +194,21 @@
     assert obj2.storage == [501, 601, 701, 51, 61, 71]
     assert obj.map is obj2.map
 
+def test_slots_same_name():
+    cls = Class()
+    obj = cls.instantiate()
+    a =  FakeMember("a")
+    b =  FakeMember("a", 1)
+    c =  FakeMember("a", 2)
+    obj.setslotvalue(a, 50)
+    obj.setslotvalue(b, 60)
+    obj.setslotvalue(c, 70)
+    assert obj.getslotvalue(a) == 50
+    assert obj.getslotvalue(b) == 60
+    assert obj.getslotvalue(c) == 70
+    assert obj.storage == [50, 60, 70]
+
+
 def test_slots_no_dict():
     cls = Class(hasdict=False)
     obj = cls.instantiate()
@@ -366,6 +382,29 @@
         assert a.y == 6
         assert a.zz == 7
 
+    def test_read_write_dict(self):
+        class A(object):
+            pass
+        a = A()
+        a.x = 5
+        a.y = 6
+        a.zz = 7
+        d = a.__dict__
+        assert d == {"x": 5, "y": 6, "zz": 7}
+        d['dd'] = 41
+        assert a.dd == 41
+        del a.x
+        assert d == {"y": 6, "zz": 7, 'dd': 41}
+        d2 = d.copy()
+        d2[1] = 2
+        a.__dict__ = d2
+        assert a.y == 6
+        assert a.zz == 7
+        assert a.dd == 41
+        d['dd'] = 43
+        assert a.dd == 41
+
+
     def test_slot_name_conflict(self):
         class A(object):
             __slots__ = 'slot1'



More information about the Pypy-commit mailing list