Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
This commit is contained in:
parent
0759dd66c5
commit
3f10a952f6
|
@ -320,9 +320,8 @@ class MutableSet(Set):
|
|||
self.add(value)
|
||||
return self
|
||||
|
||||
def __iand__(self, c: Container):
|
||||
for value in self:
|
||||
if value not in c:
|
||||
def __iand__(self, it: Iterable):
|
||||
for value in (self - it):
|
||||
self.discard(value)
|
||||
return self
|
||||
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue