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: |
Antoine Pitrou | 805f283 | 2016-12-19 11:12:58 +0100 | [diff] [blame] | 205 | o = None |
| 206 | if o is None: |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 207 | if args: |
| 208 | return args[0] |
Antoine Pitrou | 805f283 | 2016-12-19 11:12:58 +0100 | [diff] [blame] | 209 | else: |
| 210 | raise KeyError, key |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 211 | else: |
| 212 | return o |
| 213 | |
Walter Dörwald | 80ce6dd | 2004-05-27 18:16:25 +0000 | [diff] [blame] | 214 | def setdefault(self, key, default=None): |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 215 | try: |
Antoine Pitrou | 805f283 | 2016-12-19 11:12:58 +0100 | [diff] [blame] | 216 | o = self.data[key]() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 217 | except KeyError: |
Antoine Pitrou | 805f283 | 2016-12-19 11:12:58 +0100 | [diff] [blame] | 218 | o = None |
| 219 | if o is None: |
Kristján Valur Jónsson | 222b284 | 2013-12-05 10:03:45 +0000 | [diff] [blame] | 220 | if self._pending_removals: |
| 221 | self._commit_removals() |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 222 | self.data[key] = KeyedRef(default, self._remove, key) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 223 | return default |
| 224 | else: |
Antoine Pitrou | 805f283 | 2016-12-19 11:12:58 +0100 | [diff] [blame] | 225 | return o |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 226 | |
Serhiy Storchaka | f522bbc | 2015-09-29 23:51:27 +0300 | [diff] [blame] | 227 | def update(*args, **kwargs): |
| 228 | if not args: |
| 229 | raise TypeError("descriptor 'update' of 'WeakValueDictionary' " |
| 230 | "object needs an argument") |
| 231 | self = args[0] |
| 232 | args = args[1:] |
| 233 | if len(args) > 1: |
| 234 | raise TypeError('expected at most 1 arguments, got %d' % len(args)) |
| 235 | dict = args[0] if args else None |
Kristján Valur Jónsson | 222b284 | 2013-12-05 10:03:45 +0000 | [diff] [blame] | 236 | if self._pending_removals: |
| 237 | self._commit_removals() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 238 | d = self.data |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 239 | if dict is not None: |
| 240 | if not hasattr(dict, "items"): |
| 241 | dict = type({})(dict) |
| 242 | for key, o in dict.items(): |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 243 | d[key] = KeyedRef(o, self._remove, key) |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 244 | if len(kwargs): |
| 245 | self.update(kwargs) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 246 | |
Fred Drake | 017e68c | 2006-05-02 06:53:59 +0000 | [diff] [blame] | 247 | def valuerefs(self): |
| 248 | """Return a list of weak references to the values. |
| 249 | |
| 250 | The references are not guaranteed to be 'live' at the time |
| 251 | they are used, so the result of calling the references needs |
| 252 | to be checked before being used. This can be used to avoid |
| 253 | creating references that will cause the garbage collector to |
| 254 | keep the values around longer than needed. |
| 255 | |
| 256 | """ |
| 257 | return self.data.values() |
| 258 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 259 | def values(self): |
| 260 | L = [] |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 261 | for wr in self.data.values(): |
| 262 | o = wr() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 263 | if o is not None: |
| 264 | L.append(o) |
| 265 | return L |
| 266 | |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 267 | |
| 268 | class KeyedRef(ref): |
| 269 | """Specialized reference that includes a key corresponding to the value. |
| 270 | |
| 271 | This is used in the WeakValueDictionary to avoid having to create |
| 272 | a function object for each key stored in the mapping. A shared |
| 273 | callback object can use the 'key' attribute of a KeyedRef instead |
| 274 | of getting a reference to the key from an enclosing scope. |
| 275 | |
| 276 | """ |
| 277 | |
| 278 | __slots__ = "key", |
| 279 | |
| 280 | def __new__(type, ob, callback, key): |
| 281 | self = ref.__new__(type, ob, callback) |
| 282 | self.key = key |
| 283 | return self |
| 284 | |
| 285 | def __init__(self, ob, callback, key): |
| 286 | super(KeyedRef, self).__init__(ob, callback) |
Fred Drake | 746fe0f | 2001-09-28 19:01:26 +0000 | [diff] [blame] | 287 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 288 | |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 289 | class WeakKeyDictionary(UserDict.UserDict): |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 290 | """ Mapping class that references keys weakly. |
| 291 | |
| 292 | Entries in the dictionary will be discarded when there is no |
| 293 | longer a strong reference to the key. This can be used to |
| 294 | associate additional data with an object owned by other parts of |
| 295 | an application without adding attributes to those objects. This |
| 296 | can be especially useful with objects that override attribute |
| 297 | accesses. |
| 298 | """ |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 299 | |
| 300 | def __init__(self, dict=None): |
| 301 | self.data = {} |
Fred Drake | 746fe0f | 2001-09-28 19:01:26 +0000 | [diff] [blame] | 302 | def remove(k, selfref=ref(self)): |
| 303 | self = selfref() |
| 304 | if self is not None: |
Kristján Valur Jónsson | 222b284 | 2013-12-05 10:03:45 +0000 | [diff] [blame] | 305 | if self._iterating: |
| 306 | self._pending_removals.append(k) |
| 307 | else: |
| 308 | del self.data[k] |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 309 | self._remove = remove |
Kristján Valur Jónsson | 222b284 | 2013-12-05 10:03:45 +0000 | [diff] [blame] | 310 | # A list of dead weakrefs (keys to be removed) |
| 311 | self._pending_removals = [] |
| 312 | self._iterating = set() |
| 313 | if dict is not None: |
| 314 | self.update(dict) |
| 315 | |
| 316 | def _commit_removals(self): |
| 317 | # NOTE: We don't need to call this method before mutating the dict, |
| 318 | # because a dead weakref never compares equal to a live weakref, |
| 319 | # even if they happened to refer to equal objects. |
| 320 | # However, it means keys may already have been removed. |
| 321 | l = self._pending_removals |
| 322 | d = self.data |
| 323 | while l: |
| 324 | try: |
| 325 | del d[l.pop()] |
| 326 | except KeyError: |
| 327 | pass |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 328 | |
Fred Drake | b663a2c | 2001-09-06 14:51:01 +0000 | [diff] [blame] | 329 | def __delitem__(self, key): |
Tim Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 330 | del self.data[ref(key)] |
Fred Drake | b663a2c | 2001-09-06 14:51:01 +0000 | [diff] [blame] | 331 | |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 332 | def __getitem__(self, key): |
| 333 | return self.data[ref(key)] |
| 334 | |
| 335 | def __repr__(self): |
| 336 | return "<WeakKeyDictionary at %s>" % id(self) |
| 337 | |
| 338 | def __setitem__(self, key, value): |
| 339 | self.data[ref(key, self._remove)] = value |
| 340 | |
| 341 | def copy(self): |
| 342 | new = WeakKeyDictionary() |
| 343 | for key, value in self.data.items(): |
| 344 | o = key() |
| 345 | if o is not None: |
| 346 | new[o] = value |
Fred Drake | 9d2c85d | 2001-03-01 03:06:03 +0000 | [diff] [blame] | 347 | return new |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 348 | |
Antoine Pitrou | 775fd66 | 2009-05-15 16:54:52 +0000 | [diff] [blame] | 349 | __copy__ = copy |
| 350 | |
| 351 | def __deepcopy__(self, memo): |
| 352 | from copy import deepcopy |
| 353 | new = self.__class__() |
| 354 | for key, value in self.data.items(): |
| 355 | o = key() |
| 356 | if o is not None: |
| 357 | new[o] = deepcopy(value, memo) |
| 358 | return new |
| 359 | |
Fred Drake | 1d9e4b7 | 2001-04-16 17:34:48 +0000 | [diff] [blame] | 360 | def get(self, key, default=None): |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 361 | return self.data.get(ref(key),default) |
| 362 | |
Fred Drake | 1d9e4b7 | 2001-04-16 17:34:48 +0000 | [diff] [blame] | 363 | def has_key(self, key): |
Fred Drake | 3bae7dd | 2001-11-06 16:36:53 +0000 | [diff] [blame] | 364 | try: |
| 365 | wr = ref(key) |
| 366 | except TypeError: |
| 367 | return 0 |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 368 | return wr in self.data |
Fred Drake | 1d9e4b7 | 2001-04-16 17:34:48 +0000 | [diff] [blame] | 369 | |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 370 | def __contains__(self, key): |
| 371 | try: |
| 372 | wr = ref(key) |
| 373 | except TypeError: |
| 374 | return 0 |
| 375 | return wr in self.data |
Tim Peters | c411dba | 2002-07-16 21:35:23 +0000 | [diff] [blame] | 376 | |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 377 | def items(self): |
| 378 | L = [] |
| 379 | for key, value in self.data.items(): |
| 380 | o = key() |
| 381 | if o is not None: |
| 382 | L.append((o, value)) |
| 383 | return L |
| 384 | |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 385 | def iteritems(self): |
Kristján Valur Jónsson | 222b284 | 2013-12-05 10:03:45 +0000 | [diff] [blame] | 386 | with _IterationGuard(self): |
| 387 | for wr, value in self.data.iteritems(): |
| 388 | key = wr() |
| 389 | if key is not None: |
| 390 | yield key, value |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 391 | |
Fred Drake | 017e68c | 2006-05-02 06:53:59 +0000 | [diff] [blame] | 392 | def iterkeyrefs(self): |
| 393 | """Return an iterator that yields the weak references to the keys. |
| 394 | |
| 395 | The references are not guaranteed to be 'live' at the time |
| 396 | they are used, so the result of calling the references needs |
| 397 | to be checked before being used. This can be used to avoid |
| 398 | creating references that will cause the garbage collector to |
| 399 | keep the keys around longer than needed. |
| 400 | |
| 401 | """ |
Kristján Valur Jónsson | 222b284 | 2013-12-05 10:03:45 +0000 | [diff] [blame] | 402 | with _IterationGuard(self): |
| 403 | for wr in self.data.iterkeys(): |
| 404 | yield wr |
Fred Drake | 017e68c | 2006-05-02 06:53:59 +0000 | [diff] [blame] | 405 | |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 406 | def iterkeys(self): |
Kristján Valur Jónsson | 222b284 | 2013-12-05 10:03:45 +0000 | [diff] [blame] | 407 | with _IterationGuard(self): |
| 408 | for wr in self.data.iterkeys(): |
| 409 | obj = wr() |
| 410 | if obj is not None: |
| 411 | yield obj |
Raymond Hettinger | 6114679 | 2004-08-19 21:32:06 +0000 | [diff] [blame] | 412 | |
Kristján Valur Jónsson | 222b284 | 2013-12-05 10:03:45 +0000 | [diff] [blame] | 413 | __iter__ = iterkeys |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 414 | |
| 415 | def itervalues(self): |
Kristján Valur Jónsson | 222b284 | 2013-12-05 10:03:45 +0000 | [diff] [blame] | 416 | with _IterationGuard(self): |
| 417 | for value in self.data.itervalues(): |
| 418 | yield value |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 419 | |
Fred Drake | 017e68c | 2006-05-02 06:53:59 +0000 | [diff] [blame] | 420 | def keyrefs(self): |
| 421 | """Return a list of weak references to the keys. |
| 422 | |
| 423 | The references are not guaranteed to be 'live' at the time |
| 424 | they are used, so the result of calling the references needs |
| 425 | to be checked before being used. This can be used to avoid |
| 426 | creating references that will cause the garbage collector to |
| 427 | keep the keys around longer than needed. |
| 428 | |
| 429 | """ |
| 430 | return self.data.keys() |
| 431 | |
Fred Drake | 1d9e4b7 | 2001-04-16 17:34:48 +0000 | [diff] [blame] | 432 | def keys(self): |
| 433 | L = [] |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 434 | for wr in self.data.keys(): |
| 435 | o = wr() |
Fred Drake | 1d9e4b7 | 2001-04-16 17:34:48 +0000 | [diff] [blame] | 436 | if o is not None: |
| 437 | L.append(o) |
| 438 | return L |
| 439 | |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 440 | def popitem(self): |
| 441 | while 1: |
| 442 | key, value = self.data.popitem() |
| 443 | o = key() |
| 444 | if o is not None: |
| 445 | return o, value |
| 446 | |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 447 | def pop(self, key, *args): |
| 448 | return self.data.pop(ref(key), *args) |
| 449 | |
Walter Dörwald | 80ce6dd | 2004-05-27 18:16:25 +0000 | [diff] [blame] | 450 | def setdefault(self, key, default=None): |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 451 | return self.data.setdefault(ref(key, self._remove),default) |
| 452 | |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 453 | def update(self, dict=None, **kwargs): |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 454 | d = self.data |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 455 | if dict is not None: |
| 456 | if not hasattr(dict, "items"): |
| 457 | dict = type({})(dict) |
| 458 | for key, value in dict.items(): |
| 459 | d[ref(key, self._remove)] = value |
| 460 | if len(kwargs): |
| 461 | self.update(kwargs) |