mirror of
https://github.com/python/cpython.git
synced 2025-12-04 08:34:25 +00:00
Fix defect in __ixor__ which would get the wrong
answer if the input iterable had a duplicate element (two calls to toggle() reverse each other). Borrow the correct code from sets.py.
This commit is contained in:
parent
cba36bbe65
commit
e67420d72e
1 changed files with 6 additions and 13 deletions
|
|
@ -266,16 +266,6 @@ class MutableSet(Set):
|
||||||
self.discard(value)
|
self.discard(value)
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def toggle(self, value):
|
|
||||||
"""Return True if it was added, False if deleted."""
|
|
||||||
# XXX This implementation is not thread-safe
|
|
||||||
if value in self:
|
|
||||||
self.discard(value)
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
self.add(value)
|
|
||||||
return True
|
|
||||||
|
|
||||||
def clear(self):
|
def clear(self):
|
||||||
"""This is slow (creates N new iterators!) but effective."""
|
"""This is slow (creates N new iterators!) but effective."""
|
||||||
try:
|
try:
|
||||||
|
|
@ -296,10 +286,13 @@ class MutableSet(Set):
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __ixor__(self, it):
|
def __ixor__(self, it):
|
||||||
# This calls toggle(), so if that is overridded, we call the override
|
if not isinstance(it, Set):
|
||||||
|
it = self._from_iterable(it)
|
||||||
for value in it:
|
for value in it:
|
||||||
self.toggle(it)
|
if value in self:
|
||||||
return self
|
self.discard(value)
|
||||||
|
else:
|
||||||
|
self.add(value)
|
||||||
|
|
||||||
def __isub__(self, it):
|
def __isub__(self, it):
|
||||||
for value in it:
|
for value in it:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue