Issue #9626: Fix views in collections.OrderedDict().
diff --git a/Lib/collections.py b/Lib/collections.py
index 20cc600..10c8903 100644
--- a/Lib/collections.py
+++ b/Lib/collections.py
@@ -119,6 +119,18 @@
iteritems = MutableMapping.iteritems
__ne__ = MutableMapping.__ne__
+ def viewkeys(self):
+ "od.viewkeys() -> a set-like object providing a view on od's keys"
+ return KeysView(self)
+
+ def viewvalues(self):
+ "od.viewvalues() -> an object providing a view on od's values"
+ return ValuesView(self)
+
+ def viewitems(self):
+ "od.viewitems() -> a set-like object providing a view on od's items"
+ return ItemsView(self)
+
def popitem(self, last=True):
'''od.popitem() -> (k, v), return and remove a (key, value) pair.
Pairs are returned in LIFO order if last is true or FIFO order if false.
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py
index a0f0fbc..f10f956 100644
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -933,6 +933,12 @@
od['a'] = 1
self.assertEqual(list(od.items()), [('b', 2), ('a', 1)])
+ def test_views(self):
+ s = 'the quick brown fox jumped over a lazy dog yesterday before dawn'.split()
+ od = OrderedDict.fromkeys(s)
+ self.assertEqual(list(od.viewkeys()), s)
+ self.assertEqual(list(od.viewvalues()), [None for k in s])
+ self.assertEqual(list(od.viewitems()), [(k, None) for k in s])
class GeneralMappingTests(mapping_tests.BasicTestMappingProtocol):