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 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 5 | http://www.python.org/dev/peps/pep-0205/ |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 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 | |
Andrew M. Kuchling | 33ad28b | 2004-08-31 11:38:12 +0000 | [diff] [blame] | 12 | from _weakref import ( |
| 13 | getweakrefcount, |
| 14 | getweakrefs, |
| 15 | ref, |
| 16 | proxy, |
| 17 | CallableProxyType, |
| 18 | ProxyType, |
| 19 | ReferenceType) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 20 | |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 21 | from _weakrefset import WeakSet, _IterationGuard |
Fred Drake | e029242 | 2001-10-05 21:54:09 +0000 | [diff] [blame] | 22 | |
Brett Cannon | 663fffa | 2009-03-25 23:31:22 +0000 | [diff] [blame] | 23 | import collections # Import after _weakref to avoid circular import. |
| 24 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 25 | ProxyTypes = (ProxyType, CallableProxyType) |
| 26 | |
Fred Drake | 9a9d219 | 2001-04-10 19:11:23 +0000 | [diff] [blame] | 27 | __all__ = ["ref", "proxy", "getweakrefcount", "getweakrefs", |
Gregory P. Smith | 7d10c2b | 2008-08-18 03:41:46 +0000 | [diff] [blame] | 28 | "WeakKeyDictionary", "ReferenceType", "ProxyType", |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 29 | "CallableProxyType", "ProxyTypes", "WeakValueDictionary", |
Antoine Pitrou | c3afba1 | 2012-11-17 18:57:38 +0100 | [diff] [blame] | 30 | "WeakSet", "WeakMethod"] |
| 31 | |
| 32 | |
| 33 | class WeakMethod(ref): |
| 34 | """ |
| 35 | A custom `weakref.ref` subclass which simulates a weak reference to |
| 36 | a bound method, working around the lifetime problem of bound methods. |
| 37 | """ |
| 38 | |
| 39 | __slots__ = "_func_ref", "_meth_type", "_alive", "__weakref__" |
| 40 | |
| 41 | def __new__(cls, meth, callback=None): |
| 42 | try: |
| 43 | obj = meth.__self__ |
| 44 | func = meth.__func__ |
| 45 | except AttributeError: |
| 46 | raise TypeError("argument should be a bound method, not {}" |
| 47 | .format(type(meth))) from None |
| 48 | def _cb(arg): |
| 49 | # The self-weakref trick is needed to avoid creating a reference |
| 50 | # cycle. |
| 51 | self = self_wr() |
| 52 | if self._alive: |
| 53 | self._alive = False |
| 54 | if callback is not None: |
| 55 | callback(self) |
| 56 | self = ref.__new__(cls, obj, _cb) |
| 57 | self._func_ref = ref(func, _cb) |
| 58 | self._meth_type = type(meth) |
| 59 | self._alive = True |
| 60 | self_wr = ref(self) |
| 61 | return self |
| 62 | |
| 63 | def __call__(self): |
| 64 | obj = super().__call__() |
| 65 | func = self._func_ref() |
| 66 | if obj is None or func is None: |
| 67 | return None |
| 68 | return self._meth_type(func, obj) |
| 69 | |
| 70 | def __eq__(self, other): |
| 71 | if isinstance(other, WeakMethod): |
| 72 | if not self._alive or not other._alive: |
| 73 | return self is other |
| 74 | return ref.__eq__(self, other) and self._func_ref == other._func_ref |
| 75 | return False |
| 76 | |
| 77 | def __ne__(self, other): |
| 78 | if isinstance(other, WeakMethod): |
| 79 | if not self._alive or not other._alive: |
| 80 | return self is not other |
| 81 | return ref.__ne__(self, other) or self._func_ref != other._func_ref |
| 82 | return True |
| 83 | |
| 84 | __hash__ = ref.__hash__ |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 85 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 86 | |
Raymond Hettinger | 7ac6095 | 2008-02-05 01:15:57 +0000 | [diff] [blame] | 87 | class WeakValueDictionary(collections.MutableMapping): |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 88 | """Mapping class that references values weakly. |
| 89 | |
| 90 | Entries in the dictionary will be discarded when no strong |
| 91 | reference to the value exists anymore |
| 92 | """ |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 93 | # We inherit the constructor without worrying about the input |
| 94 | # dictionary; since it uses our .update() method, we get the right |
Fred Drake | 9d2c85d | 2001-03-01 03:06:03 +0000 | [diff] [blame] | 95 | # checks (if the other dictionary is a WeakValueDictionary, |
| 96 | # objects are unwrapped on the way out, and we always wrap on the |
| 97 | # way in). |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 98 | |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 99 | def __init__(self, *args, **kw): |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 100 | def remove(wr, selfref=ref(self)): |
| 101 | self = selfref() |
| 102 | if self is not None: |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 103 | if self._iterating: |
| 104 | self._pending_removals.append(wr.key) |
| 105 | else: |
| 106 | del self.data[wr.key] |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 107 | self._remove = remove |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 108 | # A list of keys to be removed |
| 109 | self._pending_removals = [] |
| 110 | self._iterating = set() |
Raymond Hettinger | 7ac6095 | 2008-02-05 01:15:57 +0000 | [diff] [blame] | 111 | self.data = d = {} |
Antoine Pitrou | c06de47 | 2009-05-30 21:04:26 +0000 | [diff] [blame] | 112 | self.update(*args, **kw) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 113 | |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 114 | def _commit_removals(self): |
| 115 | l = self._pending_removals |
| 116 | d = self.data |
| 117 | # We shouldn't encounter any KeyError, because this method should |
| 118 | # always be called *before* mutating the dict. |
| 119 | while l: |
| 120 | del d[l.pop()] |
| 121 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 122 | def __getitem__(self, key): |
Fred Drake | 4fd06e0 | 2001-08-03 04:11:27 +0000 | [diff] [blame] | 123 | o = self.data[key]() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 124 | if o is None: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 125 | raise KeyError(key) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 126 | else: |
| 127 | return o |
| 128 | |
Raymond Hettinger | 7ac6095 | 2008-02-05 01:15:57 +0000 | [diff] [blame] | 129 | def __delitem__(self, key): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 130 | if self._pending_removals: |
| 131 | self._commit_removals() |
Raymond Hettinger | 7ac6095 | 2008-02-05 01:15:57 +0000 | [diff] [blame] | 132 | del self.data[key] |
| 133 | |
| 134 | def __len__(self): |
Antoine Pitrou | bbe2f60 | 2012-03-01 16:26:35 +0100 | [diff] [blame] | 135 | return len(self.data) - len(self._pending_removals) |
Raymond Hettinger | 7ac6095 | 2008-02-05 01:15:57 +0000 | [diff] [blame] | 136 | |
Raymond Hettinger | 6114679 | 2004-08-19 21:32:06 +0000 | [diff] [blame] | 137 | def __contains__(self, key): |
| 138 | try: |
| 139 | o = self.data[key]() |
| 140 | except KeyError: |
| 141 | return False |
| 142 | return o is not None |
| 143 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 144 | def __repr__(self): |
Fred Drake | 9d2c85d | 2001-03-01 03:06:03 +0000 | [diff] [blame] | 145 | return "<WeakValueDictionary at %s>" % id(self) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 146 | |
| 147 | def __setitem__(self, key, value): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 148 | if self._pending_removals: |
| 149 | self._commit_removals() |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 150 | self.data[key] = KeyedRef(value, self._remove, key) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 151 | |
| 152 | def copy(self): |
Fred Drake | 9d2c85d | 2001-03-01 03:06:03 +0000 | [diff] [blame] | 153 | new = WeakValueDictionary() |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 154 | for key, wr in self.data.items(): |
| 155 | o = wr() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 156 | if o is not None: |
| 157 | new[key] = o |
Fred Drake | 9d2c85d | 2001-03-01 03:06:03 +0000 | [diff] [blame] | 158 | return new |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 159 | |
Antoine Pitrou | 6e61006 | 2009-05-15 17:04:50 +0000 | [diff] [blame] | 160 | __copy__ = copy |
| 161 | |
| 162 | def __deepcopy__(self, memo): |
| 163 | from copy import deepcopy |
| 164 | new = self.__class__() |
| 165 | for key, wr in self.data.items(): |
| 166 | o = wr() |
| 167 | if o is not None: |
| 168 | new[deepcopy(key, memo)] = o |
| 169 | return new |
| 170 | |
Fred Drake | 1d9e4b7 | 2001-04-16 17:34:48 +0000 | [diff] [blame] | 171 | def get(self, key, default=None): |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 172 | try: |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 173 | wr = self.data[key] |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 174 | except KeyError: |
| 175 | return default |
| 176 | else: |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 177 | o = wr() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 178 | if o is None: |
| 179 | # This should only happen |
| 180 | return default |
| 181 | else: |
| 182 | return o |
| 183 | |
| 184 | def items(self): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 185 | with _IterationGuard(self): |
| 186 | for k, wr in self.data.items(): |
| 187 | v = wr() |
| 188 | if v is not None: |
| 189 | yield k, v |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 190 | |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 191 | def keys(self): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 192 | with _IterationGuard(self): |
| 193 | for k, wr in self.data.items(): |
| 194 | if wr() is not None: |
| 195 | yield k |
Raymond Hettinger | 6114679 | 2004-08-19 21:32:06 +0000 | [diff] [blame] | 196 | |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 197 | __iter__ = keys |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 198 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 199 | def itervaluerefs(self): |
| 200 | """Return an iterator that yields the weak references to the values. |
| 201 | |
| 202 | The references are not guaranteed to be 'live' at the time |
| 203 | they are used, so the result of calling the references needs |
| 204 | to be checked before being used. This can be used to avoid |
| 205 | creating references that will cause the garbage collector to |
| 206 | keep the values around longer than needed. |
| 207 | |
| 208 | """ |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 209 | with _IterationGuard(self): |
Philip Jenvey | 4993cc0 | 2012-10-01 12:53:43 -0700 | [diff] [blame] | 210 | yield from self.data.values() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 211 | |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 212 | def values(self): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 213 | with _IterationGuard(self): |
| 214 | for wr in self.data.values(): |
| 215 | obj = wr() |
| 216 | if obj is not None: |
| 217 | yield obj |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 218 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 219 | def popitem(self): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 220 | if self._pending_removals: |
| 221 | self._commit_removals() |
Georg Brandl | bd87d08 | 2010-12-03 07:49:09 +0000 | [diff] [blame] | 222 | while True: |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 223 | key, wr = self.data.popitem() |
| 224 | o = wr() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 225 | if o is not None: |
| 226 | return key, o |
| 227 | |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 228 | def pop(self, key, *args): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 229 | if self._pending_removals: |
| 230 | self._commit_removals() |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 231 | try: |
| 232 | o = self.data.pop(key)() |
| 233 | except KeyError: |
| 234 | if args: |
| 235 | return args[0] |
| 236 | raise |
| 237 | if o is None: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 238 | raise KeyError(key) |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 239 | else: |
| 240 | return o |
| 241 | |
Walter Dörwald | 80ce6dd | 2004-05-27 18:16:25 +0000 | [diff] [blame] | 242 | def setdefault(self, key, default=None): |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 243 | try: |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 244 | wr = self.data[key] |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 245 | except KeyError: |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 246 | if self._pending_removals: |
| 247 | self._commit_removals() |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 248 | self.data[key] = KeyedRef(default, self._remove, key) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 249 | return default |
| 250 | else: |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 251 | return wr() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 252 | |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 253 | def update(self, dict=None, **kwargs): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 254 | if self._pending_removals: |
| 255 | self._commit_removals() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 256 | d = self.data |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 257 | if dict is not None: |
| 258 | if not hasattr(dict, "items"): |
| 259 | dict = type({})(dict) |
| 260 | for key, o in dict.items(): |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 261 | d[key] = KeyedRef(o, self._remove, key) |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 262 | if len(kwargs): |
| 263 | self.update(kwargs) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 264 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 265 | def valuerefs(self): |
| 266 | """Return a list of weak references to the values. |
| 267 | |
| 268 | The references are not guaranteed to be 'live' at the time |
| 269 | they are used, so the result of calling the references needs |
| 270 | to be checked before being used. This can be used to avoid |
| 271 | creating references that will cause the garbage collector to |
| 272 | keep the values around longer than needed. |
| 273 | |
| 274 | """ |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 275 | return list(self.data.values()) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 276 | |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 277 | |
| 278 | class KeyedRef(ref): |
| 279 | """Specialized reference that includes a key corresponding to the value. |
| 280 | |
| 281 | This is used in the WeakValueDictionary to avoid having to create |
| 282 | a function object for each key stored in the mapping. A shared |
| 283 | callback object can use the 'key' attribute of a KeyedRef instead |
| 284 | of getting a reference to the key from an enclosing scope. |
| 285 | |
| 286 | """ |
| 287 | |
| 288 | __slots__ = "key", |
| 289 | |
| 290 | def __new__(type, ob, callback, key): |
| 291 | self = ref.__new__(type, ob, callback) |
| 292 | self.key = key |
| 293 | return self |
| 294 | |
| 295 | def __init__(self, ob, callback, key): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 296 | super().__init__(ob, callback) |
Fred Drake | 746fe0f | 2001-09-28 19:01:26 +0000 | [diff] [blame] | 297 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 298 | |
Raymond Hettinger | 7ac6095 | 2008-02-05 01:15:57 +0000 | [diff] [blame] | 299 | class WeakKeyDictionary(collections.MutableMapping): |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 300 | """ Mapping class that references keys weakly. |
| 301 | |
| 302 | Entries in the dictionary will be discarded when there is no |
| 303 | longer a strong reference to the key. This can be used to |
| 304 | associate additional data with an object owned by other parts of |
| 305 | an application without adding attributes to those objects. This |
| 306 | can be especially useful with objects that override attribute |
| 307 | accesses. |
| 308 | """ |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 309 | |
| 310 | def __init__(self, dict=None): |
| 311 | self.data = {} |
Fred Drake | 746fe0f | 2001-09-28 19:01:26 +0000 | [diff] [blame] | 312 | def remove(k, selfref=ref(self)): |
| 313 | self = selfref() |
| 314 | if self is not None: |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 315 | if self._iterating: |
| 316 | self._pending_removals.append(k) |
| 317 | else: |
| 318 | del self.data[k] |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 319 | self._remove = remove |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 320 | # A list of dead weakrefs (keys to be removed) |
| 321 | self._pending_removals = [] |
| 322 | self._iterating = set() |
| 323 | if dict is not None: |
| 324 | self.update(dict) |
| 325 | |
| 326 | def _commit_removals(self): |
| 327 | # NOTE: We don't need to call this method before mutating the dict, |
| 328 | # because a dead weakref never compares equal to a live weakref, |
| 329 | # even if they happened to refer to equal objects. |
| 330 | # However, it means keys may already have been removed. |
| 331 | l = self._pending_removals |
| 332 | d = self.data |
| 333 | while l: |
| 334 | try: |
| 335 | del d[l.pop()] |
| 336 | except KeyError: |
| 337 | pass |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 338 | |
Fred Drake | b663a2c | 2001-09-06 14:51:01 +0000 | [diff] [blame] | 339 | def __delitem__(self, key): |
Tim Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 340 | del self.data[ref(key)] |
Fred Drake | b663a2c | 2001-09-06 14:51:01 +0000 | [diff] [blame] | 341 | |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 342 | def __getitem__(self, key): |
| 343 | return self.data[ref(key)] |
| 344 | |
Raymond Hettinger | 7ac6095 | 2008-02-05 01:15:57 +0000 | [diff] [blame] | 345 | def __len__(self): |
Antoine Pitrou | bbe2f60 | 2012-03-01 16:26:35 +0100 | [diff] [blame] | 346 | return len(self.data) - len(self._pending_removals) |
Raymond Hettinger | 7ac6095 | 2008-02-05 01:15:57 +0000 | [diff] [blame] | 347 | |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 348 | def __repr__(self): |
| 349 | return "<WeakKeyDictionary at %s>" % id(self) |
| 350 | |
| 351 | def __setitem__(self, key, value): |
| 352 | self.data[ref(key, self._remove)] = value |
| 353 | |
| 354 | def copy(self): |
| 355 | new = WeakKeyDictionary() |
| 356 | for key, value in self.data.items(): |
| 357 | o = key() |
| 358 | if o is not None: |
| 359 | new[o] = value |
Fred Drake | 9d2c85d | 2001-03-01 03:06:03 +0000 | [diff] [blame] | 360 | return new |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 361 | |
Antoine Pitrou | 6e61006 | 2009-05-15 17:04:50 +0000 | [diff] [blame] | 362 | __copy__ = copy |
| 363 | |
| 364 | def __deepcopy__(self, memo): |
| 365 | from copy import deepcopy |
| 366 | new = self.__class__() |
| 367 | for key, value in self.data.items(): |
| 368 | o = key() |
| 369 | if o is not None: |
| 370 | new[o] = deepcopy(value, memo) |
| 371 | return new |
| 372 | |
Fred Drake | 1d9e4b7 | 2001-04-16 17:34:48 +0000 | [diff] [blame] | 373 | def get(self, key, default=None): |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 374 | return self.data.get(ref(key),default) |
| 375 | |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 376 | def __contains__(self, key): |
| 377 | try: |
| 378 | wr = ref(key) |
| 379 | except TypeError: |
Georg Brandl | bd87d08 | 2010-12-03 07:49:09 +0000 | [diff] [blame] | 380 | return False |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 381 | return wr in self.data |
Tim Peters | c411dba | 2002-07-16 21:35:23 +0000 | [diff] [blame] | 382 | |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 383 | def items(self): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 384 | with _IterationGuard(self): |
| 385 | for wr, value in self.data.items(): |
| 386 | key = wr() |
| 387 | if key is not None: |
| 388 | yield key, value |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 389 | |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 390 | def keys(self): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 391 | with _IterationGuard(self): |
| 392 | for wr in self.data: |
| 393 | obj = wr() |
| 394 | if obj is not None: |
| 395 | yield obj |
Raymond Hettinger | 6114679 | 2004-08-19 21:32:06 +0000 | [diff] [blame] | 396 | |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 397 | __iter__ = keys |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 398 | |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 399 | def values(self): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 400 | with _IterationGuard(self): |
| 401 | for wr, value in self.data.items(): |
| 402 | if wr() is not None: |
| 403 | yield value |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 404 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 405 | def keyrefs(self): |
| 406 | """Return a list of weak references to the keys. |
| 407 | |
| 408 | The references are not guaranteed to be 'live' at the time |
| 409 | they are used, so the result of calling the references needs |
| 410 | to be checked before being used. This can be used to avoid |
| 411 | creating references that will cause the garbage collector to |
| 412 | keep the keys around longer than needed. |
| 413 | |
| 414 | """ |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 415 | return list(self.data) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 416 | |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 417 | def popitem(self): |
Georg Brandl | bd87d08 | 2010-12-03 07:49:09 +0000 | [diff] [blame] | 418 | while True: |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 419 | key, value = self.data.popitem() |
| 420 | o = key() |
| 421 | if o is not None: |
| 422 | return o, value |
| 423 | |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 424 | def pop(self, key, *args): |
| 425 | return self.data.pop(ref(key), *args) |
| 426 | |
Walter Dörwald | 80ce6dd | 2004-05-27 18:16:25 +0000 | [diff] [blame] | 427 | def setdefault(self, key, default=None): |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 428 | return self.data.setdefault(ref(key, self._remove),default) |
| 429 | |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 430 | def update(self, dict=None, **kwargs): |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 431 | d = self.data |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 432 | if dict is not None: |
| 433 | if not hasattr(dict, "items"): |
| 434 | dict = type({})(dict) |
| 435 | for key, value in dict.items(): |
| 436 | d[ref(key, self._remove)] = value |
| 437 | if len(kwargs): |
| 438 | self.update(kwargs) |