You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
5.4 KiB

  1. # Access WeakSet through the weakref module.
  2. # This code is separated-out because it is needed
  3. # by abc.py to load everything else at startup.
  4. from _weakref import ref
  5. __all__ = ['WeakSet']
  6. class _IterationGuard:
  7. # This context manager registers itself in the current iterators of the
  8. # weak container, such as to delay all removals until the context manager
  9. # exits.
  10. # This technique should be relatively thread-safe (since sets are).
  11. def __init__(self, weakcontainer):
  12. # Don't create cycles
  13. self.weakcontainer = ref(weakcontainer)
  14. def __enter__(self):
  15. w = self.weakcontainer()
  16. if w is not None:
  17. w._iterating.add(self)
  18. return self
  19. def __exit__(self, e, t, b):
  20. w = self.weakcontainer()
  21. if w is not None:
  22. s = w._iterating
  23. s.remove(self)
  24. if not s:
  25. w._commit_removals()
  26. class WeakSet:
  27. def __init__(self, data=None):
  28. self.data = set()
  29. def _remove(item, selfref=ref(self)):
  30. self = selfref()
  31. if self is not None:
  32. if self._iterating:
  33. self._pending_removals.append(item)
  34. else:
  35. self.data.discard(item)
  36. self._remove = _remove
  37. # A list of keys to be removed
  38. self._pending_removals = []
  39. self._iterating = set()
  40. if data is not None:
  41. self.update(data)
  42. def _commit_removals(self):
  43. l = self._pending_removals
  44. discard = self.data.discard
  45. while l:
  46. discard(l.pop())
  47. def __iter__(self):
  48. with _IterationGuard(self):
  49. for itemref in self.data:
  50. item = itemref()
  51. if item is not None:
  52. yield item
  53. def __len__(self):
  54. return len(self.data) - len(self._pending_removals)
  55. def __contains__(self, item):
  56. try:
  57. wr = ref(item)
  58. except TypeError:
  59. return False
  60. return wr in self.data
  61. def __reduce__(self):
  62. return (self.__class__, (list(self),),
  63. getattr(self, '__dict__', None))
  64. def add(self, item):
  65. if self._pending_removals:
  66. self._commit_removals()
  67. self.data.add(ref(item, self._remove))
  68. def clear(self):
  69. if self._pending_removals:
  70. self._commit_removals()
  71. self.data.clear()
  72. def copy(self):
  73. return self.__class__(self)
  74. def pop(self):
  75. if self._pending_removals:
  76. self._commit_removals()
  77. while True:
  78. try:
  79. itemref = self.data.pop()
  80. except KeyError:
  81. raise KeyError('pop from empty WeakSet')
  82. item = itemref()
  83. if item is not None:
  84. return item
  85. def remove(self, item):
  86. if self._pending_removals:
  87. self._commit_removals()
  88. self.data.remove(ref(item))
  89. def discard(self, item):
  90. if self._pending_removals:
  91. self._commit_removals()
  92. self.data.discard(ref(item))
  93. def update(self, other):
  94. if self._pending_removals:
  95. self._commit_removals()
  96. for element in other:
  97. self.add(element)
  98. def __ior__(self, other):
  99. self.update(other)
  100. return self
  101. def difference(self, other):
  102. newset = self.copy()
  103. newset.difference_update(other)
  104. return newset
  105. __sub__ = difference
  106. def difference_update(self, other):
  107. self.__isub__(other)
  108. def __isub__(self, other):
  109. if self._pending_removals:
  110. self._commit_removals()
  111. if self is other:
  112. self.data.clear()
  113. else:
  114. self.data.difference_update(ref(item) for item in other)
  115. return self
  116. def intersection(self, other):
  117. return self.__class__(item for item in other if item in self)
  118. __and__ = intersection
  119. def intersection_update(self, other):
  120. self.__iand__(other)
  121. def __iand__(self, other):
  122. if self._pending_removals:
  123. self._commit_removals()
  124. self.data.intersection_update(ref(item) for item in other)
  125. return self
  126. def issubset(self, other):
  127. return self.data.issubset(ref(item) for item in other)
  128. __le__ = issubset
  129. def __lt__(self, other):
  130. return self.data < set(ref(item) for item in other)
  131. def issuperset(self, other):
  132. return self.data.issuperset(ref(item) for item in other)
  133. __ge__ = issuperset
  134. def __gt__(self, other):
  135. return self.data > set(ref(item) for item in other)
  136. def __eq__(self, other):
  137. if not isinstance(other, self.__class__):
  138. return NotImplemented
  139. return self.data == set(ref(item) for item in other)
  140. def symmetric_difference(self, other):
  141. newset = self.copy()
  142. newset.symmetric_difference_update(other)
  143. return newset
  144. __xor__ = symmetric_difference
  145. def symmetric_difference_update(self, other):
  146. self.__ixor__(other)
  147. def __ixor__(self, other):
  148. if self._pending_removals:
  149. self._commit_removals()
  150. if self is other:
  151. self.data.clear()
  152. else:
  153. self.data.symmetric_difference_update(ref(item, self._remove) for item in other)
  154. return self
  155. def union(self, other):
  156. return self.__class__(e for s in (self, other) for e in s)
  157. __or__ = union
  158. def isdisjoint(self, other):
  159. return len(self.intersection(other)) == 0