Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 1 | """Weak reference support for Python. |
| 2 | |
| 3 | This module is an implementation of PEP 205: |
| 4 | |
| 5 | http://python.sourceforge.net/peps/pep-0205.html |
| 6 | """ |
| 7 | |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 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. |
| 11 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 12 | import UserDict |
| 13 | |
Andrew M. Kuchling | 33ad28b | 2004-08-31 11:38:12 +0000 | [diff] [blame] | 14 | from _weakref import ( |
| 15 | getweakrefcount, |
| 16 | getweakrefs, |
| 17 | ref, |
| 18 | proxy, |
| 19 | CallableProxyType, |
| 20 | ProxyType, |
| 21 | ReferenceType) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 22 | |
Fred Drake | e029242 | 2001-10-05 21:54:09 +0000 | [diff] [blame] | 23 | from exceptions import ReferenceError |
| 24 | |
| 25 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 26 | ProxyTypes = (ProxyType, CallableProxyType) |
| 27 | |
Fred Drake | 9a9d219 | 2001-04-10 19:11:23 +0000 | [diff] [blame] | 28 | __all__ = ["ref", "proxy", "getweakrefcount", "getweakrefs", |
Skip Montanaro | 40fc160 | 2001-03-01 04:27:19 +0000 | [diff] [blame] | 29 | "WeakKeyDictionary", "ReferenceType", "ProxyType", |
| 30 | "CallableProxyType", "ProxyTypes", "WeakValueDictionary"] |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 31 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 32 | |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 33 | class WeakValueDictionary(UserDict.UserDict): |
| 34 | """Mapping class that references values weakly. |
| 35 | |
| 36 | Entries in the dictionary will be discarded when no strong |
| 37 | reference to the value exists anymore |
| 38 | """ |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 39 | # We inherit the constructor without worrying about the input |
| 40 | # dictionary; since it uses our .update() method, we get the right |
Fred Drake | 9d2c85d | 2001-03-01 03:06:03 +0000 | [diff] [blame] | 41 | # checks (if the other dictionary is a WeakValueDictionary, |
| 42 | # objects are unwrapped on the way out, and we always wrap on the |
| 43 | # way in). |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 44 | |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 45 | def __init__(self, *args, **kw): |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 46 | def remove(wr, selfref=ref(self)): |
| 47 | self = selfref() |
| 48 | if self is not None: |
| 49 | del self.data[wr.key] |
| 50 | self._remove = remove |
Georg Brandl | 9166e1a | 2005-06-04 09:20:03 +0000 | [diff] [blame] | 51 | UserDict.UserDict.__init__(self, *args, **kw) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 52 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 53 | def __getitem__(self, key): |
Fred Drake | 4fd06e0 | 2001-08-03 04:11:27 +0000 | [diff] [blame] | 54 | o = self.data[key]() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 55 | if o is None: |
| 56 | raise KeyError, key |
| 57 | else: |
| 58 | return o |
| 59 | |
Raymond Hettinger | 6114679 | 2004-08-19 21:32:06 +0000 | [diff] [blame] | 60 | def __contains__(self, key): |
| 61 | try: |
| 62 | o = self.data[key]() |
| 63 | except KeyError: |
| 64 | return False |
| 65 | return o is not None |
| 66 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 67 | def __repr__(self): |
Fred Drake | 9d2c85d | 2001-03-01 03:06:03 +0000 | [diff] [blame] | 68 | return "<WeakValueDictionary at %s>" % id(self) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 69 | |
| 70 | def __setitem__(self, key, value): |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 71 | self.data[key] = KeyedRef(value, self._remove, key) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 72 | |
| 73 | def copy(self): |
Fred Drake | 9d2c85d | 2001-03-01 03:06:03 +0000 | [diff] [blame] | 74 | new = WeakValueDictionary() |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 75 | for key, wr in self.data.items(): |
| 76 | o = wr() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 77 | if o is not None: |
| 78 | new[key] = o |
Fred Drake | 9d2c85d | 2001-03-01 03:06:03 +0000 | [diff] [blame] | 79 | return new |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 80 | |
Fred Drake | 1d9e4b7 | 2001-04-16 17:34:48 +0000 | [diff] [blame] | 81 | def get(self, key, default=None): |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 82 | try: |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 83 | wr = self.data[key] |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 84 | except KeyError: |
| 85 | return default |
| 86 | else: |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 87 | o = wr() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 88 | if o is None: |
| 89 | # This should only happen |
| 90 | return default |
| 91 | else: |
| 92 | return o |
| 93 | |
| 94 | def items(self): |
Fred Drake | 312a5dc | 2001-02-02 15:13:24 +0000 | [diff] [blame] | 95 | L = [] |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 96 | for key, wr in self.data.items(): |
| 97 | o = wr() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 98 | if o is not None: |
Fred Drake | 312a5dc | 2001-02-02 15:13:24 +0000 | [diff] [blame] | 99 | L.append((key, o)) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 100 | return L |
| 101 | |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 102 | def iteritems(self): |
Raymond Hettinger | 6114679 | 2004-08-19 21:32:06 +0000 | [diff] [blame] | 103 | for wr in self.data.itervalues(): |
| 104 | value = wr() |
| 105 | if value is not None: |
| 106 | yield wr.key, value |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 107 | |
| 108 | def iterkeys(self): |
| 109 | return self.data.iterkeys() |
Raymond Hettinger | 6114679 | 2004-08-19 21:32:06 +0000 | [diff] [blame] | 110 | |
| 111 | def __iter__(self): |
| 112 | return self.data.iterkeys() |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 113 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 114 | def itervaluerefs(self): |
| 115 | """Return an iterator that yields the weak references to the values. |
| 116 | |
| 117 | The references are not guaranteed to be 'live' at the time |
| 118 | they are used, so the result of calling the references needs |
| 119 | to be checked before being used. This can be used to avoid |
| 120 | creating references that will cause the garbage collector to |
| 121 | keep the values around longer than needed. |
| 122 | |
| 123 | """ |
| 124 | return self.data.itervalues() |
| 125 | |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 126 | def itervalues(self): |
Raymond Hettinger | 6114679 | 2004-08-19 21:32:06 +0000 | [diff] [blame] | 127 | for wr in self.data.itervalues(): |
| 128 | obj = wr() |
| 129 | if obj is not None: |
| 130 | yield obj |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 131 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 132 | def popitem(self): |
| 133 | while 1: |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 134 | key, wr = self.data.popitem() |
| 135 | o = wr() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 136 | if o is not None: |
| 137 | return key, o |
| 138 | |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 139 | def pop(self, key, *args): |
| 140 | try: |
| 141 | o = self.data.pop(key)() |
| 142 | except KeyError: |
| 143 | if args: |
| 144 | return args[0] |
| 145 | raise |
| 146 | if o is None: |
| 147 | raise KeyError, key |
| 148 | else: |
| 149 | return o |
| 150 | |
Walter Dörwald | 80ce6dd | 2004-05-27 18:16:25 +0000 | [diff] [blame] | 151 | def setdefault(self, key, default=None): |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 152 | try: |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 153 | wr = self.data[key] |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 154 | except KeyError: |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 155 | self.data[key] = KeyedRef(default, self._remove, key) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 156 | return default |
| 157 | else: |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 158 | return wr() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 159 | |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 160 | def update(self, dict=None, **kwargs): |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 161 | d = self.data |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 162 | if dict is not None: |
| 163 | if not hasattr(dict, "items"): |
| 164 | dict = type({})(dict) |
| 165 | for key, o in dict.items(): |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 166 | d[key] = KeyedRef(o, self._remove, key) |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 167 | if len(kwargs): |
| 168 | self.update(kwargs) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 169 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 170 | def valuerefs(self): |
| 171 | """Return a list of weak references to the values. |
| 172 | |
| 173 | The references are not guaranteed to be 'live' at the time |
| 174 | they are used, so the result of calling the references needs |
| 175 | to be checked before being used. This can be used to avoid |
| 176 | creating references that will cause the garbage collector to |
| 177 | keep the values around longer than needed. |
| 178 | |
| 179 | """ |
| 180 | return self.data.values() |
| 181 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 182 | def values(self): |
| 183 | L = [] |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 184 | for wr in self.data.values(): |
| 185 | o = wr() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 186 | if o is not None: |
| 187 | L.append(o) |
| 188 | return L |
| 189 | |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 190 | |
| 191 | class KeyedRef(ref): |
| 192 | """Specialized reference that includes a key corresponding to the value. |
| 193 | |
| 194 | This is used in the WeakValueDictionary to avoid having to create |
| 195 | a function object for each key stored in the mapping. A shared |
| 196 | callback object can use the 'key' attribute of a KeyedRef instead |
| 197 | of getting a reference to the key from an enclosing scope. |
| 198 | |
| 199 | """ |
| 200 | |
| 201 | __slots__ = "key", |
| 202 | |
| 203 | def __new__(type, ob, callback, key): |
| 204 | self = ref.__new__(type, ob, callback) |
| 205 | self.key = key |
| 206 | return self |
| 207 | |
| 208 | def __init__(self, ob, callback, key): |
| 209 | super(KeyedRef, self).__init__(ob, callback) |
Fred Drake | 746fe0f | 2001-09-28 19:01:26 +0000 | [diff] [blame] | 210 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 211 | |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 212 | class WeakKeyDictionary(UserDict.UserDict): |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 213 | """ Mapping class that references keys weakly. |
| 214 | |
| 215 | Entries in the dictionary will be discarded when there is no |
| 216 | longer a strong reference to the key. This can be used to |
| 217 | associate additional data with an object owned by other parts of |
| 218 | an application without adding attributes to those objects. This |
| 219 | can be especially useful with objects that override attribute |
| 220 | accesses. |
| 221 | """ |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 222 | |
| 223 | def __init__(self, dict=None): |
| 224 | self.data = {} |
Fred Drake | 746fe0f | 2001-09-28 19:01:26 +0000 | [diff] [blame] | 225 | def remove(k, selfref=ref(self)): |
| 226 | self = selfref() |
| 227 | if self is not None: |
| 228 | del self.data[k] |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 229 | self._remove = remove |
Guido van Rossum | 009afb7 | 2002-06-10 20:00:52 +0000 | [diff] [blame] | 230 | if dict is not None: self.update(dict) |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 231 | |
Fred Drake | b663a2c | 2001-09-06 14:51:01 +0000 | [diff] [blame] | 232 | def __delitem__(self, key): |
Tim Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 233 | del self.data[ref(key)] |
Fred Drake | b663a2c | 2001-09-06 14:51:01 +0000 | [diff] [blame] | 234 | |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 235 | def __getitem__(self, key): |
| 236 | return self.data[ref(key)] |
| 237 | |
| 238 | def __repr__(self): |
| 239 | return "<WeakKeyDictionary at %s>" % id(self) |
| 240 | |
| 241 | def __setitem__(self, key, value): |
| 242 | self.data[ref(key, self._remove)] = value |
| 243 | |
| 244 | def copy(self): |
| 245 | new = WeakKeyDictionary() |
| 246 | for key, value in self.data.items(): |
| 247 | o = key() |
| 248 | if o is not None: |
| 249 | new[o] = value |
Fred Drake | 9d2c85d | 2001-03-01 03:06:03 +0000 | [diff] [blame] | 250 | return new |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 251 | |
Fred Drake | 1d9e4b7 | 2001-04-16 17:34:48 +0000 | [diff] [blame] | 252 | def get(self, key, default=None): |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 253 | return self.data.get(ref(key),default) |
| 254 | |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 255 | def __contains__(self, key): |
| 256 | try: |
| 257 | wr = ref(key) |
| 258 | except TypeError: |
| 259 | return 0 |
| 260 | return wr in self.data |
Tim Peters | c411dba | 2002-07-16 21:35:23 +0000 | [diff] [blame] | 261 | |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 262 | def items(self): |
| 263 | L = [] |
| 264 | for key, value in self.data.items(): |
| 265 | o = key() |
| 266 | if o is not None: |
| 267 | L.append((o, value)) |
| 268 | return L |
| 269 | |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 270 | def iteritems(self): |
Raymond Hettinger | 6114679 | 2004-08-19 21:32:06 +0000 | [diff] [blame] | 271 | for wr, value in self.data.iteritems(): |
| 272 | key = wr() |
| 273 | if key is not None: |
| 274 | yield key, value |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 275 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 276 | def iterkeyrefs(self): |
| 277 | """Return an iterator that yields the weak references to the keys. |
| 278 | |
| 279 | The references are not guaranteed to be 'live' at the time |
| 280 | they are used, so the result of calling the references needs |
| 281 | to be checked before being used. This can be used to avoid |
| 282 | creating references that will cause the garbage collector to |
| 283 | keep the keys around longer than needed. |
| 284 | |
| 285 | """ |
| 286 | return self.data.iterkeys() |
| 287 | |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 288 | def iterkeys(self): |
Raymond Hettinger | 6114679 | 2004-08-19 21:32:06 +0000 | [diff] [blame] | 289 | for wr in self.data.iterkeys(): |
| 290 | obj = wr() |
| 291 | if obj is not None: |
| 292 | yield obj |
| 293 | |
| 294 | def __iter__(self): |
| 295 | return self.iterkeys() |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 296 | |
| 297 | def itervalues(self): |
| 298 | return self.data.itervalues() |
| 299 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 300 | def keyrefs(self): |
| 301 | """Return a list of weak references to the keys. |
| 302 | |
| 303 | The references are not guaranteed to be 'live' at the time |
| 304 | they are used, so the result of calling the references needs |
| 305 | to be checked before being used. This can be used to avoid |
| 306 | creating references that will cause the garbage collector to |
| 307 | keep the keys around longer than needed. |
| 308 | |
| 309 | """ |
| 310 | return self.data.keys() |
| 311 | |
Fred Drake | 1d9e4b7 | 2001-04-16 17:34:48 +0000 | [diff] [blame] | 312 | def keys(self): |
| 313 | L = [] |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 314 | for wr in self.data.keys(): |
| 315 | o = wr() |
Fred Drake | 1d9e4b7 | 2001-04-16 17:34:48 +0000 | [diff] [blame] | 316 | if o is not None: |
| 317 | L.append(o) |
| 318 | return L |
| 319 | |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 320 | def popitem(self): |
| 321 | while 1: |
| 322 | key, value = self.data.popitem() |
| 323 | o = key() |
| 324 | if o is not None: |
| 325 | return o, value |
| 326 | |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 327 | def pop(self, key, *args): |
| 328 | return self.data.pop(ref(key), *args) |
| 329 | |
Walter Dörwald | 80ce6dd | 2004-05-27 18:16:25 +0000 | [diff] [blame] | 330 | def setdefault(self, key, default=None): |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 331 | return self.data.setdefault(ref(key, self._remove),default) |
| 332 | |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 333 | def update(self, dict=None, **kwargs): |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 334 | d = self.data |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 335 | if dict is not None: |
| 336 | if not hasattr(dict, "items"): |
| 337 | dict = type({})(dict) |
| 338 | for key, value in dict.items(): |
| 339 | d[ref(key, self._remove)] = value |
| 340 | if len(kwargs): |
| 341 | self.update(kwargs) |