From: Raymond Hettinger Date: Wed, 1 Apr 2009 18:55:57 +0000 (+0000) Subject: Issue #5647: MutableSet.__iand__() no longer mutates self during iteration. X-Git-Tag: v2.6.2c1~45 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=38f57a1c757630c248ff139a9f123df32931c357;p=thirdparty%2FPython%2Fcpython.git Issue #5647: MutableSet.__iand__() no longer mutates self during iteration. --- diff --git a/Lib/_abcoll.py b/Lib/_abcoll.py index 942a72c0ce76..a355be9a79b3 100644 --- a/Lib/_abcoll.py +++ b/Lib/_abcoll.py @@ -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): diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index fe96117ffdd6..3db5f252424d 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -311,6 +311,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): @@ -347,6 +366,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):