Mercurial > cpython-lru_cache-weakref
annotate Lib/_weakrefset.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | 5b88475aae96 |
| children |
| 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): |
|
75356
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
66631
diff
changeset
|
66 return len(self.data) - len(self._pending_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
|
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() |
|
75398
5a2bc0d574f6
Port 2.7 fix for sporadic failure in test_weakset.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75356
diff
changeset
|
117 for element in other: |
|
5a2bc0d574f6
Port 2.7 fix for sporadic failure in test_weakset.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75356
diff
changeset
|
118 self.add(element) |
|
58194
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
119 |
|
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
|
120 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
|
121 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
|
122 return self |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
123 |
|
75402
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75398
diff
changeset
|
124 def difference(self, other): |
|
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75398
diff
changeset
|
125 newset = self.copy() |
|
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75398
diff
changeset
|
126 newset.difference_update(other) |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
127 return newset |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
128 __sub__ = difference |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
129 |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
130 def difference_update(self, other): |
|
75402
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75398
diff
changeset
|
131 self.__isub__(other) |
|
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
|
132 def __isub__(self, other): |
|
58194
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
133 if self._pending_removals: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
134 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
|
135 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
|
136 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
|
137 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
|
138 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
|
139 return self |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
140 |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
141 def intersection(self, other): |
|
75402
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75398
diff
changeset
|
142 return self.__class__(item for item in other if item in self) |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
143 __and__ = intersection |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
144 |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
145 def intersection_update(self, other): |
|
75402
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75398
diff
changeset
|
146 self.__iand__(other) |
|
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
|
147 def __iand__(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 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
|
151 return self |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
152 |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
153 def issubset(self, other): |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
154 return self.data.issubset(ref(item) for item in other) |
|
75405
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
155 __le__ = issubset |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
156 |
|
75405
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
157 def __lt__(self, other): |
|
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
158 return self.data < set(ref(item) for item in other) |
|
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
|
159 |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
160 def issuperset(self, other): |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
161 return self.data.issuperset(ref(item) for item in other) |
|
75405
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
162 __ge__ = issuperset |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
163 |
|
75405
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
164 def __gt__(self, other): |
|
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
165 return self.data > set(ref(item) for item in other) |
|
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
|
166 |
|
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 def __eq__(self, other): |
|
54179
a7ad1696866b
Merged revisions 72751 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47159
diff
changeset
|
168 if not isinstance(other, self.__class__): |
|
a7ad1696866b
Merged revisions 72751 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47159
diff
changeset
|
169 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
|
170 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
|
171 |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
172 def symmetric_difference(self, other): |
|
75402
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75398
diff
changeset
|
173 newset = self.copy() |
|
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75398
diff
changeset
|
174 newset.symmetric_difference_update(other) |
|
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75398
diff
changeset
|
175 return newset |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
176 __xor__ = symmetric_difference |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
177 |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
178 def symmetric_difference_update(self, other): |
|
75402
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75398
diff
changeset
|
179 self.__ixor__(other) |
|
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
|
180 def __ixor__(self, other): |
|
58194
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
181 if self._pending_removals: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
54179
diff
changeset
|
182 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
|
183 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
|
184 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
|
185 else: |
|
75402
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75398
diff
changeset
|
186 self.data.symmetric_difference_update(ref(item, self._remove) for item in other) |
|
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 |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
188 |
|
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
189 def union(self, other): |
|
75402
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75398
diff
changeset
|
190 return self.__class__(e for s in (self, other) for e in s) |
|
45159
e6069b24031e
Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
191 __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
|
192 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
47150
diff
changeset
|
193 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
|
194 return len(self.intersection(other)) == 0 |
