[Python-checkins] r70970 - in python/branches/release26-maint/Lib: _abcoll.py test/test_collections.py

raymond.hettinger python-checkins at python.org
Wed Apr 1 20:55:57 CEST 2009


Author: raymond.hettinger
Date: Wed Apr  1 20:55:57 2009
New Revision: 70970

Log:
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.

Modified:
   python/branches/release26-maint/Lib/_abcoll.py
   python/branches/release26-maint/Lib/test/test_collections.py

Modified: python/branches/release26-maint/Lib/_abcoll.py
==============================================================================
--- python/branches/release26-maint/Lib/_abcoll.py	(original)
+++ python/branches/release26-maint/Lib/_abcoll.py	Wed Apr  1 20:55:57 2009
@@ -286,10 +286,9 @@
             self.add(value)
         return self
 
-    def __iand__(self, c):
-        for value in self:
-            if value not in c:
-                self.discard(value)
+    def __iand__(self, it):
+        for value in (self - it):
+            self.discard(value)
         return self
 
     def __ixor__(self, it):

Modified: python/branches/release26-maint/Lib/test/test_collections.py
==============================================================================
--- python/branches/release26-maint/Lib/test/test_collections.py	(original)
+++ python/branches/release26-maint/Lib/test/test_collections.py	Wed Apr  1 20:55:57 2009
@@ -311,6 +311,25 @@
             B.register(C)
             self.failUnless(issubclass(C, B))
 
+class WithSet(MutableSet):
+
+    def __init__(self, it=()):
+        self.data = set(it)
+
+    def __len__(self):
+        return len(self.data)
+
+    def __iter__(self):
+        return iter(self.data)
+
+    def __contains__(self, item):
+        return item in self.data
+
+    def add(self, item):
+        self.data.add(item)
+
+    def discard(self, item):
+        self.data.discard(item)
 
 class TestCollectionABCs(ABCTestCase):
 
@@ -347,6 +366,12 @@
         self.validate_abstract_methods(MutableSet, '__contains__', '__iter__', '__len__',
             'add', 'discard')
 
+    def test_issue_5647(self):
+        # MutableSet.__iand__ mutated the set during iteration
+        s = WithSet('abcd')
+        s &= WithSet('cdef')            # This used to fail
+        self.assertEqual(set(s), set('cd'))
+
     def test_issue_4920(self):
         # MutableSet.pop() method did not work
         class MySet(collections.MutableSet):


More information about the Python-checkins mailing list