]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
authorRaymond Hettinger <python@rcn.com>
Wed, 1 Apr 2009 19:03:30 +0000 (19:03 +0000)
committerRaymond Hettinger <python@rcn.com>
Wed, 1 Apr 2009 19:03:30 +0000 (19:03 +0000)
Lib/_abcoll.py
Lib/test/test_collections.py
Misc/NEWS

index c7636ec80e6f5e0a863f6facbe4b0d78d24e751c..73966dbce6b65bd5e842b23b6caf41ee15436f9b 100644 (file)
@@ -320,10 +320,9 @@ class MutableSet(Set):
             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):
index ce886521a6393c839d79a292ad6555654654c083..0c8f7ff246c09ea528f3fb230b80b1aecbb823ce 100644 (file)
@@ -312,6 +312,25 @@ class TestOneTrickPonyABCs(ABCTestCase):
             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 @@ class TestCollectionABCs(ABCTestCase):
         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):
index 95bbb53c5b2837933aa29bba1b986ccda647db7b..3426a6f73189deb55044e0d3d19bdbe63d630cae 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -33,6 +33,8 @@ Library
 - 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().