[Python-checkins] r70973 - in python/branches/release30-maint: Lib/_abcoll.py Lib/test/test_collections.py Misc/NEWS

raymond.hettinger python-checkins at python.org
Wed Apr 1 21:03:31 CEST 2009


Author: raymond.hettinger
Date: Wed Apr  1 21:03:30 2009
New Revision: 70973

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

Modified:
   python/branches/release30-maint/Lib/_abcoll.py
   python/branches/release30-maint/Lib/test/test_collections.py
   python/branches/release30-maint/Misc/NEWS

Modified: python/branches/release30-maint/Lib/_abcoll.py
==============================================================================
--- python/branches/release30-maint/Lib/_abcoll.py	(original)
+++ python/branches/release30-maint/Lib/_abcoll.py	Wed Apr  1 21:03:30 2009
@@ -320,10 +320,9 @@
             self.add(value)
         return self
 
-    def __iand__(self, c: Container):
-        for value in self:
-            if value not in c:
-                self.discard(value)
+    def __iand__(self, it: Iterable):
+        for value in (self - it):
+            self.discard(value)
         return self
 
     def __ixor__(self, it: Iterable):

Modified: python/branches/release30-maint/Lib/test/test_collections.py
==============================================================================
--- python/branches/release30-maint/Lib/test/test_collections.py	(original)
+++ python/branches/release30-maint/Lib/test/test_collections.py	Wed Apr  1 21:03:30 2009
@@ -312,6 +312,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):
 
@@ -348,6 +367,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):

Modified: python/branches/release30-maint/Misc/NEWS
==============================================================================
--- python/branches/release30-maint/Misc/NEWS	(original)
+++ python/branches/release30-maint/Misc/NEWS	Wed Apr  1 21:03:30 2009
@@ -33,6 +33,8 @@
 - Issue #5619: Multiprocessing children disobey the debug flag and causes
   popups on windows buildbots. Patch applied to work around this issue.
 
+- Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
+
 - Issue #5387: Fixed mmap.move crash by integer overflow.
 
 - Issue #5595: Fix UnboundedLocalError in ntpath.ismount().


More information about the Python-checkins mailing list