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