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, |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 19 | ReferenceType, |
| 20 | _remove_dead_weakref) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 21 | |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 22 | from _weakrefset import WeakSet, _IterationGuard |
Fred Drake | e029242 | 2001-10-05 21:54:09 +0000 | [diff] [blame] | 23 | |
Serhiy Storchaka | 8110837 | 2017-09-26 00:55:55 +0300 | [diff] [blame] | 24 | import _collections_abc # Import after _weakref to avoid circular import. |
Richard Oudkerk | 7a3dae0 | 2013-05-05 23:05:00 +0100 | [diff] [blame] | 25 | import sys |
| 26 | import itertools |
Brett Cannon | 663fffa | 2009-03-25 23:31:22 +0000 | [diff] [blame] | 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", |
Gregory P. Smith | 7d10c2b | 2008-08-18 03:41:46 +0000 | [diff] [blame] | 31 | "WeakKeyDictionary", "ReferenceType", "ProxyType", |
Raymond Hettinger | 93fa608 | 2008-02-05 00:20:01 +0000 | [diff] [blame] | 32 | "CallableProxyType", "ProxyTypes", "WeakValueDictionary", |
Richard Oudkerk | 7a3dae0 | 2013-05-05 23:05:00 +0100 | [diff] [blame] | 33 | "WeakSet", "WeakMethod", "finalize"] |
Antoine Pitrou | c3afba1 | 2012-11-17 18:57:38 +0100 | [diff] [blame] | 34 | |
| 35 | |
Raymond Hettinger | 84ac437 | 2019-11-10 20:12:04 -0800 | [diff] [blame] | 36 | _collections_abc.Set.register(WeakSet) |
| 37 | _collections_abc.MutableSet.register(WeakSet) |
| 38 | |
Antoine Pitrou | c3afba1 | 2012-11-17 18:57:38 +0100 | [diff] [blame] | 39 | class WeakMethod(ref): |
| 40 | """ |
| 41 | A custom `weakref.ref` subclass which simulates a weak reference to |
| 42 | a bound method, working around the lifetime problem of bound methods. |
| 43 | """ |
| 44 | |
| 45 | __slots__ = "_func_ref", "_meth_type", "_alive", "__weakref__" |
| 46 | |
| 47 | def __new__(cls, meth, callback=None): |
| 48 | try: |
| 49 | obj = meth.__self__ |
| 50 | func = meth.__func__ |
| 51 | except AttributeError: |
| 52 | raise TypeError("argument should be a bound method, not {}" |
| 53 | .format(type(meth))) from None |
| 54 | def _cb(arg): |
| 55 | # The self-weakref trick is needed to avoid creating a reference |
| 56 | # cycle. |
| 57 | self = self_wr() |
| 58 | if self._alive: |
| 59 | self._alive = False |
| 60 | if callback is not None: |
| 61 | callback(self) |
| 62 | self = ref.__new__(cls, obj, _cb) |
| 63 | self._func_ref = ref(func, _cb) |
| 64 | self._meth_type = type(meth) |
| 65 | self._alive = True |
| 66 | self_wr = ref(self) |
| 67 | return self |
| 68 | |
| 69 | def __call__(self): |
| 70 | obj = super().__call__() |
| 71 | func = self._func_ref() |
| 72 | if obj is None or func is None: |
| 73 | return None |
| 74 | return self._meth_type(func, obj) |
| 75 | |
| 76 | def __eq__(self, other): |
| 77 | if isinstance(other, WeakMethod): |
| 78 | if not self._alive or not other._alive: |
| 79 | return self is other |
| 80 | return ref.__eq__(self, other) and self._func_ref == other._func_ref |
Serhiy Storchaka | 662db12 | 2019-08-08 08:42:54 +0300 | [diff] [blame] | 81 | return NotImplemented |
Antoine Pitrou | c3afba1 | 2012-11-17 18:57:38 +0100 | [diff] [blame] | 82 | |
| 83 | def __ne__(self, other): |
| 84 | if isinstance(other, WeakMethod): |
| 85 | if not self._alive or not other._alive: |
| 86 | return self is not other |
| 87 | return ref.__ne__(self, other) or self._func_ref != other._func_ref |
Serhiy Storchaka | 662db12 | 2019-08-08 08:42:54 +0300 | [diff] [blame] | 88 | return NotImplemented |
Antoine Pitrou | c3afba1 | 2012-11-17 18:57:38 +0100 | [diff] [blame] | 89 | |
| 90 | __hash__ = ref.__hash__ |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 91 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 92 | |
Serhiy Storchaka | 8110837 | 2017-09-26 00:55:55 +0300 | [diff] [blame] | 93 | class WeakValueDictionary(_collections_abc.MutableMapping): |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 94 | """Mapping class that references values weakly. |
| 95 | |
| 96 | Entries in the dictionary will be discarded when no strong |
| 97 | reference to the value exists anymore |
| 98 | """ |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 99 | # We inherit the constructor without worrying about the input |
| 100 | # dictionary; since it uses our .update() method, we get the right |
Fred Drake | 9d2c85d | 2001-03-01 03:06:03 +0000 | [diff] [blame] | 101 | # checks (if the other dictionary is a WeakValueDictionary, |
| 102 | # objects are unwrapped on the way out, and we always wrap on the |
| 103 | # way in). |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 104 | |
Serhiy Storchaka | 2085bd0 | 2019-06-01 11:00:15 +0300 | [diff] [blame] | 105 | def __init__(self, other=(), /, **kw): |
Ćukasz Langa | 9cd7e17 | 2017-02-10 00:14:55 -0800 | [diff] [blame] | 106 | def remove(wr, selfref=ref(self), _atomic_removal=_remove_dead_weakref): |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 107 | self = selfref() |
| 108 | if self is not None: |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 109 | if self._iterating: |
| 110 | self._pending_removals.append(wr.key) |
| 111 | else: |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 112 | # Atomic removal is necessary since this function |
| 113 | # can be called asynchronously by the GC |
Victor Stinner | a2af05a | 2019-09-09 16:55:58 +0200 | [diff] [blame] | 114 | _atomic_removal(self.data, wr.key) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 115 | self._remove = remove |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 116 | # A list of keys to be removed |
| 117 | self._pending_removals = [] |
| 118 | self._iterating = set() |
Victor Stinner | a2af05a | 2019-09-09 16:55:58 +0200 | [diff] [blame] | 119 | self.data = {} |
Serhiy Storchaka | 2085bd0 | 2019-06-01 11:00:15 +0300 | [diff] [blame] | 120 | self.update(other, **kw) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 121 | |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 122 | def _commit_removals(self): |
| 123 | l = self._pending_removals |
| 124 | d = self.data |
| 125 | # We shouldn't encounter any KeyError, because this method should |
| 126 | # always be called *before* mutating the dict. |
| 127 | while l: |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 128 | key = l.pop() |
| 129 | _remove_dead_weakref(d, key) |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 130 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 131 | def __getitem__(self, key): |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 132 | if self._pending_removals: |
| 133 | self._commit_removals() |
Fred Drake | 4fd06e0 | 2001-08-03 04:11:27 +0000 | [diff] [blame] | 134 | o = self.data[key]() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 135 | if o is None: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 136 | raise KeyError(key) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 137 | else: |
| 138 | return o |
| 139 | |
Raymond Hettinger | 7ac6095 | 2008-02-05 01:15:57 +0000 | [diff] [blame] | 140 | def __delitem__(self, key): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 141 | if self._pending_removals: |
| 142 | self._commit_removals() |
Raymond Hettinger | 7ac6095 | 2008-02-05 01:15:57 +0000 | [diff] [blame] | 143 | del self.data[key] |
| 144 | |
| 145 | def __len__(self): |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 146 | if self._pending_removals: |
| 147 | self._commit_removals() |
| 148 | return len(self.data) |
Raymond Hettinger | 7ac6095 | 2008-02-05 01:15:57 +0000 | [diff] [blame] | 149 | |
Raymond Hettinger | 6114679 | 2004-08-19 21:32:06 +0000 | [diff] [blame] | 150 | def __contains__(self, key): |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 151 | if self._pending_removals: |
| 152 | self._commit_removals() |
Raymond Hettinger | 6114679 | 2004-08-19 21:32:06 +0000 | [diff] [blame] | 153 | try: |
| 154 | o = self.data[key]() |
| 155 | except KeyError: |
| 156 | return False |
| 157 | return o is not None |
| 158 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 159 | def __repr__(self): |
Serhiy Storchaka | 465e60e | 2014-07-25 23:36:00 +0300 | [diff] [blame] | 160 | return "<%s at %#x>" % (self.__class__.__name__, id(self)) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 161 | |
| 162 | def __setitem__(self, key, value): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 163 | if self._pending_removals: |
| 164 | self._commit_removals() |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 165 | self.data[key] = KeyedRef(value, self._remove, key) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 166 | |
| 167 | def copy(self): |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 168 | if self._pending_removals: |
| 169 | self._commit_removals() |
Fred Drake | 9d2c85d | 2001-03-01 03:06:03 +0000 | [diff] [blame] | 170 | new = WeakValueDictionary() |
Fish | 96d37db | 2019-02-07 14:51:59 -0500 | [diff] [blame] | 171 | with _IterationGuard(self): |
| 172 | for key, wr in self.data.items(): |
| 173 | o = wr() |
| 174 | if o is not None: |
| 175 | new[key] = o |
Fred Drake | 9d2c85d | 2001-03-01 03:06:03 +0000 | [diff] [blame] | 176 | return new |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 177 | |
Antoine Pitrou | 6e61006 | 2009-05-15 17:04:50 +0000 | [diff] [blame] | 178 | __copy__ = copy |
| 179 | |
| 180 | def __deepcopy__(self, memo): |
| 181 | from copy import deepcopy |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 182 | if self._pending_removals: |
| 183 | self._commit_removals() |
Antoine Pitrou | 6e61006 | 2009-05-15 17:04:50 +0000 | [diff] [blame] | 184 | new = self.__class__() |
Fish | 96d37db | 2019-02-07 14:51:59 -0500 | [diff] [blame] | 185 | with _IterationGuard(self): |
| 186 | for key, wr in self.data.items(): |
| 187 | o = wr() |
| 188 | if o is not None: |
| 189 | new[deepcopy(key, memo)] = o |
Antoine Pitrou | 6e61006 | 2009-05-15 17:04:50 +0000 | [diff] [blame] | 190 | return new |
| 191 | |
Fred Drake | 1d9e4b7 | 2001-04-16 17:34:48 +0000 | [diff] [blame] | 192 | def get(self, key, default=None): |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 193 | if self._pending_removals: |
| 194 | self._commit_removals() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 195 | try: |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 196 | wr = self.data[key] |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 197 | except KeyError: |
| 198 | return default |
| 199 | else: |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 200 | o = wr() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 201 | if o is None: |
| 202 | # This should only happen |
| 203 | return default |
| 204 | else: |
| 205 | return o |
| 206 | |
| 207 | def items(self): |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 208 | if self._pending_removals: |
| 209 | self._commit_removals() |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 210 | with _IterationGuard(self): |
| 211 | for k, wr in self.data.items(): |
| 212 | v = wr() |
| 213 | if v is not None: |
| 214 | yield k, v |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 215 | |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 216 | def keys(self): |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 217 | if self._pending_removals: |
| 218 | self._commit_removals() |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 219 | with _IterationGuard(self): |
| 220 | for k, wr in self.data.items(): |
| 221 | if wr() is not None: |
| 222 | yield k |
Raymond Hettinger | 6114679 | 2004-08-19 21:32:06 +0000 | [diff] [blame] | 223 | |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 224 | __iter__ = keys |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 225 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 226 | def itervaluerefs(self): |
| 227 | """Return an iterator that yields the weak references to the values. |
| 228 | |
| 229 | The references are not guaranteed to be 'live' at the time |
| 230 | they are used, so the result of calling the references needs |
| 231 | to be checked before being used. This can be used to avoid |
| 232 | creating references that will cause the garbage collector to |
| 233 | keep the values around longer than needed. |
| 234 | |
| 235 | """ |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 236 | if self._pending_removals: |
| 237 | self._commit_removals() |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 238 | with _IterationGuard(self): |
Philip Jenvey | 4993cc0 | 2012-10-01 12:53:43 -0700 | [diff] [blame] | 239 | yield from self.data.values() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 240 | |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 241 | def values(self): |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 242 | if self._pending_removals: |
| 243 | self._commit_removals() |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 244 | with _IterationGuard(self): |
| 245 | for wr in self.data.values(): |
| 246 | obj = wr() |
| 247 | if obj is not None: |
| 248 | yield obj |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 249 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 250 | def popitem(self): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 251 | if self._pending_removals: |
| 252 | self._commit_removals() |
Georg Brandl | bd87d08 | 2010-12-03 07:49:09 +0000 | [diff] [blame] | 253 | while True: |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 254 | key, wr = self.data.popitem() |
| 255 | o = wr() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 256 | if o is not None: |
| 257 | return key, o |
| 258 | |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 259 | def pop(self, key, *args): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 260 | if self._pending_removals: |
| 261 | self._commit_removals() |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 262 | try: |
| 263 | o = self.data.pop(key)() |
| 264 | except KeyError: |
Antoine Pitrou | c1ee488 | 2016-12-19 10:56:40 +0100 | [diff] [blame] | 265 | o = None |
| 266 | if o is None: |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 267 | if args: |
| 268 | return args[0] |
Antoine Pitrou | c1ee488 | 2016-12-19 10:56:40 +0100 | [diff] [blame] | 269 | else: |
| 270 | raise KeyError(key) |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 271 | else: |
| 272 | return o |
| 273 | |
Walter Dörwald | 80ce6dd | 2004-05-27 18:16:25 +0000 | [diff] [blame] | 274 | def setdefault(self, key, default=None): |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 275 | try: |
Antoine Pitrou | c1ee488 | 2016-12-19 10:56:40 +0100 | [diff] [blame] | 276 | o = self.data[key]() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 277 | except KeyError: |
Antoine Pitrou | c1ee488 | 2016-12-19 10:56:40 +0100 | [diff] [blame] | 278 | o = None |
| 279 | if o is None: |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 280 | if self._pending_removals: |
| 281 | self._commit_removals() |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 282 | self.data[key] = KeyedRef(default, self._remove, key) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 283 | return default |
| 284 | else: |
Antoine Pitrou | c1ee488 | 2016-12-19 10:56:40 +0100 | [diff] [blame] | 285 | return o |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 286 | |
Serhiy Storchaka | 2085bd0 | 2019-06-01 11:00:15 +0300 | [diff] [blame] | 287 | def update(self, other=None, /, **kwargs): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 288 | if self._pending_removals: |
| 289 | self._commit_removals() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 290 | d = self.data |
Serhiy Storchaka | 2085bd0 | 2019-06-01 11:00:15 +0300 | [diff] [blame] | 291 | if other is not None: |
| 292 | if not hasattr(other, "items"): |
| 293 | other = dict(other) |
| 294 | for key, o in other.items(): |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 295 | d[key] = KeyedRef(o, self._remove, key) |
Serhiy Storchaka | 2085bd0 | 2019-06-01 11:00:15 +0300 | [diff] [blame] | 296 | for key, o in kwargs.items(): |
| 297 | d[key] = KeyedRef(o, self._remove, key) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 298 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 299 | def valuerefs(self): |
| 300 | """Return a list of weak references to the values. |
| 301 | |
| 302 | The references are not guaranteed to be 'live' at the time |
| 303 | they are used, so the result of calling the references needs |
| 304 | to be checked before being used. This can be used to avoid |
| 305 | creating references that will cause the garbage collector to |
| 306 | keep the values around longer than needed. |
| 307 | |
| 308 | """ |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 309 | if self._pending_removals: |
| 310 | self._commit_removals() |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 311 | return list(self.data.values()) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 312 | |
Curtis Bucher | 8f1ed21 | 2020-03-24 18:51:29 -0700 | [diff] [blame] | 313 | def __ior__(self, other): |
| 314 | self.update(other) |
| 315 | return self |
| 316 | |
| 317 | def __or__(self, other): |
| 318 | if isinstance(other, _collections_abc.Mapping): |
| 319 | c = self.copy() |
| 320 | c.update(other) |
| 321 | return c |
| 322 | return NotImplemented |
| 323 | |
| 324 | def __ror__(self, other): |
| 325 | if isinstance(other, _collections_abc.Mapping): |
| 326 | c = self.__class__() |
| 327 | c.update(other) |
| 328 | c.update(self) |
| 329 | return c |
| 330 | return NotImplemented |
| 331 | |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 332 | |
| 333 | class KeyedRef(ref): |
| 334 | """Specialized reference that includes a key corresponding to the value. |
| 335 | |
| 336 | This is used in the WeakValueDictionary to avoid having to create |
| 337 | a function object for each key stored in the mapping. A shared |
| 338 | callback object can use the 'key' attribute of a KeyedRef instead |
| 339 | of getting a reference to the key from an enclosing scope. |
| 340 | |
| 341 | """ |
| 342 | |
| 343 | __slots__ = "key", |
| 344 | |
| 345 | def __new__(type, ob, callback, key): |
| 346 | self = ref.__new__(type, ob, callback) |
| 347 | self.key = key |
| 348 | return self |
| 349 | |
| 350 | def __init__(self, ob, callback, key): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 351 | super().__init__(ob, callback) |
Fred Drake | 746fe0f | 2001-09-28 19:01:26 +0000 | [diff] [blame] | 352 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 353 | |
Serhiy Storchaka | 8110837 | 2017-09-26 00:55:55 +0300 | [diff] [blame] | 354 | class WeakKeyDictionary(_collections_abc.MutableMapping): |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 355 | """ Mapping class that references keys weakly. |
| 356 | |
| 357 | Entries in the dictionary will be discarded when there is no |
| 358 | longer a strong reference to the key. This can be used to |
| 359 | associate additional data with an object owned by other parts of |
| 360 | an application without adding attributes to those objects. This |
| 361 | can be especially useful with objects that override attribute |
| 362 | accesses. |
| 363 | """ |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 364 | |
| 365 | def __init__(self, dict=None): |
| 366 | self.data = {} |
Fred Drake | 746fe0f | 2001-09-28 19:01:26 +0000 | [diff] [blame] | 367 | def remove(k, selfref=ref(self)): |
| 368 | self = selfref() |
| 369 | if self is not None: |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 370 | if self._iterating: |
| 371 | self._pending_removals.append(k) |
| 372 | else: |
| 373 | del self.data[k] |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 374 | self._remove = remove |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 375 | # A list of dead weakrefs (keys to be removed) |
| 376 | self._pending_removals = [] |
| 377 | self._iterating = set() |
Antoine Pitrou | 1bf974d | 2014-10-05 20:02:28 +0200 | [diff] [blame] | 378 | self._dirty_len = False |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 379 | if dict is not None: |
| 380 | self.update(dict) |
| 381 | |
| 382 | def _commit_removals(self): |
| 383 | # NOTE: We don't need to call this method before mutating the dict, |
| 384 | # because a dead weakref never compares equal to a live weakref, |
| 385 | # even if they happened to refer to equal objects. |
| 386 | # However, it means keys may already have been removed. |
| 387 | l = self._pending_removals |
| 388 | d = self.data |
| 389 | while l: |
| 390 | try: |
| 391 | del d[l.pop()] |
| 392 | except KeyError: |
| 393 | pass |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 394 | |
Antoine Pitrou | 1bf974d | 2014-10-05 20:02:28 +0200 | [diff] [blame] | 395 | def _scrub_removals(self): |
| 396 | d = self.data |
| 397 | self._pending_removals = [k for k in self._pending_removals if k in d] |
| 398 | self._dirty_len = False |
| 399 | |
Fred Drake | b663a2c | 2001-09-06 14:51:01 +0000 | [diff] [blame] | 400 | def __delitem__(self, key): |
Antoine Pitrou | 1bf974d | 2014-10-05 20:02:28 +0200 | [diff] [blame] | 401 | self._dirty_len = True |
Tim Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 402 | del self.data[ref(key)] |
Fred Drake | b663a2c | 2001-09-06 14:51:01 +0000 | [diff] [blame] | 403 | |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 404 | def __getitem__(self, key): |
| 405 | return self.data[ref(key)] |
| 406 | |
Raymond Hettinger | 7ac6095 | 2008-02-05 01:15:57 +0000 | [diff] [blame] | 407 | def __len__(self): |
Antoine Pitrou | 1bf974d | 2014-10-05 20:02:28 +0200 | [diff] [blame] | 408 | if self._dirty_len and self._pending_removals: |
| 409 | # self._pending_removals may still contain keys which were |
| 410 | # explicitly removed, we have to scrub them (see issue #21173). |
| 411 | self._scrub_removals() |
Antoine Pitrou | bbe2f60 | 2012-03-01 16:26:35 +0100 | [diff] [blame] | 412 | return len(self.data) - len(self._pending_removals) |
Raymond Hettinger | 7ac6095 | 2008-02-05 01:15:57 +0000 | [diff] [blame] | 413 | |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 414 | def __repr__(self): |
Serhiy Storchaka | 465e60e | 2014-07-25 23:36:00 +0300 | [diff] [blame] | 415 | return "<%s at %#x>" % (self.__class__.__name__, id(self)) |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 416 | |
| 417 | def __setitem__(self, key, value): |
| 418 | self.data[ref(key, self._remove)] = value |
| 419 | |
| 420 | def copy(self): |
| 421 | new = WeakKeyDictionary() |
Fish | 96d37db | 2019-02-07 14:51:59 -0500 | [diff] [blame] | 422 | with _IterationGuard(self): |
| 423 | for key, value in self.data.items(): |
| 424 | o = key() |
| 425 | if o is not None: |
| 426 | new[o] = value |
Fred Drake | 9d2c85d | 2001-03-01 03:06:03 +0000 | [diff] [blame] | 427 | return new |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 428 | |
Antoine Pitrou | 6e61006 | 2009-05-15 17:04:50 +0000 | [diff] [blame] | 429 | __copy__ = copy |
| 430 | |
| 431 | def __deepcopy__(self, memo): |
| 432 | from copy import deepcopy |
| 433 | new = self.__class__() |
Fish | 96d37db | 2019-02-07 14:51:59 -0500 | [diff] [blame] | 434 | with _IterationGuard(self): |
| 435 | for key, value in self.data.items(): |
| 436 | o = key() |
| 437 | if o is not None: |
| 438 | new[o] = deepcopy(value, memo) |
Antoine Pitrou | 6e61006 | 2009-05-15 17:04:50 +0000 | [diff] [blame] | 439 | return new |
| 440 | |
Fred Drake | 1d9e4b7 | 2001-04-16 17:34:48 +0000 | [diff] [blame] | 441 | def get(self, key, default=None): |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 442 | return self.data.get(ref(key),default) |
| 443 | |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 444 | def __contains__(self, key): |
| 445 | try: |
| 446 | wr = ref(key) |
| 447 | except TypeError: |
Georg Brandl | bd87d08 | 2010-12-03 07:49:09 +0000 | [diff] [blame] | 448 | return False |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 449 | return wr in self.data |
Tim Peters | c411dba | 2002-07-16 21:35:23 +0000 | [diff] [blame] | 450 | |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 451 | def items(self): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 452 | with _IterationGuard(self): |
| 453 | for wr, value in self.data.items(): |
| 454 | key = wr() |
| 455 | if key is not None: |
| 456 | yield key, value |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 457 | |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 458 | def keys(self): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 459 | with _IterationGuard(self): |
| 460 | for wr in self.data: |
| 461 | obj = wr() |
| 462 | if obj is not None: |
| 463 | yield obj |
Raymond Hettinger | 6114679 | 2004-08-19 21:32:06 +0000 | [diff] [blame] | 464 | |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 465 | __iter__ = keys |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 466 | |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 467 | def values(self): |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 468 | with _IterationGuard(self): |
| 469 | for wr, value in self.data.items(): |
| 470 | if wr() is not None: |
| 471 | yield value |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 472 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 473 | def keyrefs(self): |
| 474 | """Return a list of weak references to the keys. |
| 475 | |
| 476 | The references are not guaranteed to be 'live' at the time |
| 477 | they are used, so the result of calling the references needs |
| 478 | to be checked before being used. This can be used to avoid |
| 479 | creating references that will cause the garbage collector to |
| 480 | keep the keys around longer than needed. |
| 481 | |
| 482 | """ |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 483 | return list(self.data) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 484 | |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 485 | def popitem(self): |
Antoine Pitrou | 1bf974d | 2014-10-05 20:02:28 +0200 | [diff] [blame] | 486 | self._dirty_len = True |
Georg Brandl | bd87d08 | 2010-12-03 07:49:09 +0000 | [diff] [blame] | 487 | while True: |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 488 | key, value = self.data.popitem() |
| 489 | o = key() |
| 490 | if o is not None: |
| 491 | return o, value |
| 492 | |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 493 | def pop(self, key, *args): |
Antoine Pitrou | 1bf974d | 2014-10-05 20:02:28 +0200 | [diff] [blame] | 494 | self._dirty_len = True |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 495 | return self.data.pop(ref(key), *args) |
| 496 | |
Walter Dörwald | 80ce6dd | 2004-05-27 18:16:25 +0000 | [diff] [blame] | 497 | def setdefault(self, key, default=None): |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 498 | return self.data.setdefault(ref(key, self._remove),default) |
| 499 | |
Serhiy Storchaka | 2085bd0 | 2019-06-01 11:00:15 +0300 | [diff] [blame] | 500 | def update(self, dict=None, /, **kwargs): |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 501 | d = self.data |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 502 | if dict is not None: |
| 503 | if not hasattr(dict, "items"): |
| 504 | dict = type({})(dict) |
| 505 | for key, value in dict.items(): |
| 506 | d[ref(key, self._remove)] = value |
| 507 | if len(kwargs): |
| 508 | self.update(kwargs) |
Richard Oudkerk | 7a3dae0 | 2013-05-05 23:05:00 +0100 | [diff] [blame] | 509 | |
Curtis Bucher | 25e580a | 2020-03-23 13:49:46 -0700 | [diff] [blame] | 510 | def __ior__(self, other): |
| 511 | self.update(other) |
| 512 | return self |
| 513 | |
| 514 | def __or__(self, other): |
| 515 | if isinstance(other, _collections_abc.Mapping): |
| 516 | c = self.copy() |
| 517 | c.update(other) |
| 518 | return c |
| 519 | return NotImplemented |
| 520 | |
| 521 | def __ror__(self, other): |
| 522 | if isinstance(other, _collections_abc.Mapping): |
| 523 | c = self.__class__() |
| 524 | c.update(other) |
| 525 | c.update(self) |
| 526 | return c |
| 527 | return NotImplemented |
| 528 | |
Richard Oudkerk | 7a3dae0 | 2013-05-05 23:05:00 +0100 | [diff] [blame] | 529 | |
| 530 | class finalize: |
| 531 | """Class for finalization of weakrefable objects |
| 532 | |
| 533 | finalize(obj, func, *args, **kwargs) returns a callable finalizer |
| 534 | object which will be called when obj is garbage collected. The |
| 535 | first time the finalizer is called it evaluates func(*arg, **kwargs) |
| 536 | and returns the result. After this the finalizer is dead, and |
| 537 | calling it just returns None. |
| 538 | |
| 539 | When the program exits any remaining finalizers for which the |
| 540 | atexit attribute is true will be run in reverse order of creation. |
| 541 | By default atexit is true. |
| 542 | """ |
| 543 | |
| 544 | # Finalizer objects don't have any state of their own. They are |
| 545 | # just used as keys to lookup _Info objects in the registry. This |
| 546 | # ensures that they cannot be part of a ref-cycle. |
| 547 | |
| 548 | __slots__ = () |
| 549 | _registry = {} |
| 550 | _shutdown = False |
| 551 | _index_iter = itertools.count() |
| 552 | _dirty = False |
Richard Oudkerk | a81dd65 | 2013-06-08 16:52:29 +0100 | [diff] [blame] | 553 | _registered_with_atexit = False |
Richard Oudkerk | 7a3dae0 | 2013-05-05 23:05:00 +0100 | [diff] [blame] | 554 | |
| 555 | class _Info: |
| 556 | __slots__ = ("weakref", "func", "args", "kwargs", "atexit", "index") |
| 557 | |
Serhiy Storchaka | 142566c | 2019-06-05 18:22:31 +0300 | [diff] [blame] | 558 | def __init__(self, obj, func, /, *args, **kwargs): |
Richard Oudkerk | a81dd65 | 2013-06-08 16:52:29 +0100 | [diff] [blame] | 559 | if not self._registered_with_atexit: |
| 560 | # We may register the exit function more than once because |
| 561 | # of a thread race, but that is harmless |
| 562 | import atexit |
| 563 | atexit.register(self._exitfunc) |
| 564 | finalize._registered_with_atexit = True |
Richard Oudkerk | 7a3dae0 | 2013-05-05 23:05:00 +0100 | [diff] [blame] | 565 | info = self._Info() |
| 566 | info.weakref = ref(obj, self) |
| 567 | info.func = func |
| 568 | info.args = args |
| 569 | info.kwargs = kwargs or None |
| 570 | info.atexit = True |
| 571 | info.index = next(self._index_iter) |
| 572 | self._registry[self] = info |
| 573 | finalize._dirty = True |
| 574 | |
| 575 | def __call__(self, _=None): |
| 576 | """If alive then mark as dead and return func(*args, **kwargs); |
| 577 | otherwise return None""" |
| 578 | info = self._registry.pop(self, None) |
| 579 | if info and not self._shutdown: |
| 580 | return info.func(*info.args, **(info.kwargs or {})) |
| 581 | |
| 582 | def detach(self): |
| 583 | """If alive then mark as dead and return (obj, func, args, kwargs); |
| 584 | otherwise return None""" |
| 585 | info = self._registry.get(self) |
| 586 | obj = info and info.weakref() |
| 587 | if obj is not None and self._registry.pop(self, None): |
| 588 | return (obj, info.func, info.args, info.kwargs or {}) |
| 589 | |
| 590 | def peek(self): |
| 591 | """If alive then return (obj, func, args, kwargs); |
| 592 | otherwise return None""" |
| 593 | info = self._registry.get(self) |
| 594 | obj = info and info.weakref() |
| 595 | if obj is not None: |
| 596 | return (obj, info.func, info.args, info.kwargs or {}) |
| 597 | |
| 598 | @property |
| 599 | def alive(self): |
| 600 | """Whether finalizer is alive""" |
| 601 | return self in self._registry |
| 602 | |
| 603 | @property |
| 604 | def atexit(self): |
| 605 | """Whether finalizer should be called at exit""" |
| 606 | info = self._registry.get(self) |
| 607 | return bool(info) and info.atexit |
| 608 | |
| 609 | @atexit.setter |
| 610 | def atexit(self, value): |
| 611 | info = self._registry.get(self) |
| 612 | if info: |
| 613 | info.atexit = bool(value) |
| 614 | |
| 615 | def __repr__(self): |
| 616 | info = self._registry.get(self) |
| 617 | obj = info and info.weakref() |
| 618 | if obj is None: |
| 619 | return '<%s object at %#x; dead>' % (type(self).__name__, id(self)) |
| 620 | else: |
| 621 | return '<%s object at %#x; for %r at %#x>' % \ |
| 622 | (type(self).__name__, id(self), type(obj).__name__, id(obj)) |
| 623 | |
| 624 | @classmethod |
| 625 | def _select_for_exit(cls): |
| 626 | # Return live finalizers marked for exit, oldest first |
| 627 | L = [(f,i) for (f,i) in cls._registry.items() if i.atexit] |
| 628 | L.sort(key=lambda item:item[1].index) |
| 629 | return [f for (f,i) in L] |
| 630 | |
| 631 | @classmethod |
| 632 | def _exitfunc(cls): |
| 633 | # At shutdown invoke finalizers for which atexit is true. |
| 634 | # This is called once all other non-daemonic threads have been |
| 635 | # joined. |
| 636 | reenable_gc = False |
| 637 | try: |
| 638 | if cls._registry: |
| 639 | import gc |
| 640 | if gc.isenabled(): |
| 641 | reenable_gc = True |
| 642 | gc.disable() |
| 643 | pending = None |
| 644 | while True: |
| 645 | if pending is None or finalize._dirty: |
| 646 | pending = cls._select_for_exit() |
| 647 | finalize._dirty = False |
| 648 | if not pending: |
| 649 | break |
| 650 | f = pending.pop() |
| 651 | try: |
| 652 | # gc is disabled, so (assuming no daemonic |
| 653 | # threads) the following is the only line in |
| 654 | # this function which might trigger creation |
| 655 | # of a new finalizer |
| 656 | f() |
| 657 | except Exception: |
| 658 | sys.excepthook(*sys.exc_info()) |
| 659 | assert f not in cls._registry |
| 660 | finally: |
| 661 | # prevent any more finalizers from executing during shutdown |
| 662 | finalize._shutdown = True |
| 663 | if reenable_gc: |
| 664 | gc.enable() |