def _remove(item, selfref=ref(self)): self = selfref() if self is not None: if self._iterating: self._pending_removals.append(item) else: self.data.discard(item)
def __isub__(self, other): if self._pending_removals: self._commit_removals() if self is other: self.data.clear() else: self.data.difference_update(ref(item) for item in other) return self
def __ixor__(self, other): if self._pending_removals: self._commit_removals() if self is other: self.data.clear() else: self.data.symmetric_difference_update(ref(item, self._remove) for item in other) return self
def __ixor__(self, other): if self._pending_removals: self._commit_removals() if self is other: self.data.clear() else: self.data.symmetric_difference_update( ref(item, self._remove) for item in other) return self
def issuperset(self, other): return self.data.issuperset(ref(item) for item in other)
def __iand__(self, other): if self._pending_removals: self._commit_removals() self.data.intersection_update(ref(item) for item in other) return self
def __init__(self, weakcontainer): # Don't create cycles self.weakcontainer = ref(weakcontainer)
def discard(self, item): if self._pending_removals: self._commit_removals() self.data.discard(ref(item))
def __contains__(self, item): try: wr = ref(item) except TypeError: return False return wr in self.data
def __gt__(self, other): return self.data > set(ref(item) for item in other)
def __eq__(self, other): if not isinstance(other, self.__class__): return NotImplemented return self.data == set(ref(item) for item in other)
def add(self, item): if self._pending_removals: self._commit_removals() self.data.add(ref(item, self._remove))
def remove(self, item): if self._pending_removals: self._commit_removals() self.data.remove(ref(item))