def debug_deferreds():

    # Helper method for retrieving and restoring the current logging context
    # around a callback.
    def with_logging_context(fn):
        context = LoggingContext.current_context()

        def restore_context_callback(x):
            with PreserveLoggingContext(context):
                return fn(x)

        return restore_context_callback

    # We are going to modify the __init__ method of defer.Deferred so we
    # need to get a copy of the old method so we can still call it.
    old__init__ = defer.Deferred.__init__

    # We need to create a deferred to bounce the callbacks through the reactor
    # but we don't want to add a callback when we create that deferred so we
    # we create a new type of deferred that uses the old __init__ method.
    # This is safe as long as the old __init__ method doesn't invoke an
    # __init__ using super.
    class Bouncer(defer.Deferred):
        __init__ = old__init__

    # We'll add this as a callback to all Deferreds. Twisted will wait until
    # the bouncer deferred resolves before calling the callbacks of the
    # original deferred.
    def bounce_callback(x):
        bouncer = Bouncer()
        reactor.callLater(0, with_logging_context(bouncer.callback), x)
        return bouncer

    # We'll add this as an errback to all Deferreds. Twisted will wait until
    # the bouncer deferred resolves before calling the errbacks of the
    # original deferred.
    def bounce_errback(x):
        bouncer = Bouncer()
        reactor.callLater(0, with_logging_context(bouncer.errback), x)
        return bouncer

    @wraps(old__init__)
    def new__init__(self, *args, **kargs):
        old__init__(self, *args, **kargs)
        self.addCallbacks(bounce_callback, bounce_errback)

    defer.Deferred.__init__ = new__init__
Helper function for grad function.
dataset/ETHPy150Open matrix-org/synapse/synapse/util/debug.py/debug_deferreds--crossover with--dataset/ETHPy150Open rizar/attention-lvcsr/libs/Theano/theano/gradient.py/_populate_grad_dict