[pypy-svn] r76694 - in pypy/branch/better-map-instances/pypy/objspace/std: . test
cfbolz at codespeak.net
cfbolz at codespeak.net
Sat Aug 21 00:33:18 CEST 2010
Author: cfbolz
Date: Sat Aug 21 00:33:14 2010
New Revision: 76694
Modified:
pypy/branch/better-map-instances/pypy/objspace/std/mapdict.py
pypy/branch/better-map-instances/pypy/objspace/std/test/test_mapdict.py
Log:
another piece of the puzzle: turn map-instances into devolved multi-dicts
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 Sat Aug 21 00:33:14 2010
@@ -52,6 +52,9 @@
obj.storage[attr.position] = w_value
obj.map = attr
+ def materialize_r_dict(self, space, obj, w_d):
+ raise NotImplementedError("abstract base class")
+
class Terminator(AbstractAttribute):
def __init__(self, w_cls=None):
@@ -80,6 +83,11 @@
result._init_empty(terminator)
return result
+ def materialize_r_dict(self, space, obj, w_d):
+ result = Object()
+ result._init_empty(self) # XXX
+ return result
+
class NoDictTerminator(Terminator):
def write(self, obj, selector, w_value):
if selector[1] == DICT:
@@ -130,6 +138,15 @@
return self
return self.back.search(attrtype)
+ def materialize_r_dict(self, space, obj, w_d):
+ new_obj = self.back.materialize_r_dict(space, obj, w_d)
+ if self.selector[1] == DICT:
+ w_attr = space.wrap(self.selector[0])
+ w_d.r_dict_content[w_attr] = obj.storage[self.position]
+ else:
+ self._copy_attr(obj, new_obj)
+ return new_obj
+
# ____________________________________________________________
# object implementation
@@ -169,7 +186,8 @@
if w_dict is not None:
return w_dict
w_dict = MapDictImplementation(self.space, self)
- self.map.write(self, ("dict", SPECIAL), w_dict)
+ flag = self.map.write(self, ("dict", SPECIAL), w_dict)
+ assert flag
return w_dict
def setdict(self, space, w_dict):
@@ -274,19 +292,16 @@
r_dict_content = self.initialize_as_rdict()
space = self.space
w_obj = self.w_obj
- curr = w_obj.map.search(DICT)
- while curr is not None:
- attr = curr.selector[0]
- r_dict_content[space.wrap(attr)] = w_obj.getdictvalue(space, attr)
- curr = curr.back
- curr = curr.search(DICT)
+ materialize_r_dict(space, w_obj, self)
self._clear_fields()
return self
-def _materialize_r_dict(space, obj, w_d):
- assert isinstance(w_d, MapDictImplementation)
- #XXX
+def materialize_r_dict(space, obj, w_d):
+ map = obj.map # XXX
+ assert obj.getdict() is w_d
+ new_obj = map.materialize_r_dict(space, obj, w_d)
+ obj._become(new_obj)
class MapDictIteratorImplementation(IteratorImplementation):
def __init__(self, space, dictimplementation):
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 Sat Aug 21 00:33:14 2010
@@ -211,6 +211,32 @@
assert obj.getdict().length() == 3
+def test_materialize_r_dict():
+ cls = Class()
+ obj = cls.instantiate()
+ a = FakeMember("a")
+ b = FakeMember("b")
+ c = FakeMember("c")
+ obj.setslotvalue(a, 50)
+ obj.setslotvalue(b, 60)
+ obj.setslotvalue(c, 70)
+ obj.setdictvalue(space, "a", 5)
+ obj.setdictvalue(space, "b", 6)
+ obj.setdictvalue(space, "c", 7)
+ assert obj.storage == [50, 60, 70, 5, 6, 7]
+
+ class FakeDict(object):
+ def __init__(self, d):
+ self.r_dict_content = d
+
+ d = {}
+ w_d = FakeDict(d)
+ flag = obj.map.write(obj, ("dict", SPECIAL), w_d)
+ assert flag
+ materialize_r_dict(space, obj, w_d)
+ assert d == {"a": 5, "b": 6, "c": 7}
+ assert obj.storage == [50, 60, 70, w_d]
+
def test_size_prediction():
for i in range(10):
More information about the Pypy-commit
mailing list