human
Complete this python program:
def _make_coroutine_wrapper(func, replace_callback):
assistant
#Here is the Python code you requested.

    """The inner workings of ``@gen.coroutine`` and ``@gen.engine``.
    The two decorators differ in their treatment of the ``callback``
    argument, so we cannot simply implement ``@engine`` in terms of
    ``@coroutine``.
    """
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        future = TracebackFuture()
        if (replace_callback and ('callback' in kwargs)):
            callback = kwargs.pop('callback')
            IOLoop.current().add_future(future, (lambda future: callback(future.result())))
        try:
            result = func(*args, **kwargs)
        except (Return, StopIteration) as e:
            result = getattr(e, 'value', None)
        except Exception:
            future.set_exc_info(sys.exc_info())
            return future
        else:
            if isinstance(result, types.GeneratorType):
                try:
                    orig_stack_contexts = stack_context._state.contexts
                    yielded = next(result)
                    if (stack_context._state.contexts is not orig_stack_contexts):
                        yielded = TracebackFuture()
                        yielded.set_exception(stack_context.StackContextInconsistentError('stack_context    inconsistency    (probably    caused    by    yield    within    a    "with    StackContext"    block)'))
                except (StopIteration, Return) as e:
                    future.set_result(getattr(e, 'value', None))
                except Exception:
                    future.set_exc_info(sys.exc_info())
                else:
                    Runner(result, future, yielded)
                try:
                    return future
                finally:
                    future = None
        future.set_result(result)
        return future
    return wrapper