Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 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 | |
| 5 | from _weakref import ref |
Ethan Smith | 8ef8750 | 2020-04-13 21:54:40 -0700 | [diff] [blame] | 6 | from types import GenericAlias |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 7 | |
| 8 | __all__ = ['WeakSet'] |
| 9 | |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 10 | |
| 11 | class _IterationGuard: |
| 12 | # This context manager registers itself in the current iterators of the |
| 13 | # weak container, such as to delay all removals until the context manager |
| 14 | # exits. |
| 15 | # This technique should be relatively thread-safe (since sets are). |
| 16 | |
| 17 | def __init__(self, weakcontainer): |
| 18 | # Don't create cycles |
| 19 | self.weakcontainer = ref(weakcontainer) |
| 20 | |
| 21 | def __enter__(self): |
| 22 | w = self.weakcontainer() |
| 23 | if w is not None: |
| 24 | w._iterating.add(self) |
| 25 | return self |
| 26 | |
| 27 | def __exit__(self, e, t, b): |
| 28 | w = self.weakcontainer() |
| 29 | if w is not None: |
| 30 | s = w._iterating |
| 31 | s.remove(self) |
| 32 | if not s: |
| 33 | w._commit_removals() |
| 34 | |
| 35 | |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 36 | class WeakSet: |
| 37 | def __init__(self, data=None): |
| 38 | self.data = set() |
| 39 | def _remove(item, selfref=ref(self)): |
| 40 | self = selfref() |
| 41 | if self is not None: |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 42 | if self._iterating: |
| 43 | self._pending_removals.append(item) |
| 44 | else: |
| 45 | self.data.discard(item) |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 46 | self._remove = _remove |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 47 | # A list of keys to be removed |
| 48 | self._pending_removals = [] |
| 49 | self._iterating = set() |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 50 | if data is not None: |
| 51 | self.update(data) |
| 52 | |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 53 | def _commit_removals(self): |
Miss Islington (bot) | 8aa64cc | 2021-08-28 11:09:21 -0700 | [diff] [blame] | 54 | pop = self._pending_removals.pop |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 55 | discard = self.data.discard |
Miss Islington (bot) | 8aa64cc | 2021-08-28 11:09:21 -0700 | [diff] [blame] | 56 | while True: |
| 57 | try: |
| 58 | item = pop() |
| 59 | except IndexError: |
| 60 | return |
| 61 | discard(item) |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 62 | |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 63 | def __iter__(self): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 64 | with _IterationGuard(self): |
| 65 | for itemref in self.data: |
| 66 | item = itemref() |
| 67 | if item is not None: |
Antoine Pitrou | 320b391 | 2013-12-18 00:28:36 +0100 | [diff] [blame] | 68 | # Caveat: the iterator will keep a strong reference to |
| 69 | # `item` until it is resumed or closed. |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 70 | yield item |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 71 | |
Georg Brandl | 9dba5d9 | 2008-05-18 16:27:29 +0000 | [diff] [blame] | 72 | def __len__(self): |
Antoine Pitrou | bbe2f60 | 2012-03-01 16:26:35 +0100 | [diff] [blame] | 73 | return len(self.data) - len(self._pending_removals) |
Georg Brandl | 9dba5d9 | 2008-05-18 16:27:29 +0000 | [diff] [blame] | 74 | |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 75 | def __contains__(self, item): |
Georg Brandl | f8de3fe | 2010-12-03 07:55:44 +0000 | [diff] [blame] | 76 | try: |
| 77 | wr = ref(item) |
| 78 | except TypeError: |
| 79 | return False |
| 80 | return wr in self.data |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 81 | |
| 82 | def __reduce__(self): |
| 83 | return (self.__class__, (list(self),), |
| 84 | getattr(self, '__dict__', None)) |
| 85 | |
| 86 | def add(self, item): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 87 | if self._pending_removals: |
| 88 | self._commit_removals() |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 89 | self.data.add(ref(item, self._remove)) |
| 90 | |
| 91 | def clear(self): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 92 | if self._pending_removals: |
| 93 | self._commit_removals() |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 94 | self.data.clear() |
| 95 | |
| 96 | def copy(self): |
| 97 | return self.__class__(self) |
| 98 | |
| 99 | def pop(self): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 100 | if self._pending_removals: |
| 101 | self._commit_removals() |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 102 | while True: |
Georg Brandl | bf93b04 | 2008-05-18 07:46:13 +0000 | [diff] [blame] | 103 | try: |
| 104 | itemref = self.data.pop() |
| 105 | except KeyError: |
Serhiy Storchaka | 5affd23 | 2017-04-05 09:37:24 +0300 | [diff] [blame] | 106 | raise KeyError('pop from empty WeakSet') from None |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 107 | item = itemref() |
| 108 | if item is not None: |
| 109 | return item |
| 110 | |
| 111 | def remove(self, item): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 112 | if self._pending_removals: |
| 113 | self._commit_removals() |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 114 | self.data.remove(ref(item)) |
| 115 | |
| 116 | def discard(self, item): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 117 | if self._pending_removals: |
| 118 | self._commit_removals() |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 119 | self.data.discard(ref(item)) |
| 120 | |
| 121 | def update(self, other): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 122 | if self._pending_removals: |
| 123 | self._commit_removals() |
Antoine Pitrou | de89d4b | 2012-03-04 20:20:34 +0100 | [diff] [blame] | 124 | for element in other: |
| 125 | self.add(element) |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 126 | |
Georg Brandl | 9dba5d9 | 2008-05-18 16:27:29 +0000 | [diff] [blame] | 127 | def __ior__(self, other): |
| 128 | self.update(other) |
| 129 | return self |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 130 | |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 131 | def difference(self, other): |
Antoine Pitrou | 9c47ac0 | 2012-03-04 20:47:05 +0100 | [diff] [blame] | 132 | newset = self.copy() |
| 133 | newset.difference_update(other) |
| 134 | return newset |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 135 | __sub__ = difference |
| 136 | |
| 137 | def difference_update(self, other): |
Antoine Pitrou | 9c47ac0 | 2012-03-04 20:47:05 +0100 | [diff] [blame] | 138 | self.__isub__(other) |
Georg Brandl | 9dba5d9 | 2008-05-18 16:27:29 +0000 | [diff] [blame] | 139 | def __isub__(self, other): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 140 | if self._pending_removals: |
| 141 | self._commit_removals() |
Georg Brandl | 9dba5d9 | 2008-05-18 16:27:29 +0000 | [diff] [blame] | 142 | if self is other: |
| 143 | self.data.clear() |
| 144 | else: |
| 145 | self.data.difference_update(ref(item) for item in other) |
| 146 | return self |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 147 | |
| 148 | def intersection(self, other): |
Antoine Pitrou | 9c47ac0 | 2012-03-04 20:47:05 +0100 | [diff] [blame] | 149 | return self.__class__(item for item in other if item in self) |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 150 | __and__ = intersection |
| 151 | |
| 152 | def intersection_update(self, other): |
Antoine Pitrou | 9c47ac0 | 2012-03-04 20:47:05 +0100 | [diff] [blame] | 153 | self.__iand__(other) |
Georg Brandl | 9dba5d9 | 2008-05-18 16:27:29 +0000 | [diff] [blame] | 154 | def __iand__(self, other): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 155 | if self._pending_removals: |
| 156 | self._commit_removals() |
Georg Brandl | 9dba5d9 | 2008-05-18 16:27:29 +0000 | [diff] [blame] | 157 | self.data.intersection_update(ref(item) for item in other) |
| 158 | return self |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 159 | |
| 160 | def issubset(self, other): |
| 161 | return self.data.issubset(ref(item) for item in other) |
Meador Inge | 653f932 | 2012-03-04 22:15:38 -0600 | [diff] [blame] | 162 | __le__ = issubset |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 163 | |
Meador Inge | 653f932 | 2012-03-04 22:15:38 -0600 | [diff] [blame] | 164 | def __lt__(self, other): |
Jon Dufresne | 3972628 | 2017-05-18 07:35:54 -0700 | [diff] [blame] | 165 | return self.data < set(map(ref, other)) |
Georg Brandl | 9dba5d9 | 2008-05-18 16:27:29 +0000 | [diff] [blame] | 166 | |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 167 | def issuperset(self, other): |
| 168 | return self.data.issuperset(ref(item) for item in other) |
Meador Inge | 653f932 | 2012-03-04 22:15:38 -0600 | [diff] [blame] | 169 | __ge__ = issuperset |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 170 | |
Meador Inge | 653f932 | 2012-03-04 22:15:38 -0600 | [diff] [blame] | 171 | def __gt__(self, other): |
Jon Dufresne | 3972628 | 2017-05-18 07:35:54 -0700 | [diff] [blame] | 172 | return self.data > set(map(ref, other)) |
Georg Brandl | 9dba5d9 | 2008-05-18 16:27:29 +0000 | [diff] [blame] | 173 | |
| 174 | def __eq__(self, other): |
Robert Schuppenies | 4ad1d6f | 2009-05-17 17:32:20 +0000 | [diff] [blame] | 175 | if not isinstance(other, self.__class__): |
| 176 | return NotImplemented |
Jon Dufresne | 3972628 | 2017-05-18 07:35:54 -0700 | [diff] [blame] | 177 | return self.data == set(map(ref, other)) |
Georg Brandl | 9dba5d9 | 2008-05-18 16:27:29 +0000 | [diff] [blame] | 178 | |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 179 | def symmetric_difference(self, other): |
Antoine Pitrou | 9c47ac0 | 2012-03-04 20:47:05 +0100 | [diff] [blame] | 180 | newset = self.copy() |
| 181 | newset.symmetric_difference_update(other) |
| 182 | return newset |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 183 | __xor__ = symmetric_difference |
| 184 | |
| 185 | def symmetric_difference_update(self, other): |
Antoine Pitrou | 9c47ac0 | 2012-03-04 20:47:05 +0100 | [diff] [blame] | 186 | self.__ixor__(other) |
Georg Brandl | 9dba5d9 | 2008-05-18 16:27:29 +0000 | [diff] [blame] | 187 | def __ixor__(self, other): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 188 | if self._pending_removals: |
| 189 | self._commit_removals() |
Georg Brandl | 9dba5d9 | 2008-05-18 16:27:29 +0000 | [diff] [blame] | 190 | if self is other: |
| 191 | self.data.clear() |
| 192 | else: |
Antoine Pitrou | 9c47ac0 | 2012-03-04 20:47:05 +0100 | [diff] [blame] | 193 | self.data.symmetric_difference_update(ref(item, self._remove) for item in other) |
Georg Brandl | 9dba5d9 | 2008-05-18 16:27:29 +0000 | [diff] [blame] | 194 | return self |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 195 | |
| 196 | def union(self, other): |
Antoine Pitrou | 9c47ac0 | 2012-03-04 20:47:05 +0100 | [diff] [blame] | 197 | return self.__class__(e for s in (self, other) for e in s) |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 198 | __or__ = union |
Georg Brandl | 9dba5d9 | 2008-05-18 16:27:29 +0000 | [diff] [blame] | 199 | |
| 200 | def isdisjoint(self, other): |
| 201 | return len(self.intersection(other)) == 0 |
Batuhan Taşkaya | 5ae1c84 | 2019-05-20 20:01:07 +0300 | [diff] [blame] | 202 | |
| 203 | def __repr__(self): |
| 204 | return repr(self.data) |
Ethan Smith | 8ef8750 | 2020-04-13 21:54:40 -0700 | [diff] [blame] | 205 | |
| 206 | __class_getitem__ = classmethod(GenericAlias) |