annotate Lib/_weakrefset.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 4b3f1decb1af
children fa9c0c651f78
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
60073
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
1 # Access WeakSet through the weakref module.
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
2 # This code is separated-out because it is needed
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
3 # by abc.py to load everything else at startup.
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
4
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
5 from _weakref import ref
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
6
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
7 __all__ = ['WeakSet']
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
8
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
9
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
10 class _IterationGuard(object):
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
11 # This context manager registers itself in the current iterators of the
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
12 # weak container, such as to delay all removals until the context manager
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
13 # exits.
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
14 # This technique should be relatively thread-safe (since sets are).
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
15
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
16 def __init__(self, weakcontainer):
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
17 # Don't create cycles
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
18 self.weakcontainer = ref(weakcontainer)
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
19
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
20 def __enter__(self):
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
21 w = self.weakcontainer()
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
22 if w is not None:
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
23 w._iterating.add(self)
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
24 return self
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
25
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
26 def __exit__(self, e, t, b):
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
27 w = self.weakcontainer()
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
28 if w is not None:
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
29 s = w._iterating
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
30 s.remove(self)
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
31 if not s:
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
32 w._commit_removals()
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
33
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
34
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
35 class WeakSet(object):
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
36 def __init__(self, data=None):
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
37 self.data = set()
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
38 def _remove(item, selfref=ref(self)):
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
39 self = selfref()
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
40 if self is not None:
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
41 if self._iterating:
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
42 self._pending_removals.append(item)
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
43 else:
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
44 self.data.discard(item)
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
45 self._remove = _remove
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
46 # A list of keys to be removed
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
47 self._pending_removals = []
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
48 self._iterating = set()
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
49 if data is not None:
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
50 self.update(data)
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
51
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
52 def _commit_removals(self):
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
53 l = self._pending_removals
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
54 discard = self.data.discard
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
55 while l:
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
56 discard(l.pop())
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
57
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
58 def __iter__(self):
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
59 with _IterationGuard(self):
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
60 for itemref in self.data:
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
61 item = itemref()
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
62 if item is not None:
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
63 yield item
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
64
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
65 def __len__(self):
75358
b6acfbe2bdbe Issue #14159: Fix the len() of weak sets to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68014
diff changeset
66 return len(self.data) - len(self._pending_removals)
60073
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
67
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
68 def __contains__(self, item):
68014
d356649504f9 Merged revisions 86881,86887,86913-86915,86933,86943,86960,86964,86974,86980,86996,87008 via svnmerge from
Georg Brandl <georg@python.org>
parents: 60073
diff changeset
69 try:
d356649504f9 Merged revisions 86881,86887,86913-86915,86933,86943,86960,86964,86974,86980,86996,87008 via svnmerge from
Georg Brandl <georg@python.org>
parents: 60073
diff changeset
70 wr = ref(item)
d356649504f9 Merged revisions 86881,86887,86913-86915,86933,86943,86960,86964,86974,86980,86996,87008 via svnmerge from
Georg Brandl <georg@python.org>
parents: 60073
diff changeset
71 except TypeError:
d356649504f9 Merged revisions 86881,86887,86913-86915,86933,86943,86960,86964,86974,86980,86996,87008 via svnmerge from
Georg Brandl <georg@python.org>
parents: 60073
diff changeset
72 return False
d356649504f9 Merged revisions 86881,86887,86913-86915,86933,86943,86960,86964,86974,86980,86996,87008 via svnmerge from
Georg Brandl <georg@python.org>
parents: 60073
diff changeset
73 return wr in self.data
60073
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
74
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
75 def __reduce__(self):
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
76 return (self.__class__, (list(self),),
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
77 getattr(self, '__dict__', None))
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
78
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
79 __hash__ = None
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
80
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
81 def add(self, item):
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
82 if self._pending_removals:
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
83 self._commit_removals()
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
84 self.data.add(ref(item, self._remove))
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
85
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
86 def clear(self):
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
87 if self._pending_removals:
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
88 self._commit_removals()
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
89 self.data.clear()
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
90
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
91 def copy(self):
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
92 return self.__class__(self)
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
93
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
94 def pop(self):
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
95 if self._pending_removals:
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
96 self._commit_removals()
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
97 while True:
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
98 try:
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
99 itemref = self.data.pop()
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
100 except KeyError:
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
101 raise KeyError('pop from empty WeakSet')
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
102 item = itemref()
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
103 if item is not None:
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
104 return item
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
105
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
106 def remove(self, item):
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
107 if self._pending_removals:
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
108 self._commit_removals()
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
109 self.data.remove(ref(item))
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
110
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
111 def discard(self, item):
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
112 if self._pending_removals:
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
113 self._commit_removals()
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
114 self.data.discard(ref(item))
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
115
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
116 def update(self, other):
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
117 if self._pending_removals:
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
118 self._commit_removals()
75395
5f79d68ba087 Fix sporadic failure in test_weakset
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
119 for element in other:
5f79d68ba087 Fix sporadic failure in test_weakset
Antoine Pitrou <solipsis@pitrou.net>
parents: 75358
diff changeset
120 self.add(element)
60073
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
121
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
122 def __ior__(self, other):
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
123 self.update(other)
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
124 return self
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
125
75397
428bfb58e3b3 Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75395
diff changeset
126 def difference(self, other):
428bfb58e3b3 Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75395
diff changeset
127 newset = self.copy()
428bfb58e3b3 Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75395
diff changeset
128 newset.difference_update(other)
60073
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
129 return newset
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
130 __sub__ = difference
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
131
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
132 def difference_update(self, other):
75397
428bfb58e3b3 Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75395
diff changeset
133 self.__isub__(other)
60073
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
134 def __isub__(self, other):
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
135 if self._pending_removals:
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
136 self._commit_removals()
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
137 if self is other:
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
138 self.data.clear()
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
139 else:
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
140 self.data.difference_update(ref(item) for item in other)
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
141 return self
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
142
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
143 def intersection(self, other):
75397
428bfb58e3b3 Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75395
diff changeset
144 return self.__class__(item for item in other if item in self)
60073
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
145 __and__ = intersection
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
146
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
147 def intersection_update(self, other):
75397
428bfb58e3b3 Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75395
diff changeset
148 self.__iand__(other)
60073
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
149 def __iand__(self, other):
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
150 if self._pending_removals:
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
151 self._commit_removals()
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
152 self.data.intersection_update(ref(item) for item in other)
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
153 return self
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
154
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
155 def issubset(self, other):
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
156 return self.data.issubset(ref(item) for item in other)
75404
4b3f1decb1af Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents: 75397
diff changeset
157 __le__ = issubset
60073
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
158
75404
4b3f1decb1af Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents: 75397
diff changeset
159 def __lt__(self, other):
4b3f1decb1af Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents: 75397
diff changeset
160 return self.data < set(ref(item) for item in other)
60073
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
161
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
162 def issuperset(self, other):
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
163 return self.data.issuperset(ref(item) for item in other)
75404
4b3f1decb1af Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents: 75397
diff changeset
164 __ge__ = issuperset
60073
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
165
75404
4b3f1decb1af Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents: 75397
diff changeset
166 def __gt__(self, other):
4b3f1decb1af Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents: 75397
diff changeset
167 return self.data > set(ref(item) for item in other)
60073
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
168
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
169 def __eq__(self, other):
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
170 if not isinstance(other, self.__class__):
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
171 return NotImplemented
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
172 return self.data == set(ref(item) for item in other)
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
173
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
174 def symmetric_difference(self, other):
75397
428bfb58e3b3 Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75395
diff changeset
175 newset = self.copy()
428bfb58e3b3 Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75395
diff changeset
176 newset.symmetric_difference_update(other)
428bfb58e3b3 Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75395
diff changeset
177 return newset
60073
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
178 __xor__ = symmetric_difference
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
179
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
180 def symmetric_difference_update(self, other):
75397
428bfb58e3b3 Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75395
diff changeset
181 self.__ixor__(other)
60073
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
182 def __ixor__(self, other):
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
183 if self._pending_removals:
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
184 self._commit_removals()
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
185 if self is other:
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
186 self.data.clear()
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
187 else:
75397
428bfb58e3b3 Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75395
diff changeset
188 self.data.symmetric_difference_update(ref(item, self._remove) for item in other)
60073
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
189 return self
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
190
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
191 def union(self, other):
75397
428bfb58e3b3 Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75395
diff changeset
192 return self.__class__(e for s in (self, other) for e in s)
60073
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
193 __or__ = union
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
194
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
195 def isdisjoint(self, other):
04cc075bddf5 Backport of weakref.WeakSet and tests from Python 3.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
diff changeset
196 return len(self.intersection(other)) == 0