Skip to content

weak_call

Weakly-referenced callables.

This module provides WeakCall, a callable which holds a weak reference to its target function - typically a bound method of an object - so that holding a WeakCall does not prevent the target object from being garbage collected.

WeakCall

A callable holding a weak reference to a function or bound method.

Calling a WeakCall invokes its target function with the arguments - both positional and keyword - given at construction time, plus the ones given at call time. Bound methods whose object has been garbage collected are not invoked: the GONE sentinel is returned instead, so that callers can detect the situation.

Source code in py_misc_utils/weak_call.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class WeakCall:
  """A callable holding a weak reference to a function or bound method.

  Calling a WeakCall invokes its target function with the arguments - both
  positional and keyword - given at construction time, plus the ones given
  at call time. Bound methods whose object has been garbage collected are
  not invoked: the GONE sentinel is returned instead, so that callers can
  detect the situation.
  """

  def __init__(self, fn, *args, **kwargs):
    """Creates a weak call.

    Args:
      fn: The target function - or weak reference to it. Bound methods are
        held via weakref.WeakMethod, plain functions via a lambda closure.
      *args: Positional arguments to be passed to the target function.
      **kwargs: Keyword arguments to be passed to the target function.
    """
    if isinstance(fn, weakref.WeakMethod):
      self._fn = fn
    elif hasattr(fn, '__self__'):
      self._fn = weakref.WeakMethod(fn)
    else:
      self._fn = lambda: fn

    self._args = args
    self._kwargs = kwargs

  def __call__(self, *args, **kwargs):
    """Invokes the target function - if still alive.

    Args:
      *args: Positional arguments appended to the ones given at
        construction time.
      **kwargs: Keyword arguments merged over the ones given at
        construction time.

    Returns:
      The result of the target function call, or the GONE sentinel if the
      target bound method object has been garbage collected.

    Example:
      class Handler:

        def on_event(self, eid, user=None):
          print(eid, user)

      call = weak_call.WeakCall(Handler().on_event, 1)
      call(user='me')  # on_event(1, user='me')
    """
    fn = self._fn()
    if fn is not None:
      cargs = self._args + args
      ckwargs = self._kwargs.copy()
      ckwargs.update(kwargs)

      return fn(*cargs, **ckwargs)
    else:
      return GONE