weakref.py 21 KB

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