weakref.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. """Weak reference support for Python.
  2. This module is an implementation of PEP 205:
  3. https://www.python.org/dev/peps/pep-0205/
  4. """
  5. # Naming convention: Variables named "wr" are weak reference objects;
  6. # they are called this instead of "ref" to avoid name collisions with
  7. # the module-global ref() function imported from _weakref.
  8. from _weakref import (
  9. getweakrefcount,
  10. getweakrefs,
  11. ref,
  12. proxy,
  13. CallableProxyType,
  14. ProxyType,
  15. ReferenceType,
  16. _remove_dead_weakref)
  17. from _weakrefset import WeakSet, _IterationGuard
  18. import _collections_abc # Import after _weakref to avoid circular import.
  19. import sys
  20. import itertools
  21. ProxyTypes = (ProxyType, CallableProxyType)
  22. __all__ = ["ref", "proxy", "getweakrefcount", "getweakrefs",
  23. "WeakKeyDictionary", "ReferenceType", "ProxyType",
  24. "CallableProxyType", "ProxyTypes", "WeakValueDictionary",
  25. "WeakSet", "WeakMethod", "finalize"]
  26. _collections_abc.Set.register(WeakSet)
  27. _collections_abc.MutableSet.register(WeakSet)
  28. class WeakMethod(ref):
  29. """
  30. A custom `weakref.ref` subclass which simulates a weak reference to
  31. a bound method, working around the lifetime problem of bound methods.
  32. """
  33. __slots__ = "_func_ref", "_meth_type", "_alive", "__weakref__"
  34. def __new__(cls, meth, callback=None):
  35. try:
  36. obj = meth.__self__
  37. func = meth.__func__
  38. except AttributeError:
  39. raise TypeError("argument should be a bound method, not {}"
  40. .format(type(meth))) from None
  41. def _cb(arg):
  42. # The self-weakref trick is needed to avoid creating a reference
  43. # cycle.
  44. self = self_wr()
  45. if self._alive:
  46. self._alive = False
  47. if callback is not None:
  48. callback(self)
  49. self = ref.__new__(cls, obj, _cb)
  50. self._func_ref = ref(func, _cb)
  51. self._meth_type = type(meth)
  52. self._alive = True
  53. self_wr = ref(self)
  54. return self
  55. def __call__(self):
  56. obj = super().__call__()
  57. func = self._func_ref()
  58. if obj is None or func is None:
  59. return None
  60. return self._meth_type(func, obj)
  61. def __eq__(self, other):
  62. if isinstance(other, WeakMethod):
  63. if not self._alive or not other._alive:
  64. return self is other
  65. return ref.__eq__(self, other) and self._func_ref == other._func_ref
  66. return NotImplemented
  67. def __ne__(self, other):
  68. if isinstance(other, WeakMethod):
  69. if not self._alive or not other._alive:
  70. return self is not other
  71. return ref.__ne__(self, other) or self._func_ref != other._func_ref
  72. return NotImplemented
  73. __hash__ = ref.__hash__
  74. class WeakValueDictionary(_collections_abc.MutableMapping):
  75. """Mapping class that references values weakly.
  76. Entries in the dictionary will be discarded when no strong
  77. reference to the value exists anymore
  78. """
  79. # We inherit the constructor without worrying about the input
  80. # dictionary; since it uses our .update() method, we get the right
  81. # checks (if the other dictionary is a WeakValueDictionary,
  82. # objects are unwrapped on the way out, and we always wrap on the
  83. # way in).
  84. def __init__(self, other=(), /, **kw):
  85. def remove(wr, selfref=ref(self), _atomic_removal=_remove_dead_weakref):
  86. self = selfref()
  87. if self is not None:
  88. if self._iterating:
  89. self._pending_removals.append(wr.key)
  90. else:
  91. # Atomic removal is necessary since this function
  92. # can be called asynchronously by the GC
  93. _atomic_removal(self.data, wr.key)
  94. self._remove = remove
  95. # A list of keys to be removed
  96. self._pending_removals = []
  97. self._iterating = set()
  98. self.data = {}
  99. self.update(other, **kw)
  100. def _commit_removals(self, _atomic_removal=_remove_dead_weakref):
  101. pop = self._pending_removals.pop
  102. d = self.data
  103. # We shouldn't encounter any KeyError, because this method should
  104. # always be called *before* mutating the dict.
  105. while True:
  106. try:
  107. key = pop()
  108. except IndexError:
  109. return
  110. _atomic_removal(d, key)
  111. def __getitem__(self, key):
  112. if self._pending_removals:
  113. self._commit_removals()
  114. o = self.data[key]()
  115. if o is None:
  116. raise KeyError(key)
  117. else:
  118. return o
  119. def __delitem__(self, key):
  120. if self._pending_removals:
  121. self._commit_removals()
  122. del self.data[key]
  123. def __len__(self):
  124. if self._pending_removals:
  125. self._commit_removals()
  126. return len(self.data)
  127. def __contains__(self, key):
  128. if self._pending_removals:
  129. self._commit_removals()
  130. try:
  131. o = self.data[key]()
  132. except KeyError:
  133. return False
  134. return o is not None
  135. def __repr__(self):
  136. return "<%s at %#x>" % (self.__class__.__name__, id(self))
  137. def __setitem__(self, key, value):
  138. if self._pending_removals:
  139. self._commit_removals()
  140. self.data[key] = KeyedRef(value, self._remove, key)
  141. def copy(self):
  142. if self._pending_removals:
  143. self._commit_removals()
  144. new = WeakValueDictionary()
  145. with _IterationGuard(self):
  146. for key, wr in self.data.items():
  147. o = wr()
  148. if o is not None:
  149. new[key] = o
  150. return new
  151. __copy__ = copy
  152. def __deepcopy__(self, memo):
  153. from copy import deepcopy
  154. if self._pending_removals:
  155. self._commit_removals()
  156. new = self.__class__()
  157. with _IterationGuard(self):
  158. for key, wr in self.data.items():
  159. o = wr()
  160. if o is not None:
  161. new[deepcopy(key, memo)] = o
  162. return new
  163. def get(self, key, default=None):
  164. if self._pending_removals:
  165. self._commit_removals()
  166. try:
  167. wr = self.data[key]
  168. except KeyError:
  169. return default
  170. else:
  171. o = wr()
  172. if o is None:
  173. # This should only happen
  174. return default
  175. else:
  176. return o
  177. def items(self):
  178. if self._pending_removals:
  179. self._commit_removals()
  180. with _IterationGuard(self):
  181. for k, wr in self.data.items():
  182. v = wr()
  183. if v is not None:
  184. yield k, v
  185. def keys(self):
  186. if self._pending_removals:
  187. self._commit_removals()
  188. with _IterationGuard(self):
  189. for k, wr in self.data.items():
  190. if wr() is not None:
  191. yield k
  192. __iter__ = keys
  193. def itervaluerefs(self):
  194. """Return an iterator that yields the weak references to the values.
  195. The references are not guaranteed to be 'live' at the time
  196. they are used, so the result of calling the references needs
  197. to be checked before being used. This can be used to avoid
  198. creating references that will cause the garbage collector to
  199. keep the values around longer than needed.
  200. """
  201. if self._pending_removals:
  202. self._commit_removals()
  203. with _IterationGuard(self):
  204. yield from self.data.values()
  205. def values(self):
  206. if self._pending_removals:
  207. self._commit_removals()
  208. with _IterationGuard(self):
  209. for wr in self.data.values():
  210. obj = wr()
  211. if obj is not None:
  212. yield obj
  213. def popitem(self):
  214. if self._pending_removals:
  215. self._commit_removals()
  216. while True:
  217. key, wr = self.data.popitem()
  218. o = wr()
  219. if o is not None:
  220. return key, o
  221. def pop(self, key, *args):
  222. if self._pending_removals:
  223. self._commit_removals()
  224. try:
  225. o = self.data.pop(key)()
  226. except KeyError:
  227. o = None
  228. if o is None:
  229. if args:
  230. return args[0]
  231. else:
  232. raise KeyError(key)
  233. else:
  234. return o
  235. def setdefault(self, key, default=None):
  236. try:
  237. o = self.data[key]()
  238. except KeyError:
  239. o = None
  240. if o is None:
  241. if self._pending_removals:
  242. self._commit_removals()
  243. self.data[key] = KeyedRef(default, self._remove, key)
  244. return default
  245. else:
  246. return o
  247. def update(self, other=None, /, **kwargs):
  248. if self._pending_removals:
  249. self._commit_removals()
  250. d = self.data
  251. if other is not None:
  252. if not hasattr(other, "items"):
  253. other = dict(other)
  254. for key, o in other.items():
  255. d[key] = KeyedRef(o, self._remove, key)
  256. for key, o in kwargs.items():
  257. d[key] = KeyedRef(o, self._remove, key)
  258. def valuerefs(self):
  259. """Return a list of weak references to the values.
  260. The references are not guaranteed to be 'live' at the time
  261. they are used, so the result of calling the references needs
  262. to be checked before being used. This can be used to avoid
  263. creating references that will cause the garbage collector to
  264. keep the values around longer than needed.
  265. """
  266. if self._pending_removals:
  267. self._commit_removals()
  268. return list(self.data.values())
  269. def __ior__(self, other):
  270. self.update(other)
  271. return self
  272. def __or__(self, other):
  273. if isinstance(other, _collections_abc.Mapping):
  274. c = self.copy()
  275. c.update(other)
  276. return c
  277. return NotImplemented
  278. def __ror__(self, other):
  279. if isinstance(other, _collections_abc.Mapping):
  280. c = self.__class__()
  281. c.update(other)
  282. c.update(self)
  283. return c
  284. return NotImplemented
  285. class KeyedRef(ref):
  286. """Specialized reference that includes a key corresponding to the value.
  287. This is used in the WeakValueDictionary to avoid having to create
  288. a function object for each key stored in the mapping. A shared
  289. callback object can use the 'key' attribute of a KeyedRef instead
  290. of getting a reference to the key from an enclosing scope.
  291. """
  292. __slots__ = "key",
  293. def __new__(type, ob, callback, key):
  294. self = ref.__new__(type, ob, callback)
  295. self.key = key
  296. return self
  297. def __init__(self, ob, callback, key):
  298. super().__init__(ob, callback)
  299. class WeakKeyDictionary(_collections_abc.MutableMapping):
  300. """ Mapping class that references keys weakly.
  301. Entries in the dictionary will be discarded when there is no
  302. longer a strong reference to the key. This can be used to
  303. associate additional data with an object owned by other parts of
  304. an application without adding attributes to those objects. This
  305. can be especially useful with objects that override attribute
  306. accesses.
  307. """
  308. def __init__(self, dict=None):
  309. self.data = {}
  310. def remove(k, selfref=ref(self)):
  311. self = selfref()
  312. if self is not None:
  313. if self._iterating:
  314. self._pending_removals.append(k)
  315. else:
  316. try:
  317. del self.data[k]
  318. except KeyError:
  319. pass
  320. self._remove = remove
  321. # A list of dead weakrefs (keys to be removed)
  322. self._pending_removals = []
  323. self._iterating = set()
  324. self._dirty_len = False
  325. if dict is not None:
  326. self.update(dict)
  327. def _commit_removals(self):
  328. # NOTE: We don't need to call this method before mutating the dict,
  329. # because a dead weakref never compares equal to a live weakref,
  330. # even if they happened to refer to equal objects.
  331. # However, it means keys may already have been removed.
  332. pop = self._pending_removals.pop
  333. d = self.data
  334. while True:
  335. try:
  336. key = pop()
  337. except IndexError:
  338. return
  339. try:
  340. del d[key]
  341. except KeyError:
  342. pass
  343. def _scrub_removals(self):
  344. d = self.data
  345. self._pending_removals = [k for k in self._pending_removals if k in d]
  346. self._dirty_len = False
  347. def __delitem__(self, key):
  348. self._dirty_len = True
  349. del self.data[ref(key)]
  350. def __getitem__(self, key):
  351. return self.data[ref(key)]
  352. def __len__(self):
  353. if self._dirty_len and self._pending_removals:
  354. # self._pending_removals may still contain keys which were
  355. # explicitly removed, we have to scrub them (see issue #21173).
  356. self._scrub_removals()
  357. return len(self.data) - len(self._pending_removals)
  358. def __repr__(self):
  359. return "<%s at %#x>" % (self.__class__.__name__, id(self))
  360. def __setitem__(self, key, value):
  361. self.data[ref(key, self._remove)] = value
  362. def copy(self):
  363. new = WeakKeyDictionary()
  364. with _IterationGuard(self):
  365. for key, value in self.data.items():
  366. o = key()
  367. if o is not None:
  368. new[o] = value
  369. return new
  370. __copy__ = copy
  371. def __deepcopy__(self, memo):
  372. from copy import deepcopy
  373. new = self.__class__()
  374. with _IterationGuard(self):
  375. for key, value in self.data.items():
  376. o = key()
  377. if o is not None:
  378. new[o] = deepcopy(value, memo)
  379. return new
  380. def get(self, key, default=None):
  381. return self.data.get(ref(key),default)
  382. def __contains__(self, key):
  383. try:
  384. wr = ref(key)
  385. except TypeError:
  386. return False
  387. return wr in self.data
  388. def items(self):
  389. with _IterationGuard(self):
  390. for wr, value in self.data.items():
  391. key = wr()
  392. if key is not None:
  393. yield key, value
  394. def keys(self):
  395. with _IterationGuard(self):
  396. for wr in self.data:
  397. obj = wr()
  398. if obj is not None:
  399. yield obj
  400. __iter__ = keys
  401. def values(self):
  402. with _IterationGuard(self):
  403. for wr, value in self.data.items():
  404. if wr() is not None:
  405. yield value
  406. def keyrefs(self):
  407. """Return a list of weak references to the keys.
  408. The references are not guaranteed to be 'live' at the time
  409. they are used, so the result of calling the references needs
  410. to be checked before being used. This can be used to avoid
  411. creating references that will cause the garbage collector to
  412. keep the keys around longer than needed.
  413. """
  414. return list(self.data)
  415. def popitem(self):
  416. self._dirty_len = True
  417. while True:
  418. key, value = self.data.popitem()
  419. o = key()
  420. if o is not None:
  421. return o, value
  422. def pop(self, key, *args):
  423. self._dirty_len = True
  424. return self.data.pop(ref(key), *args)
  425. def setdefault(self, key, default=None):
  426. return self.data.setdefault(ref(key, self._remove),default)
  427. def update(self, dict=None, /, **kwargs):
  428. d = self.data
  429. if dict is not None:
  430. if not hasattr(dict, "items"):
  431. dict = type({})(dict)
  432. for key, value in dict.items():
  433. d[ref(key, self._remove)] = value
  434. if len(kwargs):
  435. self.update(kwargs)
  436. def __ior__(self, other):
  437. self.update(other)
  438. return self
  439. def __or__(self, other):
  440. if isinstance(other, _collections_abc.Mapping):
  441. c = self.copy()
  442. c.update(other)
  443. return c
  444. return NotImplemented
  445. def __ror__(self, other):
  446. if isinstance(other, _collections_abc.Mapping):
  447. c = self.__class__()
  448. c.update(other)
  449. c.update(self)
  450. return c
  451. return NotImplemented
  452. class finalize:
  453. """Class for finalization of weakrefable objects
  454. finalize(obj, func, *args, **kwargs) returns a callable finalizer
  455. object which will be called when obj is garbage collected. The
  456. first time the finalizer is called it evaluates func(*arg, **kwargs)
  457. and returns the result. After this the finalizer is dead, and
  458. calling it just returns None.
  459. When the program exits any remaining finalizers for which the
  460. atexit attribute is true will be run in reverse order of creation.
  461. By default atexit is true.
  462. """
  463. # Finalizer objects don't have any state of their own. They are
  464. # just used as keys to lookup _Info objects in the registry. This
  465. # ensures that they cannot be part of a ref-cycle.
  466. __slots__ = ()
  467. _registry = {}
  468. _shutdown = False
  469. _index_iter = itertools.count()
  470. _dirty = False
  471. _registered_with_atexit = False
  472. class _Info:
  473. __slots__ = ("weakref", "func", "args", "kwargs", "atexit", "index")
  474. def __init__(self, obj, func, /, *args, **kwargs):
  475. if not self._registered_with_atexit:
  476. # We may register the exit function more than once because
  477. # of a thread race, but that is harmless
  478. import atexit
  479. atexit.register(self._exitfunc)
  480. finalize._registered_with_atexit = True
  481. info = self._Info()
  482. info.weakref = ref(obj, self)
  483. info.func = func
  484. info.args = args
  485. info.kwargs = kwargs or None
  486. info.atexit = True
  487. info.index = next(self._index_iter)
  488. self._registry[self] = info
  489. finalize._dirty = True
  490. def __call__(self, _=None):
  491. """If alive then mark as dead and return func(*args, **kwargs);
  492. otherwise return None"""
  493. info = self._registry.pop(self, None)
  494. if info and not self._shutdown:
  495. return info.func(*info.args, **(info.kwargs or {}))
  496. def detach(self):
  497. """If alive then mark as dead and return (obj, func, args, kwargs);
  498. otherwise return None"""
  499. info = self._registry.get(self)
  500. obj = info and info.weakref()
  501. if obj is not None and self._registry.pop(self, None):
  502. return (obj, info.func, info.args, info.kwargs or {})
  503. def peek(self):
  504. """If alive then return (obj, func, args, kwargs);
  505. otherwise return None"""
  506. info = self._registry.get(self)
  507. obj = info and info.weakref()
  508. if obj is not None:
  509. return (obj, info.func, info.args, info.kwargs or {})
  510. @property
  511. def alive(self):
  512. """Whether finalizer is alive"""
  513. return self in self._registry
  514. @property
  515. def atexit(self):
  516. """Whether finalizer should be called at exit"""
  517. info = self._registry.get(self)
  518. return bool(info) and info.atexit
  519. @atexit.setter
  520. def atexit(self, value):
  521. info = self._registry.get(self)
  522. if info:
  523. info.atexit = bool(value)
  524. def __repr__(self):
  525. info = self._registry.get(self)
  526. obj = info and info.weakref()
  527. if obj is None:
  528. return '<%s object at %#x; dead>' % (type(self).__name__, id(self))
  529. else:
  530. return '<%s object at %#x; for %r at %#x>' % \
  531. (type(self).__name__, id(self), type(obj).__name__, id(obj))
  532. @classmethod
  533. def _select_for_exit(cls):
  534. # Return live finalizers marked for exit, oldest first
  535. L = [(f,i) for (f,i) in cls._registry.items() if i.atexit]
  536. L.sort(key=lambda item:item[1].index)
  537. return [f for (f,i) in L]
  538. @classmethod
  539. def _exitfunc(cls):
  540. # At shutdown invoke finalizers for which atexit is true.
  541. # This is called once all other non-daemonic threads have been
  542. # joined.
  543. reenable_gc = False
  544. try:
  545. if cls._registry:
  546. import gc
  547. if gc.isenabled():
  548. reenable_gc = True
  549. gc.disable()
  550. pending = None
  551. while True:
  552. if pending is None or finalize._dirty:
  553. pending = cls._select_for_exit()
  554. finalize._dirty = False
  555. if not pending:
  556. break
  557. f = pending.pop()
  558. try:
  559. # gc is disabled, so (assuming no daemonic
  560. # threads) the following is the only line in
  561. # this function which might trigger creation
  562. # of a new finalizer
  563. f()
  564. except Exception:
  565. sys.excepthook(*sys.exc_info())
  566. assert f not in cls._registry
  567. finally:
  568. # prevent any more finalizers from executing during shutdown
  569. finalize._shutdown = True
  570. if reenable_gc:
  571. gc.enable()