add a callback to run every time a greenlet is about to be switched to

    :param handler:
        the callback function, must be a function taking 2 arguments:

        - an integer indicating whether it is being called as an incoming (1)
          hook or an outgoing (2) hook (in this case it will always receive 1).
        - the coroutine being switched into (in this case it will always be the
          same as the one indicated by the ``coro`` argument to
          ``local_incoming_hook``.

        Be aware that only a weak reference to this function will be held.
    :type handler: function
    :param coro:
        the coroutine for which to apply the trace hook (defaults to current)
    :type coro: greenlet

def local_incoming_hook(handler=None, coro=None):
    """add a callback to run every time a greenlet is about to be switched to

    :param handler:
        the callback function, must be a function taking 2 arguments:

        - an integer indicating whether it is being called as an incoming (1)
          hook or an outgoing (2) hook (in this case it will always receive 1).
        - the coroutine being switched into (in this case it will always be the
          same as the one indicated by the ``coro`` argument to
          ``local_incoming_hook``.

        Be aware that only a weak reference to this function will be held.
    :type handler: function
    :param coro:
        the coroutine for which to apply the trace hook (defaults to current)
    :type coro: greenlet
    """
    if handler is None:
        return lambda h: local_incoming_hook(h, coro)

    if not hasattr(handler, "__call__"):
        raise TypeError("trace hooks must be callable")

    if coro is None:
        coro = compat.getcurrent()

    log.info("setting a coroutine incoming local hook callback")

    state.local_to_hooks.setdefault(coro, []).append(
        weakref.ref(handler))

    return handler