MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / WeakSet

Class WeakSet

tools/python-3.11.9-amd64/Lib/_weakrefset.py:36–205  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

34
35
36class WeakSet:
37 def __init__(self, data=None):
38 self.data = set()
39 def _remove(item, selfref=ref(self)):
40 self = selfref()
41 if self is not None:
42 if self._iterating:
43 self._pending_removals.append(item)
44 else:
45 self.data.discard(item)
46 self._remove = _remove
47 # A list of keys to be removed
48 self._pending_removals = []
49 self._iterating = set()
50 if data is not None:
51 self.update(data)
52
53 def _commit_removals(self):
54 pop = self._pending_removals.pop
55 discard = self.data.discard
56 while True:
57 try:
58 item = pop()
59 except IndexError:
60 return
61 discard(item)
62
63 def __iter__(self):
64 with _IterationGuard(self):
65 for itemref in self.data:
66 item = itemref()
67 if item is not None:
68 # Caveat: the iterator will keep a strong reference to
69 # `item` until it is resumed or closed.
70 yield item
71
72 def __len__(self):
73 return len(self.data) - len(self._pending_removals)
74
75 def __contains__(self, item):
76 try:
77 wr = ref(item)
78 except TypeError:
79 return False
80 return wr in self.data
81
82 def __reduce__(self):
83 return self.__class__, (list(self),), self.__getstate__()
84
85 def add(self, item):
86 if self._pending_removals:
87 self._commit_removals()
88 self.data.add(ref(item, self._remove))
89
90 def clear(self):
91 if self._pending_removals:
92 self._commit_removals()
93 self.data.clear()

Callers 4

threading.pyFile · 0.90
__new__Method · 0.90
__subclasscheck__Method · 0.90
process.pyFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected