MCPcopy Index your code
hub / github.com/python/cpython / WeakSet

Class WeakSet

Lib/_weakrefset.py:11–147  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9
10
11class WeakSet:
12 def __init__(self, data=None):
13 self.data = set()
14
15 def _remove(item, selfref=ref(self)):
16 self = selfref()
17 if self is not None:
18 self.data.discard(item)
19
20 self._remove = _remove
21 if data is not None:
22 self.update(data)
23
24 def __iter__(self):
25 for itemref in self.data.copy():
26 item = itemref()
27 if item is not None:
28 # Caveat: the iterator will keep a strong reference to
29 # `item` until it is resumed or closed.
30 yield item
31
32 def __len__(self):
33 return len(self.data)
34
35 def __contains__(self, item):
36 try:
37 wr = ref(item)
38 except TypeError:
39 return False
40 return wr in self.data
41
42 def __reduce__(self):
43 return self.__class__, (list(self),), self.__getstate__()
44
45 def add(self, item):
46 self.data.add(ref(item, self._remove))
47
48 def clear(self):
49 self.data.clear()
50
51 def copy(self):
52 return self.__class__(self)
53
54 def pop(self):
55 while True:
56 try:
57 itemref = self.data.pop()
58 except KeyError:
59 raise KeyError('pop from empty WeakSet') from None
60 item = itemref()
61 if item is not None:
62 return item
63
64 def remove(self, item):
65 self.data.remove(ref(item))
66
67 def discard(self, item):
68 self.data.discard(ref(item))

Callers 15

threading.pyFile · 0.90
__new__Method · 0.90
__subclasscheck__Method · 0.90
setUpMethod · 0.90
test_unionMethod · 0.90
test_intersectionMethod · 0.90
test_isdisjointMethod · 0.90
test_differenceMethod · 0.90
test_ltMethod · 0.90
test_gtMethod · 0.90
test_gcMethod · 0.90

Calls

no outgoing calls

Tested by 15

setUpMethod · 0.72
test_unionMethod · 0.72
test_intersectionMethod · 0.72
test_isdisjointMethod · 0.72
test_differenceMethod · 0.72
test_ltMethod · 0.72
test_gtMethod · 0.72
test_gcMethod · 0.72
test_initMethod · 0.72
test_clearMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…