# Path: tornado/ioloop.py
# class IOLoop(Configurable):
# class PollIOLoop(IOLoop):
# class _Timeout(object):
# class PeriodicCallback(object):
#     _EPOLLIN = 0x001
#     _EPOLLPRI = 0x002
#     _EPOLLOUT = 0x004
#     _EPOLLERR = 0x008
#     _EPOLLHUP = 0x010
#     _EPOLLRDHUP = 0x2000
#     _EPOLLONESHOT = (1 << 30)
#     _EPOLLET = (1 << 31)
#     NONE = 0
#     READ = _EPOLLIN
#     WRITE = _EPOLLOUT
#     ERROR = _EPOLLERR | _EPOLLHUP
#         _FUTURE_TYPES = (futures.Future, DummyFuture)
#         _FUTURE_TYPES = DummyFuture
#     def instance():
#     def initialized():
#     def install(self):
#     def current():
#     def make_current(self):
#     def clear_current(self):
#     def configurable_base(cls):
#     def configurable_default(cls):
#     def initialize(self):
#     def close(self, all_fds=False):
#     def add_handler(self, fd, handler, events):
#     def update_handler(self, fd, events):
#     def remove_handler(self, fd):
#     def set_blocking_signal_threshold(self, seconds, action):
#     def set_blocking_log_threshold(self, seconds):
#     def log_stack(self, signal, frame):
#     def start(self):
#     def stop(self):
#     def time(self):
#     def add_timeout(self, deadline, callback):
#     def remove_timeout(self, timeout):
#     def add_callback(self, callback, *args, **kwargs):
#     def add_callback_from_signal(self, callback, *args, **kwargs):
#     def add_future(self, future, callback):
#     def _run_callback(self, callback):
#     def handle_callback_exception(self, callback):
#     def initialize(self, impl, time_func=None):
#     def close(self, all_fds=False):
#     def add_handler(self, fd, handler, events):
#     def update_handler(self, fd, events):
#     def remove_handler(self, fd):
#     def set_blocking_signal_threshold(self, seconds, action):
#     def start(self):
#     def stop(self):
#     def time(self):
#     def add_timeout(self, deadline, callback):
#     def remove_timeout(self, timeout):
#     def add_callback(self, callback, *args, **kwargs):
#     def add_callback_from_signal(self, callback, *args, **kwargs):
#     def __init__(self, deadline, callback, io_loop):
#     def timedelta_to_seconds(td):
#     def __lt__(self, other):
#     def __le__(self, other):
#     def __init__(self, callback, callback_time, io_loop=None):
#     def start(self):
#     def stop(self):
#     def _run(self):
#     def _schedule_next(self):
# 
# Path: tornado/log.py
# def _stderr_supports_color():
#     def __init__(self, color=True, *args, **kwargs):
#     def format(self, record):
# def enable_pretty_logging(options=None):
# def define_logging_options(options=None):
# class LogFormatter(logging.Formatter):
# 
# Path: tornado/stack_context.py
# class _State(threading.local):
# class StackContext(object):
# class ExceptionStackContext(object):
# class NullContext(object):
# class _StackContextWrapper(functools.partial):
#     def __init__(self):
#     def __init__(self, context_factory, _active_cell=None):
#     def __enter__(self):
#     def __exit__(self, type, value, traceback):
#     def __init__(self, exception_handler, _active_cell=None):
#     def __enter__(self):
#     def __exit__(self, type, value, traceback):
#     def __enter__(self):
#     def __exit__(self, type, value, traceback):
# def wrap(fn):
#     def wrapped(*args, **kwargs):
# def _nested(*managers):
# 
# Path: tornado/util.py
# class ObjectDict(dict):
# class GzipDecompressor(object):
# class Configurable(object):
#     def __getattr__(self, name):
#     def __setattr__(self, name, value):
#     def __init__(self):
#     def decompress(self, value):
#     def flush(self):
# def import_object(name):
#     def b(s):
#     def b(s):
# def raise_exc_info(exc_info):
#     def __new__(cls, **kwargs):
#     def configurable_base(cls):
#     def configurable_default(cls):
#     def initialize(self):
#     def configure(cls, impl, **kwargs):
#     def configured_class(cls):
#     def _save_configuration(cls):
#     def _restore_configuration(cls, saved):
# def doctests():


import collections
import errno
import os
import socket
import sys
import re
    import ssl  # Python 2.6+
    import doctest
from tornado import ioloop
from tornado.log import gen_log, app_log
from tornado import stack_context
from tornado.util import b, bytes_type
    from tornado.platform.posix import _set_nonblocking
                # callback.  At the end of this method we'll either
                # estabilsh a real pending callback via
                # _read_from_buffer or run the close callback.
                #
                # We need two try statements here so that
                # pending_callbacks is decremented before the `except`
                # clause below (which calls `close` and does need to
                # trigger the callback)
                self._pending_callbacks += 1
                while not self.closed():
                    # Read from the socket until we get EWOULDBLOCK or equivalent.
                    # SSL sockets do some internal buffering, and if the data is
                    # sitting in the SSL object's buffer select() and friends
                    # can't see it; the only way to find out if it's there is to
                    # try to read it.
                    if self._read_to_buffer() == 0:
                        break
            finally:
                self._pending_callbacks -= 1
        except Exception:
            gen_log.warning("error on read", exc_info=True)
            self.close()
            return
        if self._read_from_buffer():
            return
        else:
            self._maybe_run_close_callback()

    def _set_read_callback(self, callback):
        assert not self._read_callback, "Already reading"

# Path: tornado/ioloop.py
# class IOLoop(Configurable):
# class PollIOLoop(IOLoop):
# class _Timeout(object):
# class PeriodicCallback(object):
#     _EPOLLIN = 0x001
#     _EPOLLPRI = 0x002
#     _EPOLLOUT = 0x004
#     _EPOLLERR = 0x008
#     _EPOLLHUP = 0x010
#     _EPOLLRDHUP = 0x2000
#     _EPOLLONESHOT = (1 << 30)
#     _EPOLLET = (1 << 31)
#     NONE = 0
#     READ = _EPOLLIN
#     WRITE = _EPOLLOUT
#     ERROR = _EPOLLERR | _EPOLLHUP
#         _FUTURE_TYPES = (futures.Future, DummyFuture)
#         _FUTURE_TYPES = DummyFuture
#     def instance():
#     def initialized():
#     def install(self):
#     def current():
#     def make_current(self):
#     def clear_current(self):
#     def configurable_base(cls):
#     def configurable_default(cls):
#     def initialize(self):
#     def close(self, all_fds=False):
#     def add_handler(self, fd, handler, events):
#     def update_handler(self, fd, events):
#     def remove_handler(self, fd):
#     def set_blocking_signal_threshold(self, seconds, action):
#     def set_blocking_log_threshold(self, seconds):
#     def log_stack(self, signal, frame):
#     def start(self):
#     def stop(self):
#     def time(self):
#     def add_timeout(self, deadline, callback):
#     def remove_timeout(self, timeout):
#     def add_callback(self, callback, *args, **kwargs):
#     def add_callback_from_signal(self, callback, *args, **kwargs):
#     def add_future(self, future, callback):
#     def _run_callback(self, callback):
#     def handle_callback_exception(self, callback):
#     def initialize(self, impl, time_func=None):
#     def close(self, all_fds=False):
#     def add_handler(self, fd, handler, events):
#     def update_handler(self, fd, events):
#     def remove_handler(self, fd):
#     def set_blocking_signal_threshold(self, seconds, action):
#     def start(self):
#     def stop(self):
#     def time(self):
#     def add_timeout(self, deadline, callback):
#     def remove_timeout(self, timeout):
#     def add_callback(self, callback, *args, **kwargs):
#     def add_callback_from_signal(self, callback, *args, **kwargs):
#     def __init__(self, deadline, callback, io_loop):
#     def timedelta_to_seconds(td):
#     def __lt__(self, other):
#     def __le__(self, other):
#     def __init__(self, callback, callback_time, io_loop=None):
#     def start(self):
#     def stop(self):
#     def _run(self):
#     def _schedule_next(self):
# 
# Path: tornado/log.py
# def _stderr_supports_color():
#     def __init__(self, color=True, *args, **kwargs):
#     def format(self, record):
# def enable_pretty_logging(options=None):
# def define_logging_options(options=None):
# class LogFormatter(logging.Formatter):
# 
# Path: tornado/stack_context.py
# class _State(threading.local):
# class StackContext(object):
# class ExceptionStackContext(object):
# class NullContext(object):
# class _StackContextWrapper(functools.partial):
#     def __init__(self):
#     def __init__(self, context_factory, _active_cell=None):
#     def __enter__(self):
#     def __exit__(self, type, value, traceback):
#     def __init__(self, exception_handler, _active_cell=None):
#     def __enter__(self):
#     def __exit__(self, type, value, traceback):
#     def __enter__(self):
#     def __exit__(self, type, value, traceback):
# def wrap(fn):
#     def wrapped(*args, **kwargs):
# def _nested(*managers):
# 
# Path: tornado/util.py
# class ObjectDict(dict):
# class GzipDecompressor(object):
# class Configurable(object):
#     def __getattr__(self, name):
#     def __setattr__(self, name, value):
#     def __init__(self):
#     def decompress(self, value):
#     def flush(self):
# def import_object(name):
#     def b(s):
#     def b(s):
# def raise_exc_info(exc_info):
#     def __new__(cls, **kwargs):
#     def configurable_base(cls):
#     def configurable_default(cls):
#     def initialize(self):
#     def configure(cls, impl, **kwargs):
#     def configured_class(cls):
#     def _save_configuration(cls):
#     def _restore_configuration(cls, saved):
# def doctests():


# Path: tornado/iostream.py
import collections
import errno
import os
import socket
import sys
import re
    import ssl  # Python 2.6+
    import doctest
from tornado import ioloop
from tornado.log import gen_log, app_log
from tornado import stack_context
from tornado.util import b, bytes_type
    from tornado.platform.posix import _set_nonblocking

                # callback.  At the end of this method we'll either
                # estabilsh a real pending callback via
                # _read_from_buffer or run the close callback.
                #
                # We need two try statements here so that
                # pending_callbacks is decremented before the `except`
                # clause below (which calls `close` and does need to
                # trigger the callback)
                self._pending_callbacks += 1
                while not self.closed():
                    # Read from the socket until we get EWOULDBLOCK or equivalent.
                    # SSL sockets do some internal buffering, and if the data is
                    # sitting in the SSL object's buffer select() and friends
                    # can't see it; the only way to find out if it's there is to
                    # try to read it.
                    if self._read_to_buffer() == 0:
                        break
            finally:
                self._pending_callbacks -= 1
        except Exception:
            gen_log.warning("error on read", exc_info=True)
            self.close()
            return
        if self._read_from_buffer():
            return
        else:
            self._maybe_run_close_callback()

    def _set_read_callback(self, callback):
        assert not self._read_callback, "Already reading"

        self._read_callback = stack_context.wrap(callback)