1 """Weak reference support for Python.
3 This module is an implementation of PEP 205:
5 http://python.sourceforge.net/peps/pep-0205.html
8 # Naming convention: Variables named "wr" are weak reference objects;
9 # they are called this instead of "ref" to avoid name collisions with
10 # the module-global ref() function imported from _weakref.
14 from _weakref
import \
24 ProxyTypes
= (ProxyType
, CallableProxyType
)
26 __all__
= ["ref", "proxy", "getweakrefcount", "getweakrefs",
27 "WeakKeyDictionary", "ReferenceType", "ProxyType",
28 "CallableProxyType", "ProxyTypes", "WeakValueDictionary"]
31 class WeakValueDictionary(UserDict
.UserDict
):
32 """Mapping class that references values weakly.
34 Entries in the dictionary will be discarded when no strong
35 reference to the value exists anymore
37 # We inherit the constructor without worrying about the input
38 # dictionary; since it uses our .update() method, we get the right
39 # checks (if the other dictionary is a WeakValueDictionary,
40 # objects are unwrapped on the way out, and we always wrap on the
43 def __getitem__(self
, key
):
44 o
= self
.data
.get(key
)()
51 return "<WeakValueDictionary at %s>" % id(self
)
53 def __setitem__(self
, key
, value
):
54 def remove(o
, data
=self
.data
, key
=key
):
56 self
.data
[key
] = ref(value
, remove
)
59 new
= WeakValueDictionary()
60 for key
, wr
in self
.data
.items():
66 def get(self
, key
, default
=None):
74 # This should only happen
81 for key
, wr
in self
.data
.items():
89 key
, wr
= self
.data
.popitem()
94 def setdefault(self
, key
, default
):
98 def remove(o
, data
=self
.data
, key
=key
):
100 self
.data
[key
] = ref(default
, remove
)
105 def update(self
, dict):
107 for key
, o
in dict.items():
108 def remove(o
, data
=d
, key
=key
):
110 d
[key
] = ref(o
, remove
)
114 for wr
in self
.data
.values():
121 class WeakKeyDictionary(UserDict
.UserDict
):
122 """ Mapping class that references keys weakly.
124 Entries in the dictionary will be discarded when there is no
125 longer a strong reference to the key. This can be used to
126 associate additional data with an object owned by other parts of
127 an application without adding attributes to those objects. This
128 can be especially useful with objects that override attribute
132 def __init__(self
, dict=None):
134 if dict is not None: self
.update(dict)
135 def remove(k
, data
=self
.data
):
137 self
._remove
= remove
139 def __getitem__(self
, key
):
140 return self
.data
[ref(key
)]
143 return "<WeakKeyDictionary at %s>" % id(self
)
145 def __setitem__(self
, key
, value
):
146 self
.data
[ref(key
, self
._remove
)] = value
149 new
= WeakKeyDictionary()
150 for key
, value
in self
.data
.items():
156 def get(self
, key
, default
=None):
157 return self
.data
.get(ref(key
),default
)
159 def has_key(self
, key
):
160 return self
.data
.has_key(ref(key
))
164 for key
, value
in self
.data
.items():
172 for wr
in self
.data
.keys():
180 key
, value
= self
.data
.popitem()
185 def setdefault(self
, key
, default
):
186 return self
.data
.setdefault(ref(key
, self
._remove
),default
)
188 def update(self
, dict):
190 for key
, value
in dict.items():
191 d
[ref(key
, self
._remove
)] = value