[Python-checkins] r84252 - in python/branches/py3k: Lib/_abcoll.py Lib/test/test_collections.py Misc/NEWS

raymond.hettinger python-checkins at python.org
Sun Aug 22 09:44:24 CEST 2010


Author: raymond.hettinger
Date: Sun Aug 22 09:44:24 2010
New Revision: 84252

Log:
Issue #9214:  Fix set operations on KeysView and ItemsView.

Modified:
   python/branches/py3k/Lib/_abcoll.py
   python/branches/py3k/Lib/test/test_collections.py
   python/branches/py3k/Misc/NEWS

Modified: python/branches/py3k/Lib/_abcoll.py
==============================================================================
--- python/branches/py3k/Lib/_abcoll.py	(original)
+++ python/branches/py3k/Lib/_abcoll.py	Sun Aug 22 09:44:24 2010
@@ -393,6 +393,10 @@
 
 class KeysView(MappingView, Set):
 
+    @classmethod
+    def _from_iterable(self, it):
+        return set(it)
+
     def __contains__(self, key):
         return key in self._mapping
 
@@ -405,6 +409,10 @@
 
 class ItemsView(MappingView, Set):
 
+    @classmethod
+    def _from_iterable(self, it):
+        return set(it)
+
     def __contains__(self, item):
         key, value = item
         try:

Modified: python/branches/py3k/Lib/test/test_collections.py
==============================================================================
--- python/branches/py3k/Lib/test/test_collections.py	(original)
+++ python/branches/py3k/Lib/test/test_collections.py	Sun Aug 22 09:44:24 2010
@@ -13,7 +13,7 @@
 from collections import Hashable, Iterable, Iterator
 from collections import Sized, Container, Callable
 from collections import Set, MutableSet
-from collections import Mapping, MutableMapping
+from collections import Mapping, MutableMapping, KeysView, ItemsView, UserDict
 from collections import Sequence, MutableSequence
 from collections import ByteString
 
@@ -548,6 +548,31 @@
         self.validate_abstract_methods(MutableMapping, '__contains__', '__iter__', '__len__',
             '__getitem__', '__setitem__', '__delitem__')
 
+    def test_MutableMapping_subclass(self):
+        # Test issue 9214
+        mymap = UserDict()
+        mymap['red'] = 5
+        self.assertIsInstance(mymap.keys(), Set)
+        self.assertIsInstance(mymap.keys(), KeysView)
+        self.assertIsInstance(mymap.items(), Set)
+        self.assertIsInstance(mymap.items(), ItemsView)
+
+        mymap = UserDict()
+        mymap['red'] = 5
+        z = mymap.keys() | {'orange'}
+        self.assertIsInstance(z, set)
+        list(z)
+        mymap['blue'] = 7               # Shouldn't affect 'z'
+        self.assertEqual(sorted(z), ['orange', 'red'])
+
+        mymap = UserDict()
+        mymap['red'] = 5
+        z = mymap.items() | {('orange', 3)}
+        self.assertIsInstance(z, set)
+        list(z)
+        mymap['blue'] = 7               # Shouldn't affect 'z'
+        self.assertEqual(sorted(z), [('orange', 3), ('red', 5)])
+
     def test_Sequence(self):
         for sample in [tuple, list, bytes, str]:
             self.assertIsInstance(sample(), Sequence)

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Sun Aug 22 09:44:24 2010
@@ -66,6 +66,9 @@
 Extensions
 ----------
 
+- Issue #9214: Set operations on a KeysView or ItemsView in collections
+  now correctly return a set.  (Patch by Eli Bendersky.)
+
 - Issue #5737: Add Solaris-specific mnemonics in the errno module.  Patch by
   Matthew Ahrens.
 


More information about the Python-checkins mailing list