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 | |
| 14 | from _weakref import \ |
| 15 | getweakrefcount, \ |
| 16 | getweakrefs, \ |
| 17 | ref, \ |
| 18 | proxy, \ |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 19 | CallableProxyType, \ |
| 20 | ProxyType, \ |
| 21 | ReferenceType |
| 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 | |
| 45 | def __getitem__(self, key): |
Fred Drake | 4fd06e0 | 2001-08-03 04:11:27 +0000 | [diff] [blame] | 46 | o = self.data[key]() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 47 | if o is None: |
| 48 | raise KeyError, key |
| 49 | else: |
| 50 | return o |
| 51 | |
| 52 | def __repr__(self): |
Fred Drake | 9d2c85d | 2001-03-01 03:06:03 +0000 | [diff] [blame] | 53 | return "<WeakValueDictionary at %s>" % id(self) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 54 | |
| 55 | def __setitem__(self, key, value): |
Fred Drake | 746fe0f | 2001-09-28 19:01:26 +0000 | [diff] [blame] | 56 | self.data[key] = ref(value, self.__makeremove(key)) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 57 | |
| 58 | def copy(self): |
Fred Drake | 9d2c85d | 2001-03-01 03:06:03 +0000 | [diff] [blame] | 59 | new = WeakValueDictionary() |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 60 | for key, wr in self.data.items(): |
| 61 | o = wr() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 62 | if o is not None: |
| 63 | new[key] = o |
Fred Drake | 9d2c85d | 2001-03-01 03:06:03 +0000 | [diff] [blame] | 64 | return new |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 65 | |
Fred Drake | 1d9e4b7 | 2001-04-16 17:34:48 +0000 | [diff] [blame] | 66 | def get(self, key, default=None): |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 67 | try: |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 68 | wr = self.data[key] |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 69 | except KeyError: |
| 70 | return default |
| 71 | else: |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 72 | o = wr() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 73 | if o is None: |
| 74 | # This should only happen |
| 75 | return default |
| 76 | else: |
| 77 | return o |
| 78 | |
| 79 | def items(self): |
Fred Drake | 312a5dc | 2001-02-02 15:13:24 +0000 | [diff] [blame] | 80 | L = [] |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 81 | for key, wr in self.data.items(): |
| 82 | o = wr() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 83 | if o is not None: |
Fred Drake | 312a5dc | 2001-02-02 15:13:24 +0000 | [diff] [blame] | 84 | L.append((key, o)) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 85 | return L |
| 86 | |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 87 | def iteritems(self): |
| 88 | return WeakValuedItemIterator(self) |
| 89 | |
| 90 | def iterkeys(self): |
| 91 | return self.data.iterkeys() |
| 92 | __iter__ = iterkeys |
| 93 | |
| 94 | def itervalues(self): |
| 95 | return WeakValuedValueIterator(self) |
| 96 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 97 | def popitem(self): |
| 98 | while 1: |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 99 | key, wr = self.data.popitem() |
| 100 | o = wr() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 101 | if o is not None: |
| 102 | return key, o |
| 103 | |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 104 | def pop(self, key, *args): |
| 105 | try: |
| 106 | o = self.data.pop(key)() |
| 107 | except KeyError: |
| 108 | if args: |
| 109 | return args[0] |
| 110 | raise |
| 111 | if o is None: |
| 112 | raise KeyError, key |
| 113 | else: |
| 114 | return o |
| 115 | |
Walter Dörwald | 80ce6dd | 2004-05-27 18:16:25 +0000 | [diff] [blame] | 116 | def setdefault(self, key, default=None): |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 117 | try: |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 118 | wr = self.data[key] |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 119 | except KeyError: |
Fred Drake | 746fe0f | 2001-09-28 19:01:26 +0000 | [diff] [blame] | 120 | self.data[key] = ref(default, self.__makeremove(key)) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 121 | return default |
| 122 | else: |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 123 | return wr() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 124 | |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 125 | def update(self, dict=None, **kwargs): |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 126 | d = self.data |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 127 | if dict is not None: |
| 128 | if not hasattr(dict, "items"): |
| 129 | dict = type({})(dict) |
| 130 | for key, o in dict.items(): |
| 131 | d[key] = ref(o, self.__makeremove(key)) |
| 132 | if len(kwargs): |
| 133 | self.update(kwargs) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 134 | |
| 135 | def values(self): |
| 136 | L = [] |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 137 | for wr in self.data.values(): |
| 138 | o = wr() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 139 | if o is not None: |
| 140 | L.append(o) |
| 141 | return L |
| 142 | |
Fred Drake | 746fe0f | 2001-09-28 19:01:26 +0000 | [diff] [blame] | 143 | def __makeremove(self, key): |
| 144 | def remove(o, selfref=ref(self), key=key): |
| 145 | self = selfref() |
| 146 | if self is not None: |
| 147 | del self.data[key] |
| 148 | return remove |
| 149 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 150 | |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 151 | class WeakKeyDictionary(UserDict.UserDict): |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 152 | """ Mapping class that references keys weakly. |
| 153 | |
| 154 | Entries in the dictionary will be discarded when there is no |
| 155 | longer a strong reference to the key. This can be used to |
| 156 | associate additional data with an object owned by other parts of |
| 157 | an application without adding attributes to those objects. This |
| 158 | can be especially useful with objects that override attribute |
| 159 | accesses. |
| 160 | """ |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 161 | |
| 162 | def __init__(self, dict=None): |
| 163 | self.data = {} |
Fred Drake | 746fe0f | 2001-09-28 19:01:26 +0000 | [diff] [blame] | 164 | def remove(k, selfref=ref(self)): |
| 165 | self = selfref() |
| 166 | if self is not None: |
| 167 | del self.data[k] |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 168 | self._remove = remove |
Guido van Rossum | 009afb7 | 2002-06-10 20:00:52 +0000 | [diff] [blame] | 169 | if dict is not None: self.update(dict) |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 170 | |
Fred Drake | b663a2c | 2001-09-06 14:51:01 +0000 | [diff] [blame] | 171 | def __delitem__(self, key): |
Tim Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 172 | del self.data[ref(key)] |
Fred Drake | b663a2c | 2001-09-06 14:51:01 +0000 | [diff] [blame] | 173 | |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 174 | def __getitem__(self, key): |
| 175 | return self.data[ref(key)] |
| 176 | |
| 177 | def __repr__(self): |
| 178 | return "<WeakKeyDictionary at %s>" % id(self) |
| 179 | |
| 180 | def __setitem__(self, key, value): |
| 181 | self.data[ref(key, self._remove)] = value |
| 182 | |
| 183 | def copy(self): |
| 184 | new = WeakKeyDictionary() |
| 185 | for key, value in self.data.items(): |
| 186 | o = key() |
| 187 | if o is not None: |
| 188 | new[o] = value |
Fred Drake | 9d2c85d | 2001-03-01 03:06:03 +0000 | [diff] [blame] | 189 | return new |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 190 | |
Fred Drake | 1d9e4b7 | 2001-04-16 17:34:48 +0000 | [diff] [blame] | 191 | def get(self, key, default=None): |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 192 | return self.data.get(ref(key),default) |
| 193 | |
Fred Drake | 1d9e4b7 | 2001-04-16 17:34:48 +0000 | [diff] [blame] | 194 | def has_key(self, key): |
Fred Drake | 3bae7dd | 2001-11-06 16:36:53 +0000 | [diff] [blame] | 195 | try: |
| 196 | wr = ref(key) |
| 197 | except TypeError: |
| 198 | return 0 |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 199 | return wr in self.data |
Fred Drake | 1d9e4b7 | 2001-04-16 17:34:48 +0000 | [diff] [blame] | 200 | |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 201 | def __contains__(self, key): |
| 202 | try: |
| 203 | wr = ref(key) |
| 204 | except TypeError: |
| 205 | return 0 |
| 206 | return wr in self.data |
Tim Peters | c411dba | 2002-07-16 21:35:23 +0000 | [diff] [blame] | 207 | |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 208 | def items(self): |
| 209 | L = [] |
| 210 | for key, value in self.data.items(): |
| 211 | o = key() |
| 212 | if o is not None: |
| 213 | L.append((o, value)) |
| 214 | return L |
| 215 | |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 216 | def iteritems(self): |
| 217 | return WeakKeyedItemIterator(self) |
| 218 | |
| 219 | def iterkeys(self): |
| 220 | return WeakKeyedKeyIterator(self) |
| 221 | __iter__ = iterkeys |
| 222 | |
| 223 | def itervalues(self): |
| 224 | return self.data.itervalues() |
| 225 | |
Fred Drake | 1d9e4b7 | 2001-04-16 17:34:48 +0000 | [diff] [blame] | 226 | def keys(self): |
| 227 | L = [] |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 228 | for wr in self.data.keys(): |
| 229 | o = wr() |
Fred Drake | 1d9e4b7 | 2001-04-16 17:34:48 +0000 | [diff] [blame] | 230 | if o is not None: |
| 231 | L.append(o) |
| 232 | return L |
| 233 | |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 234 | def popitem(self): |
| 235 | while 1: |
| 236 | key, value = self.data.popitem() |
| 237 | o = key() |
| 238 | if o is not None: |
| 239 | return o, value |
| 240 | |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 241 | def pop(self, key, *args): |
| 242 | return self.data.pop(ref(key), *args) |
| 243 | |
Walter Dörwald | 80ce6dd | 2004-05-27 18:16:25 +0000 | [diff] [blame] | 244 | def setdefault(self, key, default=None): |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 245 | return self.data.setdefault(ref(key, self._remove),default) |
| 246 | |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 247 | def update(self, dict=None, **kwargs): |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 248 | d = self.data |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 249 | if dict is not None: |
| 250 | if not hasattr(dict, "items"): |
| 251 | dict = type({})(dict) |
| 252 | for key, value in dict.items(): |
| 253 | d[ref(key, self._remove)] = value |
| 254 | if len(kwargs): |
| 255 | self.update(kwargs) |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 256 | |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 257 | |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 258 | class BaseIter: |
| 259 | def __iter__(self): |
| 260 | return self |
| 261 | |
| 262 | |
| 263 | class WeakKeyedKeyIterator(BaseIter): |
| 264 | def __init__(self, weakdict): |
| 265 | self._next = weakdict.data.iterkeys().next |
| 266 | |
| 267 | def next(self): |
| 268 | while 1: |
| 269 | wr = self._next() |
| 270 | obj = wr() |
| 271 | if obj is not None: |
| 272 | return obj |
| 273 | |
| 274 | |
| 275 | class WeakKeyedItemIterator(BaseIter): |
| 276 | def __init__(self, weakdict): |
| 277 | self._next = weakdict.data.iteritems().next |
| 278 | |
| 279 | def next(self): |
| 280 | while 1: |
| 281 | wr, value = self._next() |
| 282 | key = wr() |
| 283 | if key is not None: |
| 284 | return key, value |
| 285 | |
| 286 | |
| 287 | class WeakValuedValueIterator(BaseIter): |
| 288 | def __init__(self, weakdict): |
| 289 | self._next = weakdict.data.itervalues().next |
| 290 | |
| 291 | def next(self): |
| 292 | while 1: |
| 293 | wr = self._next() |
| 294 | obj = wr() |
| 295 | if obj is not None: |
| 296 | return obj |
| 297 | |
| 298 | |
| 299 | class WeakValuedItemIterator(BaseIter): |
| 300 | def __init__(self, weakdict): |
| 301 | self._next = weakdict.data.iteritems().next |
| 302 | |
| 303 | def next(self): |
| 304 | while 1: |
| 305 | key, wr = self._next() |
| 306 | value = wr() |
| 307 | if value is not None: |
| 308 | return key, value |
| 309 | |
| 310 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 311 | # no longer needed |
| 312 | del UserDict |