weakref.py 19 KB

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