From a54b2dac90d3505176bdc589ee1ef09ff2b7113a Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Tue, 17 Aug 2010 19:03:06 +0000 Subject: [PATCH] Issue #9626: Fix views in collections.OrderedDict(). --- Lib/collections.py | 12 ++++++++++++ Lib/test/test_collections.py | 6 ++++++ Misc/NEWS | 4 ++++ 3 files changed, 22 insertions(+) diff --git a/Lib/collections.py b/Lib/collections.py index 20cc6003bc5..10c8903697c 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -119,6 +119,18 @@ class OrderedDict(dict, MutableMapping): 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 a0f0fbc5563..f10f956f9b9 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -933,6 +933,12 @@ class TestOrderedDict(unittest.TestCase): 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): diff --git a/Misc/NEWS b/Misc/NEWS index 09f651af0d2..e8b68e0bab9 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -29,6 +29,10 @@ Core and Builtins Library ------- +- Issue #9626: the view methods for collections.OrderedDict() were returning + the unordered versions inherited from dict. Those methods are now + overridden to provide ordered views. + - Issue #8688: MANIFEST files created by distutils now include a magic comment indicating they are generated. Manually maintained MANIFESTs without this marker will not be overwritten or removed.