def _unwind_generator(gen_expr, cb=None, res=None):
    try:
        while True:
            maybe_func = res
            args = []
            # if the first arg is callable, we need to call it (and assume the last argument is a callback)
            if type(res) == tuple:
                maybe_func = len(res) and res[0]

            if not callable(maybe_func):
                # send only accepts one argument... this is slightly dangerous if
                # we ever just return a tuple of one elemetn
                # TODO: catch no generator
                if type(res) == tuple and len(res) == 1:
                    res = gen_expr.send(res[0])
                else:
                    res = gen_expr.send(res)
                continue

            def f(*args):
                return _unwind_generator(gen_expr, cb, args)

            try:
                args = list(res)[1:]
            except:
                # assume not iterable
                args = []

            args.append(f)
            return maybe_func(*args)

    # TODO: probably shouldn't catch StopIteration to return since that can occur by accident...
    except __HOLE__:
        pass
    except __StopUnwindingException as e:
        res = e.ret_val
    if cb:
        return cb(res)
    return res
dataset/ETHPy150Open Floobits/floobits-sublime/floo/common/utils.py/_unwind_generator