saferef.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. # -*- coding: utf-8 -*-
  2. """
  3. "Safe weakrefs", originally from pyDispatcher.
  4. Provides a way to safely weakref any function, including bound methods (which
  5. aren't handled by the core weakref module).
  6. """
  7. from __future__ import absolute_import
  8. import weakref
  9. import traceback
  10. from collections import Callable
  11. __all__ = ['safe_ref']
  12. def safe_ref(target, on_delete=None): # pragma: no cover
  13. """Return a *safe* weak reference to a callable target
  14. :param target: the object to be weakly referenced, if it's a
  15. bound method reference, will create a :class:`BoundMethodWeakref`,
  16. otherwise creates a simple :class:`weakref.ref`.
  17. :keyword on_delete: if provided, will have a hard reference stored
  18. to the callable to be called after the safe reference
  19. goes out of scope with the reference object, (either a
  20. :class:`weakref.ref` or a :class:`BoundMethodWeakref`) as argument.
  21. """
  22. if getattr(target, '__self__', None) is not None:
  23. # Turn a bound method into a BoundMethodWeakref instance.
  24. # Keep track of these instances for lookup by disconnect().
  25. assert hasattr(target, '__func__'), \
  26. """safe_ref target {0!r} has __self__, but no __func__: \
  27. don't know how to create reference""".format(target)
  28. return get_bound_method_weakref(target=target,
  29. on_delete=on_delete)
  30. if isinstance(on_delete, Callable):
  31. return weakref.ref(target, on_delete)
  32. else:
  33. return weakref.ref(target)
  34. class BoundMethodWeakref(object): # pragma: no cover
  35. """'Safe' and reusable weak references to instance methods.
  36. BoundMethodWeakref objects provide a mechanism for
  37. referencing a bound method without requiring that the
  38. method object itself (which is normally a transient
  39. object) is kept alive. Instead, the BoundMethodWeakref
  40. object keeps weak references to both the object and the
  41. function which together define the instance method.
  42. .. attribute:: key
  43. the identity key for the reference, calculated
  44. by the class's :meth:`calculate_key` method applied to the
  45. target instance method
  46. .. attribute:: deletion_methods
  47. sequence of callable objects taking
  48. single argument, a reference to this object which
  49. will be called when *either* the target object or
  50. target function is garbage collected (i.e. when
  51. this object becomes invalid). These are specified
  52. as the on_delete parameters of :func:`safe_ref` calls.
  53. .. attribute:: weak_self
  54. weak reference to the target object
  55. .. attribute:: weak_fun
  56. weak reference to the target function
  57. .. attribute:: _all_instances
  58. class attribute pointing to all live
  59. BoundMethodWeakref objects indexed by the class's
  60. `calculate_key(target)` method applied to the target
  61. objects. This weak value dictionary is used to
  62. short-circuit creation so that multiple references
  63. to the same (object, function) pair produce the
  64. same BoundMethodWeakref instance.
  65. """
  66. _all_instances = weakref.WeakValueDictionary()
  67. def __new__(cls, target, on_delete=None, *arguments, **named):
  68. """Create new instance or return current instance
  69. Basically this method of construction allows us to
  70. short-circuit creation of references to already-
  71. referenced instance methods. The key corresponding
  72. to the target is calculated, and if there is already
  73. an existing reference, that is returned, with its
  74. deletionMethods attribute updated. Otherwise the
  75. new instance is created and registered in the table
  76. of already-referenced methods.
  77. """
  78. key = cls.calculate_key(target)
  79. current = cls._all_instances.get(key)
  80. if current is not None:
  81. current.deletion_methods.append(on_delete)
  82. return current
  83. else:
  84. base = super(BoundMethodWeakref, cls).__new__(cls)
  85. cls._all_instances[key] = base
  86. base.__init__(target, on_delete, *arguments, **named)
  87. return base
  88. def __init__(self, target, on_delete=None):
  89. """Return a weak-reference-like instance for a bound method
  90. :param target: the instance-method target for the weak
  91. reference, must have `__self__` and `__func__` attributes
  92. and be reconstructable via::
  93. target.__func__.__get__(target.__self__)
  94. which is true of built-in instance methods.
  95. :keyword on_delete: optional callback which will be called
  96. when this weak reference ceases to be valid
  97. (i.e. either the object or the function is garbage
  98. collected). Should take a single argument,
  99. which will be passed a pointer to this object.
  100. """
  101. def remove(weak, self=self):
  102. """Set self.is_dead to true when method or instance is destroyed"""
  103. methods = self.deletion_methods[:]
  104. del(self.deletion_methods[:])
  105. try:
  106. del(self.__class__._all_instances[self.key])
  107. except KeyError:
  108. pass
  109. for function in methods:
  110. try:
  111. if isinstance(function, Callable):
  112. function(self)
  113. except Exception as exc:
  114. try:
  115. traceback.print_exc()
  116. except AttributeError:
  117. print('Exception during saferef {0} cleanup function '
  118. '{1}: {2}'.format(self, function, exc))
  119. self.deletion_methods = [on_delete]
  120. self.key = self.calculate_key(target)
  121. self.weak_self = weakref.ref(target.__self__, remove)
  122. self.weak_fun = weakref.ref(target.__func__, remove)
  123. self.self_name = str(target.__self__)
  124. self.fun_name = str(target.__func__.__name__)
  125. def calculate_key(cls, target):
  126. """Calculate the reference key for this reference
  127. Currently this is a two-tuple of the `id()`'s of the
  128. target object and the target function respectively.
  129. """
  130. return id(target.__self__), id(target.__func__)
  131. calculate_key = classmethod(calculate_key)
  132. def __str__(self):
  133. """Give a friendly representation of the object"""
  134. return '{0}( {1}.{2} )'.format(
  135. type(self).__name__,
  136. self.self_name,
  137. self.fun_name,
  138. )
  139. __repr__ = __str__
  140. def __bool__(self):
  141. """Whether we are still a valid reference"""
  142. return self() is not None
  143. __nonzero__ = __bool__ # py2
  144. def __cmp__(self, other):
  145. """Compare with another reference"""
  146. if not isinstance(other, self.__class__):
  147. return cmp(self.__class__, type(other))
  148. return cmp(self.key, other.key)
  149. def __call__(self):
  150. """Return a strong reference to the bound method
  151. If the target cannot be retrieved, then will
  152. return None, otherwise return a bound instance
  153. method for our object and function.
  154. Note:
  155. You may call this method any number of times,
  156. as it does not invalidate the reference.
  157. """
  158. target = self.weak_self()
  159. if target is not None:
  160. function = self.weak_fun()
  161. if function is not None:
  162. return function.__get__(target)
  163. class BoundNonDescriptorMethodWeakref(BoundMethodWeakref): # pragma: no cover
  164. """A specialized :class:`BoundMethodWeakref`, for platforms where
  165. instance methods are not descriptors.
  166. It assumes that the function name and the target attribute name are the
  167. same, instead of assuming that the function is a descriptor. This approach
  168. is equally fast, but not 100% reliable because functions can be stored on
  169. an attribute named differenty than the function's name such as in::
  170. >>> class A(object):
  171. ... pass
  172. >>> def foo(self):
  173. ... return 'foo'
  174. >>> A.bar = foo
  175. But this shouldn't be a common use case. So, on platforms where methods
  176. aren't descriptors (such as Jython) this implementation has the advantage
  177. of working in the most cases.
  178. """
  179. def __init__(self, target, on_delete=None):
  180. """Return a weak-reference-like instance for a bound method
  181. :param target: the instance-method target for the weak
  182. reference, must have `__self__` and `__func__` attributes
  183. and be reconstructable via::
  184. target.__func__.__get__(target.__self__)
  185. which is true of built-in instance methods.
  186. :keyword on_delete: optional callback which will be called
  187. when this weak reference ceases to be valid
  188. (i.e. either the object or the function is garbage
  189. collected). Should take a single argument,
  190. which will be passed a pointer to this object.
  191. """
  192. assert getattr(target.__self__, target.__name__) == target, \
  193. "method %s isn't available as the attribute %s of %s" % (
  194. target, target.__name__, target.__self__)
  195. super(BoundNonDescriptorMethodWeakref, self).__init__(target,
  196. on_delete)
  197. def __call__(self):
  198. """Return a strong reference to the bound method
  199. If the target cannot be retrieved, then will
  200. return None, otherwise return a bound instance
  201. method for our object and function.
  202. Note:
  203. You may call this method any number of times,
  204. as it does not invalidate the reference.
  205. """
  206. target = self.weak_self()
  207. if target is not None:
  208. function = self.weak_fun()
  209. if function is not None:
  210. # Using curry() would be another option, but it erases the
  211. # "signature" of the function. That is, after a function is
  212. # curried, the inspect module can't be used to determine how
  213. # many arguments the function expects, nor what keyword
  214. # arguments it supports, and pydispatcher needs this
  215. # information.
  216. return getattr(target, function.__name__)
  217. def get_bound_method_weakref(target, on_delete): # pragma: no cover
  218. """Instantiates the appropiate :class:`BoundMethodWeakRef`, depending
  219. on the details of the underlying class method implementation."""
  220. if hasattr(target, '__get__'):
  221. # target method is a descriptor, so the default implementation works:
  222. return BoundMethodWeakref(target=target, on_delete=on_delete)
  223. else:
  224. # no luck, use the alternative implementation:
  225. return BoundNonDescriptorMethodWeakref(target=target,
  226. on_delete=on_delete)