[Python-checkins] r84148 - in python/branches/release27-maint: Lib/collections.py Lib/test/test_collections.py Misc/NEWS

raymond.hettinger python-checkins at python.org
Tue Aug 17 21:03:06 CEST 2010


Author: raymond.hettinger
Date: Tue Aug 17 21:03:06 2010
New Revision: 84148

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

Modified:
   python/branches/release27-maint/Lib/collections.py
   python/branches/release27-maint/Lib/test/test_collections.py
   python/branches/release27-maint/Misc/NEWS

Modified: python/branches/release27-maint/Lib/collections.py
==============================================================================
--- python/branches/release27-maint/Lib/collections.py	(original)
+++ python/branches/release27-maint/Lib/collections.py	Tue Aug 17 21:03:06 2010
@@ -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.

Modified: python/branches/release27-maint/Lib/test/test_collections.py
==============================================================================
--- python/branches/release27-maint/Lib/test/test_collections.py	(original)
+++ python/branches/release27-maint/Lib/test/test_collections.py	Tue Aug 17 21:03:06 2010
@@ -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):

Modified: python/branches/release27-maint/Misc/NEWS
==============================================================================
--- python/branches/release27-maint/Misc/NEWS	(original)
+++ python/branches/release27-maint/Misc/NEWS	Tue Aug 17 21:03:06 2010
@@ -29,6 +29,10 @@
 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.


More information about the Python-checkins mailing list