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