def maybeDeferred(f, *args, **kw):
    try:
        result = f(*args, **kw)
    except:
        return fail(failure.Failure(captureVars=Deferred.debug))

    if isinstance(result, Deferred):
        return result
    elif isinstance(result, failure.Failure):
        return fail(result)
    else:
        return succeed(result)
Invoke a function that may or may not return a L{Deferred}.
dataset/ETHPy150Open twisted/twisted/twisted/internet/defer.py/maybeDeferred