[pypy-svn] r79377 - in pypy/branch/fast-forward/pypy/objspace/std: . test
afa at codespeak.net
afa at codespeak.net
Tue Nov 23 00:04:14 CET 2010
Author: afa
Date: Tue Nov 23 00:04:12 2010
New Revision: 79377
Modified:
pypy/branch/fast-forward/pypy/objspace/std/dictmultiobject.py
pypy/branch/fast-forward/pypy/objspace/std/dicttype.py
pypy/branch/fast-forward/pypy/objspace/std/model.py
pypy/branch/fast-forward/pypy/objspace/std/test/test_dictmultiobject.py
Log:
Start implementing dictionary views
Modified: pypy/branch/fast-forward/pypy/objspace/std/dictmultiobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/dictmultiobject.py (original)
+++ pypy/branch/fast-forward/pypy/objspace/std/dictmultiobject.py Tue Nov 23 00:04:12 2010
@@ -756,6 +756,15 @@
def dict_itervalues__DictMulti(space, w_self):
return W_DictMultiIterObject(space, w_self.iter(), VALUESITER)
+def dict_viewitems__DictMulti(space, w_self):
+ return W_DictMultiViewItemsObject(space, w_self)
+
+def dict_viewkeys__DictMulti(space, w_self):
+ return W_DictMultiViewKeysObject(space, w_self)
+
+def dict_viewvalues__DictMulti(space, w_self):
+ return W_DictMultiViewValuesObject(space, w_self)
+
def dict_clear__DictMulti(space, w_self):
w_self.clear()
@@ -874,6 +883,29 @@
raise OperationError(space.w_StopIteration, space.w_None)
# ____________________________________________________________
+# Views
+
+class W_DictViewObject(W_Object):
+ def __init__(w_self, space, w_dict):
+ w_self.w_dict = w_dict
+
+class W_DictMultiViewKeysObject(W_DictViewObject):
+ from pypy.objspace.std.dicttype import dict_keys_typedef as typedef
+registerimplementation(W_DictMultiViewKeysObject)
+
+class W_DictMultiViewItemsObject(W_DictViewObject):
+ from pypy.objspace.std.dicttype import dict_items_typedef as typedef
+registerimplementation(W_DictMultiViewItemsObject)
+
+class W_DictMultiViewValuesObject(W_DictViewObject):
+ from pypy.objspace.std.dicttype import dict_values_typedef as typedef
+registerimplementation(W_DictMultiViewValuesObject)
+
+def len__DictMultiViewKeys(space, w_dictview):
+ return space.len(w_dictview.w_dict)
+len__DictMultiViewItems = len__DictMultiViewValues = len__DictMultiViewKeys
+
+# ____________________________________________________________
from pypy.objspace.std import dicttype
register_all(vars(), dicttype)
Modified: pypy/branch/fast-forward/pypy/objspace/std/dicttype.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/dicttype.py (original)
+++ pypy/branch/fast-forward/pypy/objspace/std/dicttype.py Tue Nov 23 00:04:12 2010
@@ -44,6 +44,12 @@
doc='D.iterkeys() -> an iterator over the keys of D')
dict_itervalues = SMM('itervalues', 1,
doc='D.itervalues() -> an iterator over the values of D')
+dict_viewkeys = SMM('viewkeys', 1,
+ doc="D.viewkeys() -> a set-like object providing a view on D's keys")
+dict_viewitems = SMM('viewitems', 1,
+ doc="D.viewitems() -> a set-like object providing a view on D's items")
+dict_viewvalues = SMM('viewvalues', 1,
+ doc="D.viewvalues() -> an object providing a view on D's values")
dict_reversed = SMM('__reversed__', 1)
def dict_reversed__ANY(space, w_dict):
@@ -160,3 +166,29 @@
__reduce__ = gateway.interp2app(descr_dictiter__reduce__,
unwrap_spec=[gateway.W_Root, gateway.ObjSpace]),
)
+
+# ____________________________________________________________
+# Dict views
+
+
+ at gateway.unwrap_spec(gateway.ObjSpace, gateway.W_Root, gateway.W_Root)
+def descr_view__new__(space, w_viewtype, w_dict):
+ from pypy.objspace.std.dictmultiobject import W_DictView
+ w_obj = space.allocate_instance(W_DictView, w_viewtype)
+ w_obj.__init__(space, w_dict)
+ return w_obj
+
+dict_keys_typedef = StdTypeDef(
+ "dict_keys",
+ __new__ = descr_view__new__,
+ )
+
+dict_items_typedef = StdTypeDef(
+ "dict_items",
+ __new__ = descr_view__new__,
+ )
+
+dict_values_typedef = StdTypeDef(
+ "dict_values",
+ __new__ = descr_view__new__,
+ )
Modified: pypy/branch/fast-forward/pypy/objspace/std/model.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/model.py (original)
+++ pypy/branch/fast-forward/pypy/objspace/std/model.py Tue Nov 23 00:04:12 2010
@@ -122,6 +122,9 @@
iterobject.W_ReverseSeqIterObject: [],
unicodeobject.W_UnicodeObject: [],
dictproxyobject.W_DictProxyObject: [],
+ dictmultiobject.W_DictMultiViewKeysObject: [],
+ dictmultiobject.W_DictMultiViewItemsObject: [],
+ dictmultiobject.W_DictMultiViewValuesObject: [],
pypy.interpreter.pycode.PyCode: [],
pypy.interpreter.special.Ellipsis: [],
}
Modified: pypy/branch/fast-forward/pypy/objspace/std/test/test_dictmultiobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/test/test_dictmultiobject.py (original)
+++ pypy/branch/fast-forward/pypy/objspace/std/test/test_dictmultiobject.py Tue Nov 23 00:04:12 2010
@@ -500,6 +500,12 @@
iterable = {}
raises(TypeError, len, iter(iterable))
+ def test_dictview(self):
+ d = {1: 2, 3: 4}
+ assert len(d.viewkeys()) == 2
+ assert len(d.viewitems()) == 2
+ assert len(d.viewvalues()) == 2
+
class AppTest_DictMultiObject(AppTest_DictObject):
More information about the Pypy-commit
mailing list