Mercurial > cpython-lru_cache-weakref
annotate Lib/_weakrefset.py @ 74006:88198b93ff2f
Issue #13560: Add PyUnicode_EncodeLocale()
* Use PyUnicode_EncodeLocale() in time.strftime() if wcsftime() is not
available
* Document my last changes in Misc/NEWS
| author | Victor Stinner <victor.stinner@haypocalc.com> |
|---|---|
| date | Sat, 17 Dec 2011 04:13:41 +0100 |
| parents | 76854e45c465 |
| children | 1cd0688ff004 |
| rev | line source |
|---|---|
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1 # Access WeakSet through the weakref module. |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2 # This code is separated-out because it is needed |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
3 # by abc.py to load everything else at startup. |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
4 |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
5 from _weakref import ref |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
6 |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
7 __all__ = ['WeakSet'] |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
8 |
|
58194
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
9 |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
10 class _IterationGuard: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
11 # This context manager registers itself in the current iterators of the |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
12 # weak container, such as to delay all removals until the context manager |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
13 # exits. |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
14 # This technique should be relatively thread-safe (since sets are). |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
15 |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
16 def __init__(self, weakcontainer): |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
17 # Don't create cycles |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
18 self.weakcontainer = ref(weakcontainer) |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
19 |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
20 def __enter__(self): |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
21 w = self.weakcontainer() |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
22 if w is not None: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
23 w._iterating.add(self) |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
24 return self |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
25 |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
26 def __exit__(self, e, t, b): |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
27 w = self.weakcontainer() |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
28 if w is not None: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
29 s = w._iterating |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
30 s.remove(self) |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
31 if not s: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
32 w._commit_removals() |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
33 |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
34 |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
35 class WeakSet: |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
36 def __init__(self, data=None): |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
37 self.data = set() |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
38 def _remove(item, selfref=ref(self)): |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
39 self = selfref() |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
40 if self is not None: |
|
58194
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
41 if self._iterating: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
42 self._pending_removals.append(item) |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
43 else: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
44 self.data.discard(item) |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
45 self._remove = _remove |
|
58194
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
46 # A list of keys to be removed |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
47 self._pending_removals = [] |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
48 self._iterating = set() |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
49 if data is not None: |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
50 self.update(data) |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
51 |
|
58194
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
52 def _commit_removals(self): |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
53 l = self._pending_removals |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
54 discard = self.data.discard |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
55 while l: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
56 discard(l.pop()) |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
57 |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
58 def __iter__(self): |
|
58194
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
59 with _IterationGuard(self): |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
60 for itemref in self.data: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
61 item = itemref() |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
62 if item is not None: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
63 yield item |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
64 |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
65 def __len__(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
66 return sum(x() is not None for x in self.data) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
67 |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
68 def __contains__(self, item): |
|
66631
76854e45c465
#10360: catch TypeError in WeakSet.__contains__, just like WeakKeyDictionary does.
Georg Brandl <georg@python.org>
parents:
58194
diff
changeset
|
69 try: |
|
76854e45c465
#10360: catch TypeError in WeakSet.__contains__, just like WeakKeyDictionary does.
Georg Brandl <georg@python.org>
parents:
58194
diff
changeset
|
70 wr = ref(item) |
|
76854e45c465
#10360: catch TypeError in WeakSet.__contains__, just like WeakKeyDictionary does.
Georg Brandl <georg@python.org>
parents:
58194
diff
changeset
|
71 except TypeError: |
|
76854e45c465
#10360: catch TypeError in WeakSet.__contains__, just like WeakKeyDictionary does.
Georg Brandl <georg@python.org>
parents:
58194
diff
changeset
|
72 return False |
|
76854e45c465
#10360: catch TypeError in WeakSet.__contains__, just like WeakKeyDictionary does.
Georg Brandl <georg@python.org>
parents:
58194
diff
changeset
|
73 return wr in self.data |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
74 |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
75 def __reduce__(self): |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
76 return (self.__class__, (list(self),), |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
77 getattr(self, '__dict__', None)) |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
78 |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
79 def add(self, item): |
|
58194
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
80 if self._pending_removals: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
81 self._commit_removals() |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
82 self.data.add(ref(item, self._remove)) |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
83 |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
84 def clear(self): |
|
58194
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
85 if self._pending_removals: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
86 self._commit_removals() |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
87 self.data.clear() |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
88 |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
89 def copy(self): |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
90 return self.__class__(self) |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
91 |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
92 def pop(self): |
|
58194
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
93 if self._pending_removals: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
94 self._commit_removals() |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
95 while True: |
|
47150
bbd30c492a99
Fix two issues in the weak set implementation.
Georg Brandl <georg@python.org>
parents:
45159
diff
changeset
|
96 try: |
|
bbd30c492a99
Fix two issues in the weak set implementation.
Georg Brandl <georg@python.org>
parents:
45159
diff
changeset
|
97 itemref = self.data.pop() |
|
bbd30c492a99
Fix two issues in the weak set implementation.
Georg Brandl <georg@python.org>
parents:
45159
diff
changeset
|
98 except KeyError: |
|
bbd30c492a99
Fix two issues in the weak set implementation.
Georg Brandl <georg@python.org>
parents:
45159
diff
changeset
|
99 raise KeyError('pop from empty WeakSet') |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
100 item = itemref() |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
101 if item is not None: |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
102 return item |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
103 |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
104 def remove(self, item): |
|
58194
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
105 if self._pending_removals: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
106 self._commit_removals() |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
107 self.data.remove(ref(item)) |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
108 |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
109 def discard(self, item): |
|
58194
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
110 if self._pending_removals: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
111 self._commit_removals() |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
112 self.data.discard(ref(item)) |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
113 |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
114 def update(self, other): |
|
58194
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
115 if self._pending_removals: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
116 self._commit_removals() |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
117 if isinstance(other, self.__class__): |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
118 self.data.update(other.data) |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
119 else: |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
120 for element in other: |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
121 self.add(element) |
|
58194
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
122 |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
123 def __ior__(self, other): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
124 self.update(other) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
125 return self |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
126 |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
127 # Helper functions for simple delegating methods. |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
128 def _apply(self, other, method): |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
129 if not isinstance(other, self.__class__): |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
130 other = self.__class__(other) |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
131 newdata = method(other.data) |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
132 newset = self.__class__() |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
133 newset.data = newdata |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
134 return newset |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
135 |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
136 def difference(self, other): |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
137 return self._apply(other, self.data.difference) |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
138 __sub__ = difference |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
139 |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
140 def difference_update(self, other): |
|
58194
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
141 if self._pending_removals: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
142 self._commit_removals() |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
143 if self is other: |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
144 self.data.clear() |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
145 else: |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
146 self.data.difference_update(ref(item) for item in other) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
147 def __isub__(self, other): |
|
58194
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
148 if self._pending_removals: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
149 self._commit_removals() |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
150 if self is other: |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
151 self.data.clear() |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
152 else: |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
153 self.data.difference_update(ref(item) for item in other) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
154 return self |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
155 |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
156 def intersection(self, other): |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
157 return self._apply(other, self.data.intersection) |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
158 __and__ = intersection |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
159 |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
160 def intersection_update(self, other): |
|
58194
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
161 if self._pending_removals: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
162 self._commit_removals() |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
163 self.data.intersection_update(ref(item) for item in other) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
164 def __iand__(self, other): |
|
58194
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
165 if self._pending_removals: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
166 self._commit_removals() |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
167 self.data.intersection_update(ref(item) for item in other) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
168 return self |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
169 |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
170 def issubset(self, other): |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
171 return self.data.issubset(ref(item) for item in other) |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
172 __lt__ = issubset |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
173 |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
174 def __le__(self, other): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
175 return self.data <= set(ref(item) for item in other) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
176 |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
177 def issuperset(self, other): |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
178 return self.data.issuperset(ref(item) for item in other) |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
179 __gt__ = issuperset |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
180 |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
181 def __ge__(self, other): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
182 return self.data >= set(ref(item) for item in other) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
183 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
184 def __eq__(self, other): |
|
54179
a7ad1696866b
Merged revisions 72751 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47159
diff
changeset
|
185 if not isinstance(other, self.__class__): |
|
a7ad1696866b
Merged revisions 72751 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47159
diff
changeset
|
186 return NotImplemented |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
187 return self.data == set(ref(item) for item in other) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
188 |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
189 def symmetric_difference(self, other): |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
190 return self._apply(other, self.data.symmetric_difference) |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
191 __xor__ = symmetric_difference |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
192 |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
193 def symmetric_difference_update(self, other): |
|
58194
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
194 if self._pending_removals: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
195 self._commit_removals() |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
196 if self is other: |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
197 self.data.clear() |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
198 else: |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
199 self.data.symmetric_difference_update(ref(item) for item in other) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
200 def __ixor__(self, other): |
|
58194
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
201 if self._pending_removals: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
202 self._commit_removals() |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
203 if self is other: |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
204 self.data.clear() |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
205 else: |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
206 self.data.symmetric_difference_update(ref(item) for item in other) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
207 return self |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
208 |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
209 def union(self, other): |
|
47150
bbd30c492a99
Fix two issues in the weak set implementation.
Georg Brandl <georg@python.org>
parents:
45159
diff
changeset
|
210 return self._apply(other, self.data.union) |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
211 __or__ = union |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
212 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
213 def isdisjoint(self, other): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
214 return len(self.intersection(other)) == 0 |
