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