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