Issue #9626: Fix views in collections.OrderedDict().

This commit is contained in:
Raymond Hettinger 2010-08-17 19:03:06 +00:00
parent d9ed62cde3
commit a54b2dac90
3 changed files with 22 additions and 0 deletions

View file

@ -119,6 +119,18 @@ class OrderedDict(dict, MutableMapping):
iteritems = MutableMapping.iteritems iteritems = MutableMapping.iteritems
__ne__ = MutableMapping.__ne__ __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): def popitem(self, last=True):
'''od.popitem() -> (k, v), return and remove a (key, value) pair. '''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. Pairs are returned in LIFO order if last is true or FIFO order if false.

View file

@ -933,6 +933,12 @@ class TestOrderedDict(unittest.TestCase):
od['a'] = 1 od['a'] = 1
self.assertEqual(list(od.items()), [('b', 2), ('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): class GeneralMappingTests(mapping_tests.BasicTestMappingProtocol):

View file

@ -29,6 +29,10 @@ Core and Builtins
Library 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 - Issue #8688: MANIFEST files created by distutils now include a magic
comment indicating they are generated. Manually maintained MANIFESTs comment indicating they are generated. Manually maintained MANIFESTs
without this marker will not be overwritten or removed. without this marker will not be overwritten or removed.