8000 Moved WeakSet into a bootstap module use by abc.py. · python/cpython@93fa608 · GitHub
[go: up one dir, main page]

Skip to content

Commit 93fa608

Browse files
committed
Moved WeakSet into a bootstap module use by abc.py.
This makes it possible to use ABCs in weakref.py (which will be done in a later checkin).
1 parent 2add352 commit 93fa608

File tree

3 files changed

+115
-107
lines changed

3 files changed

+115
-107
lines changed

Lib/_weakrefset.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Access WeakSet through the weakref module.
2+
# This code is separated-out because it is needed
3+
# by abc.py to load everything else at startup.
4+
5+
from _weakref import ref
6+
7+
__all__ = ['WeakSet']
8+
9+
class WeakSet:
10+
def __init__(self, data=None):
11+
self.data = set()
12+
def _remove(item, selfref=ref(self)):
13+
self = selfref()
14+
if self is not None:
15+
self.data.discard(item)
16+
self._remove = _remove
17+
if data is not None:
18+
self.update(data)
19+
20+
def __iter__(self):
21+
for itemref in self.data:
22+
item = itemref()
23+
if item is not None:
24+
yield item
25+
26+
def __contains__(self, item):
27+
return ref(item) in self.data
28+
29+
def __reduce__(self):
30+
return (self.__class__, (list(self),),
31+
getattr(self, '__dict__', None))
32+
33+
def add(self, item):
34+
self.data.add(ref(item, self._remove))
35+
36+
def clear(self):
37+
self.data.clear()
38+
39+
def copy(self):
40+
return self.__class__(self)
41+
42+
def pop(self):
43+
while True:
44+
itemref = self.data.pop()
45+
item = itemref()
46+
if item is not None:
47+
return item
48+
49+
def remove(self, item):
50+
self.data.remove(ref(item))
51+
52+
def discard(self, item):
53+
self.data.discard(ref(item))
54+
55+
def update(self, other):
56+
if isinstance(other, self.__class__):
57+
self.data.update(other.data)
58+
else:
59+
for element in other:
60+
self.add(element)
61+
__ior__ = update
62+
63+
# Helper functions for simple delegating methods.
64+
def _apply(self, other, method):
65+
if not isinstance(other, self.__class__):
66+
other = self.__class__(other)
67+
newdata = method(other.data)
68+
newset = self.__class__()
69+
newset.data = newdata
70+
return newset
71+
72+
def _apply_mutate(self, other, method):
73+
if not isinstance(other, self.__class__):
74+
other = self.__class__(other)
75+
method(other)
76+
77+
def difference(self, other):
78+
return self._apply(other, self.data.difference)
79+
__sub__ = difference
80+
81+
def difference_update(self, other):
82+
self._apply_mutate(self, self.data.difference_update)
83+
__isub__ = difference_update
84+
85+
def intersection(self, other):
86+
return self._apply(other, self.data.intersection)
87+
__and__ = intersection
88+
89+
def intersection_update(self, other):
90+
self._apply_mutate(self, self.data.intersection_update)
91+
__iand__ = intersection_update
92+
93+
def issubset(self, other):
94+
return self.data.issubset(ref(item) for item in other)
95+
__lt__ = issubset
96+
97+
def issuperset(self, other):
98+
return self.data.issuperset(ref(item) for item in other)
99+
__gt__ = issuperset
100+
101+
def symmetric_difference(self, other):
102+
return self._apply(other, self.data.symmetric_difference)
103+
__xor__ = symmetric_difference
104+
105+
def symmetric_difference_update(self, other):
106+
self._apply_mutate(other, self.data.symmetric_difference_update)
107+
__ixor__ = symmetric_difference_update
108+
109+
def union(self, other):
110+
self._apply_mutate(other, self.data.union)
111+
__or__ = union

Lib/abc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
"""Abstract Base Classes (ABCs) according to PEP 3119."""
55

6-
from weakref import WeakSet
6+
from _weakrefset import WeakSet
77

88
def abstractmethod(funcobj):
99
"""A decorator indicating abstract methods.

Lib/weakref.py

Lines changed: 3 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
ProxyType,
2121
ReferenceType)
2222

23+
from _weakrefset import WeakSet
2324

2425
ProxyTypes = (ProxyType, CallableProxyType)
2526

2627
__all__ = ["ref", "proxy", "getweakrefcount", "getweakrefs",
2728
"WeakKeyDictionary", "ReferenceType", "ProxyType",
28-
"CallableProxyType", "ProxyTypes", "WeakValueDictionary"]
29+
"CallableProxyType", "ProxyTypes", "WeakValueDictionary",
30+
"WeakSet"]
2931

3032

3133
class WeakValueDictionary(UserDict.UserDict):
@@ -337,108 +339,3 @@ def update(self, dict=None, **kwargs):
337339
d[ref(key, self._remove)] = value
338340
if len(kwargs):
339341
self.update(kwargs)
340-
341-
342-
class WeakSet:
343-
def __init__(self, data=None):
344-
self.data = set()
345-
def _remove(item, selfref=ref(self)):
346-
self = selfref()
347-
if self is not None:
348-
self.data.discard(item)
349-
self._remove = _remove
350-
if data is not None:
351-
self.update(data)
352-
353-
def __iter__(self):
354-
for itemref in self.data:
355-
item = itemref()
356-
if item is not None:
357-
yield item
358-
359-
def __contains__(self, item):
360-
return ref(item) in self.data
361-
362-
def __reduce__(self):
363-
return (self.__class__, (list(self),),
364-
getattr(self, '__dict__', None))
365-
366-
def add(self, item):
367-
self.data.add(ref(item, self._remove))
368-
369-
def clear(self):
370-
self.data.clear()
371-
372-
def copy(self):
373-
return self.__class__(self)
374-
375-
def pop(self):
376-
while True:
377-
itemref = self.data.pop()
378-
item = itemref()
379-
if item is not None:
380-
return item
381-
382-
def remove(self, item):
383-
self.data.remove(ref(item))
384-
385-
def discard(self, item):
386-
self.data.discard(ref(item))
387-
388-
def update(self, other):
389-
if isinstance(other, self.__class__):
390-
self.data.update(other.data)
391-
else:
392-
for element in other:
393-
self.add(element)
394-
__ior__ = update
395-
396-
# Helper functions for simple delegating methods.
397-
def _apply(self, other, method):
398-
if not isinstance(other, self.__class__):
399-
other = self.__class__(other)
400-
newdata = method(other.data)
401-
newset = self.__class__()
402-
newset.data = newdata
403-
return newset
404-
405-
def _apply_mutate(self, other, method):
406-
if not isinstance(other, self.__class__):
407-
other = self.__class__(other)
408-
method(other)
409-
410-
def difference(self, other):
411-
return self._apply(other, self.data.difference)
412-
__sub__ = difference
413-
414-
def difference_update(self, other):
415-
self._apply_mutate(self, self.data.difference_update)
416-
__isub__ = difference_update
417-
418-
def intersection(self, other):
419-
return self._apply(other, self.data.intersection)
420-
__and__ = intersection
421-
422-
def intersection_update(self, other):
423-
self._apply_mutate(self, self.data.intersection_update)
424-
__iand__ = intersection_update
425-
426-
def issubset(self, other):
427-
return self.data.issubset(ref(item) for item in other)
428-
__lt__ = issubset
429-
430-
def issuperset(self, other):
431-
return self.data.issuperset(ref(item) for item in other)
432-
__gt__ = issuperset
433-
434-
def symmetric_difference(self, other):
435-
return self._apply(other, self.data.symmetric_difference)
436-
__xor__ = symmetric_difference
437-
438-
def symmetric_difference_update(self, other):
439-
self._apply_mutate(other, self.data.symmetric_difference_update)
440-
__ixor__ = symmetric_difference_update
441-
442-
def union(self, other):
443-
self._apply_mutate(other, self.data.union)
444-
__or__ = union

0 commit comments

Comments
 (0)
0