ยปCore Development>Code coverage>Lib/_weakrefset.py

Python code coverage for Lib/_weakrefset.py

#countcontent
1n/a# Access WeakSet through the weakref module.
2n/a# This code is separated-out because it is needed
3n/a# by abc.py to load everything else at startup.
4n/a
5n/afrom _weakref import ref
6n/a
7n/a__all__ = ['WeakSet']
8n/a
9n/a
10n/aclass _IterationGuard:
11n/a # This context manager registers itself in the current iterators of the
12n/a # weak container, such as to delay all removals until the context manager
13n/a # exits.
14n/a # This technique should be relatively thread-safe (since sets are).
15n/a
16n/a def __init__(self, weakcontainer):
17n/a # Don't create cycles
18n/a self.weakcontainer = ref(weakcontainer)
19n/a
20n/a def __enter__(self):
21n/a w = self.weakcontainer()
22n/a if w is not None:
23n/a w._iterating.add(self)
24n/a return self
25n/a
26n/a def __exit__(self, e, t, b):
27n/a w = self.weakcontainer()
28n/a if w is not None:
29n/a s = w._iterating
30n/a s.remove(self)
31n/a if not s:
32n/a w._commit_removals()
33n/a
34n/a
35n/aclass WeakSet:
36n/a def __init__(self, data=None):
37n/a self.data = set()
38n/a def _remove(item, selfref=ref(self)):
39n/a self = selfref()
40n/a if self is not None:
41n/a if self._iterating:
42n/a self._pending_removals.append(item)
43n/a else:
44n/a self.data.discard(item)
45n/a self._remove = _remove
46n/a # A list of keys to be removed
47n/a self._pending_removals = []
48n/a self._iterating = set()
49n/a if data is not None:
50n/a self.update(data)
51n/a
52n/a def _commit_removals(self):
53n/a l = self._pending_removals
54n/a discard = self.data.discard
55n/a while l:
56n/a discard(l.pop())
57n/a
58n/a def __iter__(self):
59n/a with _IterationGuard(self):
60n/a for itemref in self.data:
61n/a item = itemref()
62n/a if item is not None:
63n/a # Caveat: the iterator will keep a strong reference to
64n/a # `item` until it is resumed or closed.
65n/a yield item
66n/a
67n/a def __len__(self):
68n/a return len(self.data) - len(self._pending_removals)
69n/a
70n/a def __contains__(self, item):
71n/a try:
72n/a wr = ref(item)
73n/a except TypeError:
74n/a return False
75n/a return wr in self.data
76n/a
77n/a def __reduce__(self):
78n/a return (self.__class__, (list(self),),
79n/a getattr(self, '__dict__', None))
80n/a
81n/a def add(self, item):
82n/a if self._pending_removals:
83n/a self._commit_removals()
84n/a self.data.add(ref(item, self._remove))
85n/a
86n/a def clear(self):
87n/a if self._pending_removals:
88n/a self._commit_removals()
89n/a self.data.clear()
90n/a
91n/a def copy(self):
92n/a return self.__class__(self)
93n/a
94n/a def pop(self):
95n/a if self._pending_removals:
96n/a self._commit_removals()
97n/a while True:
98n/a try:
99n/a itemref = self.data.pop()
100n/a except KeyError:
101n/a raise KeyError('pop from empty WeakSet')
102n/a item = itemref()
103n/a if item is not None:
104n/a return item
105n/a
106n/a def remove(self, item):
107n/a if self._pending_removals:
108n/a self._commit_removals()
109n/a self.data.remove(ref(item))
110n/a
111n/a def discard(self, item):
112n/a if self._pending_removals:
113n/a self._commit_removals()
114n/a self.data.discard(ref(item))
115n/a
116n/a def update(self, other):
117n/a if self._pending_removals:
118n/a self._commit_removals()
119n/a for element in other:
120n/a self.add(element)
121n/a
122n/a def __ior__(self, other):
123n/a self.update(other)
124n/a return self
125n/a
126n/a def difference(self, other):
127n/a newset = self.copy()
128n/a newset.difference_update(other)
129n/a return newset
130n/a __sub__ = difference
131n/a
132n/a def difference_update(self, other):
133n/a self.__isub__(other)
134n/a def __isub__(self, other):
135n/a if self._pending_removals:
136n/a self._commit_removals()
137n/a if self is other:
138n/a self.data.clear()
139n/a else:
140n/a self.data.difference_update(ref(item) for item in other)
141n/a return self
142n/a
143n/a def intersection(self, other):
144n/a return self.__class__(item for item in other if item in self)
145n/a __and__ = intersection
146n/a
147n/a def intersection_update(self, other):
148n/a self.__iand__(other)
149n/a def __iand__(self, other):
150n/a if self._pending_removals:
151n/a self._commit_removals()
152n/a self.data.intersection_update(ref(item) for item in other)
153n/a return self
154n/a
155n/a def issubset(self, other):
156n/a return self.data.issubset(ref(item) for item in other)
157n/a __le__ = issubset
158n/a
159n/a def __lt__(self, other):
160n/a return self.data < set(ref(item) for item in other)
161n/a
162n/a def issuperset(self, other):
163n/a return self.data.issuperset(ref(item) for item in other)
164n/a __ge__ = issuperset
165n/a
166n/a def __gt__(self, other):
167n/a return self.data > set(ref(item) for item in other)
168n/a
169n/a def __eq__(self, other):
170n/a if not isinstance(other, self.__class__):
171n/a return NotImplemented
172n/a return self.data == set(ref(item) for item in other)
173n/a
174n/a def symmetric_difference(self, other):
175n/a newset = self.copy()
176n/a newset.symmetric_difference_update(other)
177n/a return newset
178n/a __xor__ = symmetric_difference
179n/a
180n/a def symmetric_difference_update(self, other):
181n/a self.__ixor__(other)
182n/a def __ixor__(self, other):
183n/a if self._pending_removals:
184n/a self._commit_removals()
185n/a if self is other:
186n/a self.data.clear()
187n/a else:
188n/a self.data.symmetric_difference_update(ref(item, self._remove) for item in other)
189n/a return self
190n/a
191n/a def union(self, other):
192n/a return self.__class__(e for s in (self, other) for e in s)
193n/a __or__ = union
194n/a
195n/a def isdisjoint(self, other):
196n/a return len(self.intersection(other)) == 0