Mercurial > cpython-lru_cache-weakref
annotate Lib/_weakrefset.py @ 65035:87f4b32905df 2.7
Cosmetic fix to use print function.
| author | Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp> |
|---|---|
| date | Fri, 01 Oct 2010 10:48:47 +0000 |
| parents | 04cc075bddf5 |
| children | d356649504f9 |
| rev | line source |
|---|---|
|
60073
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
1 # Access WeakSet through the weakref module. |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
2 # This code is separated-out because it is needed |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
3 # by abc.py to load everything else at startup. |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
4 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
5 from _weakref import ref |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
6 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
7 __all__ = ['WeakSet'] |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
8 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
9 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
10 class _IterationGuard(object): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
11 # This context manager registers itself in the current iterators of the |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
12 # weak container, such as to delay all removals until the context manager |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
13 # exits. |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
14 # This technique should be relatively thread-safe (since sets are). |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
15 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
16 def __init__(self, weakcontainer): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
17 # Don't create cycles |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
18 self.weakcontainer = ref(weakcontainer) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
19 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
20 def __enter__(self): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
21 w = self.weakcontainer() |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
22 if w is not None: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
23 w._iterating.add(self) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
24 return self |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
25 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
26 def __exit__(self, e, t, b): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
27 w = self.weakcontainer() |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
28 if w is not None: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
29 s = w._iterating |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
30 s.remove(self) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
31 if not s: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
32 w._commit_removals() |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
33 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
34 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
35 class WeakSet(object): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
36 def __init__(self, data=None): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
37 self.data = set() |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
38 def _remove(item, selfref=ref(self)): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
39 self = selfref() |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
40 if self is not None: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
41 if self._iterating: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
42 self._pending_removals.append(item) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
43 else: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
44 self.data.discard(item) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
45 self._remove = _remove |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
46 # A list of keys to be removed |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
47 self._pending_removals = [] |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
48 self._iterating = set() |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
49 if data is not None: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
50 self.update(data) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
51 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
52 def _commit_removals(self): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
53 l = self._pending_removals |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
54 discard = self.data.discard |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
55 while l: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
56 discard(l.pop()) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
57 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
58 def __iter__(self): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
59 with _IterationGuard(self): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
60 for itemref in self.data: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
61 item = itemref() |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
62 if item is not None: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
63 yield item |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
64 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
65 def __len__(self): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
66 return sum(x() is not None for x in self.data) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
67 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
68 def __contains__(self, item): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
69 return ref(item) in self.data |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
70 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
71 def __reduce__(self): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
72 return (self.__class__, (list(self),), |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
73 getattr(self, '__dict__', None)) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
74 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
75 __hash__ = None |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
76 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
77 def add(self, item): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
78 if self._pending_removals: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
79 self._commit_removals() |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
80 self.data.add(ref(item, self._remove)) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
81 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
82 def clear(self): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
83 if self._pending_removals: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
84 self._commit_removals() |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
85 self.data.clear() |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
86 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
87 def copy(self): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
88 return self.__class__(self) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
89 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
90 def pop(self): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
91 if self._pending_removals: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
92 self._commit_removals() |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
93 while True: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
94 try: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
95 itemref = self.data.pop() |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
96 except KeyError: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
97 raise KeyError('pop from empty WeakSet') |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
98 item = itemref() |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
99 if item is not None: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
100 return item |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
101 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
102 def remove(self, item): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
103 if self._pending_removals: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
104 self._commit_removals() |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
105 self.data.remove(ref(item)) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
106 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
107 def discard(self, item): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
108 if self._pending_removals: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
109 self._commit_removals() |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
110 self.data.discard(ref(item)) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
111 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
112 def update(self, other): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
113 if self._pending_removals: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
114 self._commit_removals() |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
115 if isinstance(other, self.__class__): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
116 self.data.update(other.data) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
117 else: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
118 for element in other: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
119 self.add(element) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
120 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
121 def __ior__(self, other): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
122 self.update(other) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
123 return self |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
124 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
125 # Helper functions for simple delegating methods. |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
126 def _apply(self, other, method): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
127 if not isinstance(other, self.__class__): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
128 other = self.__class__(other) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
129 newdata = method(other.data) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
130 newset = self.__class__() |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
131 newset.data = newdata |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
132 return newset |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
133 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
134 def difference(self, other): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
135 return self._apply(other, self.data.difference) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
136 __sub__ = difference |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
137 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
138 def difference_update(self, other): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
139 if self._pending_removals: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
140 self._commit_removals() |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
141 if self is other: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
142 self.data.clear() |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
143 else: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
144 self.data.difference_update(ref(item) for item in other) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
145 def __isub__(self, other): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
146 if self._pending_removals: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
147 self._commit_removals() |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
148 if self is other: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
149 self.data.clear() |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
150 else: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
151 self.data.difference_update(ref(item) for item in other) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
152 return self |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
153 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
154 def intersection(self, other): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
155 return self._apply(other, self.data.intersection) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
156 __and__ = intersection |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
157 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
158 def intersection_update(self, other): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
159 if self._pending_removals: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
160 self._commit_removals() |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
161 self.data.intersection_update(ref(item) for item in other) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
162 def __iand__(self, other): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
163 if self._pending_removals: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
164 self._commit_removals() |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
165 self.data.intersection_update(ref(item) for item in other) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
166 return self |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
167 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
168 def issubset(self, other): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
169 return self.data.issubset(ref(item) for item in other) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
170 __lt__ = issubset |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
171 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
172 def __le__(self, other): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
173 return self.data <= set(ref(item) for item in other) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
174 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
175 def issuperset(self, other): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
176 return self.data.issuperset(ref(item) for item in other) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
177 __gt__ = issuperset |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
178 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
179 def __ge__(self, other): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
180 return self.data >= set(ref(item) for item in other) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
181 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
182 def __eq__(self, other): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
183 if not isinstance(other, self.__class__): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
184 return NotImplemented |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
185 return self.data == set(ref(item) for item in other) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
186 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
187 def symmetric_difference(self, other): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
188 return self._apply(other, self.data.symmetric_difference) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
189 __xor__ = symmetric_difference |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
190 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
191 def symmetric_difference_update(self, other): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
192 if self._pending_removals: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
193 self._commit_removals() |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
194 if self is other: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
195 self.data.clear() |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
196 else: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
197 self.data.symmetric_difference_update(ref(item) for item in other) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
198 def __ixor__(self, other): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
199 if self._pending_removals: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
200 self._commit_removals() |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
201 if self is other: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
202 self.data.clear() |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
203 else: |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
204 self.data.symmetric_difference_update(ref(item) for item in other) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
205 return self |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
206 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
207 def union(self, other): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
208 return self._apply(other, self.data.union) |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
209 __or__ = union |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
210 |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
211 def isdisjoint(self, other): |
|
04cc075bddf5
Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff
changeset
|
212 return len(self.intersection(other)) == 0 |
