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