weakref.py 21 KB

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