# Access WeakSet through the weakref module.
# This code is separated-out because it is needed
# by abc.py to load everything else at startup.
# This context manager registers itself in the current iterators of the
# weak container, such as to delay all removals until the context manager
# This technique should be relatively thread-safe (since sets are).
def __init__(self, weakcontainer):
self.weakcontainer = ref(weakcontainer)
def __exit__(self, e, t, b):
def __init__(self, data=None):
def _remove(item, selfref=ref(self)):
self._pending_removals.append(item)
# A list of keys to be removed
self._pending_removals = []
def _commit_removals(self):
l = self._pending_removals
discard = self.data.discard
with _IterationGuard(self):
for itemref in self.data:
# Caveat: the iterator will keep a strong reference to
# `item` until it is resumed or closed.
return len(self.data) - len(self._pending_removals)
def __contains__(self, item):
return (self.__class__, (list(self),),
getattr(self, '__dict__', None))
if self._pending_removals:
self.data.add(ref(item, self._remove))
if self._pending_removals:
return self.__class__(self)
if self._pending_removals:
itemref = self.data.pop()
raise KeyError('pop from empty WeakSet')
if self._pending_removals:
self.data.remove(ref(item))
if self._pending_removals:
self.data.discard(ref(item))
if self._pending_removals:
def __ior__(self, other):
def difference(self, other):
newset.difference_update(other)
def difference_update(self, other):
def __isub__(self, other):
if self._pending_removals:
self.data.difference_update(ref(item) for item in other)
def intersection(self, other):
return self.__class__(item for item in other if item in self)
def intersection_update(self, other):
def __iand__(self, other):
if self._pending_removals:
self.data.intersection_update(ref(item) for item in other)
def issubset(self, other):
return self.data.issubset(ref(item) for item in other)
return self.data < set(ref(item) for item in other)
def issuperset(self, other):
return self.data.issuperset(ref(item) for item in other)
return self.data > set(ref(item) for item in other)
if not isinstance(other, self.__class__):
return self.data == set(ref(item) for item in other)
def symmetric_difference(self, other):
newset.symmetric_difference_update(other)
__xor__ = symmetric_difference
def symmetric_difference_update(self, other):
def __ixor__(self, other):
if self._pending_removals:
self.data.symmetric_difference_update(ref(item, self._remove) for item in other)
return self.__class__(e for s in (self, other) for e in s)
def isdisjoint(self, other):
return len(self.intersection(other)) == 0