_saferef.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. # extracted from Louie, http://pylouie.org/
  2. # updated for Python 3
  3. #
  4. # Copyright (c) 2006 Patrick K. O'Brien, Mike C. Fletcher,
  5. # Matthew R. Scott
  6. #
  7. # Redistribution and use in source and binary forms, with or without
  8. # modification, are permitted provided that the following conditions are
  9. # met:
  10. #
  11. # * Redistributions of source code must retain the above copyright
  12. # notice, this list of conditions and the following disclaimer.
  13. #
  14. # * Redistributions in binary form must reproduce the above
  15. # copyright notice, this list of conditions and the following
  16. # disclaimer in the documentation and/or other materials provided
  17. # with the distribution.
  18. #
  19. # * Neither the name of the <ORGANIZATION> nor the names of its
  20. # contributors may be used to endorse or promote products derived
  21. # from this software without specific prior written permission.
  22. #
  23. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. #
  35. """Refactored 'safe reference from dispatcher.py"""
  36. import operator
  37. import sys
  38. import traceback
  39. import weakref
  40. get_self = operator.attrgetter("__self__")
  41. get_func = operator.attrgetter("__func__")
  42. def safe_ref(target, on_delete=None):
  43. """Return a *safe* weak reference to a callable target.
  44. - ``target``: The object to be weakly referenced, if it's a bound
  45. method reference, will create a BoundMethodWeakref, otherwise
  46. creates a simple weakref.
  47. - ``on_delete``: If provided, will have a hard reference stored to
  48. the callable to be called after the safe reference goes out of
  49. scope with the reference object, (either a weakref or a
  50. BoundMethodWeakref) as argument.
  51. """
  52. try:
  53. im_self = get_self(target)
  54. except AttributeError:
  55. if callable(on_delete):
  56. return weakref.ref(target, on_delete)
  57. else:
  58. return weakref.ref(target)
  59. else:
  60. if im_self is not None:
  61. # Turn a bound method into a BoundMethodWeakref instance.
  62. # Keep track of these instances for lookup by disconnect().
  63. assert hasattr(target, "im_func") or hasattr(target, "__func__"), (
  64. f"safe_ref target {target!r} has im_self, but no im_func, "
  65. "don't know how to create reference"
  66. )
  67. reference = BoundMethodWeakref(target=target, on_delete=on_delete)
  68. return reference
  69. class BoundMethodWeakref:
  70. """'Safe' and reusable weak references to instance methods.
  71. BoundMethodWeakref objects provide a mechanism for referencing a
  72. bound method without requiring that the method object itself
  73. (which is normally a transient object) is kept alive. Instead,
  74. the BoundMethodWeakref object keeps weak references to both the
  75. object and the function which together define the instance method.
  76. Attributes:
  77. - ``key``: The identity key for the reference, calculated by the
  78. class's calculate_key method applied to the target instance method.
  79. - ``deletion_methods``: Sequence of callable objects taking single
  80. argument, a reference to this object which will be called when
  81. *either* the target object or target function is garbage
  82. collected (i.e. when this object becomes invalid). These are
  83. specified as the on_delete parameters of safe_ref calls.
  84. - ``weak_self``: Weak reference to the target object.
  85. - ``weak_func``: Weak reference to the target function.
  86. Class Attributes:
  87. - ``_all_instances``: Class attribute pointing to all live
  88. BoundMethodWeakref objects indexed by the class's
  89. calculate_key(target) method applied to the target objects.
  90. This weak value dictionary is used to short-circuit creation so
  91. that multiple references to the same (object, function) pair
  92. produce the same BoundMethodWeakref instance.
  93. """
  94. _all_instances = weakref.WeakValueDictionary() # type: ignore[var-annotated]
  95. def __new__(cls, target, on_delete=None, *arguments, **named):
  96. """Create new instance or return current instance.
  97. Basically this method of construction allows us to
  98. short-circuit creation of references to already-referenced
  99. instance methods. The key corresponding to the target is
  100. calculated, and if there is already an existing reference,
  101. that is returned, with its deletion_methods attribute updated.
  102. Otherwise the new instance is created and registered in the
  103. table of already-referenced methods.
  104. """
  105. key = cls.calculate_key(target)
  106. current = cls._all_instances.get(key)
  107. if current is not None:
  108. current.deletion_methods.append(on_delete)
  109. return current
  110. else:
  111. base = super().__new__(cls)
  112. cls._all_instances[key] = base
  113. base.__init__(target, on_delete, *arguments, **named)
  114. return base
  115. def __init__(self, target, on_delete=None):
  116. """Return a weak-reference-like instance for a bound method.
  117. - ``target``: The instance-method target for the weak reference,
  118. must have im_self and im_func attributes and be
  119. reconstructable via the following, which is true of built-in
  120. instance methods::
  121. target.im_func.__get__( target.im_self )
  122. - ``on_delete``: Optional callback which will be called when
  123. this weak reference ceases to be valid (i.e. either the
  124. object or the function is garbage collected). Should take a
  125. single argument, which will be passed a pointer to this
  126. object.
  127. """
  128. def remove(weak, self=self):
  129. """Set self.isDead to True when method or instance is destroyed."""
  130. methods = self.deletion_methods[:]
  131. del self.deletion_methods[:]
  132. try:
  133. del self.__class__._all_instances[self.key]
  134. except KeyError:
  135. pass
  136. for function in methods:
  137. try:
  138. if callable(function):
  139. function(self)
  140. except Exception:
  141. try:
  142. traceback.print_exc()
  143. except AttributeError:
  144. e = sys.exc_info()[1]
  145. print(
  146. f"Exception during saferef {self} "
  147. f"cleanup function {function}: {e}"
  148. )
  149. self.deletion_methods = [on_delete]
  150. self.key = self.calculate_key(target)
  151. im_self = get_self(target)
  152. im_func = get_func(target)
  153. self.weak_self = weakref.ref(im_self, remove)
  154. self.weak_func = weakref.ref(im_func, remove)
  155. self.self_name = str(im_self)
  156. self.func_name = str(im_func.__name__)
  157. @classmethod
  158. def calculate_key(cls, target):
  159. """Calculate the reference key for this reference.
  160. Currently this is a two-tuple of the id()'s of the target
  161. object and the target function respectively.
  162. """
  163. return (id(get_self(target)), id(get_func(target)))
  164. def __str__(self):
  165. """Give a friendly representation of the object."""
  166. return "{}({}.{})".format(
  167. self.__class__.__name__,
  168. self.self_name,
  169. self.func_name,
  170. )
  171. __repr__ = __str__
  172. def __hash__(self):
  173. return hash((self.self_name, self.key))
  174. def __nonzero__(self):
  175. """Whether we are still a valid reference."""
  176. return self() is not None
  177. def __eq__(self, other):
  178. """Compare with another reference."""
  179. if not isinstance(other, self.__class__):
  180. return operator.eq(self.__class__, type(other))
  181. return operator.eq(self.key, other.key)
  182. def __call__(self):
  183. """Return a strong reference to the bound method.
  184. If the target cannot be retrieved, then will return None,
  185. otherwise returns a bound instance method for our object and
  186. function.
  187. Note: You may call this method any number of times, as it does
  188. not invalidate the reference.
  189. """
  190. target = self.weak_self()
  191. if target is not None:
  192. function = self.weak_func()
  193. if function is not None:
  194. return function.__get__(target)
  195. return None