annotate Lib/_weakrefset.py @ 60008:0dfd62076ded

Blocked revisions 79402,79406 via svnmerge ........ r79402 | benjamin.peterson | 2010-03-24 20:03:51 -0500 (Wed, 24 Mar 2010) | 1 line set bsddb eol style ........ r79406 | benjamin.peterson | 2010-03-24 20:05:57 -0500 (Wed, 24 Mar 2010) | 1 line fix eol properties on capsule files ........
author Benjamin Peterson <benjamin@python.org>
date Thu, 25 Mar 2010 01:11:10 +0000
parents 84caac71d79b
children 76854e45c465
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
69 return ref(item) in self.data
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
70
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
71 def __reduce__(self):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
72 return (self.__class__, (list(self),),
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
73 getattr(self, '__dict__', None))
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 add(self, item):
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54179
diff changeset
76 if self._pending_removals:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54179
diff changeset
77 self._commit_removals()
45159
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
78 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
79
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
80 def clear(self):
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54179
diff changeset
81 if self._pending_removals:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54179
diff changeset
82 self._commit_removals()
45159
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
83 self.data.clear()
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
84
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
85 def copy(self):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
86 return self.__class__(self)
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
87
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
88 def pop(self):
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54179
diff changeset
89 if self._pending_removals:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54179
diff changeset
90 self._commit_removals()
45159
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
91 while True:
47150
bbd30c492a99 Fix two issues in the weak set implementation.
Georg Brandl <georg@python.org>
parents: 45159
diff changeset
92 try:
bbd30c492a99 Fix two issues in the weak set implementation.
Georg Brandl <georg@python.org>
parents: 45159
diff changeset
93 itemref = self.data.pop()
bbd30c492a99 Fix two issues in the weak set implementation.
Georg Brandl <georg@python.org>
parents: 45159
diff changeset
94 except KeyError:
bbd30c492a99 Fix two issues in the weak set implementation.
Georg Brandl <georg@python.org>
parents: 45159
diff changeset
95 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
96 item = itemref()
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
97 if item is not None:
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
98 return item
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
99
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
100 def remove(self, item):
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54179
diff changeset
101 if self._pending_removals:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54179
diff changeset
102 self._commit_removals()
45159
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
103 self.data.remove(ref(item))
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
104
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
105 def discard(self, item):
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54179
diff changeset
106 if self._pending_removals:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54179
diff changeset
107 self._commit_removals()
45159
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
108 self.data.discard(ref(item))
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
109
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
110 def update(self, other):
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54179
diff changeset
111 if self._pending_removals:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54179
diff changeset
112 self._commit_removals()
45159
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
113 if isinstance(other, self.__class__):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
114 self.data.update(other.data)
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
115 else:
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
116 for element in other:
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
117 self.add(element)
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54179
diff changeset
118
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
119 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
120 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
121 return self
45159
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
122
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
123 # 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
124 def _apply(self, other, method):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
125 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
126 other = self.__class__(other)
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
127 newdata = method(other.data)
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
128 newset = self.__class__()
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
129 newset.data = newdata
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
130 return newset
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
131
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
132 def difference(self, other):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
133 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
134 __sub__ = difference
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_update(self, other):
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54179
diff changeset
137 if self._pending_removals:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54179
diff changeset
138 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
139 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
140 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
141 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
142 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
143 def __isub__(self, other):
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54179
diff changeset
144 if self._pending_removals:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54179
diff changeset
145 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
146 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
147 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
148 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
149 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
150 return self
45159
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
151
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
152 def intersection(self, other):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
153 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
154 __and__ = intersection
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_update(self, other):
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54179
diff changeset
157 if self._pending_removals:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54179
diff changeset
158 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
159 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
160 def __iand__(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 return self
45159
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
165
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
166 def issubset(self, other):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
167 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
168 __lt__ = issubset
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
169
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 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
171 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
172
45159
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
173 def issuperset(self, other):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
174 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
175 __gt__ = issuperset
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
176
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
177 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
178 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
179
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 __eq__(self, other):
54179
a7ad1696866b Merged revisions 72751 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47159
diff changeset
181 if not isinstance(other, self.__class__):
a7ad1696866b Merged revisions 72751 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47159
diff changeset
182 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
183 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
184
45159
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
185 def symmetric_difference(self, other):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
186 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
187 __xor__ = symmetric_difference
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 symmetric_difference_update(self, other):
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54179
diff changeset
190 if self._pending_removals:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54179
diff changeset
191 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
192 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
193 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
194 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
195 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
196 def __ixor__(self, other):
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54179
diff changeset
197 if self._pending_removals:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54179
diff changeset
198 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
199 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
200 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
201 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
202 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
203 return self
45159
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
204
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
205 def union(self, other):
47150
bbd30c492a99 Fix two issues in the weak set implementation.
Georg Brandl <georg@python.org>
parents: 45159
diff changeset
206 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
207 __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
208
37499287f758 Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents: 47150
diff changeset
209 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
210 return len(self.intersection(other)) == 0