]> 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 18:50:56 +0000 (18:50 +0000)
committerRaymond Hettinger <python@rcn.com>
Wed, 1 Apr 2009 18:50:56 +0000 (18:50 +0000)
Lib/_abcoll.py
Lib/test/test_collections.py
Misc/NEWS

index 40cc23e0489ef6eda09a1c2e18064919aa840284..990ff009824b95d6913a3aa089a498e1d921fd08 100644 (file)
@@ -286,10 +286,9 @@ class MutableSet(Set):
             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):
index 9e984ba930d2fc9c72ee6134703b010c13fb911b..8ffa75bdc5d3b335108f9195ef4fb5706e0db409 100644 (file)
@@ -327,6 +327,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):
 
@@ -363,6 +382,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 f5e280c312af6e02840e928a50b12762186dec16..2e01d0d7261d9ec1d8591e1c0aec9dc60d426950 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -206,6 +206,8 @@ Library
   instead of performing them in functions. Helps prevent import deadlocking in
   threads.
 
+- Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
+
 - Actually make the SimpleXMLRPCServer CGI handler work.
 
 - Issue #2522: locale.format now checks its first argument to ensure it has