# Path: tornado/stack_context.py
# class ExceptionStackContext(object):
#     '''Specialization of StackContext for exception handling.
# 
#     The supplied exception_handler function will be called in the
#     event of an uncaught exception in this context.  The semantics are
#     similar to a try/finally clause, and intended use cases are to log
#     an error, close a socket, or similar cleanup actions.  The
#     exc_info triple (type, value, traceback) will be passed to the
#     exception_handler function.
# 
#     If the exception handler returns true, the exception will be
#     consumed and will not be propagated to other exception handlers.
#     '''
#     def __init__(self, exception_handler, _active_cell=None):
#         self.exception_handler = exception_handler
#         self.active_cell = _active_cell or [True]
# 
#     def __enter__(self):
#         self.old_contexts = _state.contexts
#         _state.contexts = (self.old_contexts +
#                            ((ExceptionStackContext, self.exception_handler,
#                              self.active_cell),))
#         return lambda: operator.setitem(self.active_cell, 0, False)
# 
#     def __exit__(self, type, value, traceback):
#         try:
#             if type is not None:
#                 return self.exception_handler(type, value, traceback)
#         finally:
#             _state.contexts = self.old_contexts
#             self.old_contexts = None
# 
# Path: tornado/util.py
# def raise_exc_info(exc_info):
#     """Re-raise an exception (with original traceback) from an exc_info tuple.
# 
#     The argument is a ``(type, value, traceback)`` tuple as returned by
#     `sys.exc_info`.
#     """
#     # 2to3 isn't smart enough to convert three-argument raise
#     # statements correctly in some cases.
#     if isinstance(exc_info[1], exc_info[0]):
#         raise exc_info[1], None, exc_info[2]
#         # After 2to3: raise exc_info[1].with_traceback(exc_info[2])
#     else:
#         # I think this branch is only taken for string exceptions,
#         # which were removed in Python 2.6.
#         raise exc_info[0], exc_info[1], exc_info[2]
#         # After 2to3: raise exc_info[0](exc_info[1]).with_traceback(exc_info[2])


import functools
import sys
from tornado.stack_context import ExceptionStackContext
from tornado.util import raise_exc_info
    from concurrent import futures
        future = Future()
        try:
            future.set_result(fn(*args, **kwargs))
        except Exception, e:
            future.set_exception(e)
        return future

dummy_executor = DummyExecutor()

def run_on_executor(fn):
    @functools.wraps(fn)
    def wrapper(self, *args, **kwargs):
        callback = kwargs.pop("callback")
        future = self.executor.submit(fn, self, *args, **kwargs)
        if callback:
            self.io_loop.add_future(future, callback)
        return future
    return wrapper

# TODO: this needs a better name
def future_wrap(f):
    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        future = Future()
        if kwargs.get('callback') is not None:
            future.add_done_callback(kwargs.pop('callback'))
        kwargs['callback'] = future.set_result
        def handle_error(typ, value, tb):
            future.set_exception(value)
            return True

# Path: tornado/stack_context.py
# class ExceptionStackContext(object):
#     '''Specialization of StackContext for exception handling.
# 
#     The supplied exception_handler function will be called in the
#     event of an uncaught exception in this context.  The semantics are
#     similar to a try/finally clause, and intended use cases are to log
#     an error, close a socket, or similar cleanup actions.  The
#     exc_info triple (type, value, traceback) will be passed to the
#     exception_handler function.
# 
#     If the exception handler returns true, the exception will be
#     consumed and will not be propagated to other exception handlers.
#     '''
#     def __init__(self, exception_handler, _active_cell=None):
#         self.exception_handler = exception_handler
#         self.active_cell = _active_cell or [True]
# 
#     def __enter__(self):
#         self.old_contexts = _state.contexts
#         _state.contexts = (self.old_contexts +
#                            ((ExceptionStackContext, self.exception_handler,
#                              self.active_cell),))
#         return lambda: operator.setitem(self.active_cell, 0, False)
# 
#     def __exit__(self, type, value, traceback):
#         try:
#             if type is not None:
#                 return self.exception_handler(type, value, traceback)
#         finally:
#             _state.contexts = self.old_contexts
#             self.old_contexts = None
# 
# Path: tornado/util.py
# def raise_exc_info(exc_info):
#     """Re-raise an exception (with original traceback) from an exc_info tuple.
# 
#     The argument is a ``(type, value, traceback)`` tuple as returned by
#     `sys.exc_info`.
#     """
#     # 2to3 isn't smart enough to convert three-argument raise
#     # statements correctly in some cases.
#     if isinstance(exc_info[1], exc_info[0]):
#         raise exc_info[1], None, exc_info[2]
#         # After 2to3: raise exc_info[1].with_traceback(exc_info[2])
#     else:
#         # I think this branch is only taken for string exceptions,
#         # which were removed in Python 2.6.
#         raise exc_info[0], exc_info[1], exc_info[2]
#         # After 2to3: raise exc_info[0](exc_info[1]).with_traceback(exc_info[2])


# Path: tornado/concurrent.py
import functools
import sys
from tornado.stack_context import ExceptionStackContext
from tornado.util import raise_exc_info
    from concurrent import futures

        future = Future()
        try:
            future.set_result(fn(*args, **kwargs))
        except Exception, e:
            future.set_exception(e)
        return future

dummy_executor = DummyExecutor()

def run_on_executor(fn):
    @functools.wraps(fn)
    def wrapper(self, *args, **kwargs):
        callback = kwargs.pop("callback")
        future = self.executor.submit(fn, self, *args, **kwargs)
        if callback:
            self.io_loop.add_future(future, callback)
        return future
    return wrapper

# TODO: this needs a better name
def future_wrap(f):
    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        future = Future()
        if kwargs.get('callback') is not None:
            future.add_done_callback(kwargs.pop('callback'))
        kwargs['callback'] = future.set_result
        def handle_error(typ, value, tb):
            future.set_exception(value)
            return True

        with ExceptionStackContext(handle_error):