{
    "file_diffs": [
        {
            "old_file_content": "#!/usr/bin/env python\n#\n# Copyright 2011 Facebook\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\"); you may\n# not use this file except in compliance with the License. You may obtain\n# a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n# License for the specific language governing permissions and limitations\n# under the License.\n\n\"\"\"Miscellaneous network utility code.\"\"\"\n\nfrom __future__ import absolute_import, division, print_function, with_statement\n\nimport errno\nimport os\nimport sys\nimport socket\nimport stat\n\nfrom tornado.concurrent import dummy_executor, run_on_executor\nfrom tornado.ioloop import IOLoop\nfrom tornado.platform.auto import set_close_exec\nfrom tornado.util import u, Configurable, errno_from_exception\n\ntry:\n    import ssl\nexcept ImportError:\n    # ssl is not available on Google App Engine\n    ssl = None\n\ntry:\n    import certifi\nexcept ImportError:\n    # certifi is optional as long as we have ssl.create_default_context.\n    if ssl is None or hasattr(ssl, 'create_default_context'):\n        certifi = None\n    else:\n        raise\n\ntry:\n    xrange  # py2\nexcept NameError:\n    xrange = range  # py3\n\nif hasattr(ssl, 'match_hostname') and hasattr(ssl, 'CertificateError'):  # python 3.2+\n    ssl_match_hostname = ssl.match_hostname\n    SSLCertificateError = ssl.CertificateError\nelif ssl is None:\n    ssl_match_hostname = SSLCertificateError = None\nelse:\n    import backports.ssl_match_hostname\n    ssl_match_hostname = backports.ssl_match_hostname.match_hostname\n    SSLCertificateError = backports.ssl_match_hostname.CertificateError\n\nif hasattr(ssl, 'SSLContext'):\n    if hasattr(ssl, 'create_default_context'):\n        # Python 2.7.9+, 3.4+\n        # Note that the naming of ssl.Purpose is confusing; the purpose\n        # of a context is to authentiate the opposite side of the connection.\n        _client_ssl_defaults = ssl.create_default_context(\n            ssl.Purpose.SERVER_AUTH)\n        _server_ssl_defaults = ssl.create_default_context(\n            ssl.Purpose.CLIENT_AUTH)\n    else:\n        # Python 3.2-3.3\n        _client_ssl_defaults = ssl.SSLContext(ssl.PROTOCOL_SSLv23)\n        _client_ssl_defaults.verify_mode = ssl.CERT_REQUIRED\n        _client_ssl_defaults.load_verify_locations(certifi.where())\n        _server_ssl_defaults = ssl.SSLContext(ssl.PROTOCOL_SSLv23)\n        if hasattr(ssl, 'OP_NO_COMPRESSION'):\n            # Disable TLS compression to avoid CRIME and related attacks.\n            # This constant wasn't added until python 3.3.\n            _client_ssl_defaults.options |= ssl.OP_NO_COMPRESSION\n            _server_ssl_defaults.options |= ssl.OP_NO_COMPRESSION\n\nelif ssl:\n    # Python 2.6-2.7.8\n    _client_ssl_defaults = dict(cert_reqs=ssl.CERT_REQUIRED,\n                                ca_certs=certifi.where())\n    _server_ssl_defaults = {}\nelse:\n    # Google App Engine\n    _client_ssl_defaults = dict(cert_reqs=None,\n                                ca_certs=None)\n    _server_ssl_defaults = {}\n\n# ThreadedResolver runs getaddrinfo on a thread. If the hostname is unicode,\n# getaddrinfo attempts to import encodings.idna. If this is done at\n# module-import time, the import lock is already held by the main thread,\n# leading to deadlock. Avoid it by caching the idna encoder on the main\n# thread now.\nu('foo').encode('idna')\n\n# These errnos indicate that a non-blocking operation must be retried\n# at a later time.  On most platforms they're the same value, but on\n# some they differ.\n_ERRNO_WOULDBLOCK = (errno.EWOULDBLOCK, errno.EAGAIN)\n\nif hasattr(errno, \"WSAEWOULDBLOCK\"):\n    _ERRNO_WOULDBLOCK += (errno.WSAEWOULDBLOCK,)\n\n# Default backlog used when calling sock.listen()\n_DEFAULT_BACKLOG = 128\n\n\ndef bind_sockets(port, address=None, family=socket.AF_UNSPEC,\n                 backlog=_DEFAULT_BACKLOG, flags=None):\n    \"\"\"Creates listening sockets bound to the given port and address.\n\n    Returns a list of socket objects (multiple sockets are returned if\n    the given address maps to multiple IP addresses, which is most common\n    for mixed IPv4 and IPv6 use).\n\n    Address may be either an IP address or hostname.  If it's a hostname,\n    the server will listen on all IP addresses associated with the\n    name.  Address may be an empty string or None to listen on all\n    available interfaces.  Family may be set to either `socket.AF_INET`\n    or `socket.AF_INET6` to restrict to IPv4 or IPv6 addresses, otherwise\n    both will be used if available.\n\n    The ``backlog`` argument has the same meaning as for\n    `socket.listen() <socket.socket.listen>`.\n\n    ``flags`` is a bitmask of AI_* flags to `~socket.getaddrinfo`, like\n    ``socket.AI_PASSIVE | socket.AI_NUMERICHOST``.\n    \"\"\"\n    sockets = []\n    if address == \"\":\n        address = None\n    if not socket.has_ipv6 and family == socket.AF_UNSPEC:\n        # Python can be compiled with --disable-ipv6, which causes\n        # operations on AF_INET6 sockets to fail, but does not\n        # automatically exclude those results from getaddrinfo\n        # results.\n        # http://bugs.python.org/issue16208\n        family = socket.AF_INET\n    if flags is None:\n        flags = socket.AI_PASSIVE\n    bound_port = None\n    for res in set(socket.getaddrinfo(address, port, family, socket.SOCK_STREAM,\n                                      0, flags)):\n        af, socktype, proto, canonname, sockaddr = res\n        if (sys.platform == 'darwin' and address == 'localhost' and\n                af == socket.AF_INET6 and sockaddr[3] != 0):\n            # Mac OS X includes a link-local address fe80::1%lo0 in the\n            # getaddrinfo results for 'localhost'.  However, the firewall\n            # doesn't understand that this is a local address and will\n            # prompt for access (often repeatedly, due to an apparent\n            # bug in its ability to remember granting access to an\n            # application). Skip these addresses.\n            continue\n        try:\n            sock = socket.socket(af, socktype, proto)\n        except socket.error as e:\n            if errno_from_exception(e) == errno.EAFNOSUPPORT:\n                continue\n            raise\n        set_close_exec(sock.fileno())\n        if os.name != 'nt':\n            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n        if af == socket.AF_INET6:\n            # On linux, ipv6 sockets accept ipv4 too by default,\n            # but this makes it impossible to bind to both\n            # 0.0.0.0 in ipv4 and :: in ipv6.  On other systems,\n            # separate sockets *must* be used to listen for both ipv4\n            # and ipv6.  For consistency, always disable ipv4 on our\n            # ipv6 sockets and use a separate ipv4 socket when needed.\n            #\n            # Python 2.x on windows doesn't have IPPROTO_IPV6.\n            if hasattr(socket, \"IPPROTO_IPV6\"):\n                sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)\n\n        # automatic port allocation with port=None\n        # should bind on the same port on IPv4 and IPv6\n        host, requested_port = sockaddr[:2]\n        if requested_port == 0 and bound_port is not None:\n            sockaddr = tuple([host, bound_port] + list(sockaddr[2:]))\n\n        sock.setblocking(0)\n        sock.bind(sockaddr)\n        bound_port = sock.getsockname()[1]\n        sock.listen(backlog)\n        sockets.append(sock)\n    return sockets\n\nif hasattr(socket, 'AF_UNIX'):\n    def bind_unix_socket(file, mode=0o600, backlog=_DEFAULT_BACKLOG):\n        \"\"\"Creates a listening unix socket.\n\n        If a socket with the given name already exists, it will be deleted.\n        If any other file with that name exists, an exception will be\n        raised.\n\n        Returns a socket object (not a list of socket objects like\n        `bind_sockets`)\n        \"\"\"\n        sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)\n        set_close_exec(sock.fileno())\n        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n        sock.setblocking(0)\n        try:\n            st = os.stat(file)\n        except OSError as err:\n            if errno_from_exception(err) != errno.ENOENT:\n                raise\n        else:\n            if stat.S_ISSOCK(st.st_mode):\n                os.remove(file)\n            else:\n                raise ValueError(\"File %s exists and is not a socket\", file)\n        sock.bind(file)\n        os.chmod(file, mode)\n        sock.listen(backlog)\n        return sock\n\n\ndef add_accept_handler(sock, callback, io_loop=None):\n    \"\"\"Adds an `.IOLoop` event handler to accept new connections on ``sock``.\n\n    When a connection is accepted, ``callback(connection, address)`` will\n    be run (``connection`` is a socket object, and ``address`` is the\n    address of the other end of the connection).  Note that this signature\n    is different from the ``callback(fd, events)`` signature used for\n    `.IOLoop` handlers.\n\n    .. versionchanged:: 4.1\n       The ``io_loop`` argument is deprecated.\n    \"\"\"\n    if io_loop is None:\n        io_loop = IOLoop.current()\n\n    def accept_handler(fd, events):\n        # More connections may come in while we're handling callbacks;\n        # to prevent starvation of other tasks we must limit the number\n        # of connections we accept at a time.  Ideally we would accept\n        # up to the number of connections that were waiting when we\n        # entered this method, but this information is not available\n        # (and rearranging this method to call accept() as many times\n        # as possible before running any callbacks would have adverse\n        # effects on load balancing in multiprocess configurations).\n        # Instead, we use the (default) listen backlog as a rough\n        # heuristic for the number of connections we can reasonably\n        # accept at once.\n        for i in xrange(_DEFAULT_BACKLOG):\n            try:\n                connection, address = sock.accept()\n            except socket.error as e:\n                # _ERRNO_WOULDBLOCK indicate we have accepted every\n                # connection that is available.\n                if errno_from_exception(e) in _ERRNO_WOULDBLOCK:\n                    return\n                # ECONNABORTED indicates that there was a connection\n                # but it was closed while still in the accept queue.\n                # (observed on FreeBSD).\n                if errno_from_exception(e) == errno.ECONNABORTED:\n                    continue\n                raise\n            callback(connection, address)\n    io_loop.add_handler(sock, accept_handler, IOLoop.READ)\n\n\ndef is_valid_ip(ip):\n    \"\"\"Returns true if the given string is a well-formed IP address.\n\n    Supports IPv4 and IPv6.\n    \"\"\"\n    if not ip or '\\x00' in ip:\n        # getaddrinfo resolves empty strings to localhost, and truncates\n        # on zero bytes.\n        return False\n    try:\n        res = socket.getaddrinfo(ip, 0, socket.AF_UNSPEC,\n                                 socket.SOCK_STREAM,\n                                 0, socket.AI_NUMERICHOST)\n        return bool(res)\n    except socket.gaierror as e:\n        if e.args[0] == socket.EAI_NONAME:\n            return False\n        raise\n    return True\n\n\nclass Resolver(Configurable):\n    \"\"\"Configurable asynchronous DNS resolver interface.\n\n    By default, a blocking implementation is used (which simply calls\n    `socket.getaddrinfo`).  An alternative implementation can be\n    chosen with the `Resolver.configure <.Configurable.configure>`\n    class method::\n\n        Resolver.configure('tornado.netutil.ThreadedResolver')\n\n    The implementations of this interface included with Tornado are\n\n    * `tornado.netutil.BlockingResolver`\n    * `tornado.netutil.ThreadedResolver`\n    * `tornado.netutil.OverrideResolver`\n    * `tornado.platform.twisted.TwistedResolver`\n    * `tornado.platform.caresresolver.CaresResolver`\n    \"\"\"\n    @classmethod\n    def configurable_base(cls):\n        return Resolver\n\n    @classmethod\n    def configurable_default(cls):\n        return BlockingResolver\n\n    def resolve(self, host, port, family=socket.AF_UNSPEC, callback=None):\n        \"\"\"Resolves an address.\n\n        The ``host`` argument is a string which may be a hostname or a\n        literal IP address.\n\n        Returns a `.Future` whose result is a list of (family,\n        address) pairs, where address is a tuple suitable to pass to\n        `socket.connect <socket.socket.connect>` (i.e. a ``(host,\n        port)`` pair for IPv4; additional fields may be present for\n        IPv6). If a ``callback`` is passed, it will be run with the\n        result as an argument when it is complete.\n        \"\"\"\n        raise NotImplementedError()\n\n    def close(self):\n        \"\"\"Closes the `Resolver`, freeing any resources used.\n\n        .. versionadded:: 3.1\n\n        \"\"\"\n        pass\n\n\nclass ExecutorResolver(Resolver):\n    \"\"\"Resolver implementation using a `concurrent.futures.Executor`.\n\n    Use this instead of `ThreadedResolver` when you require additional\n    control over the executor being used.\n\n    The executor will be shut down when the resolver is closed unless\n    ``close_resolver=False``; use this if you want to reuse the same\n    executor elsewhere.\n\n    .. versionchanged:: 4.1\n       The ``io_loop`` argument is deprecated.\n    \"\"\"\n    def initialize(self, io_loop=None, executor=None, close_executor=True):\n        self.io_loop = io_loop or IOLoop.current()\n        if executor is not None:\n            self.executor = executor\n            self.close_executor = close_executor\n        else:\n            self.executor = dummy_executor\n            self.close_executor = False\n\n    def close(self):\n        if self.close_executor:\n            self.executor.shutdown()\n        self.executor = None\n\n    @run_on_executor\n    def resolve(self, host, port, family=socket.AF_UNSPEC):\n        # On Solaris, getaddrinfo fails if the given port is not found\n        # in /etc/services and no socket type is given, so we must pass\n        # one here.  The socket type used here doesn't seem to actually\n        # matter (we discard the one we get back in the results),\n        # so the addresses we return should still be usable with SOCK_DGRAM.\n        addrinfo = socket.getaddrinfo(host, port, family, socket.SOCK_STREAM)\n        results = []\n        for family, socktype, proto, canonname, address in addrinfo:\n            results.append((family, address))\n        return results\n\n\nclass BlockingResolver(ExecutorResolver):\n    \"\"\"Default `Resolver` implementation, using `socket.getaddrinfo`.\n\n    The `.IOLoop` will be blocked during the resolution, although the\n    callback will not be run until the next `.IOLoop` iteration.\n    \"\"\"\n    def initialize(self, io_loop=None):\n        super(BlockingResolver, self).initialize(io_loop=io_loop)\n\n\nclass ThreadedResolver(ExecutorResolver):\n    \"\"\"Multithreaded non-blocking `Resolver` implementation.\n\n    Requires the `concurrent.futures` package to be installed\n    (available in the standard library since Python 3.2,\n    installable with ``pip install futures`` in older versions).\n\n    The thread pool size can be configured with::\n\n        Resolver.configure('tornado.netutil.ThreadedResolver',\n                           num_threads=10)\n\n    .. versionchanged:: 3.1\n       All ``ThreadedResolvers`` share a single thread pool, whose\n       size is set by the first one to be created.\n    \"\"\"\n    _threadpool = None\n    _threadpool_pid = None\n\n    def initialize(self, io_loop=None, num_threads=10):\n        threadpool = ThreadedResolver._create_threadpool(num_threads)\n        super(ThreadedResolver, self).initialize(\n            io_loop=io_loop, executor=threadpool, close_executor=False)\n\n    @classmethod\n    def _create_threadpool(cls, num_threads):\n        pid = os.getpid()\n        if cls._threadpool_pid != pid:\n            # Threads cannot survive after a fork, so if our pid isn't what it\n            # was when we created the pool then delete it.\n            cls._threadpool = None\n        if cls._threadpool is None:\n            from concurrent.futures import ThreadPoolExecutor\n            cls._threadpool = ThreadPoolExecutor(num_threads)\n            cls._threadpool_pid = pid\n        return cls._threadpool\n\n\nclass OverrideResolver(Resolver):\n    \"\"\"Wraps a resolver with a mapping of overrides.\n\n    This can be used to make local DNS changes (e.g. for testing)\n    without modifying system-wide settings.\n\n    The mapping can contain either host strings or host-port pairs.\n    \"\"\"\n    def initialize(self, resolver, mapping):\n        self.resolver = resolver\n        self.mapping = mapping\n\n    def close(self):\n        self.resolver.close()\n\n    def resolve(self, host, port, *args, **kwargs):\n        if (host, port) in self.mapping:\n            host, port = self.mapping[(host, port)]\n        elif host in self.mapping:\n            host = self.mapping[host]\n        return self.resolver.resolve(host, port, *args, **kwargs)\n\n\n# These are the keyword arguments to ssl.wrap_socket that must be translated\n# to their SSLContext equivalents (the other arguments are still passed\n# to SSLContext.wrap_socket).\n_SSL_CONTEXT_KEYWORDS = frozenset(['ssl_version', 'certfile', 'keyfile',\n                                   'cert_reqs', 'ca_certs', 'ciphers'])\n\n\ndef ssl_options_to_context(ssl_options):\n    \"\"\"Try to convert an ``ssl_options`` dictionary to an\n    `~ssl.SSLContext` object.\n\n    The ``ssl_options`` dictionary contains keywords to be passed to\n    `ssl.wrap_socket`.  In Python 2.7.9+, `ssl.SSLContext` objects can\n    be used instead.  This function converts the dict form to its\n    `~ssl.SSLContext` equivalent, and may be used when a component which\n    accepts both forms needs to upgrade to the `~ssl.SSLContext` version\n    to use features like SNI or NPN.\n    \"\"\"\n    if isinstance(ssl_options, dict):\n        assert all(k in _SSL_CONTEXT_KEYWORDS for k in ssl_options), ssl_options\n    if (not hasattr(ssl, 'SSLContext') or\n            isinstance(ssl_options, ssl.SSLContext)):\n        return ssl_options\n    context = ssl.SSLContext(\n        ssl_options.get('ssl_version', ssl.PROTOCOL_SSLv23))\n    if 'certfile' in ssl_options:\n        context.load_cert_chain(ssl_options['certfile'], ssl_options.get('keyfile', None))\n    if 'cert_reqs' in ssl_options:\n        context.verify_mode = ssl_options['cert_reqs']\n    if 'ca_certs' in ssl_options:\n        context.load_verify_locations(ssl_options['ca_certs'])\n    if 'ciphers' in ssl_options:\n        context.set_ciphers(ssl_options['ciphers'])\n    if hasattr(ssl, 'OP_NO_COMPRESSION'):\n        # Disable TLS compression to avoid CRIME and related attacks.\n        # This constant wasn't added until python 3.3.\n        context.options |= ssl.OP_NO_COMPRESSION\n    return context\n\n\ndef ssl_wrap_socket(socket, ssl_options, server_hostname=None, **kwargs):\n    \"\"\"Returns an ``ssl.SSLSocket`` wrapping the given socket.\n\n    ``ssl_options`` may be either an `ssl.SSLContext` object or a\n    dictionary (as accepted by `ssl_options_to_context`).  Additional\n    keyword arguments are passed to ``wrap_socket`` (either the\n    `~ssl.SSLContext` method or the `ssl` module function as\n    appropriate).\n    \"\"\"\n    context = ssl_options_to_context(ssl_options)\n    if hasattr(ssl, 'SSLContext') and isinstance(context, ssl.SSLContext):\n        if server_hostname is not None and getattr(ssl, 'HAS_SNI'):\n            # Python doesn't have server-side SNI support so we can't\n            # really unittest this, but it can be manually tested with\n            # python3.2 -m tornado.httpclient https://sni.velox.ch\n            return context.wrap_socket(socket, server_hostname=server_hostname,\n                                       **kwargs)\n        else:\n            return context.wrap_socket(socket, **kwargs)\n    else:\n        return ssl.wrap_socket(socket, **dict(context, **kwargs))\n",
            "new_file_content": "#!/usr/bin/env python\n#\n# Copyright 2011 Facebook\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\"); you may\n# not use this file except in compliance with the License. You may obtain\n# a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n# License for the specific language governing permissions and limitations\n# under the License.\n\n\"\"\"Miscellaneous network utility code.\"\"\"\n\nfrom __future__ import absolute_import, division, print_function, with_statement\n\nimport errno\nimport os\nimport sys\nimport socket\nimport stat\n\nfrom tornado.concurrent import dummy_executor, run_on_executor\nfrom tornado.ioloop import IOLoop\nfrom tornado.platform.auto import set_close_exec\nfrom tornado.util import u, Configurable, errno_from_exception\n\ntry:\n    import ssl\nexcept ImportError:\n    # ssl is not available on Google App Engine\n    ssl = None\n\ntry:\n    import certifi\nexcept ImportError:\n    # certifi is optional as long as we have ssl.create_default_context.\n    if ssl is None or hasattr(ssl, 'create_default_context'):\n        certifi = None\n    else:\n        raise\n\ntry:\n    xrange  # py2\nexcept NameError:\n    xrange = range  # py3\n\nif hasattr(ssl, 'match_hostname') and hasattr(ssl, 'CertificateError'):  # python 3.2+\n    ssl_match_hostname = ssl.match_hostname\n    SSLCertificateError = ssl.CertificateError\nelif ssl is None:\n    ssl_match_hostname = SSLCertificateError = None\nelse:\n    import backports.ssl_match_hostname\n    ssl_match_hostname = backports.ssl_match_hostname.match_hostname\n    SSLCertificateError = backports.ssl_match_hostname.CertificateError\n\nif hasattr(ssl, 'SSLContext'):\n    if hasattr(ssl, 'create_default_context'):\n        # Python 2.7.9+, 3.4+\n        # Note that the naming of ssl.Purpose is confusing; the purpose\n        # of a context is to authentiate the opposite side of the connection.\n        _client_ssl_defaults = ssl.create_default_context(\n            ssl.Purpose.SERVER_AUTH)\n        _server_ssl_defaults = ssl.create_default_context(\n            ssl.Purpose.CLIENT_AUTH)\n    else:\n        # Python 3.2-3.3\n        _client_ssl_defaults = ssl.SSLContext(ssl.PROTOCOL_SSLv23)\n        _client_ssl_defaults.verify_mode = ssl.CERT_REQUIRED\n        _client_ssl_defaults.load_verify_locations(certifi.where())\n        _server_ssl_defaults = ssl.SSLContext(ssl.PROTOCOL_SSLv23)\n        if hasattr(ssl, 'OP_NO_COMPRESSION'):\n            # Disable TLS compression to avoid CRIME and related attacks.\n            # This constant wasn't added until python 3.3.\n            _client_ssl_defaults.options |= ssl.OP_NO_COMPRESSION\n            _server_ssl_defaults.options |= ssl.OP_NO_COMPRESSION\n\nelif ssl:\n    # Python 2.6-2.7.8\n    _client_ssl_defaults = dict(cert_reqs=ssl.CERT_REQUIRED,\n                                ca_certs=certifi.where())\n    _server_ssl_defaults = {}\nelse:\n    # Google App Engine\n    _client_ssl_defaults = dict(cert_reqs=None,\n                                ca_certs=None)\n    _server_ssl_defaults = {}\n\n# ThreadedResolver runs getaddrinfo on a thread. If the hostname is unicode,\n# getaddrinfo attempts to import encodings.idna. If this is done at\n# module-import time, the import lock is already held by the main thread,\n# leading to deadlock. Avoid it by caching the idna encoder on the main\n# thread now.\nu('foo').encode('idna')\n\n# These errnos indicate that a non-blocking operation must be retried\n# at a later time.  On most platforms they're the same value, but on\n# some they differ.\n_ERRNO_WOULDBLOCK = (errno.EWOULDBLOCK, errno.EAGAIN)\n\nif hasattr(errno, \"WSAEWOULDBLOCK\"):\n    _ERRNO_WOULDBLOCK += (errno.WSAEWOULDBLOCK,)\n\n# Default backlog used when calling sock.listen()\n_DEFAULT_BACKLOG = 128\n\n\ndef bind_sockets(port, address=None, family=socket.AF_UNSPEC,\n                 backlog=_DEFAULT_BACKLOG, flags=None, reuse_port=False):\n    \"\"\"Creates listening sockets bound to the given port and address.\n\n    Returns a list of socket objects (multiple sockets are returned if\n    the given address maps to multiple IP addresses, which is most common\n    for mixed IPv4 and IPv6 use).\n\n    Address may be either an IP address or hostname.  If it's a hostname,\n    the server will listen on all IP addresses associated with the\n    name.  Address may be an empty string or None to listen on all\n    available interfaces.  Family may be set to either `socket.AF_INET`\n    or `socket.AF_INET6` to restrict to IPv4 or IPv6 addresses, otherwise\n    both will be used if available.\n\n    The ``backlog`` argument has the same meaning as for\n    `socket.listen() <socket.socket.listen>`.\n\n    ``flags`` is a bitmask of AI_* flags to `~socket.getaddrinfo`, like\n    ``socket.AI_PASSIVE | socket.AI_NUMERICHOST``.\n\n    ``resuse_port`` option sets ``SO_REUSEPORT`` option for every socket\n    in the list. If your platform doesn't support this option ValueError will\n    be raised.\n    \"\"\"\n    if reuse_port and not hasattr(socket, \"SO_REUSEPORT\"):\n        raise ValueError(\"the platform doesn't support SO_REUSEPORT\")\n\n    sockets = []\n    if address == \"\":\n        address = None\n    if not socket.has_ipv6 and family == socket.AF_UNSPEC:\n        # Python can be compiled with --disable-ipv6, which causes\n        # operations on AF_INET6 sockets to fail, but does not\n        # automatically exclude those results from getaddrinfo\n        # results.\n        # http://bugs.python.org/issue16208\n        family = socket.AF_INET\n    if flags is None:\n        flags = socket.AI_PASSIVE\n    bound_port = None\n    for res in set(socket.getaddrinfo(address, port, family, socket.SOCK_STREAM,\n                                      0, flags)):\n        af, socktype, proto, canonname, sockaddr = res\n        if (sys.platform == 'darwin' and address == 'localhost' and\n                af == socket.AF_INET6 and sockaddr[3] != 0):\n            # Mac OS X includes a link-local address fe80::1%lo0 in the\n            # getaddrinfo results for 'localhost'.  However, the firewall\n            # doesn't understand that this is a local address and will\n            # prompt for access (often repeatedly, due to an apparent\n            # bug in its ability to remember granting access to an\n            # application). Skip these addresses.\n            continue\n        try:\n            sock = socket.socket(af, socktype, proto)\n        except socket.error as e:\n            if errno_from_exception(e) == errno.EAFNOSUPPORT:\n                continue\n            raise\n        set_close_exec(sock.fileno())\n        if os.name != 'nt':\n            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n        if reuse_port:\n            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)\n        if af == socket.AF_INET6:\n            # On linux, ipv6 sockets accept ipv4 too by default,\n            # but this makes it impossible to bind to both\n            # 0.0.0.0 in ipv4 and :: in ipv6.  On other systems,\n            # separate sockets *must* be used to listen for both ipv4\n            # and ipv6.  For consistency, always disable ipv4 on our\n            # ipv6 sockets and use a separate ipv4 socket when needed.\n            #\n            # Python 2.x on windows doesn't have IPPROTO_IPV6.\n            if hasattr(socket, \"IPPROTO_IPV6\"):\n                sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)\n\n        # automatic port allocation with port=None\n        # should bind on the same port on IPv4 and IPv6\n        host, requested_port = sockaddr[:2]\n        if requested_port == 0 and bound_port is not None:\n            sockaddr = tuple([host, bound_port] + list(sockaddr[2:]))\n\n        sock.setblocking(0)\n        sock.bind(sockaddr)\n        bound_port = sock.getsockname()[1]\n        sock.listen(backlog)\n        sockets.append(sock)\n    return sockets\n\nif hasattr(socket, 'AF_UNIX'):\n    def bind_unix_socket(file, mode=0o600, backlog=_DEFAULT_BACKLOG):\n        \"\"\"Creates a listening unix socket.\n\n        If a socket with the given name already exists, it will be deleted.\n        If any other file with that name exists, an exception will be\n        raised.\n\n        Returns a socket object (not a list of socket objects like\n        `bind_sockets`)\n        \"\"\"\n        sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)\n        set_close_exec(sock.fileno())\n        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n        sock.setblocking(0)\n        try:\n            st = os.stat(file)\n        except OSError as err:\n            if errno_from_exception(err) != errno.ENOENT:\n                raise\n        else:\n            if stat.S_ISSOCK(st.st_mode):\n                os.remove(file)\n            else:\n                raise ValueError(\"File %s exists and is not a socket\", file)\n        sock.bind(file)\n        os.chmod(file, mode)\n        sock.listen(backlog)\n        return sock\n\n\ndef add_accept_handler(sock, callback, io_loop=None):\n    \"\"\"Adds an `.IOLoop` event handler to accept new connections on ``sock``.\n\n    When a connection is accepted, ``callback(connection, address)`` will\n    be run (``connection`` is a socket object, and ``address`` is the\n    address of the other end of the connection).  Note that this signature\n    is different from the ``callback(fd, events)`` signature used for\n    `.IOLoop` handlers.\n\n    .. versionchanged:: 4.1\n       The ``io_loop`` argument is deprecated.\n    \"\"\"\n    if io_loop is None:\n        io_loop = IOLoop.current()\n\n    def accept_handler(fd, events):\n        # More connections may come in while we're handling callbacks;\n        # to prevent starvation of other tasks we must limit the number\n        # of connections we accept at a time.  Ideally we would accept\n        # up to the number of connections that were waiting when we\n        # entered this method, but this information is not available\n        # (and rearranging this method to call accept() as many times\n        # as possible before running any callbacks would have adverse\n        # effects on load balancing in multiprocess configurations).\n        # Instead, we use the (default) listen backlog as a rough\n        # heuristic for the number of connections we can reasonably\n        # accept at once.\n        for i in xrange(_DEFAULT_BACKLOG):\n            try:\n                connection, address = sock.accept()\n            except socket.error as e:\n                # _ERRNO_WOULDBLOCK indicate we have accepted every\n                # connection that is available.\n                if errno_from_exception(e) in _ERRNO_WOULDBLOCK:\n                    return\n                # ECONNABORTED indicates that there was a connection\n                # but it was closed while still in the accept queue.\n                # (observed on FreeBSD).\n                if errno_from_exception(e) == errno.ECONNABORTED:\n                    continue\n                raise\n            callback(connection, address)\n    io_loop.add_handler(sock, accept_handler, IOLoop.READ)\n\n\ndef is_valid_ip(ip):\n    \"\"\"Returns true if the given string is a well-formed IP address.\n\n    Supports IPv4 and IPv6.\n    \"\"\"\n    if not ip or '\\x00' in ip:\n        # getaddrinfo resolves empty strings to localhost, and truncates\n        # on zero bytes.\n        return False\n    try:\n        res = socket.getaddrinfo(ip, 0, socket.AF_UNSPEC,\n                                 socket.SOCK_STREAM,\n                                 0, socket.AI_NUMERICHOST)\n        return bool(res)\n    except socket.gaierror as e:\n        if e.args[0] == socket.EAI_NONAME:\n            return False\n        raise\n    return True\n\n\nclass Resolver(Configurable):\n    \"\"\"Configurable asynchronous DNS resolver interface.\n\n    By default, a blocking implementation is used (which simply calls\n    `socket.getaddrinfo`).  An alternative implementation can be\n    chosen with the `Resolver.configure <.Configurable.configure>`\n    class method::\n\n        Resolver.configure('tornado.netutil.ThreadedResolver')\n\n    The implementations of this interface included with Tornado are\n\n    * `tornado.netutil.BlockingResolver`\n    * `tornado.netutil.ThreadedResolver`\n    * `tornado.netutil.OverrideResolver`\n    * `tornado.platform.twisted.TwistedResolver`\n    * `tornado.platform.caresresolver.CaresResolver`\n    \"\"\"\n    @classmethod\n    def configurable_base(cls):\n        return Resolver\n\n    @classmethod\n    def configurable_default(cls):\n        return BlockingResolver\n\n    def resolve(self, host, port, family=socket.AF_UNSPEC, callback=None):\n        \"\"\"Resolves an address.\n\n        The ``host`` argument is a string which may be a hostname or a\n        literal IP address.\n\n        Returns a `.Future` whose result is a list of (family,\n        address) pairs, where address is a tuple suitable to pass to\n        `socket.connect <socket.socket.connect>` (i.e. a ``(host,\n        port)`` pair for IPv4; additional fields may be present for\n        IPv6). If a ``callback`` is passed, it will be run with the\n        result as an argument when it is complete.\n        \"\"\"\n        raise NotImplementedError()\n\n    def close(self):\n        \"\"\"Closes the `Resolver`, freeing any resources used.\n\n        .. versionadded:: 3.1\n\n        \"\"\"\n        pass\n\n\nclass ExecutorResolver(Resolver):\n    \"\"\"Resolver implementation using a `concurrent.futures.Executor`.\n\n    Use this instead of `ThreadedResolver` when you require additional\n    control over the executor being used.\n\n    The executor will be shut down when the resolver is closed unless\n    ``close_resolver=False``; use this if you want to reuse the same\n    executor elsewhere.\n\n    .. versionchanged:: 4.1\n       The ``io_loop`` argument is deprecated.\n    \"\"\"\n    def initialize(self, io_loop=None, executor=None, close_executor=True):\n        self.io_loop = io_loop or IOLoop.current()\n        if executor is not None:\n            self.executor = executor\n            self.close_executor = close_executor\n        else:\n            self.executor = dummy_executor\n            self.close_executor = False\n\n    def close(self):\n        if self.close_executor:\n            self.executor.shutdown()\n        self.executor = None\n\n    @run_on_executor\n    def resolve(self, host, port, family=socket.AF_UNSPEC):\n        # On Solaris, getaddrinfo fails if the given port is not found\n        # in /etc/services and no socket type is given, so we must pass\n        # one here.  The socket type used here doesn't seem to actually\n        # matter (we discard the one we get back in the results),\n        # so the addresses we return should still be usable with SOCK_DGRAM.\n        addrinfo = socket.getaddrinfo(host, port, family, socket.SOCK_STREAM)\n        results = []\n        for family, socktype, proto, canonname, address in addrinfo:\n            results.append((family, address))\n        return results\n\n\nclass BlockingResolver(ExecutorResolver):\n    \"\"\"Default `Resolver` implementation, using `socket.getaddrinfo`.\n\n    The `.IOLoop` will be blocked during the resolution, although the\n    callback will not be run until the next `.IOLoop` iteration.\n    \"\"\"\n    def initialize(self, io_loop=None):\n        super(BlockingResolver, self).initialize(io_loop=io_loop)\n\n\nclass ThreadedResolver(ExecutorResolver):\n    \"\"\"Multithreaded non-blocking `Resolver` implementation.\n\n    Requires the `concurrent.futures` package to be installed\n    (available in the standard library since Python 3.2,\n    installable with ``pip install futures`` in older versions).\n\n    The thread pool size can be configured with::\n\n        Resolver.configure('tornado.netutil.ThreadedResolver',\n                           num_threads=10)\n\n    .. versionchanged:: 3.1\n       All ``ThreadedResolvers`` share a single thread pool, whose\n       size is set by the first one to be created.\n    \"\"\"\n    _threadpool = None\n    _threadpool_pid = None\n\n    def initialize(self, io_loop=None, num_threads=10):\n        threadpool = ThreadedResolver._create_threadpool(num_threads)\n        super(ThreadedResolver, self).initialize(\n            io_loop=io_loop, executor=threadpool, close_executor=False)\n\n    @classmethod\n    def _create_threadpool(cls, num_threads):\n        pid = os.getpid()\n        if cls._threadpool_pid != pid:\n            # Threads cannot survive after a fork, so if our pid isn't what it\n            # was when we created the pool then delete it.\n            cls._threadpool = None\n        if cls._threadpool is None:\n            from concurrent.futures import ThreadPoolExecutor\n            cls._threadpool = ThreadPoolExecutor(num_threads)\n            cls._threadpool_pid = pid\n        return cls._threadpool\n\n\nclass OverrideResolver(Resolver):\n    \"\"\"Wraps a resolver with a mapping of overrides.\n\n    This can be used to make local DNS changes (e.g. for testing)\n    without modifying system-wide settings.\n\n    The mapping can contain either host strings or host-port pairs.\n    \"\"\"\n    def initialize(self, resolver, mapping):\n        self.resolver = resolver\n        self.mapping = mapping\n\n    def close(self):\n        self.resolver.close()\n\n    def resolve(self, host, port, *args, **kwargs):\n        if (host, port) in self.mapping:\n            host, port = self.mapping[(host, port)]\n        elif host in self.mapping:\n            host = self.mapping[host]\n        return self.resolver.resolve(host, port, *args, **kwargs)\n\n\n# These are the keyword arguments to ssl.wrap_socket that must be translated\n# to their SSLContext equivalents (the other arguments are still passed\n# to SSLContext.wrap_socket).\n_SSL_CONTEXT_KEYWORDS = frozenset(['ssl_version', 'certfile', 'keyfile',\n                                   'cert_reqs', 'ca_certs', 'ciphers'])\n\n\ndef ssl_options_to_context(ssl_options):\n    \"\"\"Try to convert an ``ssl_options`` dictionary to an\n    `~ssl.SSLContext` object.\n\n    The ``ssl_options`` dictionary contains keywords to be passed to\n    `ssl.wrap_socket`.  In Python 2.7.9+, `ssl.SSLContext` objects can\n    be used instead.  This function converts the dict form to its\n    `~ssl.SSLContext` equivalent, and may be used when a component which\n    accepts both forms needs to upgrade to the `~ssl.SSLContext` version\n    to use features like SNI or NPN.\n    \"\"\"\n    if isinstance(ssl_options, dict):\n        assert all(k in _SSL_CONTEXT_KEYWORDS for k in ssl_options), ssl_options\n    if (not hasattr(ssl, 'SSLContext') or\n            isinstance(ssl_options, ssl.SSLContext)):\n        return ssl_options\n    context = ssl.SSLContext(\n        ssl_options.get('ssl_version', ssl.PROTOCOL_SSLv23))\n    if 'certfile' in ssl_options:\n        context.load_cert_chain(ssl_options['certfile'], ssl_options.get('keyfile', None))\n    if 'cert_reqs' in ssl_options:\n        context.verify_mode = ssl_options['cert_reqs']\n    if 'ca_certs' in ssl_options:\n        context.load_verify_locations(ssl_options['ca_certs'])\n    if 'ciphers' in ssl_options:\n        context.set_ciphers(ssl_options['ciphers'])\n    if hasattr(ssl, 'OP_NO_COMPRESSION'):\n        # Disable TLS compression to avoid CRIME and related attacks.\n        # This constant wasn't added until python 3.3.\n        context.options |= ssl.OP_NO_COMPRESSION\n    return context\n\n\ndef ssl_wrap_socket(socket, ssl_options, server_hostname=None, **kwargs):\n    \"\"\"Returns an ``ssl.SSLSocket`` wrapping the given socket.\n\n    ``ssl_options`` may be either an `ssl.SSLContext` object or a\n    dictionary (as accepted by `ssl_options_to_context`).  Additional\n    keyword arguments are passed to ``wrap_socket`` (either the\n    `~ssl.SSLContext` method or the `ssl` module function as\n    appropriate).\n    \"\"\"\n    context = ssl_options_to_context(ssl_options)\n    if hasattr(ssl, 'SSLContext') and isinstance(context, ssl.SSLContext):\n        if server_hostname is not None and getattr(ssl, 'HAS_SNI'):\n            # Python doesn't have server-side SNI support so we can't\n            # really unittest this, but it can be manually tested with\n            # python3.2 -m tornado.httpclient https://sni.velox.ch\n            return context.wrap_socket(socket, server_hostname=server_hostname,\n                                       **kwargs)\n        else:\n            return context.wrap_socket(socket, **kwargs)\n    else:\n        return ssl.wrap_socket(socket, **dict(context, **kwargs))\n",
            "header": {
                "file": {
                    "path": "tornado/netutil.py"
                },
                "misc_line": null
            },
            "index_line": {
                "old_commit_hash": "9aa292c4",
                "new_commit_hash": "4fc8d04d",
                "mode": "100644"
            },
            "is_binary_file": false,
            "binary_line": null,
            "minus_file": {
                "path": "a/tornado/netutil.py"
            },
            "plus_file": {
                "path": "b/tornado/netutil.py"
            },
            "hunks": [
                {
                    "descriptor": {
                        "old_range": {
                            "start": 111,
                            "length": 7
                        },
                        "new_range": {
                            "start": 111,
                            "length": 7
                        },
                        "section": "_DEFAULT_BACKLOG = 128"
                    },
                    "line_group": {
                        "all_lines": [
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "def bind_sockets(port, address=None, family=socket.AF_UNSPEC,",
                                "type": "context"
                            },
                            {
                                "content": "                 backlog=_DEFAULT_BACKLOG, flags=None):",
                                "type": "deleted"
                            },
                            {
                                "content": "                 backlog=_DEFAULT_BACKLOG, flags=None, reuse_port=False):",
                                "type": "added"
                            },
                            {
                                "content": "    \"\"\"Creates listening sockets bound to the given port and address.",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "    Returns a list of socket objects (multiple sockets are returned if",
                                "type": "context"
                            }
                        ]
                    },
                    "modified_entities": [
                        {
                            "file_name": "tornado/netutil.py",
                            "type": "function",
                            "name": "bind_sockets",
                            "content": "def bind_sockets(port, address=None, family=socket.AF_UNSPEC,\n                 backlog=_DEFAULT_BACKLOG, flags=None):\n    \"\"\"Creates listening sockets bound to the given port and address.\n\n    Returns a list of socket objects (multiple sockets are returned if\n    the given address maps to multiple IP addresses, which is most common\n    for mixed IPv4 and IPv6 use).\n\n    Address may be either an IP address or hostname.  If it's a hostname,\n    the server will listen on all IP addresses associated with the\n    name.  Address may be an empty string or None to listen on all\n    available interfaces.  Family may be set to either `socket.AF_INET`\n    or `socket.AF_INET6` to restrict to IPv4 or IPv6 addresses, otherwise\n    both will be used if available.\n\n    The ``backlog`` argument has the same meaning as for\n    `socket.listen() <socket.socket.listen>`.\n\n    ``flags`` is a bitmask of AI_* flags to `~socket.getaddrinfo`, like\n    ``socket.AI_PASSIVE | socket.AI_NUMERICHOST``.\n    \"\"\"\n    sockets = []\n    if address == \"\":\n        address = None\n    if not socket.has_ipv6 and family == socket.AF_UNSPEC:\n        # Python can be compiled with --disable-ipv6, which causes\n        # operations on AF_INET6 sockets to fail, but does not\n        # automatically exclude those results from getaddrinfo\n        # results.\n        # http://bugs.python.org/issue16208\n        family = socket.AF_INET\n    if flags is None:\n        flags = socket.AI_PASSIVE\n    bound_port = None\n    for res in set(socket.getaddrinfo(address, port, family, socket.SOCK_STREAM,\n                                      0, flags)):\n        af, socktype, proto, canonname, sockaddr = res\n        if (sys.platform == 'darwin' and address == 'localhost' and\n                af == socket.AF_INET6 and sockaddr[3] != 0):\n            # Mac OS X includes a link-local address fe80::1%lo0 in the\n            # getaddrinfo results for 'localhost'.  However, the firewall\n            # doesn't understand that this is a local address and will\n            # prompt for access (often repeatedly, due to an apparent\n            # bug in its ability to remember granting access to an\n            # application). Skip these addresses.\n            continue\n        try:\n            sock = socket.socket(af, socktype, proto)\n        except socket.error as e:\n            if errno_from_exception(e) == errno.EAFNOSUPPORT:\n                continue\n            raise\n        set_close_exec(sock.fileno())\n        if os.name != 'nt':\n            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n        if af == socket.AF_INET6:\n            # On linux, ipv6 sockets accept ipv4 too by default,\n            # but this makes it impossible to bind to both\n            # 0.0.0.0 in ipv4 and :: in ipv6.  On other systems,\n            # separate sockets *must* be used to listen for both ipv4\n            # and ipv6.  For consistency, always disable ipv4 on our\n            # ipv6 sockets and use a separate ipv4 socket when needed.\n            #\n            # Python 2.x on windows doesn't have IPPROTO_IPV6.\n            if hasattr(socket, \"IPPROTO_IPV6\"):\n                sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)\n\n        # automatic port allocation with port=None\n        # should bind on the same port on IPv4 and IPv6\n        host, requested_port = sockaddr[:2]\n        if requested_port == 0 and bound_port is not None:\n            sockaddr = tuple([host, bound_port] + list(sockaddr[2:]))\n\n        sock.setblocking(0)\n        sock.bind(sockaddr)\n        bound_port = sock.getsockname()[1]\n        sock.listen(backlog)\n        sockets.append(sock)\n    return sockets",
                            "ast_type_str": "FunctionDef",
                            "start_lineno": 113,
                            "end_lineno": 191,
                            "parent": null
                        }
                    ],
                    "added_entities": [],
                    "deleted_entities": []
                },
                {
                    "descriptor": {
                        "old_range": {
                            "start": 130,
                            "length": 7
                        },
                        "new_range": {
                            "start": 130,
                            "length": 14
                        },
                        "section": "def bind_sockets(port, address=None, family=socket.AF_UNSPEC,"
                    },
                    "line_group": {
                        "all_lines": [
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "    ``flags`` is a bitmask of AI_* flags to `~socket.getaddrinfo`, like",
                                "type": "context"
                            },
                            {
                                "content": "    ``socket.AI_PASSIVE | socket.AI_NUMERICHOST``.",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "added"
                            },
                            {
                                "content": "    ``resuse_port`` option sets ``SO_REUSEPORT`` option for every socket",
                                "type": "added"
                            },
                            {
                                "content": "    in the list. If your platform doesn't support this option ValueError will",
                                "type": "added"
                            },
                            {
                                "content": "    be raised.",
                                "type": "added"
                            },
                            {
                                "content": "    \"\"\"",
                                "type": "context"
                            },
                            {
                                "content": "    if reuse_port and not hasattr(socket, \"SO_REUSEPORT\"):",
                                "type": "added"
                            },
                            {
                                "content": "        raise ValueError(\"the platform doesn't support SO_REUSEPORT\")",
                                "type": "added"
                            },
                            {
                                "content": "",
                                "type": "added"
                            },
                            {
                                "content": "    sockets = []",
                                "type": "context"
                            },
                            {
                                "content": "    if address == \"\":",
                                "type": "context"
                            },
                            {
                                "content": "        address = None",
                                "type": "context"
                            }
                        ]
                    },
                    "modified_entities": [
                        {
                            "file_name": "tornado/netutil.py",
                            "type": "function",
                            "name": "bind_sockets",
                            "content": "def bind_sockets(port, address=None, family=socket.AF_UNSPEC,\n                 backlog=_DEFAULT_BACKLOG, flags=None, reuse_port=False):\n    \"\"\"Creates listening sockets bound to the given port and address.\n\n    Returns a list of socket objects (multiple sockets are returned if\n    the given address maps to multiple IP addresses, which is most common\n    for mixed IPv4 and IPv6 use).\n\n    Address may be either an IP address or hostname.  If it's a hostname,\n    the server will listen on all IP addresses associated with the\n    name.  Address may be an empty string or None to listen on all\n    available interfaces.  Family may be set to either `socket.AF_INET`\n    or `socket.AF_INET6` to restrict to IPv4 or IPv6 addresses, otherwise\n    both will be used if available.\n\n    The ``backlog`` argument has the same meaning as for\n    `socket.listen() <socket.socket.listen>`.\n\n    ``flags`` is a bitmask of AI_* flags to `~socket.getaddrinfo`, like\n    ``socket.AI_PASSIVE | socket.AI_NUMERICHOST``.\n\n    ``resuse_port`` option sets ``SO_REUSEPORT`` option for every socket\n    in the list. If your platform doesn't support this option ValueError will\n    be raised.\n    \"\"\"\n    if reuse_port and not hasattr(socket, \"SO_REUSEPORT\"):\n        raise ValueError(\"the platform doesn't support SO_REUSEPORT\")\n\n    sockets = []\n    if address == \"\":\n        address = None\n    if not socket.has_ipv6 and family == socket.AF_UNSPEC:\n        # Python can be compiled with --disable-ipv6, which causes\n        # operations on AF_INET6 sockets to fail, but does not\n        # automatically exclude those results from getaddrinfo\n        # results.\n        # http://bugs.python.org/issue16208\n        family = socket.AF_INET\n    if flags is None:\n        flags = socket.AI_PASSIVE\n    bound_port = None\n    for res in set(socket.getaddrinfo(address, port, family, socket.SOCK_STREAM,\n                                      0, flags)):\n        af, socktype, proto, canonname, sockaddr = res\n        if (sys.platform == 'darwin' and address == 'localhost' and\n                af == socket.AF_INET6 and sockaddr[3] != 0):\n            # Mac OS X includes a link-local address fe80::1%lo0 in the\n            # getaddrinfo results for 'localhost'.  However, the firewall\n            # doesn't understand that this is a local address and will\n            # prompt for access (often repeatedly, due to an apparent\n            # bug in its ability to remember granting access to an\n            # application). Skip these addresses.\n            continue\n        try:\n            sock = socket.socket(af, socktype, proto)\n        except socket.error as e:\n            if errno_from_exception(e) == errno.EAFNOSUPPORT:\n                continue\n            raise\n        set_close_exec(sock.fileno())\n        if os.name != 'nt':\n            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n        if reuse_port:\n            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)\n        if af == socket.AF_INET6:\n            # On linux, ipv6 sockets accept ipv4 too by default,\n            # but this makes it impossible to bind to both\n            # 0.0.0.0 in ipv4 and :: in ipv6.  On other systems,\n            # separate sockets *must* be used to listen for both ipv4\n            # and ipv6.  For consistency, always disable ipv4 on our\n            # ipv6 sockets and use a separate ipv4 socket when needed.\n            #\n            # Python 2.x on windows doesn't have IPPROTO_IPV6.\n            if hasattr(socket, \"IPPROTO_IPV6\"):\n                sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)\n\n        # automatic port allocation with port=None\n        # should bind on the same port on IPv4 and IPv6\n        host, requested_port = sockaddr[:2]\n        if requested_port == 0 and bound_port is not None:\n            sockaddr = tuple([host, bound_port] + list(sockaddr[2:]))\n\n        sock.setblocking(0)\n        sock.bind(sockaddr)\n        bound_port = sock.getsockname()[1]\n        sock.listen(backlog)\n        sockets.append(sock)\n    return sockets",
                            "ast_type_str": "FunctionDef",
                            "start_lineno": 113,
                            "end_lineno": 200,
                            "parent": null
                        }
                    ],
                    "added_entities": [],
                    "deleted_entities": []
                },
                {
                    "descriptor": {
                        "old_range": {
                            "start": 165,
                            "length": 6
                        },
                        "new_range": {
                            "start": 172,
                            "length": 8
                        },
                        "section": "def bind_sockets(port, address=None, family=socket.AF_UNSPEC,"
                    },
                    "line_group": {
                        "all_lines": [
                            {
                                "content": "        set_close_exec(sock.fileno())",
                                "type": "context"
                            },
                            {
                                "content": "        if os.name != 'nt':",
                                "type": "context"
                            },
                            {
                                "content": "            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)",
                                "type": "context"
                            },
                            {
                                "content": "        if reuse_port:",
                                "type": "added"
                            },
                            {
                                "content": "            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)",
                                "type": "added"
                            },
                            {
                                "content": "        if af == socket.AF_INET6:",
                                "type": "context"
                            },
                            {
                                "content": "            # On linux, ipv6 sockets accept ipv4 too by default,",
                                "type": "context"
                            },
                            {
                                "content": "            # but this makes it impossible to bind to both",
                                "type": "context"
                            }
                        ]
                    },
                    "modified_entities": [
                        {
                            "file_name": "tornado/netutil.py",
                            "type": "function",
                            "name": "bind_sockets",
                            "content": "def bind_sockets(port, address=None, family=socket.AF_UNSPEC,\n                 backlog=_DEFAULT_BACKLOG, flags=None, reuse_port=False):\n    \"\"\"Creates listening sockets bound to the given port and address.\n\n    Returns a list of socket objects (multiple sockets are returned if\n    the given address maps to multiple IP addresses, which is most common\n    for mixed IPv4 and IPv6 use).\n\n    Address may be either an IP address or hostname.  If it's a hostname,\n    the server will listen on all IP addresses associated with the\n    name.  Address may be an empty string or None to listen on all\n    available interfaces.  Family may be set to either `socket.AF_INET`\n    or `socket.AF_INET6` to restrict to IPv4 or IPv6 addresses, otherwise\n    both will be used if available.\n\n    The ``backlog`` argument has the same meaning as for\n    `socket.listen() <socket.socket.listen>`.\n\n    ``flags`` is a bitmask of AI_* flags to `~socket.getaddrinfo`, like\n    ``socket.AI_PASSIVE | socket.AI_NUMERICHOST``.\n\n    ``resuse_port`` option sets ``SO_REUSEPORT`` option for every socket\n    in the list. If your platform doesn't support this option ValueError will\n    be raised.\n    \"\"\"\n    if reuse_port and not hasattr(socket, \"SO_REUSEPORT\"):\n        raise ValueError(\"the platform doesn't support SO_REUSEPORT\")\n\n    sockets = []\n    if address == \"\":\n        address = None\n    if not socket.has_ipv6 and family == socket.AF_UNSPEC:\n        # Python can be compiled with --disable-ipv6, which causes\n        # operations on AF_INET6 sockets to fail, but does not\n        # automatically exclude those results from getaddrinfo\n        # results.\n        # http://bugs.python.org/issue16208\n        family = socket.AF_INET\n    if flags is None:\n        flags = socket.AI_PASSIVE\n    bound_port = None\n    for res in set(socket.getaddrinfo(address, port, family, socket.SOCK_STREAM,\n                                      0, flags)):\n        af, socktype, proto, canonname, sockaddr = res\n        if (sys.platform == 'darwin' and address == 'localhost' and\n                af == socket.AF_INET6 and sockaddr[3] != 0):\n            # Mac OS X includes a link-local address fe80::1%lo0 in the\n            # getaddrinfo results for 'localhost'.  However, the firewall\n            # doesn't understand that this is a local address and will\n            # prompt for access (often repeatedly, due to an apparent\n            # bug in its ability to remember granting access to an\n            # application). Skip these addresses.\n            continue\n        try:\n            sock = socket.socket(af, socktype, proto)\n        except socket.error as e:\n            if errno_from_exception(e) == errno.EAFNOSUPPORT:\n                continue\n            raise\n        set_close_exec(sock.fileno())\n        if os.name != 'nt':\n            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n        if reuse_port:\n            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)\n        if af == socket.AF_INET6:\n            # On linux, ipv6 sockets accept ipv4 too by default,\n            # but this makes it impossible to bind to both\n            # 0.0.0.0 in ipv4 and :: in ipv6.  On other systems,\n            # separate sockets *must* be used to listen for both ipv4\n            # and ipv6.  For consistency, always disable ipv4 on our\n            # ipv6 sockets and use a separate ipv4 socket when needed.\n            #\n            # Python 2.x on windows doesn't have IPPROTO_IPV6.\n            if hasattr(socket, \"IPPROTO_IPV6\"):\n                sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)\n\n        # automatic port allocation with port=None\n        # should bind on the same port on IPv4 and IPv6\n        host, requested_port = sockaddr[:2]\n        if requested_port == 0 and bound_port is not None:\n            sockaddr = tuple([host, bound_port] + list(sockaddr[2:]))\n\n        sock.setblocking(0)\n        sock.bind(sockaddr)\n        bound_port = sock.getsockname()[1]\n        sock.listen(backlog)\n        sockets.append(sock)\n    return sockets",
                            "ast_type_str": "FunctionDef",
                            "start_lineno": 113,
                            "end_lineno": 200,
                            "parent": null
                        }
                    ],
                    "added_entities": [],
                    "deleted_entities": []
                }
            ]
        },
        {
            "old_file_content": "from __future__ import absolute_import, division, print_function, with_statement\n\nimport os\nimport signal\nimport socket\nfrom subprocess import Popen\nimport sys\nimport time\n\nfrom tornado.netutil import BlockingResolver, ThreadedResolver, is_valid_ip, bind_sockets\nfrom tornado.stack_context import ExceptionStackContext\nfrom tornado.testing import AsyncTestCase, gen_test\nfrom tornado.test.util import unittest, skipIfNoNetwork\n\ntry:\n    from concurrent import futures\nexcept ImportError:\n    futures = None\n\ntry:\n    import pycares\nexcept ImportError:\n    pycares = None\nelse:\n    from tornado.platform.caresresolver import CaresResolver\n\ntry:\n    import twisted\n    import twisted.names\nexcept ImportError:\n    twisted = None\nelse:\n    from tornado.platform.twisted import TwistedResolver\n\n\nclass _ResolverTestMixin(object):\n    def test_localhost(self):\n        self.resolver.resolve('localhost', 80, callback=self.stop)\n        result = self.wait()\n        self.assertIn((socket.AF_INET, ('127.0.0.1', 80)), result)\n\n    @gen_test\n    def test_future_interface(self):\n        addrinfo = yield self.resolver.resolve('localhost', 80,\n                                               socket.AF_UNSPEC)\n        self.assertIn((socket.AF_INET, ('127.0.0.1', 80)),\n                      addrinfo)\n\n\n# It is impossible to quickly and consistently generate an error in name\n# resolution, so test this case separately, using mocks as needed.\nclass _ResolverErrorTestMixin(object):\n    def test_bad_host(self):\n        def handler(exc_typ, exc_val, exc_tb):\n            self.stop(exc_val)\n            return True  # Halt propagation.\n\n        with ExceptionStackContext(handler):\n            self.resolver.resolve('an invalid domain', 80, callback=self.stop)\n\n        result = self.wait()\n        self.assertIsInstance(result, Exception)\n\n    @gen_test\n    def test_future_interface_bad_host(self):\n        with self.assertRaises(Exception):\n            yield self.resolver.resolve('an invalid domain', 80,\n                                        socket.AF_UNSPEC)\n\n\ndef _failing_getaddrinfo(*args):\n    \"\"\"Dummy implementation of getaddrinfo for use in mocks\"\"\"\n    raise socket.gaierror(\"mock: lookup failed\")\n\n\n@skipIfNoNetwork\nclass BlockingResolverTest(AsyncTestCase, _ResolverTestMixin):\n    def setUp(self):\n        super(BlockingResolverTest, self).setUp()\n        self.resolver = BlockingResolver(io_loop=self.io_loop)\n\n\n# getaddrinfo-based tests need mocking to reliably generate errors;\n# some configurations are slow to produce errors and take longer than\n# our default timeout.\nclass BlockingResolverErrorTest(AsyncTestCase, _ResolverErrorTestMixin):\n    def setUp(self):\n        super(BlockingResolverErrorTest, self).setUp()\n        self.resolver = BlockingResolver(io_loop=self.io_loop)\n        self.real_getaddrinfo = socket.getaddrinfo\n        socket.getaddrinfo = _failing_getaddrinfo\n\n    def tearDown(self):\n        socket.getaddrinfo = self.real_getaddrinfo\n        super(BlockingResolverErrorTest, self).tearDown()\n\n\n@skipIfNoNetwork\n@unittest.skipIf(futures is None, \"futures module not present\")\nclass ThreadedResolverTest(AsyncTestCase, _ResolverTestMixin):\n    def setUp(self):\n        super(ThreadedResolverTest, self).setUp()\n        self.resolver = ThreadedResolver(io_loop=self.io_loop)\n\n    def tearDown(self):\n        self.resolver.close()\n        super(ThreadedResolverTest, self).tearDown()\n\n\nclass ThreadedResolverErrorTest(AsyncTestCase, _ResolverErrorTestMixin):\n    def setUp(self):\n        super(ThreadedResolverErrorTest, self).setUp()\n        self.resolver = BlockingResolver(io_loop=self.io_loop)\n        self.real_getaddrinfo = socket.getaddrinfo\n        socket.getaddrinfo = _failing_getaddrinfo\n\n    def tearDown(self):\n        socket.getaddrinfo = self.real_getaddrinfo\n        super(ThreadedResolverErrorTest, self).tearDown()\n\n\n@skipIfNoNetwork\n@unittest.skipIf(futures is None, \"futures module not present\")\n@unittest.skipIf(sys.platform == 'win32', \"preexec_fn not available on win32\")\nclass ThreadedResolverImportTest(unittest.TestCase):\n    def test_import(self):\n        TIMEOUT = 5\n\n        # Test for a deadlock when importing a module that runs the\n        # ThreadedResolver at import-time. See resolve_test.py for\n        # full explanation.\n        command = [\n            sys.executable,\n            '-c',\n            'import tornado.test.resolve_test_helper']\n\n        start = time.time()\n        popen = Popen(command, preexec_fn=lambda: signal.alarm(TIMEOUT))\n        while time.time() - start < TIMEOUT:\n            return_code = popen.poll()\n            if return_code is not None:\n                self.assertEqual(0, return_code)\n                return  # Success.\n            time.sleep(0.05)\n\n        self.fail(\"import timed out\")\n\n\n# We do not test errors with CaresResolver:\n# Some DNS-hijacking ISPs (e.g. Time Warner) return non-empty results\n# with an NXDOMAIN status code.  Most resolvers treat this as an error;\n# C-ares returns the results, making the \"bad_host\" tests unreliable.\n# C-ares will try to resolve even malformed names, such as the\n# name with spaces used in this test.\n@skipIfNoNetwork\n@unittest.skipIf(pycares is None, \"pycares module not present\")\nclass CaresResolverTest(AsyncTestCase, _ResolverTestMixin):\n    def setUp(self):\n        super(CaresResolverTest, self).setUp()\n        self.resolver = CaresResolver(io_loop=self.io_loop)\n\n\n# TwistedResolver produces consistent errors in our test cases so we\n# can test the regular and error cases in the same class.\n@skipIfNoNetwork\n@unittest.skipIf(twisted is None, \"twisted module not present\")\n@unittest.skipIf(getattr(twisted, '__version__', '0.0') < \"12.1\", \"old version of twisted\")\nclass TwistedResolverTest(AsyncTestCase, _ResolverTestMixin,\n                          _ResolverErrorTestMixin):\n    def setUp(self):\n        super(TwistedResolverTest, self).setUp()\n        self.resolver = TwistedResolver(io_loop=self.io_loop)\n\n\nclass IsValidIPTest(unittest.TestCase):\n    def test_is_valid_ip(self):\n        self.assertTrue(is_valid_ip('127.0.0.1'))\n        self.assertTrue(is_valid_ip('4.4.4.4'))\n        self.assertTrue(is_valid_ip('::1'))\n        self.assertTrue(is_valid_ip('2620:0:1cfe:face:b00c::3'))\n        self.assertTrue(not is_valid_ip('www.google.com'))\n        self.assertTrue(not is_valid_ip('localhost'))\n        self.assertTrue(not is_valid_ip('4.4.4.4<'))\n        self.assertTrue(not is_valid_ip(' 127.0.0.1'))\n        self.assertTrue(not is_valid_ip(''))\n        self.assertTrue(not is_valid_ip(' '))\n        self.assertTrue(not is_valid_ip('\\n'))\n        self.assertTrue(not is_valid_ip('\\x00'))\n\n\nclass TestPortAllocation(unittest.TestCase):\n    def test_same_port_allocation(self):\n        if 'TRAVIS' in os.environ:\n            self.skipTest(\"dual-stack servers often have port conflicts on travis\")\n        sockets = bind_sockets(None, 'localhost')\n        try:\n            port = sockets[0].getsockname()[1]\n            self.assertTrue(all(s.getsockname()[1] == port\n                                for s in sockets[1:]))\n        finally:\n            for sock in sockets:\n                sock.close()\n",
            "new_file_content": "from __future__ import absolute_import, division, print_function, with_statement\n\nimport os\nimport signal\nimport socket\nfrom subprocess import Popen\nimport sys\nimport time\n\nfrom tornado.netutil import BlockingResolver, ThreadedResolver, is_valid_ip, bind_sockets\nfrom tornado.stack_context import ExceptionStackContext\nfrom tornado.testing import AsyncTestCase, gen_test, bind_unused_port\nfrom tornado.test.util import unittest, skipIfNoNetwork\n\ntry:\n    from concurrent import futures\nexcept ImportError:\n    futures = None\n\ntry:\n    import pycares\nexcept ImportError:\n    pycares = None\nelse:\n    from tornado.platform.caresresolver import CaresResolver\n\ntry:\n    import twisted\n    import twisted.names\nexcept ImportError:\n    twisted = None\nelse:\n    from tornado.platform.twisted import TwistedResolver\n\n\nclass _ResolverTestMixin(object):\n    def test_localhost(self):\n        self.resolver.resolve('localhost', 80, callback=self.stop)\n        result = self.wait()\n        self.assertIn((socket.AF_INET, ('127.0.0.1', 80)), result)\n\n    @gen_test\n    def test_future_interface(self):\n        addrinfo = yield self.resolver.resolve('localhost', 80,\n                                               socket.AF_UNSPEC)\n        self.assertIn((socket.AF_INET, ('127.0.0.1', 80)),\n                      addrinfo)\n\n\n# It is impossible to quickly and consistently generate an error in name\n# resolution, so test this case separately, using mocks as needed.\nclass _ResolverErrorTestMixin(object):\n    def test_bad_host(self):\n        def handler(exc_typ, exc_val, exc_tb):\n            self.stop(exc_val)\n            return True  # Halt propagation.\n\n        with ExceptionStackContext(handler):\n            self.resolver.resolve('an invalid domain', 80, callback=self.stop)\n\n        result = self.wait()\n        self.assertIsInstance(result, Exception)\n\n    @gen_test\n    def test_future_interface_bad_host(self):\n        with self.assertRaises(Exception):\n            yield self.resolver.resolve('an invalid domain', 80,\n                                        socket.AF_UNSPEC)\n\n\ndef _failing_getaddrinfo(*args):\n    \"\"\"Dummy implementation of getaddrinfo for use in mocks\"\"\"\n    raise socket.gaierror(\"mock: lookup failed\")\n\n\n@skipIfNoNetwork\nclass BlockingResolverTest(AsyncTestCase, _ResolverTestMixin):\n    def setUp(self):\n        super(BlockingResolverTest, self).setUp()\n        self.resolver = BlockingResolver(io_loop=self.io_loop)\n\n\n# getaddrinfo-based tests need mocking to reliably generate errors;\n# some configurations are slow to produce errors and take longer than\n# our default timeout.\nclass BlockingResolverErrorTest(AsyncTestCase, _ResolverErrorTestMixin):\n    def setUp(self):\n        super(BlockingResolverErrorTest, self).setUp()\n        self.resolver = BlockingResolver(io_loop=self.io_loop)\n        self.real_getaddrinfo = socket.getaddrinfo\n        socket.getaddrinfo = _failing_getaddrinfo\n\n    def tearDown(self):\n        socket.getaddrinfo = self.real_getaddrinfo\n        super(BlockingResolverErrorTest, self).tearDown()\n\n\n@skipIfNoNetwork\n@unittest.skipIf(futures is None, \"futures module not present\")\nclass ThreadedResolverTest(AsyncTestCase, _ResolverTestMixin):\n    def setUp(self):\n        super(ThreadedResolverTest, self).setUp()\n        self.resolver = ThreadedResolver(io_loop=self.io_loop)\n\n    def tearDown(self):\n        self.resolver.close()\n        super(ThreadedResolverTest, self).tearDown()\n\n\nclass ThreadedResolverErrorTest(AsyncTestCase, _ResolverErrorTestMixin):\n    def setUp(self):\n        super(ThreadedResolverErrorTest, self).setUp()\n        self.resolver = BlockingResolver(io_loop=self.io_loop)\n        self.real_getaddrinfo = socket.getaddrinfo\n        socket.getaddrinfo = _failing_getaddrinfo\n\n    def tearDown(self):\n        socket.getaddrinfo = self.real_getaddrinfo\n        super(ThreadedResolverErrorTest, self).tearDown()\n\n\n@skipIfNoNetwork\n@unittest.skipIf(futures is None, \"futures module not present\")\n@unittest.skipIf(sys.platform == 'win32', \"preexec_fn not available on win32\")\nclass ThreadedResolverImportTest(unittest.TestCase):\n    def test_import(self):\n        TIMEOUT = 5\n\n        # Test for a deadlock when importing a module that runs the\n        # ThreadedResolver at import-time. See resolve_test.py for\n        # full explanation.\n        command = [\n            sys.executable,\n            '-c',\n            'import tornado.test.resolve_test_helper']\n\n        start = time.time()\n        popen = Popen(command, preexec_fn=lambda: signal.alarm(TIMEOUT))\n        while time.time() - start < TIMEOUT:\n            return_code = popen.poll()\n            if return_code is not None:\n                self.assertEqual(0, return_code)\n                return  # Success.\n            time.sleep(0.05)\n\n        self.fail(\"import timed out\")\n\n\n# We do not test errors with CaresResolver:\n# Some DNS-hijacking ISPs (e.g. Time Warner) return non-empty results\n# with an NXDOMAIN status code.  Most resolvers treat this as an error;\n# C-ares returns the results, making the \"bad_host\" tests unreliable.\n# C-ares will try to resolve even malformed names, such as the\n# name with spaces used in this test.\n@skipIfNoNetwork\n@unittest.skipIf(pycares is None, \"pycares module not present\")\nclass CaresResolverTest(AsyncTestCase, _ResolverTestMixin):\n    def setUp(self):\n        super(CaresResolverTest, self).setUp()\n        self.resolver = CaresResolver(io_loop=self.io_loop)\n\n\n# TwistedResolver produces consistent errors in our test cases so we\n# can test the regular and error cases in the same class.\n@skipIfNoNetwork\n@unittest.skipIf(twisted is None, \"twisted module not present\")\n@unittest.skipIf(getattr(twisted, '__version__', '0.0') < \"12.1\", \"old version of twisted\")\nclass TwistedResolverTest(AsyncTestCase, _ResolverTestMixin,\n                          _ResolverErrorTestMixin):\n    def setUp(self):\n        super(TwistedResolverTest, self).setUp()\n        self.resolver = TwistedResolver(io_loop=self.io_loop)\n\n\nclass IsValidIPTest(unittest.TestCase):\n    def test_is_valid_ip(self):\n        self.assertTrue(is_valid_ip('127.0.0.1'))\n        self.assertTrue(is_valid_ip('4.4.4.4'))\n        self.assertTrue(is_valid_ip('::1'))\n        self.assertTrue(is_valid_ip('2620:0:1cfe:face:b00c::3'))\n        self.assertTrue(not is_valid_ip('www.google.com'))\n        self.assertTrue(not is_valid_ip('localhost'))\n        self.assertTrue(not is_valid_ip('4.4.4.4<'))\n        self.assertTrue(not is_valid_ip(' 127.0.0.1'))\n        self.assertTrue(not is_valid_ip(''))\n        self.assertTrue(not is_valid_ip(' '))\n        self.assertTrue(not is_valid_ip('\\n'))\n        self.assertTrue(not is_valid_ip('\\x00'))\n\n\nclass TestPortAllocation(unittest.TestCase):\n    def test_same_port_allocation(self):\n        if 'TRAVIS' in os.environ:\n            self.skipTest(\"dual-stack servers often have port conflicts on travis\")\n        sockets = bind_sockets(None, 'localhost')\n        try:\n            port = sockets[0].getsockname()[1]\n            self.assertTrue(all(s.getsockname()[1] == port\n                                for s in sockets[1:]))\n        finally:\n            for sock in sockets:\n                sock.close()\n\n    @unittest.skipIf(not hasattr(socket, \"SO_REUSEPORT\"), \"SO_REUSEPORT is not supported\")\n    def test_reuse_port(self):\n        socket, port = bind_unused_port(reuse_port=True)\n        try:\n            sockets = bind_sockets(port, 'localhost', reuse_port=True)\n            self.assertTrue(all(s.getsockname()[1] == port for s in sockets))\n        finally:\n            socket.close()\n            for sock in sockets:\n                sock.close()\n",
            "header": {
                "file": {
                    "path": "tornado/test/netutil_test.py"
                },
                "misc_line": null
            },
            "index_line": {
                "old_commit_hash": "7d9cad34",
                "new_commit_hash": "9ef5f7cf",
                "mode": "100644"
            },
            "is_binary_file": false,
            "binary_line": null,
            "minus_file": {
                "path": "a/tornado/test/netutil_test.py"
            },
            "plus_file": {
                "path": "b/tornado/test/netutil_test.py"
            },
            "hunks": [
                {
                    "descriptor": {
                        "old_range": {
                            "start": 9,
                            "length": 7
                        },
                        "new_range": {
                            "start": 9,
                            "length": 7
                        },
                        "section": "import time"
                    },
                    "line_group": {
                        "all_lines": [
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "from tornado.netutil import BlockingResolver, ThreadedResolver, is_valid_ip, bind_sockets",
                                "type": "context"
                            },
                            {
                                "content": "from tornado.stack_context import ExceptionStackContext",
                                "type": "context"
                            },
                            {
                                "content": "from tornado.testing import AsyncTestCase, gen_test",
                                "type": "deleted"
                            },
                            {
                                "content": "from tornado.testing import AsyncTestCase, gen_test, bind_unused_port",
                                "type": "added"
                            },
                            {
                                "content": "from tornado.test.util import unittest, skipIfNoNetwork",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "try:",
                                "type": "context"
                            }
                        ]
                    },
                    "modified_entities": [],
                    "added_entities": [
                        {
                            "file_name": "tornado/test/netutil_test.py",
                            "type": "statement",
                            "name": "",
                            "content": "from tornado.testing import AsyncTestCase, gen_test, bind_unused_port",
                            "ast_type_str": "ImportFrom",
                            "start_lineno": 12,
                            "end_lineno": 12,
                            "parent": null
                        }
                    ],
                    "deleted_entities": [
                        {
                            "file_name": "tornado/test/netutil_test.py",
                            "type": "statement",
                            "name": "",
                            "content": "from tornado.testing import AsyncTestCase, gen_test",
                            "ast_type_str": "ImportFrom",
                            "start_lineno": 12,
                            "end_lineno": 12,
                            "parent": null
                        }
                    ]
                },
                {
                    "descriptor": {
                        "old_range": {
                            "start": 200,
                            "length": 3
                        },
                        "new_range": {
                            "start": 200,
                            "length": 14
                        },
                        "section": "class TestPortAllocation(unittest.TestCase):"
                    },
                    "line_group": {
                        "all_lines": [
                            {
                                "content": "        finally:",
                                "type": "context"
                            },
                            {
                                "content": "            for sock in sockets:",
                                "type": "context"
                            },
                            {
                                "content": "                sock.close()",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "added"
                            },
                            {
                                "content": "    @unittest.skipIf(not hasattr(socket, \"SO_REUSEPORT\"), \"SO_REUSEPORT is not supported\")",
                                "type": "added"
                            },
                            {
                                "content": "    def test_reuse_port(self):",
                                "type": "added"
                            },
                            {
                                "content": "        socket, port = bind_unused_port(reuse_port=True)",
                                "type": "added"
                            },
                            {
                                "content": "        try:",
                                "type": "added"
                            },
                            {
                                "content": "            sockets = bind_sockets(port, 'localhost', reuse_port=True)",
                                "type": "added"
                            },
                            {
                                "content": "            self.assertTrue(all(s.getsockname()[1] == port for s in sockets))",
                                "type": "added"
                            },
                            {
                                "content": "        finally:",
                                "type": "added"
                            },
                            {
                                "content": "            socket.close()",
                                "type": "added"
                            },
                            {
                                "content": "            for sock in sockets:",
                                "type": "added"
                            },
                            {
                                "content": "                sock.close()",
                                "type": "added"
                            }
                        ]
                    },
                    "modified_entities": [
                        {
                            "file_name": "tornado/test/netutil_test.py",
                            "type": "class",
                            "name": "TestPortAllocation",
                            "content": "class TestPortAllocation(unittest.TestCase):\n    def test_same_port_allocation(self):\n        if 'TRAVIS' in os.environ:\n            self.skipTest(\"dual-stack servers often have port conflicts on travis\")\n        sockets = bind_sockets(None, 'localhost')\n        try:\n            port = sockets[0].getsockname()[1]\n            self.assertTrue(all(s.getsockname()[1] == port\n                                for s in sockets[1:]))\n        finally:\n            for sock in sockets:\n                sock.close()\n\n    @unittest.skipIf(not hasattr(socket, \"SO_REUSEPORT\"), \"SO_REUSEPORT is not supported\")\n    def test_reuse_port(self):\n        socket, port = bind_unused_port(reuse_port=True)\n        try:\n            sockets = bind_sockets(port, 'localhost', reuse_port=True)\n            self.assertTrue(all(s.getsockname()[1] == port for s in sockets))\n        finally:\n            socket.close()\n            for sock in sockets:\n                sock.close()",
                            "ast_type_str": "ClassDef",
                            "start_lineno": 191,
                            "end_lineno": 213,
                            "parent": null
                        }
                    ],
                    "added_entities": [
                        {
                            "file_name": "tornado/test/netutil_test.py",
                            "type": "method",
                            "name": "TestPortAllocation.test_reuse_port",
                            "content": "class TestPortAllocation(unittest.TestCase):\n    def test_same_port_allocation(self):\n        if 'TRAVIS' in os.environ:\n            self.skipTest(\"dual-stack servers often have port conflicts on travis\")\n        sockets = bind_sockets(None, 'localhost')\n        try:\n            port = sockets[0].getsockname()[1]\n            self.assertTrue(all(s.getsockname()[1] == port\n                                for s in sockets[1:]))\n        finally:\n            for sock in sockets:\n                sock.close()\n\n    @unittest.skipIf(not hasattr(socket, \"SO_REUSEPORT\"), \"SO_REUSEPORT is not supported\")\n    def test_reuse_port(self):\n        socket, port = bind_unused_port(reuse_port=True)\n        try:\n            sockets = bind_sockets(port, 'localhost', reuse_port=True)\n            self.assertTrue(all(s.getsockname()[1] == port for s in sockets))\n        finally:\n            socket.close()\n            for sock in sockets:\n                sock.close()",
                            "ast_type_str": "ClassDef",
                            "start_lineno": 205,
                            "end_lineno": 213,
                            "parent": {
                                "file_name": "tornado/test/netutil_test.py",
                                "type": "class",
                                "name": "TestPortAllocation",
                                "content": "class TestPortAllocation(unittest.TestCase):\n    def test_same_port_allocation(self):\n        if 'TRAVIS' in os.environ:\n            self.skipTest(\"dual-stack servers often have port conflicts on travis\")\n        sockets = bind_sockets(None, 'localhost')\n        try:\n            port = sockets[0].getsockname()[1]\n            self.assertTrue(all(s.getsockname()[1] == port\n                                for s in sockets[1:]))\n        finally:\n            for sock in sockets:\n                sock.close()\n\n    @unittest.skipIf(not hasattr(socket, \"SO_REUSEPORT\"), \"SO_REUSEPORT is not supported\")\n    def test_reuse_port(self):\n        socket, port = bind_unused_port(reuse_port=True)\n        try:\n            sockets = bind_sockets(port, 'localhost', reuse_port=True)\n            self.assertTrue(all(s.getsockname()[1] == port for s in sockets))\n        finally:\n            socket.close()\n            for sock in sockets:\n                sock.close()",
                                "ast_type_str": "ClassDef",
                                "start_lineno": 191,
                                "end_lineno": 213,
                                "parent": null
                            }
                        }
                    ],
                    "deleted_entities": []
                }
            ]
        },
        {
            "old_file_content": "#!/usr/bin/env python\n\"\"\"Support classes for automated testing.\n\n* `AsyncTestCase` and `AsyncHTTPTestCase`:  Subclasses of unittest.TestCase\n  with additional support for testing asynchronous (`.IOLoop` based) code.\n\n* `ExpectLog` and `LogTrapTestCase`: Make test logs less spammy.\n\n* `main()`: A simple test runner (wrapper around unittest.main()) with support\n  for the tornado.autoreload module to rerun the tests when code changes.\n\"\"\"\n\nfrom __future__ import absolute_import, division, print_function, with_statement\n\ntry:\n    from tornado import gen\n    from tornado.httpclient import AsyncHTTPClient\n    from tornado.httpserver import HTTPServer\n    from tornado.simple_httpclient import SimpleAsyncHTTPClient\n    from tornado.ioloop import IOLoop, TimeoutError\n    from tornado import netutil\n    from tornado.process import Subprocess\nexcept ImportError:\n    # These modules are not importable on app engine.  Parts of this module\n    # won't work, but e.g. LogTrapTestCase and main() will.\n    AsyncHTTPClient = None\n    gen = None\n    HTTPServer = None\n    IOLoop = None\n    netutil = None\n    SimpleAsyncHTTPClient = None\n    Subprocess = None\nfrom tornado.log import gen_log, app_log\nfrom tornado.stack_context import ExceptionStackContext\nfrom tornado.util import raise_exc_info, basestring_type\nimport functools\nimport logging\nimport os\nimport re\nimport signal\nimport socket\nimport sys\n\ntry:\n    from cStringIO import StringIO  # py2\nexcept ImportError:\n    from io import StringIO  # py3\n\ntry:\n    from collections.abc import Generator as GeneratorType  # py35+\nexcept ImportError:\n    from types import GeneratorType\n\n# Tornado's own test suite requires the updated unittest module\n# (either py27+ or unittest2) so tornado.test.util enforces\n# this requirement, but for other users of tornado.testing we want\n# to allow the older version if unitest2 is not available.\nif sys.version_info >= (3,):\n    # On python 3, mixing unittest2 and unittest (including doctest)\n    # doesn't seem to work, so always use unittest.\n    import unittest\nelse:\n    # On python 2, prefer unittest2 when available.\n    try:\n        import unittest2 as unittest\n    except ImportError:\n        import unittest\n\n_next_port = 10000\n\n\ndef get_unused_port():\n    \"\"\"Returns a (hopefully) unused port number.\n\n    This function does not guarantee that the port it returns is available,\n    only that a series of get_unused_port calls in a single process return\n    distinct ports.\n\n    .. deprecated::\n       Use bind_unused_port instead, which is guaranteed to find an unused port.\n    \"\"\"\n    global _next_port\n    port = _next_port\n    _next_port = _next_port + 1\n    return port\n\n\ndef bind_unused_port():\n    \"\"\"Binds a server socket to an available port on localhost.\n\n    Returns a tuple (socket, port).\n    \"\"\"\n    [sock] = netutil.bind_sockets(None, 'localhost', family=socket.AF_INET)\n    port = sock.getsockname()[1]\n    return sock, port\n\n\ndef get_async_test_timeout():\n    \"\"\"Get the global timeout setting for async tests.\n\n    Returns a float, the timeout in seconds.\n\n    .. versionadded:: 3.1\n    \"\"\"\n    try:\n        return float(os.environ.get('ASYNC_TEST_TIMEOUT'))\n    except (ValueError, TypeError):\n        return 5\n\n\nclass _TestMethodWrapper(object):\n    \"\"\"Wraps a test method to raise an error if it returns a value.\n\n    This is mainly used to detect undecorated generators (if a test\n    method yields it must use a decorator to consume the generator),\n    but will also detect other kinds of return values (these are not\n    necessarily errors, but we alert anyway since there is no good\n    reason to return a value from a test.\n    \"\"\"\n    def __init__(self, orig_method):\n        self.orig_method = orig_method\n\n    def __call__(self, *args, **kwargs):\n        result = self.orig_method(*args, **kwargs)\n        if isinstance(result, GeneratorType):\n            raise TypeError(\"Generator test methods should be decorated with \"\n                            \"tornado.testing.gen_test\")\n        elif result is not None:\n            raise ValueError(\"Return value from test method ignored: %r\" %\n                             result)\n\n    def __getattr__(self, name):\n        \"\"\"Proxy all unknown attributes to the original method.\n\n        This is important for some of the decorators in the `unittest`\n        module, such as `unittest.skipIf`.\n        \"\"\"\n        return getattr(self.orig_method, name)\n\n\nclass AsyncTestCase(unittest.TestCase):\n    \"\"\"`~unittest.TestCase` subclass for testing `.IOLoop`-based\n    asynchronous code.\n\n    The unittest framework is synchronous, so the test must be\n    complete by the time the test method returns.  This means that\n    asynchronous code cannot be used in quite the same way as usual.\n    To write test functions that use the same ``yield``-based patterns\n    used with the `tornado.gen` module, decorate your test methods\n    with `tornado.testing.gen_test` instead of\n    `tornado.gen.coroutine`.  This class also provides the `stop()`\n    and `wait()` methods for a more manual style of testing.  The test\n    method itself must call ``self.wait()``, and asynchronous\n    callbacks should call ``self.stop()`` to signal completion.\n\n    By default, a new `.IOLoop` is constructed for each test and is available\n    as ``self.io_loop``.  This `.IOLoop` should be used in the construction of\n    HTTP clients/servers, etc.  If the code being tested requires a\n    global `.IOLoop`, subclasses should override `get_new_ioloop` to return it.\n\n    The `.IOLoop`'s ``start`` and ``stop`` methods should not be\n    called directly.  Instead, use `self.stop <stop>` and `self.wait\n    <wait>`.  Arguments passed to ``self.stop`` are returned from\n    ``self.wait``.  It is possible to have multiple ``wait``/``stop``\n    cycles in the same test.\n\n    Example::\n\n        # This test uses coroutine style.\n        class MyTestCase(AsyncTestCase):\n            @tornado.testing.gen_test\n            def test_http_fetch(self):\n                client = AsyncHTTPClient(self.io_loop)\n                response = yield client.fetch(\"http://www.tornadoweb.org\")\n                # Test contents of response\n                self.assertIn(\"FriendFeed\", response.body)\n\n        # This test uses argument passing between self.stop and self.wait.\n        class MyTestCase2(AsyncTestCase):\n            def test_http_fetch(self):\n                client = AsyncHTTPClient(self.io_loop)\n                client.fetch(\"http://www.tornadoweb.org/\", self.stop)\n                response = self.wait()\n                # Test contents of response\n                self.assertIn(\"FriendFeed\", response.body)\n\n        # This test uses an explicit callback-based style.\n        class MyTestCase3(AsyncTestCase):\n            def test_http_fetch(self):\n                client = AsyncHTTPClient(self.io_loop)\n                client.fetch(\"http://www.tornadoweb.org/\", self.handle_fetch)\n                self.wait()\n\n            def handle_fetch(self, response):\n                # Test contents of response (failures and exceptions here\n                # will cause self.wait() to throw an exception and end the\n                # test).\n                # Exceptions thrown here are magically propagated to\n                # self.wait() in test_http_fetch() via stack_context.\n                self.assertIn(\"FriendFeed\", response.body)\n                self.stop()\n    \"\"\"\n    def __init__(self, methodName='runTest', **kwargs):\n        super(AsyncTestCase, self).__init__(methodName, **kwargs)\n        self.__stopped = False\n        self.__running = False\n        self.__failure = None\n        self.__stop_args = None\n        self.__timeout = None\n\n        # It's easy to forget the @gen_test decorator, but if you do\n        # the test will silently be ignored because nothing will consume\n        # the generator.  Replace the test method with a wrapper that will\n        # make sure it's not an undecorated generator.\n        setattr(self, methodName, _TestMethodWrapper(getattr(self, methodName)))\n\n    def setUp(self):\n        super(AsyncTestCase, self).setUp()\n        self.io_loop = self.get_new_ioloop()\n        self.io_loop.make_current()\n\n    def tearDown(self):\n        # Clean up Subprocess, so it can be used again with a new ioloop.\n        Subprocess.uninitialize()\n        self.io_loop.clear_current()\n        if (not IOLoop.initialized() or\n                self.io_loop is not IOLoop.instance()):\n            # Try to clean up any file descriptors left open in the ioloop.\n            # This avoids leaks, especially when tests are run repeatedly\n            # in the same process with autoreload (because curl does not\n            # set FD_CLOEXEC on its file descriptors)\n            self.io_loop.close(all_fds=True)\n        super(AsyncTestCase, self).tearDown()\n        # In case an exception escaped or the StackContext caught an exception\n        # when there wasn't a wait() to re-raise it, do so here.\n        # This is our last chance to raise an exception in a way that the\n        # unittest machinery understands.\n        self.__rethrow()\n\n    def get_new_ioloop(self):\n        \"\"\"Creates a new `.IOLoop` for this test.  May be overridden in\n        subclasses for tests that require a specific `.IOLoop` (usually\n        the singleton `.IOLoop.instance()`).\n        \"\"\"\n        return IOLoop()\n\n    def _handle_exception(self, typ, value, tb):\n        if self.__failure is None:\n            self.__failure = (typ, value, tb)\n        else:\n            app_log.error(\"multiple unhandled exceptions in test\",\n                          exc_info=(typ, value, tb))\n        self.stop()\n        return True\n\n    def __rethrow(self):\n        if self.__failure is not None:\n            failure = self.__failure\n            self.__failure = None\n            raise_exc_info(failure)\n\n    def run(self, result=None):\n        with ExceptionStackContext(self._handle_exception):\n            super(AsyncTestCase, self).run(result)\n        # As a last resort, if an exception escaped super.run() and wasn't\n        # re-raised in tearDown, raise it here.  This will cause the\n        # unittest run to fail messily, but that's better than silently\n        # ignoring an error.\n        self.__rethrow()\n\n    def stop(self, _arg=None, **kwargs):\n        \"\"\"Stops the `.IOLoop`, causing one pending (or future) call to `wait()`\n        to return.\n\n        Keyword arguments or a single positional argument passed to `stop()` are\n        saved and will be returned by `wait()`.\n        \"\"\"\n        assert _arg is None or not kwargs\n        self.__stop_args = kwargs or _arg\n        if self.__running:\n            self.io_loop.stop()\n            self.__running = False\n        self.__stopped = True\n\n    def wait(self, condition=None, timeout=None):\n        \"\"\"Runs the `.IOLoop` until stop is called or timeout has passed.\n\n        In the event of a timeout, an exception will be thrown. The\n        default timeout is 5 seconds; it may be overridden with a\n        ``timeout`` keyword argument or globally with the\n        ``ASYNC_TEST_TIMEOUT`` environment variable.\n\n        If ``condition`` is not None, the `.IOLoop` will be restarted\n        after `stop()` until ``condition()`` returns true.\n\n        .. versionchanged:: 3.1\n           Added the ``ASYNC_TEST_TIMEOUT`` environment variable.\n        \"\"\"\n        if timeout is None:\n            timeout = get_async_test_timeout()\n\n        if not self.__stopped:\n            if timeout:\n                def timeout_func():\n                    try:\n                        raise self.failureException(\n                            'Async operation timed out after %s seconds' %\n                            timeout)\n                    except Exception:\n                        self.__failure = sys.exc_info()\n                    self.stop()\n                self.__timeout = self.io_loop.add_timeout(self.io_loop.time() + timeout, timeout_func)\n            while True:\n                self.__running = True\n                self.io_loop.start()\n                if (self.__failure is not None or\n                        condition is None or condition()):\n                    break\n            if self.__timeout is not None:\n                self.io_loop.remove_timeout(self.__timeout)\n                self.__timeout = None\n        assert self.__stopped\n        self.__stopped = False\n        self.__rethrow()\n        result = self.__stop_args\n        self.__stop_args = None\n        return result\n\n\nclass AsyncHTTPTestCase(AsyncTestCase):\n    \"\"\"A test case that starts up an HTTP server.\n\n    Subclasses must override `get_app()`, which returns the\n    `tornado.web.Application` (or other `.HTTPServer` callback) to be tested.\n    Tests will typically use the provided ``self.http_client`` to fetch\n    URLs from this server.\n\n    Example, assuming the \"Hello, world\" example from the user guide is in\n    ``hello.py``::\n\n        import hello\n\n        class TestHelloApp(AsyncHTTPTestCase):\n            def get_app(self):\n                return hello.make_app()\n\n            def test_homepage(self):\n                response = self.fetch('/')\n                self.assertEqual(response.code, 200)\n                self.assertEqual(response.body, 'Hello, world')\n\n    That call to ``self.fetch()`` is equivalent to ::\n\n        self.http_client.fetch(self.get_url('/'), self.stop)\n        response = self.wait()\n\n    which illustrates how AsyncTestCase can turn an asynchronous operation,\n    like ``http_client.fetch()``, into a synchronous operation. If you need\n    to do other asynchronous operations in tests, you'll probably need to use\n    ``stop()`` and ``wait()`` yourself.\n    \"\"\"\n    def setUp(self):\n        super(AsyncHTTPTestCase, self).setUp()\n        sock, port = bind_unused_port()\n        self.__port = port\n\n        self.http_client = self.get_http_client()\n        self._app = self.get_app()\n        self.http_server = self.get_http_server()\n        self.http_server.add_sockets([sock])\n\n    def get_http_client(self):\n        return AsyncHTTPClient(io_loop=self.io_loop)\n\n    def get_http_server(self):\n        return HTTPServer(self._app, io_loop=self.io_loop,\n                          **self.get_httpserver_options())\n\n    def get_app(self):\n        \"\"\"Should be overridden by subclasses to return a\n        `tornado.web.Application` or other `.HTTPServer` callback.\n        \"\"\"\n        raise NotImplementedError()\n\n    def fetch(self, path, **kwargs):\n        \"\"\"Convenience method to synchronously fetch a url.\n\n        The given path will be appended to the local server's host and\n        port.  Any additional kwargs will be passed directly to\n        `.AsyncHTTPClient.fetch` (and so could be used to pass\n        ``method=\"POST\"``, ``body=\"...\"``, etc).\n        \"\"\"\n        self.http_client.fetch(self.get_url(path), self.stop, **kwargs)\n        return self.wait()\n\n    def get_httpserver_options(self):\n        \"\"\"May be overridden by subclasses to return additional\n        keyword arguments for the server.\n        \"\"\"\n        return {}\n\n    def get_http_port(self):\n        \"\"\"Returns the port used by the server.\n\n        A new port is chosen for each test.\n        \"\"\"\n        return self.__port\n\n    def get_protocol(self):\n        return 'http'\n\n    def get_url(self, path):\n        \"\"\"Returns an absolute url for the given path on the test server.\"\"\"\n        return '%s://localhost:%s%s' % (self.get_protocol(),\n                                        self.get_http_port(), path)\n\n    def tearDown(self):\n        self.http_server.stop()\n        self.io_loop.run_sync(self.http_server.close_all_connections,\n                              timeout=get_async_test_timeout())\n        if (not IOLoop.initialized() or\n                self.http_client.io_loop is not IOLoop.instance()):\n            self.http_client.close()\n        super(AsyncHTTPTestCase, self).tearDown()\n\n\nclass AsyncHTTPSTestCase(AsyncHTTPTestCase):\n    \"\"\"A test case that starts an HTTPS server.\n\n    Interface is generally the same as `AsyncHTTPTestCase`.\n    \"\"\"\n    def get_http_client(self):\n        return AsyncHTTPClient(io_loop=self.io_loop, force_instance=True,\n                               defaults=dict(validate_cert=False))\n\n    def get_httpserver_options(self):\n        return dict(ssl_options=self.get_ssl_options())\n\n    def get_ssl_options(self):\n        \"\"\"May be overridden by subclasses to select SSL options.\n\n        By default includes a self-signed testing certificate.\n        \"\"\"\n        # Testing keys were generated with:\n        # openssl req -new -keyout tornado/test/test.key -out tornado/test/test.crt -nodes -days 3650 -x509\n        module_dir = os.path.dirname(__file__)\n        return dict(\n            certfile=os.path.join(module_dir, 'test', 'test.crt'),\n            keyfile=os.path.join(module_dir, 'test', 'test.key'))\n\n    def get_protocol(self):\n        return 'https'\n\n\ndef gen_test(func=None, timeout=None):\n    \"\"\"Testing equivalent of ``@gen.coroutine``, to be applied to test methods.\n\n    ``@gen.coroutine`` cannot be used on tests because the `.IOLoop` is not\n    already running.  ``@gen_test`` should be applied to test methods\n    on subclasses of `AsyncTestCase`.\n\n    Example::\n\n        class MyTest(AsyncHTTPTestCase):\n            @gen_test\n            def test_something(self):\n                response = yield gen.Task(self.fetch('/'))\n\n    By default, ``@gen_test`` times out after 5 seconds. The timeout may be\n    overridden globally with the ``ASYNC_TEST_TIMEOUT`` environment variable,\n    or for each test with the ``timeout`` keyword argument::\n\n        class MyTest(AsyncHTTPTestCase):\n            @gen_test(timeout=10)\n            def test_something_slow(self):\n                response = yield gen.Task(self.fetch('/'))\n\n    .. versionadded:: 3.1\n       The ``timeout`` argument and ``ASYNC_TEST_TIMEOUT`` environment\n       variable.\n\n    .. versionchanged:: 4.0\n       The wrapper now passes along ``*args, **kwargs`` so it can be used\n       on functions with arguments.\n    \"\"\"\n    if timeout is None:\n        timeout = get_async_test_timeout()\n\n    def wrap(f):\n        # Stack up several decorators to allow us to access the generator\n        # object itself.  In the innermost wrapper, we capture the generator\n        # and save it in an attribute of self.  Next, we run the wrapped\n        # function through @gen.coroutine.  Finally, the coroutine is\n        # wrapped again to make it synchronous with run_sync.\n        #\n        # This is a good case study arguing for either some sort of\n        # extensibility in the gen decorators or cancellation support.\n        @functools.wraps(f)\n        def pre_coroutine(self, *args, **kwargs):\n            result = f(self, *args, **kwargs)\n            if isinstance(result, GeneratorType):\n                self._test_generator = result\n            else:\n                self._test_generator = None\n            return result\n\n        coro = gen.coroutine(pre_coroutine)\n\n        @functools.wraps(coro)\n        def post_coroutine(self, *args, **kwargs):\n            try:\n                return self.io_loop.run_sync(\n                    functools.partial(coro, self, *args, **kwargs),\n                    timeout=timeout)\n            except TimeoutError as e:\n                # run_sync raises an error with an unhelpful traceback.\n                # If we throw it back into the generator the stack trace\n                # will be replaced by the point where the test is stopped.\n                self._test_generator.throw(e)\n                # In case the test contains an overly broad except clause,\n                # we may get back here.  In this case re-raise the original\n                # exception, which is better than nothing.\n                raise\n        return post_coroutine\n\n    if func is not None:\n        # Used like:\n        #     @gen_test\n        #     def f(self):\n        #         pass\n        return wrap(func)\n    else:\n        # Used like @gen_test(timeout=10)\n        return wrap\n\n\n# Without this attribute, nosetests will try to run gen_test as a test\n# anywhere it is imported.\ngen_test.__test__ = False\n\n\nclass LogTrapTestCase(unittest.TestCase):\n    \"\"\"A test case that captures and discards all logging output\n    if the test passes.\n\n    Some libraries can produce a lot of logging output even when\n    the test succeeds, so this class can be useful to minimize the noise.\n    Simply use it as a base class for your test case.  It is safe to combine\n    with AsyncTestCase via multiple inheritance\n    (``class MyTestCase(AsyncHTTPTestCase, LogTrapTestCase):``)\n\n    This class assumes that only one log handler is configured and\n    that it is a `~logging.StreamHandler`.  This is true for both\n    `logging.basicConfig` and the \"pretty logging\" configured by\n    `tornado.options`.  It is not compatible with other log buffering\n    mechanisms, such as those provided by some test runners.\n\n    .. deprecated:: 4.1\n       Use the unittest module's ``--buffer`` option instead, or `.ExpectLog`.\n    \"\"\"\n    def run(self, result=None):\n        logger = logging.getLogger()\n        if not logger.handlers:\n            logging.basicConfig()\n        handler = logger.handlers[0]\n        if (len(logger.handlers) > 1 or\n                not isinstance(handler, logging.StreamHandler)):\n            # Logging has been configured in a way we don't recognize,\n            # so just leave it alone.\n            super(LogTrapTestCase, self).run(result)\n            return\n        old_stream = handler.stream\n        try:\n            handler.stream = StringIO()\n            gen_log.info(\"RUNNING TEST: \" + str(self))\n            old_error_count = len(result.failures) + len(result.errors)\n            super(LogTrapTestCase, self).run(result)\n            new_error_count = len(result.failures) + len(result.errors)\n            if new_error_count != old_error_count:\n                old_stream.write(handler.stream.getvalue())\n        finally:\n            handler.stream = old_stream\n\n\nclass ExpectLog(logging.Filter):\n    \"\"\"Context manager to capture and suppress expected log output.\n\n    Useful to make tests of error conditions less noisy, while still\n    leaving unexpected log entries visible.  *Not thread safe.*\n\n    The attribute ``logged_stack`` is set to true if any exception\n    stack trace was logged.\n\n    Usage::\n\n        with ExpectLog('tornado.application', \"Uncaught exception\"):\n            error_response = self.fetch(\"/some_page\")\n\n    .. versionchanged:: 4.3\n       Added the ``logged_stack`` attribute.\n    \"\"\"\n    def __init__(self, logger, regex, required=True):\n        \"\"\"Constructs an ExpectLog context manager.\n\n        :param logger: Logger object (or name of logger) to watch.  Pass\n            an empty string to watch the root logger.\n        :param regex: Regular expression to match.  Any log entries on\n            the specified logger that match this regex will be suppressed.\n        :param required: If true, an exeption will be raised if the end of\n            the ``with`` statement is reached without matching any log entries.\n        \"\"\"\n        if isinstance(logger, basestring_type):\n            logger = logging.getLogger(logger)\n        self.logger = logger\n        self.regex = re.compile(regex)\n        self.required = required\n        self.matched = False\n        self.logged_stack = False\n\n    def filter(self, record):\n        if record.exc_info:\n            self.logged_stack = True\n        message = record.getMessage()\n        if self.regex.match(message):\n            self.matched = True\n            return False\n        return True\n\n    def __enter__(self):\n        self.logger.addFilter(self)\n        return self\n\n    def __exit__(self, typ, value, tb):\n        self.logger.removeFilter(self)\n        if not typ and self.required and not self.matched:\n            raise Exception(\"did not get expected log message\")\n\n\ndef main(**kwargs):\n    \"\"\"A simple test runner.\n\n    This test runner is essentially equivalent to `unittest.main` from\n    the standard library, but adds support for tornado-style option\n    parsing and log formatting.\n\n    The easiest way to run a test is via the command line::\n\n        python -m tornado.testing tornado.test.stack_context_test\n\n    See the standard library unittest module for ways in which tests can\n    be specified.\n\n    Projects with many tests may wish to define a test script like\n    ``tornado/test/runtests.py``.  This script should define a method\n    ``all()`` which returns a test suite and then call\n    `tornado.testing.main()`.  Note that even when a test script is\n    used, the ``all()`` test suite may be overridden by naming a\n    single test on the command line::\n\n        # Runs all tests\n        python -m tornado.test.runtests\n        # Runs one test\n        python -m tornado.test.runtests tornado.test.stack_context_test\n\n    Additional keyword arguments passed through to ``unittest.main()``.\n    For example, use ``tornado.testing.main(verbosity=2)``\n    to show many test details as they are run.\n    See http://docs.python.org/library/unittest.html#unittest.main\n    for full argument list.\n    \"\"\"\n    from tornado.options import define, options, parse_command_line\n\n    define('exception_on_interrupt', type=bool, default=True,\n           help=(\"If true (default), ctrl-c raises a KeyboardInterrupt \"\n                 \"exception.  This prints a stack trace but cannot interrupt \"\n                 \"certain operations.  If false, the process is more reliably \"\n                 \"killed, but does not print a stack trace.\"))\n\n    # support the same options as unittest's command-line interface\n    define('verbose', type=bool)\n    define('quiet', type=bool)\n    define('failfast', type=bool)\n    define('catch', type=bool)\n    define('buffer', type=bool)\n\n    argv = [sys.argv[0]] + parse_command_line(sys.argv)\n\n    if not options.exception_on_interrupt:\n        signal.signal(signal.SIGINT, signal.SIG_DFL)\n\n    if options.verbose is not None:\n        kwargs['verbosity'] = 2\n    if options.quiet is not None:\n        kwargs['verbosity'] = 0\n    if options.failfast is not None:\n        kwargs['failfast'] = True\n    if options.catch is not None:\n        kwargs['catchbreak'] = True\n    if options.buffer is not None:\n        kwargs['buffer'] = True\n\n    if __name__ == '__main__' and len(argv) == 1:\n        print(\"No tests specified\", file=sys.stderr)\n        sys.exit(1)\n    try:\n        # In order to be able to run tests by their fully-qualified name\n        # on the command line without importing all tests here,\n        # module must be set to None.  Python 3.2's unittest.main ignores\n        # defaultTest if no module is given (it tries to do its own\n        # test discovery, which is incompatible with auto2to3), so don't\n        # set module if we're not asking for a specific test.\n        if len(argv) > 1:\n            unittest.main(module=None, argv=argv, **kwargs)\n        else:\n            unittest.main(defaultTest=\"all\", argv=argv, **kwargs)\n    except SystemExit as e:\n        if e.code == 0:\n            gen_log.info('PASS')\n        else:\n            gen_log.error('FAIL')\n        raise\n\nif __name__ == '__main__':\n    main()\n",
            "new_file_content": "#!/usr/bin/env python\n\"\"\"Support classes for automated testing.\n\n* `AsyncTestCase` and `AsyncHTTPTestCase`:  Subclasses of unittest.TestCase\n  with additional support for testing asynchronous (`.IOLoop` based) code.\n\n* `ExpectLog` and `LogTrapTestCase`: Make test logs less spammy.\n\n* `main()`: A simple test runner (wrapper around unittest.main()) with support\n  for the tornado.autoreload module to rerun the tests when code changes.\n\"\"\"\n\nfrom __future__ import absolute_import, division, print_function, with_statement\n\ntry:\n    from tornado import gen\n    from tornado.httpclient import AsyncHTTPClient\n    from tornado.httpserver import HTTPServer\n    from tornado.simple_httpclient import SimpleAsyncHTTPClient\n    from tornado.ioloop import IOLoop, TimeoutError\n    from tornado import netutil\n    from tornado.process import Subprocess\nexcept ImportError:\n    # These modules are not importable on app engine.  Parts of this module\n    # won't work, but e.g. LogTrapTestCase and main() will.\n    AsyncHTTPClient = None\n    gen = None\n    HTTPServer = None\n    IOLoop = None\n    netutil = None\n    SimpleAsyncHTTPClient = None\n    Subprocess = None\nfrom tornado.log import gen_log, app_log\nfrom tornado.stack_context import ExceptionStackContext\nfrom tornado.util import raise_exc_info, basestring_type\nimport functools\nimport logging\nimport os\nimport re\nimport signal\nimport socket\nimport sys\n\ntry:\n    from cStringIO import StringIO  # py2\nexcept ImportError:\n    from io import StringIO  # py3\n\ntry:\n    from collections.abc import Generator as GeneratorType  # py35+\nexcept ImportError:\n    from types import GeneratorType\n\n# Tornado's own test suite requires the updated unittest module\n# (either py27+ or unittest2) so tornado.test.util enforces\n# this requirement, but for other users of tornado.testing we want\n# to allow the older version if unitest2 is not available.\nif sys.version_info >= (3,):\n    # On python 3, mixing unittest2 and unittest (including doctest)\n    # doesn't seem to work, so always use unittest.\n    import unittest\nelse:\n    # On python 2, prefer unittest2 when available.\n    try:\n        import unittest2 as unittest\n    except ImportError:\n        import unittest\n\n_next_port = 10000\n\n\ndef get_unused_port():\n    \"\"\"Returns a (hopefully) unused port number.\n\n    This function does not guarantee that the port it returns is available,\n    only that a series of get_unused_port calls in a single process return\n    distinct ports.\n\n    .. deprecated::\n       Use bind_unused_port instead, which is guaranteed to find an unused port.\n    \"\"\"\n    global _next_port\n    port = _next_port\n    _next_port = _next_port + 1\n    return port\n\n\ndef bind_unused_port(reuse_port=False):\n    \"\"\"Binds a server socket to an available port on localhost.\n\n    Returns a tuple (socket, port).\n    \"\"\"\n    [sock] = netutil.bind_sockets(None, 'localhost', family=socket.AF_INET,\n                                  reuse_port=reuse_port)\n    port = sock.getsockname()[1]\n    return sock, port\n\n\ndef get_async_test_timeout():\n    \"\"\"Get the global timeout setting for async tests.\n\n    Returns a float, the timeout in seconds.\n\n    .. versionadded:: 3.1\n    \"\"\"\n    try:\n        return float(os.environ.get('ASYNC_TEST_TIMEOUT'))\n    except (ValueError, TypeError):\n        return 5\n\n\nclass _TestMethodWrapper(object):\n    \"\"\"Wraps a test method to raise an error if it returns a value.\n\n    This is mainly used to detect undecorated generators (if a test\n    method yields it must use a decorator to consume the generator),\n    but will also detect other kinds of return values (these are not\n    necessarily errors, but we alert anyway since there is no good\n    reason to return a value from a test.\n    \"\"\"\n    def __init__(self, orig_method):\n        self.orig_method = orig_method\n\n    def __call__(self, *args, **kwargs):\n        result = self.orig_method(*args, **kwargs)\n        if isinstance(result, GeneratorType):\n            raise TypeError(\"Generator test methods should be decorated with \"\n                            \"tornado.testing.gen_test\")\n        elif result is not None:\n            raise ValueError(\"Return value from test method ignored: %r\" %\n                             result)\n\n    def __getattr__(self, name):\n        \"\"\"Proxy all unknown attributes to the original method.\n\n        This is important for some of the decorators in the `unittest`\n        module, such as `unittest.skipIf`.\n        \"\"\"\n        return getattr(self.orig_method, name)\n\n\nclass AsyncTestCase(unittest.TestCase):\n    \"\"\"`~unittest.TestCase` subclass for testing `.IOLoop`-based\n    asynchronous code.\n\n    The unittest framework is synchronous, so the test must be\n    complete by the time the test method returns.  This means that\n    asynchronous code cannot be used in quite the same way as usual.\n    To write test functions that use the same ``yield``-based patterns\n    used with the `tornado.gen` module, decorate your test methods\n    with `tornado.testing.gen_test` instead of\n    `tornado.gen.coroutine`.  This class also provides the `stop()`\n    and `wait()` methods for a more manual style of testing.  The test\n    method itself must call ``self.wait()``, and asynchronous\n    callbacks should call ``self.stop()`` to signal completion.\n\n    By default, a new `.IOLoop` is constructed for each test and is available\n    as ``self.io_loop``.  This `.IOLoop` should be used in the construction of\n    HTTP clients/servers, etc.  If the code being tested requires a\n    global `.IOLoop`, subclasses should override `get_new_ioloop` to return it.\n\n    The `.IOLoop`'s ``start`` and ``stop`` methods should not be\n    called directly.  Instead, use `self.stop <stop>` and `self.wait\n    <wait>`.  Arguments passed to ``self.stop`` are returned from\n    ``self.wait``.  It is possible to have multiple ``wait``/``stop``\n    cycles in the same test.\n\n    Example::\n\n        # This test uses coroutine style.\n        class MyTestCase(AsyncTestCase):\n            @tornado.testing.gen_test\n            def test_http_fetch(self):\n                client = AsyncHTTPClient(self.io_loop)\n                response = yield client.fetch(\"http://www.tornadoweb.org\")\n                # Test contents of response\n                self.assertIn(\"FriendFeed\", response.body)\n\n        # This test uses argument passing between self.stop and self.wait.\n        class MyTestCase2(AsyncTestCase):\n            def test_http_fetch(self):\n                client = AsyncHTTPClient(self.io_loop)\n                client.fetch(\"http://www.tornadoweb.org/\", self.stop)\n                response = self.wait()\n                # Test contents of response\n                self.assertIn(\"FriendFeed\", response.body)\n\n        # This test uses an explicit callback-based style.\n        class MyTestCase3(AsyncTestCase):\n            def test_http_fetch(self):\n                client = AsyncHTTPClient(self.io_loop)\n                client.fetch(\"http://www.tornadoweb.org/\", self.handle_fetch)\n                self.wait()\n\n            def handle_fetch(self, response):\n                # Test contents of response (failures and exceptions here\n                # will cause self.wait() to throw an exception and end the\n                # test).\n                # Exceptions thrown here are magically propagated to\n                # self.wait() in test_http_fetch() via stack_context.\n                self.assertIn(\"FriendFeed\", response.body)\n                self.stop()\n    \"\"\"\n    def __init__(self, methodName='runTest', **kwargs):\n        super(AsyncTestCase, self).__init__(methodName, **kwargs)\n        self.__stopped = False\n        self.__running = False\n        self.__failure = None\n        self.__stop_args = None\n        self.__timeout = None\n\n        # It's easy to forget the @gen_test decorator, but if you do\n        # the test will silently be ignored because nothing will consume\n        # the generator.  Replace the test method with a wrapper that will\n        # make sure it's not an undecorated generator.\n        setattr(self, methodName, _TestMethodWrapper(getattr(self, methodName)))\n\n    def setUp(self):\n        super(AsyncTestCase, self).setUp()\n        self.io_loop = self.get_new_ioloop()\n        self.io_loop.make_current()\n\n    def tearDown(self):\n        # Clean up Subprocess, so it can be used again with a new ioloop.\n        Subprocess.uninitialize()\n        self.io_loop.clear_current()\n        if (not IOLoop.initialized() or\n                self.io_loop is not IOLoop.instance()):\n            # Try to clean up any file descriptors left open in the ioloop.\n            # This avoids leaks, especially when tests are run repeatedly\n            # in the same process with autoreload (because curl does not\n            # set FD_CLOEXEC on its file descriptors)\n            self.io_loop.close(all_fds=True)\n        super(AsyncTestCase, self).tearDown()\n        # In case an exception escaped or the StackContext caught an exception\n        # when there wasn't a wait() to re-raise it, do so here.\n        # This is our last chance to raise an exception in a way that the\n        # unittest machinery understands.\n        self.__rethrow()\n\n    def get_new_ioloop(self):\n        \"\"\"Creates a new `.IOLoop` for this test.  May be overridden in\n        subclasses for tests that require a specific `.IOLoop` (usually\n        the singleton `.IOLoop.instance()`).\n        \"\"\"\n        return IOLoop()\n\n    def _handle_exception(self, typ, value, tb):\n        if self.__failure is None:\n            self.__failure = (typ, value, tb)\n        else:\n            app_log.error(\"multiple unhandled exceptions in test\",\n                          exc_info=(typ, value, tb))\n        self.stop()\n        return True\n\n    def __rethrow(self):\n        if self.__failure is not None:\n            failure = self.__failure\n            self.__failure = None\n            raise_exc_info(failure)\n\n    def run(self, result=None):\n        with ExceptionStackContext(self._handle_exception):\n            super(AsyncTestCase, self).run(result)\n        # As a last resort, if an exception escaped super.run() and wasn't\n        # re-raised in tearDown, raise it here.  This will cause the\n        # unittest run to fail messily, but that's better than silently\n        # ignoring an error.\n        self.__rethrow()\n\n    def stop(self, _arg=None, **kwargs):\n        \"\"\"Stops the `.IOLoop`, causing one pending (or future) call to `wait()`\n        to return.\n\n        Keyword arguments or a single positional argument passed to `stop()` are\n        saved and will be returned by `wait()`.\n        \"\"\"\n        assert _arg is None or not kwargs\n        self.__stop_args = kwargs or _arg\n        if self.__running:\n            self.io_loop.stop()\n            self.__running = False\n        self.__stopped = True\n\n    def wait(self, condition=None, timeout=None):\n        \"\"\"Runs the `.IOLoop` until stop is called or timeout has passed.\n\n        In the event of a timeout, an exception will be thrown. The\n        default timeout is 5 seconds; it may be overridden with a\n        ``timeout`` keyword argument or globally with the\n        ``ASYNC_TEST_TIMEOUT`` environment variable.\n\n        If ``condition`` is not None, the `.IOLoop` will be restarted\n        after `stop()` until ``condition()`` returns true.\n\n        .. versionchanged:: 3.1\n           Added the ``ASYNC_TEST_TIMEOUT`` environment variable.\n        \"\"\"\n        if timeout is None:\n            timeout = get_async_test_timeout()\n\n        if not self.__stopped:\n            if timeout:\n                def timeout_func():\n                    try:\n                        raise self.failureException(\n                            'Async operation timed out after %s seconds' %\n                            timeout)\n                    except Exception:\n                        self.__failure = sys.exc_info()\n                    self.stop()\n                self.__timeout = self.io_loop.add_timeout(self.io_loop.time() + timeout, timeout_func)\n            while True:\n                self.__running = True\n                self.io_loop.start()\n                if (self.__failure is not None or\n                        condition is None or condition()):\n                    break\n            if self.__timeout is not None:\n                self.io_loop.remove_timeout(self.__timeout)\n                self.__timeout = None\n        assert self.__stopped\n        self.__stopped = False\n        self.__rethrow()\n        result = self.__stop_args\n        self.__stop_args = None\n        return result\n\n\nclass AsyncHTTPTestCase(AsyncTestCase):\n    \"\"\"A test case that starts up an HTTP server.\n\n    Subclasses must override `get_app()`, which returns the\n    `tornado.web.Application` (or other `.HTTPServer` callback) to be tested.\n    Tests will typically use the provided ``self.http_client`` to fetch\n    URLs from this server.\n\n    Example, assuming the \"Hello, world\" example from the user guide is in\n    ``hello.py``::\n\n        import hello\n\n        class TestHelloApp(AsyncHTTPTestCase):\n            def get_app(self):\n                return hello.make_app()\n\n            def test_homepage(self):\n                response = self.fetch('/')\n                self.assertEqual(response.code, 200)\n                self.assertEqual(response.body, 'Hello, world')\n\n    That call to ``self.fetch()`` is equivalent to ::\n\n        self.http_client.fetch(self.get_url('/'), self.stop)\n        response = self.wait()\n\n    which illustrates how AsyncTestCase can turn an asynchronous operation,\n    like ``http_client.fetch()``, into a synchronous operation. If you need\n    to do other asynchronous operations in tests, you'll probably need to use\n    ``stop()`` and ``wait()`` yourself.\n    \"\"\"\n    def setUp(self):\n        super(AsyncHTTPTestCase, self).setUp()\n        sock, port = bind_unused_port()\n        self.__port = port\n\n        self.http_client = self.get_http_client()\n        self._app = self.get_app()\n        self.http_server = self.get_http_server()\n        self.http_server.add_sockets([sock])\n\n    def get_http_client(self):\n        return AsyncHTTPClient(io_loop=self.io_loop)\n\n    def get_http_server(self):\n        return HTTPServer(self._app, io_loop=self.io_loop,\n                          **self.get_httpserver_options())\n\n    def get_app(self):\n        \"\"\"Should be overridden by subclasses to return a\n        `tornado.web.Application` or other `.HTTPServer` callback.\n        \"\"\"\n        raise NotImplementedError()\n\n    def fetch(self, path, **kwargs):\n        \"\"\"Convenience method to synchronously fetch a url.\n\n        The given path will be appended to the local server's host and\n        port.  Any additional kwargs will be passed directly to\n        `.AsyncHTTPClient.fetch` (and so could be used to pass\n        ``method=\"POST\"``, ``body=\"...\"``, etc).\n        \"\"\"\n        self.http_client.fetch(self.get_url(path), self.stop, **kwargs)\n        return self.wait()\n\n    def get_httpserver_options(self):\n        \"\"\"May be overridden by subclasses to return additional\n        keyword arguments for the server.\n        \"\"\"\n        return {}\n\n    def get_http_port(self):\n        \"\"\"Returns the port used by the server.\n\n        A new port is chosen for each test.\n        \"\"\"\n        return self.__port\n\n    def get_protocol(self):\n        return 'http'\n\n    def get_url(self, path):\n        \"\"\"Returns an absolute url for the given path on the test server.\"\"\"\n        return '%s://localhost:%s%s' % (self.get_protocol(),\n                                        self.get_http_port(), path)\n\n    def tearDown(self):\n        self.http_server.stop()\n        self.io_loop.run_sync(self.http_server.close_all_connections,\n                              timeout=get_async_test_timeout())\n        if (not IOLoop.initialized() or\n                self.http_client.io_loop is not IOLoop.instance()):\n            self.http_client.close()\n        super(AsyncHTTPTestCase, self).tearDown()\n\n\nclass AsyncHTTPSTestCase(AsyncHTTPTestCase):\n    \"\"\"A test case that starts an HTTPS server.\n\n    Interface is generally the same as `AsyncHTTPTestCase`.\n    \"\"\"\n    def get_http_client(self):\n        return AsyncHTTPClient(io_loop=self.io_loop, force_instance=True,\n                               defaults=dict(validate_cert=False))\n\n    def get_httpserver_options(self):\n        return dict(ssl_options=self.get_ssl_options())\n\n    def get_ssl_options(self):\n        \"\"\"May be overridden by subclasses to select SSL options.\n\n        By default includes a self-signed testing certificate.\n        \"\"\"\n        # Testing keys were generated with:\n        # openssl req -new -keyout tornado/test/test.key -out tornado/test/test.crt -nodes -days 3650 -x509\n        module_dir = os.path.dirname(__file__)\n        return dict(\n            certfile=os.path.join(module_dir, 'test', 'test.crt'),\n            keyfile=os.path.join(module_dir, 'test', 'test.key'))\n\n    def get_protocol(self):\n        return 'https'\n\n\ndef gen_test(func=None, timeout=None):\n    \"\"\"Testing equivalent of ``@gen.coroutine``, to be applied to test methods.\n\n    ``@gen.coroutine`` cannot be used on tests because the `.IOLoop` is not\n    already running.  ``@gen_test`` should be applied to test methods\n    on subclasses of `AsyncTestCase`.\n\n    Example::\n\n        class MyTest(AsyncHTTPTestCase):\n            @gen_test\n            def test_something(self):\n                response = yield gen.Task(self.fetch('/'))\n\n    By default, ``@gen_test`` times out after 5 seconds. The timeout may be\n    overridden globally with the ``ASYNC_TEST_TIMEOUT`` environment variable,\n    or for each test with the ``timeout`` keyword argument::\n\n        class MyTest(AsyncHTTPTestCase):\n            @gen_test(timeout=10)\n            def test_something_slow(self):\n                response = yield gen.Task(self.fetch('/'))\n\n    .. versionadded:: 3.1\n       The ``timeout`` argument and ``ASYNC_TEST_TIMEOUT`` environment\n       variable.\n\n    .. versionchanged:: 4.0\n       The wrapper now passes along ``*args, **kwargs`` so it can be used\n       on functions with arguments.\n    \"\"\"\n    if timeout is None:\n        timeout = get_async_test_timeout()\n\n    def wrap(f):\n        # Stack up several decorators to allow us to access the generator\n        # object itself.  In the innermost wrapper, we capture the generator\n        # and save it in an attribute of self.  Next, we run the wrapped\n        # function through @gen.coroutine.  Finally, the coroutine is\n        # wrapped again to make it synchronous with run_sync.\n        #\n        # This is a good case study arguing for either some sort of\n        # extensibility in the gen decorators or cancellation support.\n        @functools.wraps(f)\n        def pre_coroutine(self, *args, **kwargs):\n            result = f(self, *args, **kwargs)\n            if isinstance(result, GeneratorType):\n                self._test_generator = result\n            else:\n                self._test_generator = None\n            return result\n\n        coro = gen.coroutine(pre_coroutine)\n\n        @functools.wraps(coro)\n        def post_coroutine(self, *args, **kwargs):\n            try:\n                return self.io_loop.run_sync(\n                    functools.partial(coro, self, *args, **kwargs),\n                    timeout=timeout)\n            except TimeoutError as e:\n                # run_sync raises an error with an unhelpful traceback.\n                # If we throw it back into the generator the stack trace\n                # will be replaced by the point where the test is stopped.\n                self._test_generator.throw(e)\n                # In case the test contains an overly broad except clause,\n                # we may get back here.  In this case re-raise the original\n                # exception, which is better than nothing.\n                raise\n        return post_coroutine\n\n    if func is not None:\n        # Used like:\n        #     @gen_test\n        #     def f(self):\n        #         pass\n        return wrap(func)\n    else:\n        # Used like @gen_test(timeout=10)\n        return wrap\n\n\n# Without this attribute, nosetests will try to run gen_test as a test\n# anywhere it is imported.\ngen_test.__test__ = False\n\n\nclass LogTrapTestCase(unittest.TestCase):\n    \"\"\"A test case that captures and discards all logging output\n    if the test passes.\n\n    Some libraries can produce a lot of logging output even when\n    the test succeeds, so this class can be useful to minimize the noise.\n    Simply use it as a base class for your test case.  It is safe to combine\n    with AsyncTestCase via multiple inheritance\n    (``class MyTestCase(AsyncHTTPTestCase, LogTrapTestCase):``)\n\n    This class assumes that only one log handler is configured and\n    that it is a `~logging.StreamHandler`.  This is true for both\n    `logging.basicConfig` and the \"pretty logging\" configured by\n    `tornado.options`.  It is not compatible with other log buffering\n    mechanisms, such as those provided by some test runners.\n\n    .. deprecated:: 4.1\n       Use the unittest module's ``--buffer`` option instead, or `.ExpectLog`.\n    \"\"\"\n    def run(self, result=None):\n        logger = logging.getLogger()\n        if not logger.handlers:\n            logging.basicConfig()\n        handler = logger.handlers[0]\n        if (len(logger.handlers) > 1 or\n                not isinstance(handler, logging.StreamHandler)):\n            # Logging has been configured in a way we don't recognize,\n            # so just leave it alone.\n            super(LogTrapTestCase, self).run(result)\n            return\n        old_stream = handler.stream\n        try:\n            handler.stream = StringIO()\n            gen_log.info(\"RUNNING TEST: \" + str(self))\n            old_error_count = len(result.failures) + len(result.errors)\n            super(LogTrapTestCase, self).run(result)\n            new_error_count = len(result.failures) + len(result.errors)\n            if new_error_count != old_error_count:\n                old_stream.write(handler.stream.getvalue())\n        finally:\n            handler.stream = old_stream\n\n\nclass ExpectLog(logging.Filter):\n    \"\"\"Context manager to capture and suppress expected log output.\n\n    Useful to make tests of error conditions less noisy, while still\n    leaving unexpected log entries visible.  *Not thread safe.*\n\n    The attribute ``logged_stack`` is set to true if any exception\n    stack trace was logged.\n\n    Usage::\n\n        with ExpectLog('tornado.application', \"Uncaught exception\"):\n            error_response = self.fetch(\"/some_page\")\n\n    .. versionchanged:: 4.3\n       Added the ``logged_stack`` attribute.\n    \"\"\"\n    def __init__(self, logger, regex, required=True):\n        \"\"\"Constructs an ExpectLog context manager.\n\n        :param logger: Logger object (or name of logger) to watch.  Pass\n            an empty string to watch the root logger.\n        :param regex: Regular expression to match.  Any log entries on\n            the specified logger that match this regex will be suppressed.\n        :param required: If true, an exeption will be raised if the end of\n            the ``with`` statement is reached without matching any log entries.\n        \"\"\"\n        if isinstance(logger, basestring_type):\n            logger = logging.getLogger(logger)\n        self.logger = logger\n        self.regex = re.compile(regex)\n        self.required = required\n        self.matched = False\n        self.logged_stack = False\n\n    def filter(self, record):\n        if record.exc_info:\n            self.logged_stack = True\n        message = record.getMessage()\n        if self.regex.match(message):\n            self.matched = True\n            return False\n        return True\n\n    def __enter__(self):\n        self.logger.addFilter(self)\n        return self\n\n    def __exit__(self, typ, value, tb):\n        self.logger.removeFilter(self)\n        if not typ and self.required and not self.matched:\n            raise Exception(\"did not get expected log message\")\n\n\ndef main(**kwargs):\n    \"\"\"A simple test runner.\n\n    This test runner is essentially equivalent to `unittest.main` from\n    the standard library, but adds support for tornado-style option\n    parsing and log formatting.\n\n    The easiest way to run a test is via the command line::\n\n        python -m tornado.testing tornado.test.stack_context_test\n\n    See the standard library unittest module for ways in which tests can\n    be specified.\n\n    Projects with many tests may wish to define a test script like\n    ``tornado/test/runtests.py``.  This script should define a method\n    ``all()`` which returns a test suite and then call\n    `tornado.testing.main()`.  Note that even when a test script is\n    used, the ``all()`` test suite may be overridden by naming a\n    single test on the command line::\n\n        # Runs all tests\n        python -m tornado.test.runtests\n        # Runs one test\n        python -m tornado.test.runtests tornado.test.stack_context_test\n\n    Additional keyword arguments passed through to ``unittest.main()``.\n    For example, use ``tornado.testing.main(verbosity=2)``\n    to show many test details as they are run.\n    See http://docs.python.org/library/unittest.html#unittest.main\n    for full argument list.\n    \"\"\"\n    from tornado.options import define, options, parse_command_line\n\n    define('exception_on_interrupt', type=bool, default=True,\n           help=(\"If true (default), ctrl-c raises a KeyboardInterrupt \"\n                 \"exception.  This prints a stack trace but cannot interrupt \"\n                 \"certain operations.  If false, the process is more reliably \"\n                 \"killed, but does not print a stack trace.\"))\n\n    # support the same options as unittest's command-line interface\n    define('verbose', type=bool)\n    define('quiet', type=bool)\n    define('failfast', type=bool)\n    define('catch', type=bool)\n    define('buffer', type=bool)\n\n    argv = [sys.argv[0]] + parse_command_line(sys.argv)\n\n    if not options.exception_on_interrupt:\n        signal.signal(signal.SIGINT, signal.SIG_DFL)\n\n    if options.verbose is not None:\n        kwargs['verbosity'] = 2\n    if options.quiet is not None:\n        kwargs['verbosity'] = 0\n    if options.failfast is not None:\n        kwargs['failfast'] = True\n    if options.catch is not None:\n        kwargs['catchbreak'] = True\n    if options.buffer is not None:\n        kwargs['buffer'] = True\n\n    if __name__ == '__main__' and len(argv) == 1:\n        print(\"No tests specified\", file=sys.stderr)\n        sys.exit(1)\n    try:\n        # In order to be able to run tests by their fully-qualified name\n        # on the command line without importing all tests here,\n        # module must be set to None.  Python 3.2's unittest.main ignores\n        # defaultTest if no module is given (it tries to do its own\n        # test discovery, which is incompatible with auto2to3), so don't\n        # set module if we're not asking for a specific test.\n        if len(argv) > 1:\n            unittest.main(module=None, argv=argv, **kwargs)\n        else:\n            unittest.main(defaultTest=\"all\", argv=argv, **kwargs)\n    except SystemExit as e:\n        if e.code == 0:\n            gen_log.info('PASS')\n        else:\n            gen_log.error('FAIL')\n        raise\n\nif __name__ == '__main__':\n    main()\n",
            "header": {
                "file": {
                    "path": "tornado/testing.py"
                },
                "misc_line": null
            },
            "index_line": {
                "old_commit_hash": "304f22db",
                "new_commit_hash": "f5e9f153",
                "mode": "100644"
            },
            "is_binary_file": false,
            "binary_line": null,
            "minus_file": {
                "path": "a/tornado/testing.py"
            },
            "plus_file": {
                "path": "b/tornado/testing.py"
            },
            "hunks": [
                {
                    "descriptor": {
                        "old_range": {
                            "start": 85,
                            "length": 12
                        },
                        "new_range": {
                            "start": 85,
                            "length": 13
                        },
                        "section": "def get_unused_port():"
                    },
                    "line_group": {
                        "all_lines": [
                            {
                                "content": "    return port",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "def bind_unused_port():",
                                "type": "deleted"
                            },
                            {
                                "content": "def bind_unused_port(reuse_port=False):",
                                "type": "added"
                            },
                            {
                                "content": "    \"\"\"Binds a server socket to an available port on localhost.",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "    Returns a tuple (socket, port).",
                                "type": "context"
                            },
                            {
                                "content": "    \"\"\"",
                                "type": "context"
                            },
                            {
                                "content": "    [sock] = netutil.bind_sockets(None, 'localhost', family=socket.AF_INET)",
                                "type": "deleted"
                            },
                            {
                                "content": "    [sock] = netutil.bind_sockets(None, 'localhost', family=socket.AF_INET,",
                                "type": "added"
                            },
                            {
                                "content": "                                  reuse_port=reuse_port)",
                                "type": "added"
                            },
                            {
                                "content": "    port = sock.getsockname()[1]",
                                "type": "context"
                            },
                            {
                                "content": "    return sock, port",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            }
                        ]
                    },
                    "modified_entities": [
                        {
                            "file_name": "tornado/testing.py",
                            "type": "function",
                            "name": "bind_unused_port",
                            "content": "def bind_unused_port():\n    \"\"\"Binds a server socket to an available port on localhost.\n\n    Returns a tuple (socket, port).\n    \"\"\"\n    [sock] = netutil.bind_sockets(None, 'localhost', family=socket.AF_INET)\n    port = sock.getsockname()[1]\n    return sock, port",
                            "ast_type_str": "FunctionDef",
                            "start_lineno": 88,
                            "end_lineno": 95,
                            "parent": null
                        }
                    ],
                    "added_entities": [],
                    "deleted_entities": []
                }
            ]
        }
    ],
    "old_commit_hash": "fffaa2f7f729570d3bc28b92281d8875e39c9973^",
    "new_commit_hash": "fffaa2f7f729570d3bc28b92281d8875e39c9973",
    "commit_message": "netutil.bind_sockets: add `reuse_port` option to set SO_REUSEPORT\n\nThis option is False by default, because not all platforms support\nit.\n\nSigned-off-by: Anton Tiurin <noxiouz@yandex.ru>",
    "commit_date": "2015-09-12T01:40:47+03:00",
    "metadata": {}
}
{
    "repo_name": "tornado",
    "new_commit_hash": "fffaa2f7f729570d3bc28b92281d8875e39c9973",
    "test_file_codes": [
        "from __future__ import absolute_import, division, print_function, with_statement\n\nimport os\nimport signal\nimport socket\nfrom subprocess import Popen\nimport sys\nimport time\n\nfrom tornado.netutil import BlockingResolver, ThreadedResolver, is_valid_ip, bind_sockets\nfrom tornado.stack_context import ExceptionStackContext\nfrom tornado.testing import AsyncTestCase, gen_test, bind_unused_port\nfrom tornado.test.util import unittest, skipIfNoNetwork\n\ntry:\n    from concurrent import futures\nexcept ImportError:\n    futures = None\n\ntry:\n    import pycares\nexcept ImportError:\n    pycares = None\nelse:\n    from tornado.platform.caresresolver import CaresResolver\n\ntry:\n    import twisted\n    import twisted.names\nexcept ImportError:\n    twisted = None\nelse:\n    from tornado.platform.twisted import TwistedResolver\n\n\nclass _ResolverTestMixin(object):\n    def test_localhost(self):\n        self.resolver.resolve('localhost', 80, callback=self.stop)\n        result = self.wait()\n        self.assertIn((socket.AF_INET, ('127.0.0.1', 80)), result)\n\n    @gen_test\n    def test_future_interface(self):\n        addrinfo = yield self.resolver.resolve('localhost', 80,\n                                               socket.AF_UNSPEC)\n        self.assertIn((socket.AF_INET, ('127.0.0.1', 80)),\n                      addrinfo)\n\n\n# It is impossible to quickly and consistently generate an error in name\n# resolution, so test this case separately, using mocks as needed.\nclass _ResolverErrorTestMixin(object):\n    def test_bad_host(self):\n        def handler(exc_typ, exc_val, exc_tb):\n            self.stop(exc_val)\n            return True  # Halt propagation.\n\n        with ExceptionStackContext(handler):\n            self.resolver.resolve('an invalid domain', 80, callback=self.stop)\n\n        result = self.wait()\n        self.assertIsInstance(result, Exception)\n\n    @gen_test\n    def test_future_interface_bad_host(self):\n        with self.assertRaises(Exception):\n            yield self.resolver.resolve('an invalid domain', 80,\n                                        socket.AF_UNSPEC)\n\n\ndef _failing_getaddrinfo(*args):\n    \"\"\"Dummy implementation of getaddrinfo for use in mocks\"\"\"\n    raise socket.gaierror(\"mock: lookup failed\")\n\n\n@skipIfNoNetwork\nclass BlockingResolverTest(AsyncTestCase, _ResolverTestMixin):\n    def setUp(self):\n        super(BlockingResolverTest, self).setUp()\n        self.resolver = BlockingResolver(io_loop=self.io_loop)\n\n\n# getaddrinfo-based tests need mocking to reliably generate errors;\n# some configurations are slow to produce errors and take longer than\n# our default timeout.\nclass BlockingResolverErrorTest(AsyncTestCase, _ResolverErrorTestMixin):\n    def setUp(self):\n        super(BlockingResolverErrorTest, self).setUp()\n        self.resolver = BlockingResolver(io_loop=self.io_loop)\n        self.real_getaddrinfo = socket.getaddrinfo\n        socket.getaddrinfo = _failing_getaddrinfo\n\n    def tearDown(self):\n        socket.getaddrinfo = self.real_getaddrinfo\n        super(BlockingResolverErrorTest, self).tearDown()\n\n\n@skipIfNoNetwork\n@unittest.skipIf(futures is None, \"futures module not present\")\nclass ThreadedResolverTest(AsyncTestCase, _ResolverTestMixin):\n    def setUp(self):\n        super(ThreadedResolverTest, self).setUp()\n        self.resolver = ThreadedResolver(io_loop=self.io_loop)\n\n    def tearDown(self):\n        self.resolver.close()\n        super(ThreadedResolverTest, self).tearDown()\n\n\nclass ThreadedResolverErrorTest(AsyncTestCase, _ResolverErrorTestMixin):\n    def setUp(self):\n        super(ThreadedResolverErrorTest, self).setUp()\n        self.resolver = BlockingResolver(io_loop=self.io_loop)\n        self.real_getaddrinfo = socket.getaddrinfo\n        socket.getaddrinfo = _failing_getaddrinfo\n\n    def tearDown(self):\n        socket.getaddrinfo = self.real_getaddrinfo\n        super(ThreadedResolverErrorTest, self).tearDown()\n\n\n@skipIfNoNetwork\n@unittest.skipIf(futures is None, \"futures module not present\")\n@unittest.skipIf(sys.platform == 'win32', \"preexec_fn not available on win32\")\nclass ThreadedResolverImportTest(unittest.TestCase):\n    def test_import(self):\n        TIMEOUT = 5\n\n        # Test for a deadlock when importing a module that runs the\n        # ThreadedResolver at import-time. See resolve_test.py for\n        # full explanation.\n        command = [\n            sys.executable,\n            '-c',\n            'import tornado.test.resolve_test_helper']\n\n        start = time.time()\n        popen = Popen(command, preexec_fn=lambda: signal.alarm(TIMEOUT))\n        while time.time() - start < TIMEOUT:\n            return_code = popen.poll()\n            if return_code is not None:\n                self.assertEqual(0, return_code)\n                return  # Success.\n            time.sleep(0.05)\n\n        self.fail(\"import timed out\")\n\n\n# We do not test errors with CaresResolver:\n# Some DNS-hijacking ISPs (e.g. Time Warner) return non-empty results\n# with an NXDOMAIN status code.  Most resolvers treat this as an error;\n# C-ares returns the results, making the \"bad_host\" tests unreliable.\n# C-ares will try to resolve even malformed names, such as the\n# name with spaces used in this test.\n@skipIfNoNetwork\n@unittest.skipIf(pycares is None, \"pycares module not present\")\nclass CaresResolverTest(AsyncTestCase, _ResolverTestMixin):\n    def setUp(self):\n        super(CaresResolverTest, self).setUp()\n        self.resolver = CaresResolver(io_loop=self.io_loop)\n\n\n# TwistedResolver produces consistent errors in our test cases so we\n# can test the regular and error cases in the same class.\n@skipIfNoNetwork\n@unittest.skipIf(twisted is None, \"twisted module not present\")\n@unittest.skipIf(getattr(twisted, '__version__', '0.0') < \"12.1\", \"old version of twisted\")\nclass TwistedResolverTest(AsyncTestCase, _ResolverTestMixin,\n                          _ResolverErrorTestMixin):\n    def setUp(self):\n        super(TwistedResolverTest, self).setUp()\n        self.resolver = TwistedResolver(io_loop=self.io_loop)\n\n\nclass IsValidIPTest(unittest.TestCase):\n    def test_is_valid_ip(self):\n        self.assertTrue(is_valid_ip('127.0.0.1'))\n        self.assertTrue(is_valid_ip('4.4.4.4'))\n        self.assertTrue(is_valid_ip('::1'))\n        self.assertTrue(is_valid_ip('2620:0:1cfe:face:b00c::3'))\n        self.assertTrue(not is_valid_ip('www.google.com'))\n        self.assertTrue(not is_valid_ip('localhost'))\n        self.assertTrue(not is_valid_ip('4.4.4.4<'))\n        self.assertTrue(not is_valid_ip(' 127.0.0.1'))\n        self.assertTrue(not is_valid_ip(''))\n        self.assertTrue(not is_valid_ip(' '))\n        self.assertTrue(not is_valid_ip('\\n'))\n        self.assertTrue(not is_valid_ip('\\x00'))\n\n\nclass TestPortAllocation(unittest.TestCase):\n    def test_same_port_allocation(self):\n        if 'TRAVIS' in os.environ:\n            self.skipTest(\"dual-stack servers often have port conflicts on travis\")\n        sockets = bind_sockets(None, 'localhost')\n        try:\n            port = sockets[0].getsockname()[1]\n            self.assertTrue(all(s.getsockname()[1] == port\n                                for s in sockets[1:]))\n        finally:\n            for sock in sockets:\n                sock.close()\n\n    @unittest.skipIf(not hasattr(socket, \"SO_REUSEPORT\"), \"SO_REUSEPORT is not supported\")\n    def test_reuse_port(self):\n        socket, port = bind_unused_port(reuse_port=True)\n        try:\n            sockets = bind_sockets(port, 'localhost', reuse_port=True)\n            self.assertTrue(all(s.getsockname()[1] == port for s in sockets))\n        finally:\n            socket.close()\n            for sock in sockets:\n                sock.close()\n",
        "#!/usr/bin/env python\nimport unittest\nimport sys\nimport time\nimport traceback\nimport platform\nimport os\n\n\nclass PytestLikeResult(unittest.TestResult):\n    def __init__(self, *args, **kwargs):\n        super().__init__(*args, **kwargs)\n        self.start_time = None\n        # Store (test, outcome, info) for each test:\n        #   outcome ∈ { \"passed\", \"failed\", \"error\", \"skipped\" }\n        #   info can hold the exception or skip reason, etc.\n        self.results = []\n\n    def startTestRun(self):\n        \"\"\"Called once before all tests.\"\"\"\n        self.start_time = time.time()\n        # Print a header similar to pytest\n        print(\"=\" * 29 + \" test session starts \" + \"=\" * 29)\n        # You can customize the platform/versions/paths as you like:\n        print(\n            f\"platform {platform.system().lower()} -- Python {platform.python_version()}\"\n        )\n        print(f\"rootdir: {os.path.abspath(os.curdir)}\")\n\n    def stopTestRun(self):\n        \"\"\"Called once after all tests are done.\"\"\"\n        elapsed = time.time() - self.start_time\n        print()  # blank line after final dot\n\n        # Summarize\n        ntests = len(self.results)\n        print(f\"collected {ntests} items\")\n\n        # Show details of failures/errors\n        failures = [r for r in self.results if r[1] == \"failed\"]\n        errors = [r for r in self.results if r[1] == \"error\"]\n\n        if failures:\n            print(\"\\n\" + \"=\" * 35 + \" FAILURES \" + \"=\" * 35)\n            for test, outcome, err_info in failures:\n                test = test.split(\"::\")\n                if len(test) == 3:\n                    test = f\"{test[1]}.{test[2]}\"\n                else:\n                    test = f\"{test[1]}\"\n                print(f\"__________ {test} __________\")\n                self._print_traceback(err_info)\n        if errors:\n            print(\"\\n\" + \"=\" * 35 + \" ERRORS \" + \"=\" * 37)\n            for test, outcome, err_info in errors:\n                test = test.split(\"::\")\n                if len(test) == 3:\n                    test = f\"{test[1]}.{test[2]}\"\n                else:\n                    test = f\"{test[1]}\"\n                print(f\"__________ {test} __________\")\n                self._print_traceback(err_info)\n\n        # Optional short summary lines for passes/fails/skips\n        print(\"\\n==================== short test summary info ====================\")\n        for test, outcome, info in self.results:\n            if outcome in (\"passed\", \"failed\", \"error\", \"skipped\"):\n                print(f\"{outcome.upper()} {test}\")\n\n        # Final one-line summary: “1 failed, 1 errors, 10 passed in 2.04s”\n        passed_count = sum(1 for r in self.results if r[1] == \"passed\")\n        failed_count = len(failures)\n        error_count = len(errors)\n        skipped_count = sum(1 for r in self.results if r[1] == \"skipped\")\n\n        summary_bits = []\n        if failed_count:\n            summary_bits.append(f\"{failed_count} failed\")\n        if error_count:\n            summary_bits.append(f\"{error_count} error{'s' if error_count != 1 else ''}\")\n        if passed_count:\n            summary_bits.append(f\"{passed_count} passed\")\n        if skipped_count:\n            summary_bits.append(f\"{skipped_count} skipped\")\n\n        summary_str = \", \".join(summary_bits) if summary_bits else \"no tests run\"\n        print(\n            f\"=================== {summary_str} in {elapsed:.2f}s ===================\"\n        )\n\n    def startTest(self, test):\n        \"\"\"Called right before each test method.\"\"\"\n        super().startTest(test)\n        # Print a dot or some indicator\n        print(\".\", end=\"\", flush=True)\n\n    def addSuccess(self, test):\n        super().addSuccess(test)\n        self.results.append((self._test_id(test), \"passed\", None))\n\n    def addFailure(self, test, err):\n        super().addFailure(test, err)\n        self.results.append((self._test_id(test), \"failed\", err))\n\n    def addError(self, test, err):\n        super().addError(test, err)\n        self.results.append((self._test_id(test), \"error\", err))\n\n    def addSkip(self, test, reason):\n        super().addSkip(test, reason)\n        self.results.append((self._test_id(test), \"skipped\", reason))\n\n    def _print_traceback(self, err_info):\n        \"\"\"Helper to print the traceback like pytest does.\"\"\"\n        if isinstance(err_info, tuple) and len(err_info) == 3:\n            exc_type, exc_value, tb = err_info\n            traceback.print_exception(exc_type, exc_value, tb, file=sys.stdout)\n        else:\n            print(str(err_info))\n\n    def _test_id(self, test):\n        try:\n            return f\"{test.__class__.__module__}::{test.__class__.__name__}::{test._testMethodName}\"\n        except AttributeError:\n            # Fallback for _ErrorHolder objects\n            return f\"{test.__class__.__module__}::{test.__class__.__name__}\"\n\n\nclass PytestLikeRunner(unittest.TextTestRunner):\n    \"\"\"A custom TextTestRunner that uses the PytestLikeResult.\"\"\"\n\n    resultclass = PytestLikeResult\n\n    def run(self, test):\n        result = self._makeResult()\n        # Overriding run to ensure we call startTestRun/stopTestRun\n        result.startTestRun()\n        test(result)\n        result.stopTestRun()\n        return result\n\n    def tearDown(self):\n        # Reset signal handlers\n        from twisted.internet import reactor\n\n        reactor.removeAll()\n        reactor.stop()\n        super().tearDown()\n\n\ndef main():\n    loader = unittest.TestLoader()\n    suite = loader.discover(\".\", pattern=\"test_*.py\")  # discover tests in r2e_tests/\n    runner = PytestLikeRunner(verbosity=0)\n    result = runner.run(suite)\n    # Exit with code 0 if all tests passed/skipped, 1 if there were fails/errors\n    sys.exit(not result.wasSuccessful())\n\n\nif __name__ == \"__main__\":\n    main()\n"
    ],
    "test_file_names": [
        "test_1.py",
        "tornado_unittest_runner.py"
    ],
    "setup_res_code": 0,
    "setup_res_stdout": "",
    "setup_res_stderr": "Using CPython 3.9.21\nCreating virtual environment at: .venv\nActivate with: source .venv/bin/activate\nResolved 1 package in 775ms\nPrepared 1 package in 998ms\nInstalled 1 package in 0.79ms\n + tornado==4.3.dev1 (from file:///home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_fffaa2f7f729570d3bc28b92281d8875e39c9973)\nResolved 29 packages in 11ms\nInstalled 29 packages in 62ms\n + attrs==24.3.0\n + automat==24.8.1\n + cachetools==5.5.0\n + cffi==1.17.1\n + chardet==5.2.0\n + colorama==0.4.6\n + constantly==23.10.4\n + distlib==0.3.9\n + exceptiongroup==1.2.2\n + filelock==3.16.1\n + hyperlink==21.0.0\n + idna==3.10\n + incremental==24.7.2\n + iniconfig==2.0.0\n + packaging==24.2\n + platformdirs==4.3.6\n + pluggy==1.5.0\n + pycares==4.5.0\n + pycparser==2.22\n + pycurl==7.45.4\n + pyproject-api==1.8.0\n + pytest==8.3.4\n + setuptools==75.6.0\n + tomli==2.2.1\n + tox==4.23.2\n + twisted==24.11.0\n + typing-extensions==4.12.2\n + virtualenv==20.28.0\n + zope-interface==7.2\n",
    "new_commit_res_code": 0,
    "new_commit_res_stdout": "============================= test session starts =============================\nplatform linux -- Python 3.9.21\nrootdir: /testbed\n..................\ncollected 18 items\n\n=================================== FAILURES ===================================\n__________ CaresResolverTest.test_future_interface __________\nTraceback (most recent call last):\n  File \"/root/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/root/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/root/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/testbed/tornado/testing.py\", line 125, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/testbed/tornado/testing.py\", line 513, in post_coroutine\n    return self.io_loop.run_sync(\n  File \"/testbed/tornado/ioloop.py\", line 445, in run_sync\n    return future_cell[0].result()\n  File \"/testbed/tornado/concurrent.py\", line 223, in result\n    raise_exc_info(self._exc_info)\n  File \"<string>\", line 3, in raise_exc_info\n  File \"/testbed/tornado/gen.py\", line 931, in run\n    yielded = self.gen.send(value)\n  File \"/root/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/types.py\", line 215, in send\n    return self.__wrapped.send(val)\n  File \"/testbed/r2e_tests/test_1.py\", line 46, in test_future_interface\n    self.assertIn((socket.AF_INET, ('127.0.0.1', 80)),\nAssertionError: (<AddressFamily.AF_INET: 2>, ('127.0.0.1', 80)) not found in [(<AddressFamily.AF_INET6: 10>, ('::1', 80))]\n__________ CaresResolverTest.test_localhost __________\nTraceback (most recent call last):\n  File \"/root/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/root/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/root/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/testbed/tornado/testing.py\", line 125, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/testbed/r2e_tests/test_1.py\", line 40, in test_localhost\n    self.assertIn((socket.AF_INET, ('127.0.0.1', 80)), result)\nAssertionError: (<AddressFamily.AF_INET: 2>, ('127.0.0.1', 80)) not found in [(<AddressFamily.AF_INET6: 10>, ('::1', 80))]\n\n==================== short test summary info ====================\nPASSED r2e_tests.test_1::BlockingResolverErrorTest::test_bad_host\nPASSED r2e_tests.test_1::BlockingResolverErrorTest::test_future_interface_bad_host\nPASSED r2e_tests.test_1::BlockingResolverTest::test_future_interface\nPASSED r2e_tests.test_1::BlockingResolverTest::test_localhost\nFAILED r2e_tests.test_1::CaresResolverTest::test_future_interface\nFAILED r2e_tests.test_1::CaresResolverTest::test_localhost\nPASSED r2e_tests.test_1::IsValidIPTest::test_is_valid_ip\nPASSED r2e_tests.test_1::TestPortAllocation::test_reuse_port\nPASSED r2e_tests.test_1::TestPortAllocation::test_same_port_allocation\nPASSED r2e_tests.test_1::ThreadedResolverErrorTest::test_bad_host\nPASSED r2e_tests.test_1::ThreadedResolverErrorTest::test_future_interface_bad_host\nPASSED r2e_tests.test_1::ThreadedResolverImportTest::test_import\nPASSED r2e_tests.test_1::ThreadedResolverTest::test_future_interface\nPASSED r2e_tests.test_1::ThreadedResolverTest::test_localhost\nPASSED r2e_tests.test_1::TwistedResolverTest::test_bad_host\nPASSED r2e_tests.test_1::TwistedResolverTest::test_future_interface\nPASSED r2e_tests.test_1::TwistedResolverTest::test_future_interface_bad_host\nPASSED r2e_tests.test_1::TwistedResolverTest::test_localhost\n=================== 2 failed, 16 passed in 0.12s ===================\n",
    "new_commit_res_stderr": "",
    "old_commit_res_code": 0,
    "old_commit_res_stdout": "============================= test session starts =============================\nplatform linux -- Python 3.9.21\nrootdir: /testbed\n..................\ncollected 18 items\n\n=================================== FAILURES ===================================\n__________ CaresResolverTest.test_future_interface __________\nTraceback (most recent call last):\n  File \"/root/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/root/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/root/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/testbed/tornado/testing.py\", line 124, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/testbed/tornado/testing.py\", line 512, in post_coroutine\n    return self.io_loop.run_sync(\n  File \"/testbed/tornado/ioloop.py\", line 445, in run_sync\n    return future_cell[0].result()\n  File \"/testbed/tornado/concurrent.py\", line 223, in result\n    raise_exc_info(self._exc_info)\n  File \"<string>\", line 3, in raise_exc_info\n  File \"/testbed/tornado/gen.py\", line 931, in run\n    yielded = self.gen.send(value)\n  File \"/root/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/types.py\", line 215, in send\n    return self.__wrapped.send(val)\n  File \"/testbed/r2e_tests/test_1.py\", line 46, in test_future_interface\n    self.assertIn((socket.AF_INET, ('127.0.0.1', 80)),\nAssertionError: (<AddressFamily.AF_INET: 2>, ('127.0.0.1', 80)) not found in [(<AddressFamily.AF_INET6: 10>, ('::1', 80))]\n__________ CaresResolverTest.test_localhost __________\nTraceback (most recent call last):\n  File \"/root/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/root/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/root/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/testbed/tornado/testing.py\", line 124, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/testbed/r2e_tests/test_1.py\", line 40, in test_localhost\n    self.assertIn((socket.AF_INET, ('127.0.0.1', 80)), result)\nAssertionError: (<AddressFamily.AF_INET: 2>, ('127.0.0.1', 80)) not found in [(<AddressFamily.AF_INET6: 10>, ('::1', 80))]\n\n=================================== ERRORS =====================================\n__________ TestPortAllocation.test_reuse_port __________\nTraceback (most recent call last):\n  File \"/root/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/root/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/root/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/testbed/r2e_tests/test_1.py\", line 206, in test_reuse_port\n    socket, port = bind_unused_port(reuse_port=True)\nTypeError: bind_unused_port() got an unexpected keyword argument 'reuse_port'\n\n==================== short test summary info ====================\nPASSED r2e_tests.test_1::BlockingResolverErrorTest::test_bad_host\nPASSED r2e_tests.test_1::BlockingResolverErrorTest::test_future_interface_bad_host\nPASSED r2e_tests.test_1::BlockingResolverTest::test_future_interface\nPASSED r2e_tests.test_1::BlockingResolverTest::test_localhost\nFAILED r2e_tests.test_1::CaresResolverTest::test_future_interface\nFAILED r2e_tests.test_1::CaresResolverTest::test_localhost\nPASSED r2e_tests.test_1::IsValidIPTest::test_is_valid_ip\nERROR r2e_tests.test_1::TestPortAllocation::test_reuse_port\nPASSED r2e_tests.test_1::TestPortAllocation::test_same_port_allocation\nPASSED r2e_tests.test_1::ThreadedResolverErrorTest::test_bad_host\nPASSED r2e_tests.test_1::ThreadedResolverErrorTest::test_future_interface_bad_host\nPASSED r2e_tests.test_1::ThreadedResolverImportTest::test_import\nPASSED r2e_tests.test_1::ThreadedResolverTest::test_future_interface\nPASSED r2e_tests.test_1::ThreadedResolverTest::test_localhost\nPASSED r2e_tests.test_1::TwistedResolverTest::test_bad_host\nPASSED r2e_tests.test_1::TwistedResolverTest::test_future_interface\nPASSED r2e_tests.test_1::TwistedResolverTest::test_future_interface_bad_host\nPASSED r2e_tests.test_1::TwistedResolverTest::test_localhost\n=================== 2 failed, 1 error, 15 passed in 0.21s ===================\n",
    "old_commit_res_stderr": ""
}
tornado/netutil.py tornado/test/netutil_test.py tornado/testing.py
We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
[ISSUE]
**Title:** `bind_unused_port` Function Does Not Accept `reuse_port` Parameter, Causing TypeError

**Description:**
When attempting to bind an unused port with the `reuse_port` option enabled, a `TypeError` is raised because the `bind_unused_port` function does not accept the `reuse_port` parameter. This prevents developers from setting the `SO_REUSEPORT` socket option, which is necessary for certain networking scenarios.

**Example Buggy Code:**
```python
import unittest
from tornado.netutil import bind_unused_port

class TestPortBinding(unittest.TestCase):
    def test_reuse_port_option(self):
        # Attempting to bind an unused port with reuse_port=True
        socket, port = bind_unused_port(reuse_port=True)
        # This line raises TypeError
```

**Expected Behavior:**
The `bind_unused_port` function should accept the `reuse_port` parameter, allowing the `SO_REUSEPORT` option to be set on the socket. This enables multiple sockets to bind to the same port, provided the underlying platform supports it.

**Actual Behavior:**
Passing `reuse_port=True` to `bind_unused_port` results in the following error:
```
TypeError: bind_unused_port() got an unexpected keyword argument 'reuse_port'
```

[/ISSUE]
--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.

--- BEGIN FILE ---
```
### tornado/testing.py
#!/usr/bin/env python
"""Support classes for automated testing.

* `AsyncTestCase` and `AsyncHTTPTestCase`:  Subclasses of unittest.TestCase
  with additional support for testing asynchronous (`.IOLoop` based) code.

* `ExpectLog` and `LogTrapTestCase`: Make test logs less spammy.

* `main()`: A simple test runner (wrapper around unittest.main()) with support
  for the tornado.autoreload module to rerun the tests when code changes.
"""

from __future__ import absolute_import, division, print_function, with_statement

try:
    from tornado import gen
    from tornado.httpclient import AsyncHTTPClient
    from tornado.httpserver import HTTPServer
    from tornado.simple_httpclient import SimpleAsyncHTTPClient
    from tornado.ioloop import IOLoop, TimeoutError
    from tornado import netutil
    from tornado.process import Subprocess
except ImportError:
    # These modules are not importable on app engine.  Parts of this module
    # won't work, but e.g. LogTrapTestCase and main() will.
    AsyncHTTPClient = None
    gen = None
    HTTPServer = None
    IOLoop = None
    netutil = None
    SimpleAsyncHTTPClient = None
    Subprocess = None
from tornado.log import gen_log, app_log
from tornado.stack_context import ExceptionStackContext
from tornado.util import raise_exc_info, basestring_type
import functools
import logging
import os
import re
import signal
import socket
import sys

try:
    from cStringIO import StringIO  # py2
except ImportError:
    from io import StringIO  # py3

try:
    from collections.abc import Generator as GeneratorType  # py35+
except ImportError:
    from types import GeneratorType

# Tornado's own test suite requires the updated unittest module
# (either py27+ or unittest2) so tornado.test.util enforces
# this requirement, but for other users of tornado.testing we want
# to allow the older version if unitest2 is not available.
if sys.version_info >= (3,):
    # On python 3, mixing unittest2 and unittest (including doctest)
    # doesn't seem to work, so always use unittest.
    import unittest
else:
    # On python 2, prefer unittest2 when available.
    try:
        import unittest2 as unittest
    except ImportError:
        import unittest

_next_port = 10000


def get_unused_port():
    """Returns a (hopefully) unused port number.

    This function does not guarantee that the port it returns is available,
    only that a series of get_unused_port calls in a single process return
    distinct ports.

    .. deprecated::
       Use bind_unused_port instead, which is guaranteed to find an unused port.
    """
    global _next_port
    port = _next_port
    _next_port = _next_port + 1
    return port


def bind_unused_port():
    """Binds a server socket to an available port on localhost.

    Returns a tuple (socket, port).
    """
    [sock] = netutil.bind_sockets(None, 'localhost', family=socket.AF_INET)
    port = sock.getsockname()[1]
    return sock, port


def get_async_test_timeout():
    """Get the global timeout setting for async tests.

    Returns a float, the timeout in seconds.

    .. versionadded:: 3.1
    """
    try:
        return float(os.environ.get('ASYNC_TEST_TIMEOUT'))
    except (ValueError, TypeError):
        return 5


class _TestMethodWrapper(object):
    """Wraps a test method to raise an error if it returns a value.

    This is mainly used to detect undecorated generators (if a test
    method yields it must use a decorator to consume the generator),
    but will also detect other kinds of return values (these are not
    necessarily errors, but we alert anyway since there is no good
    reason to return a value from a test.
    """
    def __init__(self, orig_method):
        self.orig_method = orig_method

    def __call__(self, *args, **kwargs):
        result = self.orig_method(*args, **kwargs)
        if isinstance(result, GeneratorType):
            raise TypeError("Generator test methods should be decorated with "
                            "tornado.testing.gen_test")
        elif result is not None:
            raise ValueError("Return value from test method ignored: %r" %
                             result)

    def __getattr__(self, name):
        """Proxy all unknown attributes to the original method.

        This is important for some of the decorators in the `unittest`
        module, such as `unittest.skipIf`.
        """
        return getattr(self.orig_method, name)


class AsyncTestCase(unittest.TestCase):
    """`~unittest.TestCase` subclass for testing `.IOLoop`-based
    asynchronous code.

    The unittest framework is synchronous, so the test must be
    complete by the time the test method returns.  This means that
    asynchronous code cannot be used in quite the same way as usual.
    To write test functions that use the same ``yield``-based patterns
    used with the `tornado.gen` module, decorate your test methods
    with `tornado.testing.gen_test` instead of
    `tornado.gen.coroutine`.  This class also provides the `stop()`
    and `wait()` methods for a more manual style of testing.  The test
    method itself must call ``self.wait()``, and asynchronous
    callbacks should call ``self.stop()`` to signal completion.

    By default, a new `.IOLoop` is constructed for each test and is available
    as ``self.io_loop``.  This `.IOLoop` should be used in the construction of
    HTTP clients/servers, etc.  If the code being tested requires a
    global `.IOLoop`, subclasses should override `get_new_ioloop` to return it.

    The `.IOLoop`'s ``start`` and ``stop`` methods should not be
    called directly.  Instead, use `self.stop <stop>` and `self.wait
    <wait>`.  Arguments passed to ``self.stop`` are returned from
    ``self.wait``.  It is possible to have multiple ``wait``/``stop``
    cycles in the same test.

    Example::

        # This test uses coroutine style.
        class MyTestCase(AsyncTestCase):
            @tornado.testing.gen_test
            def test_http_fetch(self):
                client = AsyncHTTPClient(self.io_loop)
                response = yield client.fetch("http://www.tornadoweb.org")
                # Test contents of response
                self.assertIn("FriendFeed", response.body)

        # This test uses argument passing between self.stop and self.wait.
        class MyTestCase2(AsyncTestCase):
            def test_http_fetch(self):
                client = AsyncHTTPClient(self.io_loop)
                client.fetch("http://www.tornadoweb.org/", self.stop)
                response = self.wait()
                # Test contents of response
                self.assertIn("FriendFeed", response.body)

        # This test uses an explicit callback-based style.
        class MyTestCase3(AsyncTestCase):
            def test_http_fetch(self):
                client = AsyncHTTPClient(self.io_loop)
                client.fetch("http://www.tornadoweb.org/", self.handle_fetch)
                self.wait()

            def handle_fetch(self, response):
                # Test contents of response (failures and exceptions here
                # will cause self.wait() to throw an exception and end the
                # test).
                # Exceptions thrown here are magically propagated to
                # self.wait() in test_http_fetch() via stack_context.
                self.assertIn("FriendFeed", response.body)
                self.stop()
    """
    def __init__(self, methodName='runTest', **kwargs):
        super(AsyncTestCase, self).__init__(methodName, **kwargs)
        self.__stopped = False
        self.__running = False
        self.__failure = None
        self.__stop_args = None
        self.__timeout = None

        # It's easy to forget the @gen_test decorator, but if you do
        # the test will silently be ignored because nothing will consume
        # the generator.  Replace the test method with a wrapper that will
        # make sure it's not an undecorated generator.
        setattr(self, methodName, _TestMethodWrapper(getattr(self, methodName)))

    def setUp(self):
        super(AsyncTestCase, self).setUp()
        self.io_loop = self.get_new_ioloop()
        self.io_loop.make_current()

    def tearDown(self):
        # Clean up Subprocess, so it can be used again with a new ioloop.
        Subprocess.uninitialize()
        self.io_loop.clear_current()
        if (not IOLoop.initialized() or
                self.io_loop is not IOLoop.instance()):
            # Try to clean up any file descriptors left open in the ioloop.
            # This avoids leaks, especially when tests are run repeatedly
            # in the same process with autoreload (because curl does not
            # set FD_CLOEXEC on its file descriptors)
            self.io_loop.close(all_fds=True)
        super(AsyncTestCase, self).tearDown()
        # In case an exception escaped or the StackContext caught an exception
        # when there wasn't a wait() to re-raise it, do so here.
        # This is our last chance to raise an exception in a way that the
        # unittest machinery understands.
        self.__rethrow()

    def get_new_ioloop(self):
        """Creates a new `.IOLoop` for this test.  May be overridden in
        subclasses for tests that require a specific `.IOLoop` (usually
        the singleton `.IOLoop.instance()`).
        """
        return IOLoop()

    def _handle_exception(self, typ, value, tb):
        if self.__failure is None:
            self.__failure = (typ, value, tb)
        else:
            app_log.error("multiple unhandled exceptions in test",
                          exc_info=(typ, value, tb))
        self.stop()
        return True

    def __rethrow(self):
        if self.__failure is not None:
            failure = self.__failure
            self.__failure = None
            raise_exc_info(failure)

    def run(self, result=None):
        with ExceptionStackContext(self._handle_exception):
            super(AsyncTestCase, self).run(result)
        # As a last resort, if an exception escaped super.run() and wasn't
        # re-raised in tearDown, raise it here.  This will cause the
        # unittest run to fail messily, but that's better than silently
        # ignoring an error.
        self.__rethrow()

    def stop(self, _arg=None, **kwargs):
        """Stops the `.IOLoop`, causing one pending (or future) call to `wait()`
        to return.

        Keyword arguments or a single positional argument passed to `stop()` are
        saved and will be returned by `wait()`.
        """
        assert _arg is None or not kwargs
        self.__stop_args = kwargs or _arg
        if self.__running:
            self.io_loop.stop()
            self.__running = False
        self.__stopped = True

    def wait(self, condition=None, timeout=None):
        """Runs the `.IOLoop` until stop is called or timeout has passed.

        In the event of a timeout, an exception will be thrown. The
        default timeout is 5 seconds; it may be overridden with a
        ``timeout`` keyword argument or globally with the
        ``ASYNC_TEST_TIMEOUT`` environment variable.

        If ``condition`` is not None, the `.IOLoop` will be restarted
        after `stop()` until ``condition()`` returns true.

        .. versionchanged:: 3.1
           Added the ``ASYNC_TEST_TIMEOUT`` environment variable.
        """
        if timeout is None:
            timeout = get_async_test_timeout()

        if not self.__stopped:
            if timeout:
                def timeout_func():
                    try:
                        raise self.failureException(
                            'Async operation timed out after %s seconds' %
                            timeout)
                    except Exception:
                        self.__failure = sys.exc_info()
                    self.stop()
                self.__timeout = self.io_loop.add_timeout(self.io_loop.time() + timeout, timeout_func)
            while True:
                self.__running = True
                self.io_loop.start()
                if (self.__failure is not None or
                        condition is None or condition()):
                    break
            if self.__timeout is not None:
                self.io_loop.remove_timeout(self.__timeout)
                self.__timeout = None
        assert self.__stopped
        self.__stopped = False
        self.__rethrow()
        result = self.__stop_args
        self.__stop_args = None
        return result


class AsyncHTTPTestCase(AsyncTestCase):
    """A test case that starts up an HTTP server.

    Subclasses must override `get_app()`, which returns the
    `tornado.web.Application` (or other `.HTTPServer` callback) to be tested.
    Tests will typically use the provided ``self.http_client`` to fetch
    URLs from this server.

    Example, assuming the "Hello, world" example from the user guide is in
    ``hello.py``::

        import hello

        class TestHelloApp(AsyncHTTPTestCase):
            def get_app(self):
                return hello.make_app()

            def test_homepage(self):
                response = self.fetch('/')
                self.assertEqual(response.code, 200)
                self.assertEqual(response.body, 'Hello, world')

    That call to ``self.fetch()`` is equivalent to ::

        self.http_client.fetch(self.get_url('/'), self.stop)
        response = self.wait()

    which illustrates how AsyncTestCase can turn an asynchronous operation,
    like ``http_client.fetch()``, into a synchronous operation. If you need
    to do other asynchronous operations in tests, you'll probably need to use
    ``stop()`` and ``wait()`` yourself.
    """
    def setUp(self):
        super(AsyncHTTPTestCase, self).setUp()
        sock, port = bind_unused_port()
        self.__port = port

        self.http_client = self.get_http_client()
        self._app = self.get_app()
        self.http_server = self.get_http_server()
        self.http_server.add_sockets([sock])

    def get_http_client(self):
        return AsyncHTTPClient(io_loop=self.io_loop)

    def get_http_server(self):
        return HTTPServer(self._app, io_loop=self.io_loop,
                          **self.get_httpserver_options())

    def get_app(self):
        """Should be overridden by subclasses to return a
        `tornado.web.Application` or other `.HTTPServer` callback.
        """
        raise NotImplementedError()

    def fetch(self, path, **kwargs):
        """Convenience method to synchronously fetch a url.

        The given path will be appended to the local server's host and
        port.  Any additional kwargs will be passed directly to
        `.AsyncHTTPClient.fetch` (and so could be used to pass
        ``method="POST"``, ``body="..."``, etc).
        """
        self.http_client.fetch(self.get_url(path), self.stop, **kwargs)
        return self.wait()

    def get_httpserver_options(self):
        """May be overridden by subclasses to return additional
        keyword arguments for the server.
        """
        return {}

    def get_http_port(self):
        """Returns the port used by the server.

        A new port is chosen for each test.
        """
        return self.__port

    def get_protocol(self):
        return 'http'

    def get_url(self, path):
        """Returns an absolute url for the given path on the test server."""
        return '%s://localhost:%s%s' % (self.get_protocol(),
                                        self.get_http_port(), path)

    def tearDown(self):
        self.http_server.stop()
        self.io_loop.run_sync(self.http_server.close_all_connections,
                              timeout=get_async_test_timeout())
        if (not IOLoop.initialized() or
                self.http_client.io_loop is not IOLoop.instance()):
            self.http_client.close()
        super(AsyncHTTPTestCase, self).tearDown()


class AsyncHTTPSTestCase(AsyncHTTPTestCase):
    """A test case that starts an HTTPS server.

    Interface is generally the same as `AsyncHTTPTestCase`.
    """
    def get_http_client(self):
        return AsyncHTTPClient(io_loop=self.io_loop, force_instance=True,
                               defaults=dict(validate_cert=False))

    def get_httpserver_options(self):
        return dict(ssl_options=self.get_ssl_options())

    def get_ssl_options(self):
        """May be overridden by subclasses to select SSL options.

        By default includes a self-signed testing certificate.
        """
        # Testing keys were generated with:
        # openssl req -new -keyout tornado/test/test.key -out tornado/test/test.crt -nodes -days 3650 -x509
        module_dir = os.path.dirname(__file__)
        return dict(
            certfile=os.path.join(module_dir, 'test', 'test.crt'),
            keyfile=os.path.join(module_dir, 'test', 'test.key'))

    def get_protocol(self):
        return 'https'


def gen_test(func=None, timeout=None):
    """Testing equivalent of ``@gen.coroutine``, to be applied to test methods.

    ``@gen.coroutine`` cannot be used on tests because the `.IOLoop` is not
    already running.  ``@gen_test`` should be applied to test methods
    on subclasses of `AsyncTestCase`.

    Example::

        class MyTest(AsyncHTTPTestCase):
            @gen_test
            def test_something(self):
                response = yield gen.Task(self.fetch('/'))

    By default, ``@gen_test`` times out after 5 seconds. The timeout may be
    overridden globally with the ``ASYNC_TEST_TIMEOUT`` environment variable,
    or for each test with the ``timeout`` keyword argument::

        class MyTest(AsyncHTTPTestCase):
            @gen_test(timeout=10)
            def test_something_slow(self):
                response = yield gen.Task(self.fetch('/'))

    .. versionadded:: 3.1
       The ``timeout`` argument and ``ASYNC_TEST_TIMEOUT`` environment
       variable.

    .. versionchanged:: 4.0
       The wrapper now passes along ``*args, **kwargs`` so it can be used
       on functions with arguments.
    """
    if timeout is None:
        timeout = get_async_test_timeout()

    def wrap(f):
        # Stack up several decorators to allow us to access the generator
        # object itself.  In the innermost wrapper, we capture the generator
        # and save it in an attribute of self.  Next, we run the wrapped
        # function through @gen.coroutine.  Finally, the coroutine is
        # wrapped again to make it synchronous with run_sync.
        #
        # This is a good case study arguing for either some sort of
        # extensibility in the gen decorators or cancellation support.
        @functools.wraps(f)
        def pre_coroutine(self, *args, **kwargs):
            result = f(self, *args, **kwargs)
            if isinstance(result, GeneratorType):
                self._test_generator = result
            else:
                self._test_generator = None
            return result

        coro = gen.coroutine(pre_coroutine)

        @functools.wraps(coro)
        def post_coroutine(self, *args, **kwargs):
            try:
                return self.io_loop.run_sync(
                    functools.partial(coro, self, *args, **kwargs),
                    timeout=timeout)
            except TimeoutError as e:
                # run_sync raises an error with an unhelpful traceback.
                # If we throw it back into the generator the stack trace
                # will be replaced by the point where the test is stopped.
                self._test_generator.throw(e)
                # In case the test contains an overly broad except clause,
                # we may get back here.  In this case re-raise the original
                # exception, which is better than nothing.
                raise
        return post_coroutine

    if func is not None:
        # Used like:
        #     @gen_test
        #     def f(self):
        #         pass
        return wrap(func)
    else:
        # Used like @gen_test(timeout=10)
        return wrap


# Without this attribute, nosetests will try to run gen_test as a test
# anywhere it is imported.
gen_test.__test__ = False


class LogTrapTestCase(unittest.TestCase):
    """A test case that captures and discards all logging output
    if the test passes.

    Some libraries can produce a lot of logging output even when
    the test succeeds, so this class can be useful to minimize the noise.
    Simply use it as a base class for your test case.  It is safe to combine
    with AsyncTestCase via multiple inheritance
    (``class MyTestCase(AsyncHTTPTestCase, LogTrapTestCase):``)

    This class assumes that only one log handler is configured and
    that it is a `~logging.StreamHandler`.  This is true for both
    `logging.basicConfig` and the "pretty logging" configured by
    `tornado.options`.  It is not compatible with other log buffering
    mechanisms, such as those provided by some test runners.

    .. deprecated:: 4.1
       Use the unittest module's ``--buffer`` option instead, or `.ExpectLog`.
    """
    def run(self, result=None):
        logger = logging.getLogger()
        if not logger.handlers:
            logging.basicConfig()
        handler = logger.handlers[0]
        if (len(logger.handlers) > 1 or
                not isinstance(handler, logging.StreamHandler)):
            # Logging has been configured in a way we don't recognize,
            # so just leave it alone.
            super(LogTrapTestCase, self).run(result)
            return
        old_stream = handler.stream
        try:
            handler.stream = StringIO()
            gen_log.info("RUNNING TEST: " + str(self))
            old_error_count = len(result.failures) + len(result.errors)
            super(LogTrapTestCase, self).run(result)
            new_error_count = len(result.failures) + len(result.errors)
            if new_error_count != old_error_count:
                old_stream.write(handler.stream.getvalue())
        finally:
            handler.stream = old_stream


class ExpectLog(logging.Filter):
    """Context manager to capture and suppress expected log output.

    Useful to make tests of error conditions less noisy, while still
    leaving unexpected log entries visible.  *Not thread safe.*

    The attribute ``logged_stack`` is set to true if any exception
    stack trace was logged.

    Usage::

        with ExpectLog('tornado.application', "Uncaught exception"):
            error_response = self.fetch("/some_page")

    .. versionchanged:: 4.3
       Added the ``logged_stack`` attribute.
    """
    def __init__(self, logger, regex, required=True):
        """Constructs an ExpectLog context manager.

        :param logger: Logger object (or name of logger) to watch.  Pass
            an empty string to watch the root logger.
        :param regex: Regular expression to match.  Any log entries on
            the specified logger that match this regex will be suppressed.
        :param required: If true, an exeption will be raised if the end of
            the ``with`` statement is reached without matching any log entries.
        """
        if isinstance(logger, basestring_type):
            logger = logging.getLogger(logger)
        self.logger = logger
        self.regex = re.compile(regex)
        self.required = required
        self.matched = False
        self.logged_stack = False

    def filter(self, record):
        if record.exc_info:
            self.logged_stack = True
        message = record.getMessage()
        if self.regex.match(message):
            self.matched = True
            return False
        return True

    def __enter__(self):
        self.logger.addFilter(self)
        return self

    def __exit__(self, typ, value, tb):
        self.logger.removeFilter(self)
        if not typ and self.required and not self.matched:
            raise Exception("did not get expected log message")


def main(**kwargs):
    """A simple test runner.

    This test runner is essentially equivalent to `unittest.main` from
    the standard library, but adds support for tornado-style option
    parsing and log formatting.

    The easiest way to run a test is via the command line::

        python -m tornado.testing tornado.test.stack_context_test

    See the standard library unittest module for ways in which tests can
    be specified.

    Projects with many tests may wish to define a test script like
    ``tornado/test/runtests.py``.  This script should define a method
    ``all()`` which returns a test suite and then call
    `tornado.testing.main()`.  Note that even when a test script is
    used, the ``all()`` test suite may be overridden by naming a
    single test on the command line::

        # Runs all tests
        python -m tornado.test.runtests
        # Runs one test
        python -m tornado.test.runtests tornado.test.stack_context_test

    Additional keyword arguments passed through to ``unittest.main()``.
    For example, use ``tornado.testing.main(verbosity=2)``
    to show many test details as they are run.
    See http://docs.python.org/library/unittest.html#unittest.main
    for full argument list.
    """
    from tornado.options import define, options, parse_command_line

    define('exception_on_interrupt', type=bool, default=True,
           help=("If true (default), ctrl-c raises a KeyboardInterrupt "
                 "exception.  This prints a stack trace but cannot interrupt "
                 "certain operations.  If false, the process is more reliably "
                 "killed, but does not print a stack trace."))

    # support the same options as unittest's command-line interface
    define('verbose', type=bool)
    define('quiet', type=bool)
    define('failfast', type=bool)
    define('catch', type=bool)
    define('buffer', type=bool)

    argv = [sys.argv[0]] + parse_command_line(sys.argv)

    if not options.exception_on_interrupt:
        signal.signal(signal.SIGINT, signal.SIG_DFL)

    if options.verbose is not None:
        kwargs['verbosity'] = 2
    if options.quiet is not None:
        kwargs['verbosity'] = 0
    if options.failfast is not None:
        kwargs['failfast'] = True
    if options.catch is not None:
        kwargs['catchbreak'] = True
    if options.buffer is not None:
        kwargs['buffer'] = True

    if __name__ == '__main__' and len(argv) == 1:
        print("No tests specified", file=sys.stderr)
        sys.exit(1)
    try:
        # In order to be able to run tests by their fully-qualified name
        # on the command line without importing all tests here,
        # module must be set to None.  Python 3.2's unittest.main ignores
        # defaultTest if no module is given (it tries to do its own
        # test discovery, which is incompatible with auto2to3), so don't
        # set module if we're not asking for a specific test.
        if len(argv) > 1:
            unittest.main(module=None, argv=argv, **kwargs)
        else:
            unittest.main(defaultTest="all", argv=argv, **kwargs)
    except SystemExit as e:
        if e.code == 0:
            gen_log.info('PASS')
        else:
            gen_log.error('FAIL')
        raise

if __name__ == '__main__':
    main()


### tornado/netutil.py
#!/usr/bin/env python
#
# Copyright 2011 Facebook
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

"""Miscellaneous network utility code."""

from __future__ import absolute_import, division, print_function, with_statement

import errno
import os
import sys
import socket
import stat

from tornado.concurrent import dummy_executor, run_on_executor
from tornado.ioloop import IOLoop
from tornado.platform.auto import set_close_exec
from tornado.util import u, Configurable, errno_from_exception

try:
    import ssl
except ImportError:
    # ssl is not available on Google App Engine
    ssl = None

try:
    import certifi
except ImportError:
    # certifi is optional as long as we have ssl.create_default_context.
    if ssl is None or hasattr(ssl, 'create_default_context'):
        certifi = None
    else:
        raise

try:
    xrange  # py2
except NameError:
    xrange = range  # py3

if hasattr(ssl, 'match_hostname') and hasattr(ssl, 'CertificateError'):  # python 3.2+
    ssl_match_hostname = ssl.match_hostname
    SSLCertificateError = ssl.CertificateError
elif ssl is None:
    ssl_match_hostname = SSLCertificateError = None
else:
    import backports.ssl_match_hostname
    ssl_match_hostname = backports.ssl_match_hostname.match_hostname
    SSLCertificateError = backports.ssl_match_hostname.CertificateError

if hasattr(ssl, 'SSLContext'):
    if hasattr(ssl, 'create_default_context'):
        # Python 2.7.9+, 3.4+
        # Note that the naming of ssl.Purpose is confusing; the purpose
        # of a context is to authentiate the opposite side of the connection.
        _client_ssl_defaults = ssl.create_default_context(
            ssl.Purpose.SERVER_AUTH)
        _server_ssl_defaults = ssl.create_default_context(
            ssl.Purpose.CLIENT_AUTH)
    else:
        # Python 3.2-3.3
        _client_ssl_defaults = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
        _client_ssl_defaults.verify_mode = ssl.CERT_REQUIRED
        _client_ssl_defaults.load_verify_locations(certifi.where())
        _server_ssl_defaults = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
        if hasattr(ssl, 'OP_NO_COMPRESSION'):
            # Disable TLS compression to avoid CRIME and related attacks.
            # This constant wasn't added until python 3.3.
            _client_ssl_defaults.options |= ssl.OP_NO_COMPRESSION
            _server_ssl_defaults.options |= ssl.OP_NO_COMPRESSION

elif ssl:
    # Python 2.6-2.7.8
    _client_ssl_defaults = dict(cert_reqs=ssl.CERT_REQUIRED,
                                ca_certs=certifi.where())
    _server_ssl_defaults = {}
else:
    # Google App Engine
    _client_ssl_defaults = dict(cert_reqs=None,
                                ca_certs=None)
    _server_ssl_defaults = {}

# ThreadedResolver runs getaddrinfo on a thread. If the hostname is unicode,
# getaddrinfo attempts to import encodings.idna. If this is done at
# module-import time, the import lock is already held by the main thread,
# leading to deadlock. Avoid it by caching the idna encoder on the main
# thread now.
u('foo').encode('idna')

# These errnos indicate that a non-blocking operation must be retried
# at a later time.  On most platforms they're the same value, but on
# some they differ.
_ERRNO_WOULDBLOCK = (errno.EWOULDBLOCK, errno.EAGAIN)

if hasattr(errno, "WSAEWOULDBLOCK"):
    _ERRNO_WOULDBLOCK += (errno.WSAEWOULDBLOCK,)

# Default backlog used when calling sock.listen()
_DEFAULT_BACKLOG = 128


def bind_sockets(port, address=None, family=socket.AF_UNSPEC,
                 backlog=_DEFAULT_BACKLOG, flags=None):
    """Creates listening sockets bound to the given port and address.

    Returns a list of socket objects (multiple sockets are returned if
    the given address maps to multiple IP addresses, which is most common
    for mixed IPv4 and IPv6 use).

    Address may be either an IP address or hostname.  If it's a hostname,
    the server will listen on all IP addresses associated with the
    name.  Address may be an empty string or None to listen on all
    available interfaces.  Family may be set to either `socket.AF_INET`
    or `socket.AF_INET6` to restrict to IPv4 or IPv6 addresses, otherwise
    both will be used if available.

    The ``backlog`` argument has the same meaning as for
    `socket.listen() <socket.socket.listen>`.

    ``flags`` is a bitmask of AI_* flags to `~socket.getaddrinfo`, like
    ``socket.AI_PASSIVE | socket.AI_NUMERICHOST``.
    """
    sockets = []
    if address == "":
        address = None
    if not socket.has_ipv6 and family == socket.AF_UNSPEC:
        # Python can be compiled with --disable-ipv6, which causes
        # operations on AF_INET6 sockets to fail, but does not
        # automatically exclude those results from getaddrinfo
        # results.
        # http://bugs.python.org/issue16208
        family = socket.AF_INET
    if flags is None:
        flags = socket.AI_PASSIVE
    bound_port = None
    for res in set(socket.getaddrinfo(address, port, family, socket.SOCK_STREAM,
                                      0, flags)):
        af, socktype, proto, canonname, sockaddr = res
        if (sys.platform == 'darwin' and address == 'localhost' and
                af == socket.AF_INET6 and sockaddr[3] != 0):
            # Mac OS X includes a link-local address fe80::1%lo0 in the
            # getaddrinfo results for 'localhost'.  However, the firewall
            # doesn't understand that this is a local address and will
            # prompt for access (often repeatedly, due to an apparent
            # bug in its ability to remember granting access to an
            # application). Skip these addresses.
            continue
        try:
            sock = socket.socket(af, socktype, proto)
        except socket.error as e:
            if errno_from_exception(e) == errno.EAFNOSUPPORT:
                continue
            raise
        set_close_exec(sock.fileno())
        if os.name != 'nt':
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        if af == socket.AF_INET6:
            # On linux, ipv6 sockets accept ipv4 too by default,
            # but this makes it impossible to bind to both
            # 0.0.0.0 in ipv4 and :: in ipv6.  On other systems,
            # separate sockets *must* be used to listen for both ipv4
            # and ipv6.  For consistency, always disable ipv4 on our
            # ipv6 sockets and use a separate ipv4 socket when needed.
            #
            # Python 2.x on windows doesn't have IPPROTO_IPV6.
            if hasattr(socket, "IPPROTO_IPV6"):
                sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)

        # automatic port allocation with port=None
        # should bind on the same port on IPv4 and IPv6
        host, requested_port = sockaddr[:2]
        if requested_port == 0 and bound_port is not None:
            sockaddr = tuple([host, bound_port] + list(sockaddr[2:]))

        sock.setblocking(0)
        sock.bind(sockaddr)
        bound_port = sock.getsockname()[1]
        sock.listen(backlog)
        sockets.append(sock)
    return sockets

if hasattr(socket, 'AF_UNIX'):
    def bind_unix_socket(file, mode=0o600, backlog=_DEFAULT_BACKLOG):
        """Creates a listening unix socket.

        If a socket with the given name already exists, it will be deleted.
        If any other file with that name exists, an exception will be
        raised.

        Returns a socket object (not a list of socket objects like
        `bind_sockets`)
        """
        sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        set_close_exec(sock.fileno())
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.setblocking(0)
        try:
            st = os.stat(file)
        except OSError as err:
            if errno_from_exception(err) != errno.ENOENT:
                raise
        else:
            if stat.S_ISSOCK(st.st_mode):
                os.remove(file)
            else:
                raise ValueError("File %s exists and is not a socket", file)
        sock.bind(file)
        os.chmod(file, mode)
        sock.listen(backlog)
        return sock


def add_accept_handler(sock, callback, io_loop=None):
    """Adds an `.IOLoop` event handler to accept new connections on ``sock``.

    When a connection is accepted, ``callback(connection, address)`` will
    be run (``connection`` is a socket object, and ``address`` is the
    address of the other end of the connection).  Note that this signature
    is different from the ``callback(fd, events)`` signature used for
    `.IOLoop` handlers.

    .. versionchanged:: 4.1
       The ``io_loop`` argument is deprecated.
    """
    if io_loop is None:
        io_loop = IOLoop.current()

    def accept_handler(fd, events):
        # More connections may come in while we're handling callbacks;
        # to prevent starvation of other tasks we must limit the number
        # of connections we accept at a time.  Ideally we would accept
        # up to the number of connections that were waiting when we
        # entered this method, but this information is not available
        # (and rearranging this method to call accept() as many times
        # as possible before running any callbacks would have adverse
        # effects on load balancing in multiprocess configurations).
        # Instead, we use the (default) listen backlog as a rough
        # heuristic for the number of connections we can reasonably
        # accept at once.
        for i in xrange(_DEFAULT_BACKLOG):
            try:
                connection, address = sock.accept()
            except socket.error as e:
                # _ERRNO_WOULDBLOCK indicate we have accepted every
                # connection that is available.
                if errno_from_exception(e) in _ERRNO_WOULDBLOCK:
                    return
                # ECONNABORTED indicates that there was a connection
                # but it was closed while still in the accept queue.
                # (observed on FreeBSD).
                if errno_from_exception(e) == errno.ECONNABORTED:
                    continue
                raise
            callback(connection, address)
    io_loop.add_handler(sock, accept_handler, IOLoop.READ)


def is_valid_ip(ip):
    """Returns true if the given string is a well-formed IP address.

    Supports IPv4 and IPv6.
    """
    if not ip or '\x00' in ip:
        # getaddrinfo resolves empty strings to localhost, and truncates
        # on zero bytes.
        return False
    try:
        res = socket.getaddrinfo(ip, 0, socket.AF_UNSPEC,
                                 socket.SOCK_STREAM,
                                 0, socket.AI_NUMERICHOST)
        return bool(res)
    except socket.gaierror as e:
        if e.args[0] == socket.EAI_NONAME:
            return False
        raise
    return True


class Resolver(Configurable):
    """Configurable asynchronous DNS resolver interface.

    By default, a blocking implementation is used (which simply calls
    `socket.getaddrinfo`).  An alternative implementation can be
    chosen with the `Resolver.configure <.Configurable.configure>`
    class method::

        Resolver.configure('tornado.netutil.ThreadedResolver')

    The implementations of this interface included with Tornado are

    * `tornado.netutil.BlockingResolver`
    * `tornado.netutil.ThreadedResolver`
    * `tornado.netutil.OverrideResolver`
    * `tornado.platform.twisted.TwistedResolver`
    * `tornado.platform.caresresolver.CaresResolver`
    """
    @classmethod
    def configurable_base(cls):
        return Resolver

    @classmethod
    def configurable_default(cls):
        return BlockingResolver

    def resolve(self, host, port, family=socket.AF_UNSPEC, callback=None):
        """Resolves an address.

        The ``host`` argument is a string which may be a hostname or a
        literal IP address.

        Returns a `.Future` whose result is a list of (family,
        address) pairs, where address is a tuple suitable to pass to
        `socket.connect <socket.socket.connect>` (i.e. a ``(host,
        port)`` pair for IPv4; additional fields may be present for
        IPv6). If a ``callback`` is passed, it will be run with the
        result as an argument when it is complete.
        """
        raise NotImplementedError()

    def close(self):
        """Closes the `Resolver`, freeing any resources used.

        .. versionadded:: 3.1

        """
        pass


class ExecutorResolver(Resolver):
    """Resolver implementation using a `concurrent.futures.Executor`.

    Use this instead of `ThreadedResolver` when you require additional
    control over the executor being used.

    The executor will be shut down when the resolver is closed unless
    ``close_resolver=False``; use this if you want to reuse the same
    executor elsewhere.

    .. versionchanged:: 4.1
       The ``io_loop`` argument is deprecated.
    """
    def initialize(self, io_loop=None, executor=None, close_executor=True):
        self.io_loop = io_loop or IOLoop.current()
        if executor is not None:
            self.executor = executor
            self.close_executor = close_executor
        else:
            self.executor = dummy_executor
            self.close_executor = False

    def close(self):
        if self.close_executor:
            self.executor.shutdown()
        self.executor = None

    @run_on_executor
    def resolve(self, host, port, family=socket.AF_UNSPEC):
        # On Solaris, getaddrinfo fails if the given port is not found
        # in /etc/services and no socket type is given, so we must pass
        # one here.  The socket type used here doesn't seem to actually
        # matter (we discard the one we get back in the results),
        # so the addresses we return should still be usable with SOCK_DGRAM.
        addrinfo = socket.getaddrinfo(host, port, family, socket.SOCK_STREAM)
        results = []
        for family, socktype, proto, canonname, address in addrinfo:
            results.append((family, address))
        return results


class BlockingResolver(ExecutorResolver):
    """Default `Resolver` implementation, using `socket.getaddrinfo`.

    The `.IOLoop` will be blocked during the resolution, although the
    callback will not be run until the next `.IOLoop` iteration.
    """
    def initialize(self, io_loop=None):
        super(BlockingResolver, self).initialize(io_loop=io_loop)


class ThreadedResolver(ExecutorResolver):
    """Multithreaded non-blocking `Resolver` implementation.

    Requires the `concurrent.futures` package to be installed
    (available in the standard library since Python 3.2,
    installable with ``pip install futures`` in older versions).

    The thread pool size can be configured with::

        Resolver.configure('tornado.netutil.ThreadedResolver',
                           num_threads=10)

    .. versionchanged:: 3.1
       All ``ThreadedResolvers`` share a single thread pool, whose
       size is set by the first one to be created.
    """
    _threadpool = None
    _threadpool_pid = None

    def initialize(self, io_loop=None, num_threads=10):
        threadpool = ThreadedResolver._create_threadpool(num_threads)
        super(ThreadedResolver, self).initialize(
            io_loop=io_loop, executor=threadpool, close_executor=False)

    @classmethod
    def _create_threadpool(cls, num_threads):
        pid = os.getpid()
        if cls._threadpool_pid != pid:
            # Threads cannot survive after a fork, so if our pid isn't what it
            # was when we created the pool then delete it.
            cls._threadpool = None
        if cls._threadpool is None:
            from concurrent.futures import ThreadPoolExecutor
            cls._threadpool = ThreadPoolExecutor(num_threads)
            cls._threadpool_pid = pid
        return cls._threadpool


class OverrideResolver(Resolver):
    """Wraps a resolver with a mapping of overrides.

    This can be used to make local DNS changes (e.g. for testing)
    without modifying system-wide settings.

    The mapping can contain either host strings or host-port pairs.
    """
    def initialize(self, resolver, mapping):
        self.resolver = resolver
        self.mapping = mapping

    def close(self):
        self.resolver.close()

    def resolve(self, host, port, *args, **kwargs):
        if (host, port) in self.mapping:
            host, port = self.mapping[(host, port)]
        elif host in self.mapping:
            host = self.mapping[host]
        return self.resolver.resolve(host, port, *args, **kwargs)


# These are the keyword arguments to ssl.wrap_socket that must be translated
# to their SSLContext equivalents (the other arguments are still passed
# to SSLContext.wrap_socket).
_SSL_CONTEXT_KEYWORDS = frozenset(['ssl_version', 'certfile', 'keyfile',
                                   'cert_reqs', 'ca_certs', 'ciphers'])


def ssl_options_to_context(ssl_options):
    """Try to convert an ``ssl_options`` dictionary to an
    `~ssl.SSLContext` object.

    The ``ssl_options`` dictionary contains keywords to be passed to
    `ssl.wrap_socket`.  In Python 2.7.9+, `ssl.SSLContext` objects can
    be used instead.  This function converts the dict form to its
    `~ssl.SSLContext` equivalent, and may be used when a component which
    accepts both forms needs to upgrade to the `~ssl.SSLContext` version
    to use features like SNI or NPN.
    """
    if isinstance(ssl_options, dict):
        assert all(k in _SSL_CONTEXT_KEYWORDS for k in ssl_options), ssl_options
    if (not hasattr(ssl, 'SSLContext') or
            isinstance(ssl_options, ssl.SSLContext)):
        return ssl_options
    context = ssl.SSLContext(
        ssl_options.get('ssl_version', ssl.PROTOCOL_SSLv23))
    if 'certfile' in ssl_options:
        context.load_cert_chain(ssl_options['certfile'], ssl_options.get('keyfile', None))
    if 'cert_reqs' in ssl_options:
        context.verify_mode = ssl_options['cert_reqs']
    if 'ca_certs' in ssl_options:
        context.load_verify_locations(ssl_options['ca_certs'])
    if 'ciphers' in ssl_options:
        context.set_ciphers(ssl_options['ciphers'])
    if hasattr(ssl, 'OP_NO_COMPRESSION'):
        # Disable TLS compression to avoid CRIME and related attacks.
        # This constant wasn't added until python 3.3.
        context.options |= ssl.OP_NO_COMPRESSION
    return context


def ssl_wrap_socket(socket, ssl_options, server_hostname=None, **kwargs):
    """Returns an ``ssl.SSLSocket`` wrapping the given socket.

    ``ssl_options`` may be either an `ssl.SSLContext` object or a
    dictionary (as accepted by `ssl_options_to_context`).  Additional
    keyword arguments are passed to ``wrap_socket`` (either the
    `~ssl.SSLContext` method or the `ssl` module function as
    appropriate).
    """
    context = ssl_options_to_context(ssl_options)
    if hasattr(ssl, 'SSLContext') and isinstance(context, ssl.SSLContext):
        if server_hostname is not None and getattr(ssl, 'HAS_SNI'):
            # Python doesn't have server-side SNI support so we can't
            # really unittest this, but it can be manually tested with
            # python3.2 -m tornado.httpclient https://sni.velox.ch
            return context.wrap_socket(socket, server_hostname=server_hostname,
                                       **kwargs)
        else:
            return context.wrap_socket(socket, **kwargs)
    else:
        return ssl.wrap_socket(socket, **dict(context, **kwargs))

```
--- END FILE ---

Please first localize the bug based on the issue statement, and then generate *SEARCH/REPLACE* edits to fix the issue.

Every *SEARCH/REPLACE* edit must use this format:
1. The file path
2. The start of search block: <<<<<<< SEARCH
3. A contiguous chunk of lines to search for in the existing source code
4. The dividing line: =======
5. The lines to replace into the source code
6. The end of the replace block: >>>>>>> REPLACE

Here is an example:

```python
### mathweb/flask/app.py
<<<<<<< SEARCH
from flask import Flask
=======
import math
from flask import Flask
>>>>>>> REPLACE
```
</solution>

Please note that the *SEARCH/REPLACE* edit REQUIRES PROPER INDENTATION. If you would like to add the line '        print(x)', you must fully write that out, with all those spaces before the code!
Wrap each *SEARCH/REPLACE* edit in a code block as shown in the example above. If you have multiple *SEARCH/REPLACE* edits, use a separate code block for each one.
[ISSUE]
**Title:** `bind_unused_port` Function Does Not Accept `reuse_port` Parameter, Causing TypeError

**Description:**
When attempting to bind an unused port with the `reuse_port` option enabled, a `TypeError` is raised because the `bind_unused_port` function does not accept the `reuse_port` parameter. This prevents developers from setting the `SO_REUSEPORT` socket option, which is necessary for certain networking scenarios.

**Example Buggy Code:**
```python
import unittest
from tornado.netutil import bind_unused_port

class TestPortBinding(unittest.TestCase):
    def test_reuse_port_option(self):
        # Attempting to bind an unused port with reuse_port=True
        socket, port = bind_unused_port(reuse_port=True)
        # This line raises TypeError
```

**Expected Behavior:**
The `bind_unused_port` function should accept the `reuse_port` parameter, allowing the `SO_REUSEPORT` option to be set on the socket. This enables multiple sockets to bind to the same port, provided the underlying platform supports it.

**Actual Behavior:**
Passing `reuse_port=True` to `bind_unused_port` results in the following error:
```
TypeError: bind_unused_port() got an unexpected keyword argument 'reuse_port'
```

[/ISSUE]
{
    "BlockingResolverErrorTest.test_bad_host": "PASSED",
    "BlockingResolverErrorTest.test_future_interface_bad_host": "PASSED",
    "BlockingResolverTest.test_future_interface": "PASSED",
    "BlockingResolverTest.test_localhost": "PASSED",
    "CaresResolverTest.test_future_interface": "FAILED",
    "CaresResolverTest.test_localhost": "FAILED",
    "IsValidIPTest.test_is_valid_ip": "PASSED",
    "TestPortAllocation.test_reuse_port": "PASSED",
    "TestPortAllocation.test_same_port_allocation": "PASSED",
    "ThreadedResolverErrorTest.test_bad_host": "PASSED",
    "ThreadedResolverErrorTest.test_future_interface_bad_host": "PASSED",
    "ThreadedResolverImportTest.test_import": "PASSED",
    "ThreadedResolverTest.test_future_interface": "PASSED",
    "ThreadedResolverTest.test_localhost": "PASSED",
    "TwistedResolverTest.test_bad_host": "PASSED",
    "TwistedResolverTest.test_future_interface": "PASSED",
    "TwistedResolverTest.test_future_interface_bad_host": "PASSED",
    "TwistedResolverTest.test_localhost": "PASSED"
}
diff --git a/tornado/netutil.py b/tornado/netutil.py
index 9aa292c4..4fc8d04d 100644
--- a/tornado/netutil.py
+++ b/tornado/netutil.py
@@ -111,7 +111,7 @@ _DEFAULT_BACKLOG = 128
 
 
 def bind_sockets(port, address=None, family=socket.AF_UNSPEC,
-                 backlog=_DEFAULT_BACKLOG, flags=None):
+                 backlog=_DEFAULT_BACKLOG, flags=None, reuse_port=False):
     """Creates listening sockets bound to the given port and address.
 
     Returns a list of socket objects (multiple sockets are returned if
@@ -130,7 +130,14 @@ def bind_sockets(port, address=None, family=socket.AF_UNSPEC,
 
     ``flags`` is a bitmask of AI_* flags to `~socket.getaddrinfo`, like
     ``socket.AI_PASSIVE | socket.AI_NUMERICHOST``.
+
+    ``resuse_port`` option sets ``SO_REUSEPORT`` option for every socket
+    in the list. If your platform doesn't support this option ValueError will
+    be raised.
     """
+    if reuse_port and not hasattr(socket, "SO_REUSEPORT"):
+        raise ValueError("the platform doesn't support SO_REUSEPORT")
+
     sockets = []
     if address == "":
         address = None
@@ -165,6 +172,8 @@ def bind_sockets(port, address=None, family=socket.AF_UNSPEC,
         set_close_exec(sock.fileno())
         if os.name != 'nt':
             sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+        if reuse_port:
+            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
         if af == socket.AF_INET6:
             # On linux, ipv6 sockets accept ipv4 too by default,
             # but this makes it impossible to bind to both
diff --git a/tornado/testing.py b/tornado/testing.py
index 304f22db..f5e9f153 100644
--- a/tornado/testing.py
+++ b/tornado/testing.py
@@ -85,12 +85,13 @@ def get_unused_port():
     return port
 
 
-def bind_unused_port():
+def bind_unused_port(reuse_port=False):
     """Binds a server socket to an available port on localhost.
 
     Returns a tuple (socket, port).
     """
-    [sock] = netutil.bind_sockets(None, 'localhost', family=socket.AF_INET)
+    [sock] = netutil.bind_sockets(None, 'localhost', family=socket.AF_INET,
+                                  reuse_port=reuse_port)
     port = sock.getsockname()[1]
     return sock, port
 
 

{"tornado/netutil.py": "#!/usr/bin/env python\n#\n# Copyright 2011 Facebook\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\"); you may\n# not use this file except in compliance with the License. You may obtain\n# a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n# License for the specific language governing permissions and limitations\n# under the License.\n\n\"\"\"Miscellaneous network utility code.\"\"\"\n\nfrom __future__ import absolute_import, division, print_function, with_statement\n\nimport errno\nimport os\nimport sys\nimport socket\nimport stat\n\nfrom tornado.concurrent import dummy_executor, run_on_executor\nfrom tornado.ioloop import IOLoop\nfrom tornado.platform.auto import set_close_exec\nfrom tornado.util import u, Configurable, errno_from_exception\n\ntry:\n    import ssl\nexcept ImportError:\n    # ssl is not available on Google App Engine\n    ssl = None\n\ntry:\n    import certifi\nexcept ImportError:\n    # certifi is optional as long as we have ssl.create_default_context.\n    if ssl is None or hasattr(ssl, 'create_default_context'):\n        certifi = None\n    else:\n        raise\n\ntry:\n    xrange  # py2\nexcept NameError:\n    xrange = range  # py3\n\nif hasattr(ssl, 'match_hostname') and hasattr(ssl, 'CertificateError'):  # python 3.2+\n    ssl_match_hostname = ssl.match_hostname\n    SSLCertificateError = ssl.CertificateError\nelif ssl is None:\n    ssl_match_hostname = SSLCertificateError = None\nelse:\n    import backports.ssl_match_hostname\n    ssl_match_hostname = backports.ssl_match_hostname.match_hostname\n    SSLCertificateError = backports.ssl_match_hostname.CertificateError\n\nif hasattr(ssl, 'SSLContext'):\n    if hasattr(ssl, 'create_default_context'):\n        # Python 2.7.9+, 3.4+\n        # Note that the naming of ssl.Purpose is confusing; the purpose\n        # of a context is to authentiate the opposite side of the connection.\n        _client_ssl_defaults = ssl.create_default_context(\n            ssl.Purpose.SERVER_AUTH)\n        _server_ssl_defaults = ssl.create_default_context(\n            ssl.Purpose.CLIENT_AUTH)\n    else:\n        # Python 3.2-3.3\n        _client_ssl_defaults = ssl.SSLContext(ssl.PROTOCOL_SSLv23)\n        _client_ssl_defaults.verify_mode = ssl.CERT_REQUIRED\n        _client_ssl_defaults.load_verify_locations(certifi.where())\n        _server_ssl_defaults = ssl.SSLContext(ssl.PROTOCOL_SSLv23)\n        if hasattr(ssl, 'OP_NO_COMPRESSION'):\n            # Disable TLS compression to avoid CRIME and related attacks.\n            # This constant wasn't added until python 3.3.\n            _client_ssl_defaults.options |= ssl.OP_NO_COMPRESSION\n            _server_ssl_defaults.options |= ssl.OP_NO_COMPRESSION\n\nelif ssl:\n    # Python 2.6-2.7.8\n    _client_ssl_defaults = dict(cert_reqs=ssl.CERT_REQUIRED,\n                                ca_certs=certifi.where())\n    _server_ssl_defaults = {}\nelse:\n    # Google App Engine\n    _client_ssl_defaults = dict(cert_reqs=None,\n                                ca_certs=None)\n    _server_ssl_defaults = {}\n\n# ThreadedResolver runs getaddrinfo on a thread. If the hostname is unicode,\n# getaddrinfo attempts to import encodings.idna. If this is done at\n# module-import time, the import lock is already held by the main thread,\n# leading to deadlock. Avoid it by caching the idna encoder on the main\n# thread now.\nu('foo').encode('idna')\n\n# These errnos indicate that a non-blocking operation must be retried\n# at a later time.  On most platforms they're the same value, but on\n# some they differ.\n_ERRNO_WOULDBLOCK = (errno.EWOULDBLOCK, errno.EAGAIN)\n\nif hasattr(errno, \"WSAEWOULDBLOCK\"):\n    _ERRNO_WOULDBLOCK += (errno.WSAEWOULDBLOCK,)\n\n# Default backlog used when calling sock.listen()\n_DEFAULT_BACKLOG = 128\n\n\ndef bind_sockets(port, address=None, family=socket.AF_UNSPEC,\n                 backlog=_DEFAULT_BACKLOG, flags=None):\n    \"\"\"Creates listening sockets bound to the given port and address.\n\n    Returns a list of socket objects (multiple sockets are returned if\n    the given address maps to multiple IP addresses, which is most common\n    for mixed IPv4 and IPv6 use).\n\n    Address may be either an IP address or hostname.  If it's a hostname,\n    the server will listen on all IP addresses associated with the\n    name.  Address may be an empty string or None to listen on all\n    available interfaces.  Family may be set to either `socket.AF_INET`\n    or `socket.AF_INET6` to restrict to IPv4 or IPv6 addresses, otherwise\n    both will be used if available.\n\n    The ``backlog`` argument has the same meaning as for\n    `socket.listen() <socket.socket.listen>`.\n\n    ``flags`` is a bitmask of AI_* flags to `~socket.getaddrinfo`, like\n    ``socket.AI_PASSIVE | socket.AI_NUMERICHOST``.\n    \"\"\"\n    sockets = []\n    if address == \"\":\n        address = None\n    if not socket.has_ipv6 and family == socket.AF_UNSPEC:\n        # Python can be compiled with --disable-ipv6, which causes\n        # operations on AF_INET6 sockets to fail, but does not\n        # automatically exclude those results from getaddrinfo\n        # results.\n        # http://bugs.python.org/issue16208\n        family = socket.AF_INET\n    if flags is None:\n        flags = socket.AI_PASSIVE\n    bound_port = None\n    for res in set(socket.getaddrinfo(address, port, family, socket.SOCK_STREAM,\n                                      0, flags)):\n        af, socktype, proto, canonname, sockaddr = res\n        if (sys.platform == 'darwin' and address == 'localhost' and\n                af == socket.AF_INET6 and sockaddr[3] != 0):\n            # Mac OS X includes a link-local address fe80::1%lo0 in the\n            # getaddrinfo results for 'localhost'.  However, the firewall\n            # doesn't understand that this is a local address and will\n            # prompt for access (often repeatedly, due to an apparent\n            # bug in its ability to remember granting access to an\n            # application). Skip these addresses.\n            continue\n        try:\n            sock = socket.socket(af, socktype, proto)\n        except socket.error as e:\n            if errno_from_exception(e) == errno.EAFNOSUPPORT:\n                continue\n            raise\n        set_close_exec(sock.fileno())\n        if os.name != 'nt':\n            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n        if af == socket.AF_INET6:\n            # On linux, ipv6 sockets accept ipv4 too by default,\n            # but this makes it impossible to bind to both\n            # 0.0.0.0 in ipv4 and :: in ipv6.  On other systems,\n            # separate sockets *must* be used to listen for both ipv4\n            # and ipv6.  For consistency, always disable ipv4 on our\n            # ipv6 sockets and use a separate ipv4 socket when needed.\n            #\n            # Python 2.x on windows doesn't have IPPROTO_IPV6.\n            if hasattr(socket, \"IPPROTO_IPV6\"):\n                sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)\n\n        # automatic port allocation with port=None\n        # should bind on the same port on IPv4 and IPv6\n        host, requested_port = sockaddr[:2]\n        if requested_port == 0 and bound_port is not None:\n            sockaddr = tuple([host, bound_port] + list(sockaddr[2:]))\n\n        sock.setblocking(0)\n        sock.bind(sockaddr)\n        bound_port = sock.getsockname()[1]\n        sock.listen(backlog)\n        sockets.append(sock)\n    return sockets\n\nif hasattr(socket, 'AF_UNIX'):\n    def bind_unix_socket(file, mode=0o600, backlog=_DEFAULT_BACKLOG):\n        \"\"\"Creates a listening unix socket.\n\n        If a socket with the given name already exists, it will be deleted.\n        If any other file with that name exists, an exception will be\n        raised.\n\n        Returns a socket object (not a list of socket objects like\n        `bind_sockets`)\n        \"\"\"\n        sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)\n        set_close_exec(sock.fileno())\n        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n        sock.setblocking(0)\n        try:\n            st = os.stat(file)\n        except OSError as err:\n            if errno_from_exception(err) != errno.ENOENT:\n                raise\n        else:\n            if stat.S_ISSOCK(st.st_mode):\n                os.remove(file)\n            else:\n                raise ValueError(\"File %s exists and is not a socket\", file)\n        sock.bind(file)\n        os.chmod(file, mode)\n        sock.listen(backlog)\n        return sock\n\n\ndef add_accept_handler(sock, callback, io_loop=None):\n    \"\"\"Adds an `.IOLoop` event handler to accept new connections on ``sock``.\n\n    When a connection is accepted, ``callback(connection, address)`` will\n    be run (``connection`` is a socket object, and ``address`` is the\n    address of the other end of the connection).  Note that this signature\n    is different from the ``callback(fd, events)`` signature used for\n    `.IOLoop` handlers.\n\n    .. versionchanged:: 4.1\n       The ``io_loop`` argument is deprecated.\n    \"\"\"\n    if io_loop is None:\n        io_loop = IOLoop.current()\n\n    def accept_handler(fd, events):\n        # More connections may come in while we're handling callbacks;\n        # to prevent starvation of other tasks we must limit the number\n        # of connections we accept at a time.  Ideally we would accept\n        # up to the number of connections that were waiting when we\n        # entered this method, but this information is not available\n        # (and rearranging this method to call accept() as many times\n        # as possible before running any callbacks would have adverse\n        # effects on load balancing in multiprocess configurations).\n        # Instead, we use the (default) listen backlog as a rough\n        # heuristic for the number of connections we can reasonably\n        # accept at once.\n        for i in xrange(_DEFAULT_BACKLOG):\n            try:\n                connection, address = sock.accept()\n            except socket.error as e:\n                # _ERRNO_WOULDBLOCK indicate we have accepted every\n                # connection that is available.\n                if errno_from_exception(e) in _ERRNO_WOULDBLOCK:\n                    return\n                # ECONNABORTED indicates that there was a connection\n                # but it was closed while still in the accept queue.\n                # (observed on FreeBSD).\n                if errno_from_exception(e) == errno.ECONNABORTED:\n                    continue\n                raise\n            callback(connection, address)\n    io_loop.add_handler(sock, accept_handler, IOLoop.READ)\n\n\ndef is_valid_ip(ip):\n    \"\"\"Returns true if the given string is a well-formed IP address.\n\n    Supports IPv4 and IPv6.\n    \"\"\"\n    if not ip or '\\x00' in ip:\n        # getaddrinfo resolves empty strings to localhost, and truncates\n        # on zero bytes.\n        return False\n    try:\n        res = socket.getaddrinfo(ip, 0, socket.AF_UNSPEC,\n                                 socket.SOCK_STREAM,\n                                 0, socket.AI_NUMERICHOST)\n        return bool(res)\n    except socket.gaierror as e:\n        if e.args[0] == socket.EAI_NONAME:\n            return False\n        raise\n    return True\n\n\nclass Resolver(Configurable):\n    \"\"\"Configurable asynchronous DNS resolver interface.\n\n    By default, a blocking implementation is used (which simply calls\n    `socket.getaddrinfo`).  An alternative implementation can be\n    chosen with the `Resolver.configure <.Configurable.configure>`\n    class method::\n\n        Resolver.configure('tornado.netutil.ThreadedResolver')\n\n    The implementations of this interface included with Tornado are\n\n    * `tornado.netutil.BlockingResolver`\n    * `tornado.netutil.ThreadedResolver`\n    * `tornado.netutil.OverrideResolver`\n    * `tornado.platform.twisted.TwistedResolver`\n    * `tornado.platform.caresresolver.CaresResolver`\n    \"\"\"\n    @classmethod\n    def configurable_base(cls):\n        return Resolver\n\n    @classmethod\n    def configurable_default(cls):\n        return BlockingResolver\n\n    def resolve(self, host, port, family=socket.AF_UNSPEC, callback=None):\n        \"\"\"Resolves an address.\n\n        The ``host`` argument is a string which may be a hostname or a\n        literal IP address.\n\n        Returns a `.Future` whose result is a list of (family,\n        address) pairs, where address is a tuple suitable to pass to\n        `socket.connect <socket.socket.connect>` (i.e. a ``(host,\n        port)`` pair for IPv4; additional fields may be present for\n        IPv6). If a ``callback`` is passed, it will be run with the\n        result as an argument when it is complete.\n        \"\"\"\n        raise NotImplementedError()\n\n    def close(self):\n        \"\"\"Closes the `Resolver`, freeing any resources used.\n\n        .. versionadded:: 3.1\n\n        \"\"\"\n        pass\n\n\nclass ExecutorResolver(Resolver):\n    \"\"\"Resolver implementation using a `concurrent.futures.Executor`.\n\n    Use this instead of `ThreadedResolver` when you require additional\n    control over the executor being used.\n\n    The executor will be shut down when the resolver is closed unless\n    ``close_resolver=False``; use this if you want to reuse the same\n    executor elsewhere.\n\n    .. versionchanged:: 4.1\n       The ``io_loop`` argument is deprecated.\n    \"\"\"\n    def initialize(self, io_loop=None, executor=None, close_executor=True):\n        self.io_loop = io_loop or IOLoop.current()\n        if executor is not None:\n            self.executor = executor\n            self.close_executor = close_executor\n        else:\n            self.executor = dummy_executor\n            self.close_executor = False\n\n    def close(self):\n        if self.close_executor:\n            self.executor.shutdown()\n        self.executor = None\n\n    @run_on_executor\n    def resolve(self, host, port, family=socket.AF_UNSPEC):\n        # On Solaris, getaddrinfo fails if the given port is not found\n        # in /etc/services and no socket type is given, so we must pass\n        # one here.  The socket type used here doesn't seem to actually\n        # matter (we discard the one we get back in the results),\n        # so the addresses we return should still be usable with SOCK_DGRAM.\n        addrinfo = socket.getaddrinfo(host, port, family, socket.SOCK_STREAM)\n        results = []\n        for family, socktype, proto, canonname, address in addrinfo:\n            results.append((family, address))\n        return results\n\n\nclass BlockingResolver(ExecutorResolver):\n    \"\"\"Default `Resolver` implementation, using `socket.getaddrinfo`.\n\n    The `.IOLoop` will be blocked during the resolution, although the\n    callback will not be run until the next `.IOLoop` iteration.\n    \"\"\"\n    def initialize(self, io_loop=None):\n        super(BlockingResolver, self).initialize(io_loop=io_loop)\n\n\nclass ThreadedResolver(ExecutorResolver):\n    \"\"\"Multithreaded non-blocking `Resolver` implementation.\n\n    Requires the `concurrent.futures` package to be installed\n    (available in the standard library since Python 3.2,\n    installable with ``pip install futures`` in older versions).\n\n    The thread pool size can be configured with::\n\n        Resolver.configure('tornado.netutil.ThreadedResolver',\n                           num_threads=10)\n\n    .. versionchanged:: 3.1\n       All ``ThreadedResolvers`` share a single thread pool, whose\n       size is set by the first one to be created.\n    \"\"\"\n    _threadpool = None\n    _threadpool_pid = None\n\n    def initialize(self, io_loop=None, num_threads=10):\n        threadpool = ThreadedResolver._create_threadpool(num_threads)\n        super(ThreadedResolver, self).initialize(\n            io_loop=io_loop, executor=threadpool, close_executor=False)\n\n    @classmethod\n    def _create_threadpool(cls, num_threads):\n        pid = os.getpid()\n        if cls._threadpool_pid != pid:\n            # Threads cannot survive after a fork, so if our pid isn't what it\n            # was when we created the pool then delete it.\n            cls._threadpool = None\n        if cls._threadpool is None:\n            from concurrent.futures import ThreadPoolExecutor\n            cls._threadpool = ThreadPoolExecutor(num_threads)\n            cls._threadpool_pid = pid\n        return cls._threadpool\n\n\nclass OverrideResolver(Resolver):\n    \"\"\"Wraps a resolver with a mapping of overrides.\n\n    This can be used to make local DNS changes (e.g. for testing)\n    without modifying system-wide settings.\n\n    The mapping can contain either host strings or host-port pairs.\n    \"\"\"\n    def initialize(self, resolver, mapping):\n        self.resolver = resolver\n        self.mapping = mapping\n\n    def close(self):\n        self.resolver.close()\n\n    def resolve(self, host, port, *args, **kwargs):\n        if (host, port) in self.mapping:\n            host, port = self.mapping[(host, port)]\n        elif host in self.mapping:\n            host = self.mapping[host]\n        return self.resolver.resolve(host, port, *args, **kwargs)\n\n\n# These are the keyword arguments to ssl.wrap_socket that must be translated\n# to their SSLContext equivalents (the other arguments are still passed\n# to SSLContext.wrap_socket).\n_SSL_CONTEXT_KEYWORDS = frozenset(['ssl_version', 'certfile', 'keyfile',\n                                   'cert_reqs', 'ca_certs', 'ciphers'])\n\n\ndef ssl_options_to_context(ssl_options):\n    \"\"\"Try to convert an ``ssl_options`` dictionary to an\n    `~ssl.SSLContext` object.\n\n    The ``ssl_options`` dictionary contains keywords to be passed to\n    `ssl.wrap_socket`.  In Python 2.7.9+, `ssl.SSLContext` objects can\n    be used instead.  This function converts the dict form to its\n    `~ssl.SSLContext` equivalent, and may be used when a component which\n    accepts both forms needs to upgrade to the `~ssl.SSLContext` version\n    to use features like SNI or NPN.\n    \"\"\"\n    if isinstance(ssl_options, dict):\n        assert all(k in _SSL_CONTEXT_KEYWORDS for k in ssl_options), ssl_options\n    if (not hasattr(ssl, 'SSLContext') or\n            isinstance(ssl_options, ssl.SSLContext)):\n        return ssl_options\n    context = ssl.SSLContext(\n        ssl_options.get('ssl_version', ssl.PROTOCOL_SSLv23))\n    if 'certfile' in ssl_options:\n        context.load_cert_chain(ssl_options['certfile'], ssl_options.get('keyfile', None))\n    if 'cert_reqs' in ssl_options:\n        context.verify_mode = ssl_options['cert_reqs']\n    if 'ca_certs' in ssl_options:\n        context.load_verify_locations(ssl_options['ca_certs'])\n    if 'ciphers' in ssl_options:\n        context.set_ciphers(ssl_options['ciphers'])\n    if hasattr(ssl, 'OP_NO_COMPRESSION'):\n        # Disable TLS compression to avoid CRIME and related attacks.\n        # This constant wasn't added until python 3.3.\n        context.options |= ssl.OP_NO_COMPRESSION\n    return context\n\n\ndef ssl_wrap_socket(socket, ssl_options, server_hostname=None, **kwargs):\n    \"\"\"Returns an ``ssl.SSLSocket`` wrapping the given socket.\n\n    ``ssl_options`` may be either an `ssl.SSLContext` object or a\n    dictionary (as accepted by `ssl_options_to_context`).  Additional\n    keyword arguments are passed to ``wrap_socket`` (either the\n    `~ssl.SSLContext` method or the `ssl` module function as\n    appropriate).\n    \"\"\"\n    context = ssl_options_to_context(ssl_options)\n    if hasattr(ssl, 'SSLContext') and isinstance(context, ssl.SSLContext):\n        if server_hostname is not None and getattr(ssl, 'HAS_SNI'):\n            # Python doesn't have server-side SNI support so we can't\n            # really unittest this, but it can be manually tested with\n            # python3.2 -m tornado.httpclient https://sni.velox.ch\n            return context.wrap_socket(socket, server_hostname=server_hostname,\n                                       **kwargs)\n        else:\n            return context.wrap_socket(socket, **kwargs)\n    else:\n        return ssl.wrap_socket(socket, **dict(context, **kwargs))\n", "tornado/testing.py": "#!/usr/bin/env python\n\"\"\"Support classes for automated testing.\n\n* `AsyncTestCase` and `AsyncHTTPTestCase`:  Subclasses of unittest.TestCase\n  with additional support for testing asynchronous (`.IOLoop` based) code.\n\n* `ExpectLog` and `LogTrapTestCase`: Make test logs less spammy.\n\n* `main()`: A simple test runner (wrapper around unittest.main()) with support\n  for the tornado.autoreload module to rerun the tests when code changes.\n\"\"\"\n\nfrom __future__ import absolute_import, division, print_function, with_statement\n\ntry:\n    from tornado import gen\n    from tornado.httpclient import AsyncHTTPClient\n    from tornado.httpserver import HTTPServer\n    from tornado.simple_httpclient import SimpleAsyncHTTPClient\n    from tornado.ioloop import IOLoop, TimeoutError\n    from tornado import netutil\n    from tornado.process import Subprocess\nexcept ImportError:\n    # These modules are not importable on app engine.  Parts of this module\n    # won't work, but e.g. LogTrapTestCase and main() will.\n    AsyncHTTPClient = None\n    gen = None\n    HTTPServer = None\n    IOLoop = None\n    netutil = None\n    SimpleAsyncHTTPClient = None\n    Subprocess = None\nfrom tornado.log import gen_log, app_log\nfrom tornado.stack_context import ExceptionStackContext\nfrom tornado.util import raise_exc_info, basestring_type\nimport functools\nimport logging\nimport os\nimport re\nimport signal\nimport socket\nimport sys\n\ntry:\n    from cStringIO import StringIO  # py2\nexcept ImportError:\n    from io import StringIO  # py3\n\ntry:\n    from collections.abc import Generator as GeneratorType  # py35+\nexcept ImportError:\n    from types import GeneratorType\n\n# Tornado's own test suite requires the updated unittest module\n# (either py27+ or unittest2) so tornado.test.util enforces\n# this requirement, but for other users of tornado.testing we want\n# to allow the older version if unitest2 is not available.\nif sys.version_info >= (3,):\n    # On python 3, mixing unittest2 and unittest (including doctest)\n    # doesn't seem to work, so always use unittest.\n    import unittest\nelse:\n    # On python 2, prefer unittest2 when available.\n    try:\n        import unittest2 as unittest\n    except ImportError:\n        import unittest\n\n_next_port = 10000\n\n\ndef get_unused_port():\n    \"\"\"Returns a (hopefully) unused port number.\n\n    This function does not guarantee that the port it returns is available,\n    only that a series of get_unused_port calls in a single process return\n    distinct ports.\n\n    .. deprecated::\n       Use bind_unused_port instead, which is guaranteed to find an unused port.\n    \"\"\"\n    global _next_port\n    port = _next_port\n    _next_port = _next_port + 1\n    return port\n\n\ndef bind_unused_port():\n    \"\"\"Binds a server socket to an available port on localhost.\n\n    Returns a tuple (socket, port).\n    \"\"\"\n    [sock] = netutil.bind_sockets(None, 'localhost', family=socket.AF_INET)\n    port = sock.getsockname()[1]\n    return sock, port\n\n\ndef get_async_test_timeout():\n    \"\"\"Get the global timeout setting for async tests.\n\n    Returns a float, the timeout in seconds.\n\n    .. versionadded:: 3.1\n    \"\"\"\n    try:\n        return float(os.environ.get('ASYNC_TEST_TIMEOUT'))\n    except (ValueError, TypeError):\n        return 5\n\n\nclass _TestMethodWrapper(object):\n    \"\"\"Wraps a test method to raise an error if it returns a value.\n\n    This is mainly used to detect undecorated generators (if a test\n    method yields it must use a decorator to consume the generator),\n    but will also detect other kinds of return values (these are not\n    necessarily errors, but we alert anyway since there is no good\n    reason to return a value from a test.\n    \"\"\"\n    def __init__(self, orig_method):\n        self.orig_method = orig_method\n\n    def __call__(self, *args, **kwargs):\n        result = self.orig_method(*args, **kwargs)\n        if isinstance(result, GeneratorType):\n            raise TypeError(\"Generator test methods should be decorated with \"\n                            \"tornado.testing.gen_test\")\n        elif result is not None:\n            raise ValueError(\"Return value from test method ignored: %r\" %\n                             result)\n\n    def __getattr__(self, name):\n        \"\"\"Proxy all unknown attributes to the original method.\n\n        This is important for some of the decorators in the `unittest`\n        module, such as `unittest.skipIf`.\n        \"\"\"\n        return getattr(self.orig_method, name)\n\n\nclass AsyncTestCase(unittest.TestCase):\n    \"\"\"`~unittest.TestCase` subclass for testing `.IOLoop`-based\n    asynchronous code.\n\n    The unittest framework is synchronous, so the test must be\n    complete by the time the test method returns.  This means that\n    asynchronous code cannot be used in quite the same way as usual.\n    To write test functions that use the same ``yield``-based patterns\n    used with the `tornado.gen` module, decorate your test methods\n    with `tornado.testing.gen_test` instead of\n    `tornado.gen.coroutine`.  This class also provides the `stop()`\n    and `wait()` methods for a more manual style of testing.  The test\n    method itself must call ``self.wait()``, and asynchronous\n    callbacks should call ``self.stop()`` to signal completion.\n\n    By default, a new `.IOLoop` is constructed for each test and is available\n    as ``self.io_loop``.  This `.IOLoop` should be used in the construction of\n    HTTP clients/servers, etc.  If the code being tested requires a\n    global `.IOLoop`, subclasses should override `get_new_ioloop` to return it.\n\n    The `.IOLoop`'s ``start`` and ``stop`` methods should not be\n    called directly.  Instead, use `self.stop <stop>` and `self.wait\n    <wait>`.  Arguments passed to ``self.stop`` are returned from\n    ``self.wait``.  It is possible to have multiple ``wait``/``stop``\n    cycles in the same test.\n\n    Example::\n\n        # This test uses coroutine style.\n        class MyTestCase(AsyncTestCase):\n            @tornado.testing.gen_test\n            def test_http_fetch(self):\n                client = AsyncHTTPClient(self.io_loop)\n                response = yield client.fetch(\"http://www.tornadoweb.org\")\n                # Test contents of response\n                self.assertIn(\"FriendFeed\", response.body)\n\n        # This test uses argument passing between self.stop and self.wait.\n        class MyTestCase2(AsyncTestCase):\n            def test_http_fetch(self):\n                client = AsyncHTTPClient(self.io_loop)\n                client.fetch(\"http://www.tornadoweb.org/\", self.stop)\n                response = self.wait()\n                # Test contents of response\n                self.assertIn(\"FriendFeed\", response.body)\n\n        # This test uses an explicit callback-based style.\n        class MyTestCase3(AsyncTestCase):\n            def test_http_fetch(self):\n                client = AsyncHTTPClient(self.io_loop)\n                client.fetch(\"http://www.tornadoweb.org/\", self.handle_fetch)\n                self.wait()\n\n            def handle_fetch(self, response):\n                # Test contents of response (failures and exceptions here\n                # will cause self.wait() to throw an exception and end the\n                # test).\n                # Exceptions thrown here are magically propagated to\n                # self.wait() in test_http_fetch() via stack_context.\n                self.assertIn(\"FriendFeed\", response.body)\n                self.stop()\n    \"\"\"\n    def __init__(self, methodName='runTest', **kwargs):\n        super(AsyncTestCase, self).__init__(methodName, **kwargs)\n        self.__stopped = False\n        self.__running = False\n        self.__failure = None\n        self.__stop_args = None\n        self.__timeout = None\n\n        # It's easy to forget the @gen_test decorator, but if you do\n        # the test will silently be ignored because nothing will consume\n        # the generator.  Replace the test method with a wrapper that will\n        # make sure it's not an undecorated generator.\n        setattr(self, methodName, _TestMethodWrapper(getattr(self, methodName)))\n\n    def setUp(self):\n        super(AsyncTestCase, self).setUp()\n        self.io_loop = self.get_new_ioloop()\n        self.io_loop.make_current()\n\n    def tearDown(self):\n        # Clean up Subprocess, so it can be used again with a new ioloop.\n        Subprocess.uninitialize()\n        self.io_loop.clear_current()\n        if (not IOLoop.initialized() or\n                self.io_loop is not IOLoop.instance()):\n            # Try to clean up any file descriptors left open in the ioloop.\n            # This avoids leaks, especially when tests are run repeatedly\n            # in the same process with autoreload (because curl does not\n            # set FD_CLOEXEC on its file descriptors)\n            self.io_loop.close(all_fds=True)\n        super(AsyncTestCase, self).tearDown()\n        # In case an exception escaped or the StackContext caught an exception\n        # when there wasn't a wait() to re-raise it, do so here.\n        # This is our last chance to raise an exception in a way that the\n        # unittest machinery understands.\n        self.__rethrow()\n\n    def get_new_ioloop(self):\n        \"\"\"Creates a new `.IOLoop` for this test.  May be overridden in\n        subclasses for tests that require a specific `.IOLoop` (usually\n        the singleton `.IOLoop.instance()`).\n        \"\"\"\n        return IOLoop()\n\n    def _handle_exception(self, typ, value, tb):\n        if self.__failure is None:\n            self.__failure = (typ, value, tb)\n        else:\n            app_log.error(\"multiple unhandled exceptions in test\",\n                          exc_info=(typ, value, tb))\n        self.stop()\n        return True\n\n    def __rethrow(self):\n        if self.__failure is not None:\n            failure = self.__failure\n            self.__failure = None\n            raise_exc_info(failure)\n\n    def run(self, result=None):\n        with ExceptionStackContext(self._handle_exception):\n            super(AsyncTestCase, self).run(result)\n        # As a last resort, if an exception escaped super.run() and wasn't\n        # re-raised in tearDown, raise it here.  This will cause the\n        # unittest run to fail messily, but that's better than silently\n        # ignoring an error.\n        self.__rethrow()\n\n    def stop(self, _arg=None, **kwargs):\n        \"\"\"Stops the `.IOLoop`, causing one pending (or future) call to `wait()`\n        to return.\n\n        Keyword arguments or a single positional argument passed to `stop()` are\n        saved and will be returned by `wait()`.\n        \"\"\"\n        assert _arg is None or not kwargs\n        self.__stop_args = kwargs or _arg\n        if self.__running:\n            self.io_loop.stop()\n            self.__running = False\n        self.__stopped = True\n\n    def wait(self, condition=None, timeout=None):\n        \"\"\"Runs the `.IOLoop` until stop is called or timeout has passed.\n\n        In the event of a timeout, an exception will be thrown. The\n        default timeout is 5 seconds; it may be overridden with a\n        ``timeout`` keyword argument or globally with the\n        ``ASYNC_TEST_TIMEOUT`` environment variable.\n\n        If ``condition`` is not None, the `.IOLoop` will be restarted\n        after `stop()` until ``condition()`` returns true.\n\n        .. versionchanged:: 3.1\n           Added the ``ASYNC_TEST_TIMEOUT`` environment variable.\n        \"\"\"\n        if timeout is None:\n            timeout = get_async_test_timeout()\n\n        if not self.__stopped:\n            if timeout:\n                def timeout_func():\n                    try:\n                        raise self.failureException(\n                            'Async operation timed out after %s seconds' %\n                            timeout)\n                    except Exception:\n                        self.__failure = sys.exc_info()\n                    self.stop()\n                self.__timeout = self.io_loop.add_timeout(self.io_loop.time() + timeout, timeout_func)\n            while True:\n                self.__running = True\n                self.io_loop.start()\n                if (self.__failure is not None or\n                        condition is None or condition()):\n                    break\n            if self.__timeout is not None:\n                self.io_loop.remove_timeout(self.__timeout)\n                self.__timeout = None\n        assert self.__stopped\n        self.__stopped = False\n        self.__rethrow()\n        result = self.__stop_args\n        self.__stop_args = None\n        return result\n\n\nclass AsyncHTTPTestCase(AsyncTestCase):\n    \"\"\"A test case that starts up an HTTP server.\n\n    Subclasses must override `get_app()`, which returns the\n    `tornado.web.Application` (or other `.HTTPServer` callback) to be tested.\n    Tests will typically use the provided ``self.http_client`` to fetch\n    URLs from this server.\n\n    Example, assuming the \"Hello, world\" example from the user guide is in\n    ``hello.py``::\n\n        import hello\n\n        class TestHelloApp(AsyncHTTPTestCase):\n            def get_app(self):\n                return hello.make_app()\n\n            def test_homepage(self):\n                response = self.fetch('/')\n                self.assertEqual(response.code, 200)\n                self.assertEqual(response.body, 'Hello, world')\n\n    That call to ``self.fetch()`` is equivalent to ::\n\n        self.http_client.fetch(self.get_url('/'), self.stop)\n        response = self.wait()\n\n    which illustrates how AsyncTestCase can turn an asynchronous operation,\n    like ``http_client.fetch()``, into a synchronous operation. If you need\n    to do other asynchronous operations in tests, you'll probably need to use\n    ``stop()`` and ``wait()`` yourself.\n    \"\"\"\n    def setUp(self):\n        super(AsyncHTTPTestCase, self).setUp()\n        sock, port = bind_unused_port()\n        self.__port = port\n\n        self.http_client = self.get_http_client()\n        self._app = self.get_app()\n        self.http_server = self.get_http_server()\n        self.http_server.add_sockets([sock])\n\n    def get_http_client(self):\n        return AsyncHTTPClient(io_loop=self.io_loop)\n\n    def get_http_server(self):\n        return HTTPServer(self._app, io_loop=self.io_loop,\n                          **self.get_httpserver_options())\n\n    def get_app(self):\n        \"\"\"Should be overridden by subclasses to return a\n        `tornado.web.Application` or other `.HTTPServer` callback.\n        \"\"\"\n        raise NotImplementedError()\n\n    def fetch(self, path, **kwargs):\n        \"\"\"Convenience method to synchronously fetch a url.\n\n        The given path will be appended to the local server's host and\n        port.  Any additional kwargs will be passed directly to\n        `.AsyncHTTPClient.fetch` (and so could be used to pass\n        ``method=\"POST\"``, ``body=\"...\"``, etc).\n        \"\"\"\n        self.http_client.fetch(self.get_url(path), self.stop, **kwargs)\n        return self.wait()\n\n    def get_httpserver_options(self):\n        \"\"\"May be overridden by subclasses to return additional\n        keyword arguments for the server.\n        \"\"\"\n        return {}\n\n    def get_http_port(self):\n        \"\"\"Returns the port used by the server.\n\n        A new port is chosen for each test.\n        \"\"\"\n        return self.__port\n\n    def get_protocol(self):\n        return 'http'\n\n    def get_url(self, path):\n        \"\"\"Returns an absolute url for the given path on the test server.\"\"\"\n        return '%s://localhost:%s%s' % (self.get_protocol(),\n                                        self.get_http_port(), path)\n\n    def tearDown(self):\n        self.http_server.stop()\n        self.io_loop.run_sync(self.http_server.close_all_connections,\n                              timeout=get_async_test_timeout())\n        if (not IOLoop.initialized() or\n                self.http_client.io_loop is not IOLoop.instance()):\n            self.http_client.close()\n        super(AsyncHTTPTestCase, self).tearDown()\n\n\nclass AsyncHTTPSTestCase(AsyncHTTPTestCase):\n    \"\"\"A test case that starts an HTTPS server.\n\n    Interface is generally the same as `AsyncHTTPTestCase`.\n    \"\"\"\n    def get_http_client(self):\n        return AsyncHTTPClient(io_loop=self.io_loop, force_instance=True,\n                               defaults=dict(validate_cert=False))\n\n    def get_httpserver_options(self):\n        return dict(ssl_options=self.get_ssl_options())\n\n    def get_ssl_options(self):\n        \"\"\"May be overridden by subclasses to select SSL options.\n\n        By default includes a self-signed testing certificate.\n        \"\"\"\n        # Testing keys were generated with:\n        # openssl req -new -keyout tornado/test/test.key -out tornado/test/test.crt -nodes -days 3650 -x509\n        module_dir = os.path.dirname(__file__)\n        return dict(\n            certfile=os.path.join(module_dir, 'test', 'test.crt'),\n            keyfile=os.path.join(module_dir, 'test', 'test.key'))\n\n    def get_protocol(self):\n        return 'https'\n\n\ndef gen_test(func=None, timeout=None):\n    \"\"\"Testing equivalent of ``@gen.coroutine``, to be applied to test methods.\n\n    ``@gen.coroutine`` cannot be used on tests because the `.IOLoop` is not\n    already running.  ``@gen_test`` should be applied to test methods\n    on subclasses of `AsyncTestCase`.\n\n    Example::\n\n        class MyTest(AsyncHTTPTestCase):\n            @gen_test\n            def test_something(self):\n                response = yield gen.Task(self.fetch('/'))\n\n    By default, ``@gen_test`` times out after 5 seconds. The timeout may be\n    overridden globally with the ``ASYNC_TEST_TIMEOUT`` environment variable,\n    or for each test with the ``timeout`` keyword argument::\n\n        class MyTest(AsyncHTTPTestCase):\n            @gen_test(timeout=10)\n            def test_something_slow(self):\n                response = yield gen.Task(self.fetch('/'))\n\n    .. versionadded:: 3.1\n       The ``timeout`` argument and ``ASYNC_TEST_TIMEOUT`` environment\n       variable.\n\n    .. versionchanged:: 4.0\n       The wrapper now passes along ``*args, **kwargs`` so it can be used\n       on functions with arguments.\n    \"\"\"\n    if timeout is None:\n        timeout = get_async_test_timeout()\n\n    def wrap(f):\n        # Stack up several decorators to allow us to access the generator\n        # object itself.  In the innermost wrapper, we capture the generator\n        # and save it in an attribute of self.  Next, we run the wrapped\n        # function through @gen.coroutine.  Finally, the coroutine is\n        # wrapped again to make it synchronous with run_sync.\n        #\n        # This is a good case study arguing for either some sort of\n        # extensibility in the gen decorators or cancellation support.\n        @functools.wraps(f)\n        def pre_coroutine(self, *args, **kwargs):\n            result = f(self, *args, **kwargs)\n            if isinstance(result, GeneratorType):\n                self._test_generator = result\n            else:\n                self._test_generator = None\n            return result\n\n        coro = gen.coroutine(pre_coroutine)\n\n        @functools.wraps(coro)\n        def post_coroutine(self, *args, **kwargs):\n            try:\n                return self.io_loop.run_sync(\n                    functools.partial(coro, self, *args, **kwargs),\n                    timeout=timeout)\n            except TimeoutError as e:\n                # run_sync raises an error with an unhelpful traceback.\n                # If we throw it back into the generator the stack trace\n                # will be replaced by the point where the test is stopped.\n                self._test_generator.throw(e)\n                # In case the test contains an overly broad except clause,\n                # we may get back here.  In this case re-raise the original\n                # exception, which is better than nothing.\n                raise\n        return post_coroutine\n\n    if func is not None:\n        # Used like:\n        #     @gen_test\n        #     def f(self):\n        #         pass\n        return wrap(func)\n    else:\n        # Used like @gen_test(timeout=10)\n        return wrap\n\n\n# Without this attribute, nosetests will try to run gen_test as a test\n# anywhere it is imported.\ngen_test.__test__ = False\n\n\nclass LogTrapTestCase(unittest.TestCase):\n    \"\"\"A test case that captures and discards all logging output\n    if the test passes.\n\n    Some libraries can produce a lot of logging output even when\n    the test succeeds, so this class can be useful to minimize the noise.\n    Simply use it as a base class for your test case.  It is safe to combine\n    with AsyncTestCase via multiple inheritance\n    (``class MyTestCase(AsyncHTTPTestCase, LogTrapTestCase):``)\n\n    This class assumes that only one log handler is configured and\n    that it is a `~logging.StreamHandler`.  This is true for both\n    `logging.basicConfig` and the \"pretty logging\" configured by\n    `tornado.options`.  It is not compatible with other log buffering\n    mechanisms, such as those provided by some test runners.\n\n    .. deprecated:: 4.1\n       Use the unittest module's ``--buffer`` option instead, or `.ExpectLog`.\n    \"\"\"\n    def run(self, result=None):\n        logger = logging.getLogger()\n        if not logger.handlers:\n            logging.basicConfig()\n        handler = logger.handlers[0]\n        if (len(logger.handlers) > 1 or\n                not isinstance(handler, logging.StreamHandler)):\n            # Logging has been configured in a way we don't recognize,\n            # so just leave it alone.\n            super(LogTrapTestCase, self).run(result)\n            return\n        old_stream = handler.stream\n        try:\n            handler.stream = StringIO()\n            gen_log.info(\"RUNNING TEST: \" + str(self))\n            old_error_count = len(result.failures) + len(result.errors)\n            super(LogTrapTestCase, self).run(result)\n            new_error_count = len(result.failures) + len(result.errors)\n            if new_error_count != old_error_count:\n                old_stream.write(handler.stream.getvalue())\n        finally:\n            handler.stream = old_stream\n\n\nclass ExpectLog(logging.Filter):\n    \"\"\"Context manager to capture and suppress expected log output.\n\n    Useful to make tests of error conditions less noisy, while still\n    leaving unexpected log entries visible.  *Not thread safe.*\n\n    The attribute ``logged_stack`` is set to true if any exception\n    stack trace was logged.\n\n    Usage::\n\n        with ExpectLog('tornado.application', \"Uncaught exception\"):\n            error_response = self.fetch(\"/some_page\")\n\n    .. versionchanged:: 4.3\n       Added the ``logged_stack`` attribute.\n    \"\"\"\n    def __init__(self, logger, regex, required=True):\n        \"\"\"Constructs an ExpectLog context manager.\n\n        :param logger: Logger object (or name of logger) to watch.  Pass\n            an empty string to watch the root logger.\n        :param regex: Regular expression to match.  Any log entries on\n            the specified logger that match this regex will be suppressed.\n        :param required: If true, an exeption will be raised if the end of\n            the ``with`` statement is reached without matching any log entries.\n        \"\"\"\n        if isinstance(logger, basestring_type):\n            logger = logging.getLogger(logger)\n        self.logger = logger\n        self.regex = re.compile(regex)\n        self.required = required\n        self.matched = False\n        self.logged_stack = False\n\n    def filter(self, record):\n        if record.exc_info:\n            self.logged_stack = True\n        message = record.getMessage()\n        if self.regex.match(message):\n            self.matched = True\n            return False\n        return True\n\n    def __enter__(self):\n        self.logger.addFilter(self)\n        return self\n\n    def __exit__(self, typ, value, tb):\n        self.logger.removeFilter(self)\n        if not typ and self.required and not self.matched:\n            raise Exception(\"did not get expected log message\")\n\n\ndef main(**kwargs):\n    \"\"\"A simple test runner.\n\n    This test runner is essentially equivalent to `unittest.main` from\n    the standard library, but adds support for tornado-style option\n    parsing and log formatting.\n\n    The easiest way to run a test is via the command line::\n\n        python -m tornado.testing tornado.test.stack_context_test\n\n    See the standard library unittest module for ways in which tests can\n    be specified.\n\n    Projects with many tests may wish to define a test script like\n    ``tornado/test/runtests.py``.  This script should define a method\n    ``all()`` which returns a test suite and then call\n    `tornado.testing.main()`.  Note that even when a test script is\n    used, the ``all()`` test suite may be overridden by naming a\n    single test on the command line::\n\n        # Runs all tests\n        python -m tornado.test.runtests\n        # Runs one test\n        python -m tornado.test.runtests tornado.test.stack_context_test\n\n    Additional keyword arguments passed through to ``unittest.main()``.\n    For example, use ``tornado.testing.main(verbosity=2)``\n    to show many test details as they are run.\n    See http://docs.python.org/library/unittest.html#unittest.main\n    for full argument list.\n    \"\"\"\n    from tornado.options import define, options, parse_command_line\n\n    define('exception_on_interrupt', type=bool, default=True,\n           help=(\"If true (default), ctrl-c raises a KeyboardInterrupt \"\n                 \"exception.  This prints a stack trace but cannot interrupt \"\n                 \"certain operations.  If false, the process is more reliably \"\n                 \"killed, but does not print a stack trace.\"))\n\n    # support the same options as unittest's command-line interface\n    define('verbose', type=bool)\n    define('quiet', type=bool)\n    define('failfast', type=bool)\n    define('catch', type=bool)\n    define('buffer', type=bool)\n\n    argv = [sys.argv[0]] + parse_command_line(sys.argv)\n\n    if not options.exception_on_interrupt:\n        signal.signal(signal.SIGINT, signal.SIG_DFL)\n\n    if options.verbose is not None:\n        kwargs['verbosity'] = 2\n    if options.quiet is not None:\n        kwargs['verbosity'] = 0\n    if options.failfast is not None:\n        kwargs['failfast'] = True\n    if options.catch is not None:\n        kwargs['catchbreak'] = True\n    if options.buffer is not None:\n        kwargs['buffer'] = True\n\n    if __name__ == '__main__' and len(argv) == 1:\n        print(\"No tests specified\", file=sys.stderr)\n        sys.exit(1)\n    try:\n        # In order to be able to run tests by their fully-qualified name\n        # on the command line without importing all tests here,\n        # module must be set to None.  Python 3.2's unittest.main ignores\n        # defaultTest if no module is given (it tries to do its own\n        # test discovery, which is incompatible with auto2to3), so don't\n        # set module if we're not asking for a specific test.\n        if len(argv) > 1:\n            unittest.main(module=None, argv=argv, **kwargs)\n        else:\n            unittest.main(defaultTest=\"all\", argv=argv, **kwargs)\n    except SystemExit as e:\n        if e.code == 0:\n            gen_log.info('PASS')\n        else:\n            gen_log.error('FAIL')\n        raise\n\nif __name__ == '__main__':\n    main()\n"}
A user will ask you to solve a task. You should first draft your thinking process (inner monologue). Then, generate the solution.

Your response format must follow the template below:
<think>
Your thoughts or/and draft, like working through an exercise on scratch paper. Be as casual and as long as you want until you are confident to generate a correct solution.
</think>
<solution>
Final solution presented to the user.
</solution>
system
We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
[ISSUE]
**Title:** `bind_unused_port` Function Does Not Accept `reuse_port` Parameter, Causing TypeError

**Description:**
When attempting to bind an unused port with the `reuse_port` option enabled, a `TypeError` is raised because the `bind_unused_port` function does not accept the `reuse_port` parameter. This prevents developers from setting the `SO_REUSEPORT` socket option, which is necessary for certain networking scenarios.

**Example Buggy Code:**
```python
import unittest
from tornado.netutil import bind_unused_port

class TestPortBinding(unittest.TestCase):
    def test_reuse_port_option(self):
        # Attempting to bind an unused port with reuse_port=True
        socket, port = bind_unused_port(reuse_port=True)
        # This line raises TypeError
```

**Expected Behavior:**
The `bind_unused_port` function should accept the `reuse_port` parameter, allowing the `SO_REUSEPORT` option to be set on the socket. This enables multiple sockets to bind to the same port, provided the underlying platform supports it.

**Actual Behavior:**
Passing `reuse_port=True` to `bind_unused_port` results in the following error:
```
TypeError: bind_unused_port() got an unexpected keyword argument 'reuse_port'
```

[/ISSUE]
--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.

--- BEGIN FILE ---
```
### tornado/testing.py
#!/usr/bin/env python
"""Support classes for automated testing.

* `AsyncTestCase` and `AsyncHTTPTestCase`:  Subclasses of unittest.TestCase
  with additional support for testing asynchronous (`.IOLoop` based) code.

* `ExpectLog` and `LogTrapTestCase`: Make test logs less spammy.

* `main()`: A simple test runner (wrapper around unittest.main()) with support
  for the tornado.autoreload module to rerun the tests when code changes.
"""

from __future__ import absolute_import, division, print_function, with_statement

try:
    from tornado import gen
    from tornado.httpclient import AsyncHTTPClient
    from tornado.httpserver import HTTPServer
    from tornado.simple_httpclient import SimpleAsyncHTTPClient
    from tornado.ioloop import IOLoop, TimeoutError
    from tornado import netutil
    from tornado.process import Subprocess
except ImportError:
    # These modules are not importable on app engine.  Parts of this module
    # won't work, but e.g. LogTrapTestCase and main() will.
    AsyncHTTPClient = None
    gen = None
    HTTPServer = None
    IOLoop = None
    netutil = None
    SimpleAsyncHTTPClient = None
    Subprocess = None
from tornado.log import gen_log, app_log
from tornado.stack_context import ExceptionStackContext
from tornado.util import raise_exc_info, basestring_type
import functools
import logging
import os
import re
import signal
import socket
import sys

try:
    from cStringIO import StringIO  # py2
except ImportError:
    from io import StringIO  # py3

try:
    from collections.abc import Generator as GeneratorType  # py35+
except ImportError:
    from types import GeneratorType

# Tornado's own test suite requires the updated unittest module
# (either py27+ or unittest2) so tornado.test.util enforces
# this requirement, but for other users of tornado.testing we want
# to allow the older version if unitest2 is not available.
if sys.version_info >= (3,):
    # On python 3, mixing unittest2 and unittest (including doctest)
    # doesn't seem to work, so always use unittest.
    import unittest
else:
    # On python 2, prefer unittest2 when available.
    try:
        import unittest2 as unittest
    except ImportError:
        import unittest

_next_port = 10000


def get_unused_port():
    """Returns a (hopefully) unused port number.

    This function does not guarantee that the port it returns is available,
    only that a series of get_unused_port calls in a single process return
    distinct ports.

    .. deprecated::
       Use bind_unused_port instead, which is guaranteed to find an unused port.
    """
    global _next_port
    port = _next_port
    _next_port = _next_port + 1
    return port


def bind_unused_port():
    """Binds a server socket to an available port on localhost.

    Returns a tuple (socket, port).
    """
    [sock] = netutil.bind_sockets(None, 'localhost', family=socket.AF_INET)
    port = sock.getsockname()[1]
    return sock, port


def get_async_test_timeout():
    """Get the global timeout setting for async tests.

    Returns a float, the timeout in seconds.

    .. versionadded:: 3.1
    """
    try:
        return float(os.environ.get('ASYNC_TEST_TIMEOUT'))
    except (ValueError, TypeError):
        return 5


class _TestMethodWrapper(object):
    """Wraps a test method to raise an error if it returns a value.

    This is mainly used to detect undecorated generators (if a test
    method yields it must use a decorator to consume the generator),
    but will also detect other kinds of return values (these are not
    necessarily errors, but we alert anyway since there is no good
    reason to return a value from a test.
    """
    def __init__(self, orig_method):
        self.orig_method = orig_method

    def __call__(self, *args, **kwargs):
        result = self.orig_method(*args, **kwargs)
        if isinstance(result, GeneratorType):
            raise TypeError("Generator test methods should be decorated with "
                            "tornado.testing.gen_test")
        elif result is not None:
            raise ValueError("Return value from test method ignored: %r" %
                             result)

    def __getattr__(self, name):
        """Proxy all unknown attributes to the original method.

        This is important for some of the decorators in the `unittest`
        module, such as `unittest.skipIf`.
        """
        return getattr(self.orig_method, name)


class AsyncTestCase(unittest.TestCase):
    """`~unittest.TestCase` subclass for testing `.IOLoop`-based
    asynchronous code.

    The unittest framework is synchronous, so the test must be
    complete by the time the test method returns.  This means that
    asynchronous code cannot be used in quite the same way as usual.
    To write test functions that use the same ``yield``-based patterns
    used with the `tornado.gen` module, decorate your test methods
    with `tornado.testing.gen_test` instead of
    `tornado.gen.coroutine`.  This class also provides the `stop()`
    and `wait()` methods for a more manual style of testing.  The test
    method itself must call ``self.wait()``, and asynchronous
    callbacks should call ``self.stop()`` to signal completion.

    By default, a new `.IOLoop` is constructed for each test and is available
    as ``self.io_loop``.  This `.IOLoop` should be used in the construction of
    HTTP clients/servers, etc.  If the code being tested requires a
    global `.IOLoop`, subclasses should override `get_new_ioloop` to return it.

    The `.IOLoop`'s ``start`` and ``stop`` methods should not be
    called directly.  Instead, use `self.stop <stop>` and `self.wait
    <wait>`.  Arguments passed to ``self.stop`` are returned from
    ``self.wait``.  It is possible to have multiple ``wait``/``stop``
    cycles in the same test.

    Example::

        # This test uses coroutine style.
        class MyTestCase(AsyncTestCase):
            @tornado.testing.gen_test
            def test_http_fetch(self):
                client = AsyncHTTPClient(self.io_loop)
                response = yield client.fetch("http://www.tornadoweb.org")
                # Test contents of response
                self.assertIn("FriendFeed", response.body)

        # This test uses argument passing between self.stop and self.wait.
        class MyTestCase2(AsyncTestCase):
            def test_http_fetch(self):
                client = AsyncHTTPClient(self.io_loop)
                client.fetch("http://www.tornadoweb.org/", self.stop)
                response = self.wait()
                # Test contents of response
                self.assertIn("FriendFeed", response.body)

        # This test uses an explicit callback-based style.
        class MyTestCase3(AsyncTestCase):
            def test_http_fetch(self):
                client = AsyncHTTPClient(self.io_loop)
                client.fetch("http://www.tornadoweb.org/", self.handle_fetch)
                self.wait()

            def handle_fetch(self, response):
                # Test contents of response (failures and exceptions here
                # will cause self.wait() to throw an exception and end the
                # test).
                # Exceptions thrown here are magically propagated to
                # self.wait() in test_http_fetch() via stack_context.
                self.assertIn("FriendFeed", response.body)
                self.stop()
    """
    def __init__(self, methodName='runTest', **kwargs):
        super(AsyncTestCase, self).__init__(methodName, **kwargs)
        self.__stopped = False
        self.__running = False
        self.__failure = None
        self.__stop_args = None
        self.__timeout = None

        # It's easy to forget the @gen_test decorator, but if you do
        # the test will silently be ignored because nothing will consume
        # the generator.  Replace the test method with a wrapper that will
        # make sure it's not an undecorated generator.
        setattr(self, methodName, _TestMethodWrapper(getattr(self, methodName)))

    def setUp(self):
        super(AsyncTestCase, self).setUp()
        self.io_loop = self.get_new_ioloop()
        self.io_loop.make_current()

    def tearDown(self):
        # Clean up Subprocess, so it can be used again with a new ioloop.
        Subprocess.uninitialize()
        self.io_loop.clear_current()
        if (not IOLoop.initialized() or
                self.io_loop is not IOLoop.instance()):
            # Try to clean up any file descriptors left open in the ioloop.
            # This avoids leaks, especially when tests are run repeatedly
            # in the same process with autoreload (because curl does not
            # set FD_CLOEXEC on its file descriptors)
            self.io_loop.close(all_fds=True)
        super(AsyncTestCase, self).tearDown()
        # In case an exception escaped or the StackContext caught an exception
        # when there wasn't a wait() to re-raise it, do so here.
        # This is our last chance to raise an exception in a way that the
        # unittest machinery understands.
        self.__rethrow()

    def get_new_ioloop(self):
        """Creates a new `.IOLoop` for this test.  May be overridden in
        subclasses for tests that require a specific `.IOLoop` (usually
        the singleton `.IOLoop.instance()`).
        """
        return IOLoop()

    def _handle_exception(self, typ, value, tb):
        if self.__failure is None:
            self.__failure = (typ, value, tb)
        else:
            app_log.error("multiple unhandled exceptions in test",
                          exc_info=(typ, value, tb))
        self.stop()
        return True

    def __rethrow(self):
        if self.__failure is not None:
            failure = self.__failure
            self.__failure = None
            raise_exc_info(failure)

    def run(self, result=None):
        with ExceptionStackContext(self._handle_exception):
            super(AsyncTestCase, self).run(result)
        # As a last resort, if an exception escaped super.run() and wasn't
        # re-raised in tearDown, raise it here.  This will cause the
        # unittest run to fail messily, but that's better than silently
        # ignoring an error.
        self.__rethrow()

    def stop(self, _arg=None, **kwargs):
        """Stops the `.IOLoop`, causing one pending (or future) call to `wait()`
        to return.

        Keyword arguments or a single positional argument passed to `stop()` are
        saved and will be returned by `wait()`.
        """
        assert _arg is None or not kwargs
        self.__stop_args = kwargs or _arg
        if self.__running:
            self.io_loop.stop()
            self.__running = False
        self.__stopped = True

    def wait(self, condition=None, timeout=None):
        """Runs the `.IOLoop` until stop is called or timeout has passed.

        In the event of a timeout, an exception will be thrown. The
        default timeout is 5 seconds; it may be overridden with a
        ``timeout`` keyword argument or globally with the
        ``ASYNC_TEST_TIMEOUT`` environment variable.

        If ``condition`` is not None, the `.IOLoop` will be restarted
        after `stop()` until ``condition()`` returns true.

        .. versionchanged:: 3.1
           Added the ``ASYNC_TEST_TIMEOUT`` environment variable.
        """
        if timeout is None:
            timeout = get_async_test_timeout()

        if not self.__stopped:
            if timeout:
                def timeout_func():
                    try:
                        raise self.failureException(
                            'Async operation timed out after %s seconds' %
                            timeout)
                    except Exception:
                        self.__failure = sys.exc_info()
                    self.stop()
                self.__timeout = self.io_loop.add_timeout(self.io_loop.time() + timeout, timeout_func)
            while True:
                self.__running = True
                self.io_loop.start()
                if (self.__failure is not None or
                        condition is None or condition()):
                    break
            if self.__timeout is not None:
                self.io_loop.remove_timeout(self.__timeout)
                self.__timeout = None
        assert self.__stopped
        self.__stopped = False
        self.__rethrow()
        result = self.__stop_args
        self.__stop_args = None
        return result


class AsyncHTTPTestCase(AsyncTestCase):
    """A test case that starts up an HTTP server.

    Subclasses must override `get_app()`, which returns the
    `tornado.web.Application` (or other `.HTTPServer` callback) to be tested.
    Tests will typically use the provided ``self.http_client`` to fetch
    URLs from this server.

    Example, assuming the "Hello, world" example from the user guide is in
    ``hello.py``::

        import hello

        class TestHelloApp(AsyncHTTPTestCase):
            def get_app(self):
                return hello.make_app()

            def test_homepage(self):
                response = self.fetch('/')
                self.assertEqual(response.code, 200)
                self.assertEqual(response.body, 'Hello, world')

    That call to ``self.fetch()`` is equivalent to ::

        self.http_client.fetch(self.get_url('/'), self.stop)
        response = self.wait()

    which illustrates how AsyncTestCase can turn an asynchronous operation,
    like ``http_client.fetch()``, into a synchronous operation. If you need
    to do other asynchronous operations in tests, you'll probably need to use
    ``stop()`` and ``wait()`` yourself.
    """
    def setUp(self):
        super(AsyncHTTPTestCase, self).setUp()
        sock, port = bind_unused_port()
        self.__port = port

        self.http_client = self.get_http_client()
        self._app = self.get_app()
        self.http_server = self.get_http_server()
        self.http_server.add_sockets([sock])

    def get_http_client(self):
        return AsyncHTTPClient(io_loop=self.io_loop)

    def get_http_server(self):
        return HTTPServer(self._app, io_loop=self.io_loop,
                          **self.get_httpserver_options())

    def get_app(self):
        """Should be overridden by subclasses to return a
        `tornado.web.Application` or other `.HTTPServer` callback.
        """
        raise NotImplementedError()

    def fetch(self, path, **kwargs):
        """Convenience method to synchronously fetch a url.

        The given path will be appended to the local server's host and
        port.  Any additional kwargs will be passed directly to
        `.AsyncHTTPClient.fetch` (and so could be used to pass
        ``method="POST"``, ``body="..."``, etc).
        """
        self.http_client.fetch(self.get_url(path), self.stop, **kwargs)
        return self.wait()

    def get_httpserver_options(self):
        """May be overridden by subclasses to return additional
        keyword arguments for the server.
        """
        return {}

    def get_http_port(self):
        """Returns the port used by the server.

        A new port is chosen for each test.
        """
        return self.__port

    def get_protocol(self):
        return 'http'

    def get_url(self, path):
        """Returns an absolute url for the given path on the test server."""
        return '%s://localhost:%s%s' % (self.get_protocol(),
                                        self.get_http_port(), path)

    def tearDown(self):
        self.http_server.stop()
        self.io_loop.run_sync(self.http_server.close_all_connections,
                              timeout=get_async_test_timeout())
        if (not IOLoop.initialized() or
                self.http_client.io_loop is not IOLoop.instance()):
            self.http_client.close()
        super(AsyncHTTPTestCase, self).tearDown()


class AsyncHTTPSTestCase(AsyncHTTPTestCase):
    """A test case that starts an HTTPS server.

    Interface is generally the same as `AsyncHTTPTestCase`.
    """
    def get_http_client(self):
        return AsyncHTTPClient(io_loop=self.io_loop, force_instance=True,
                               defaults=dict(validate_cert=False))

    def get_httpserver_options(self):
        return dict(ssl_options=self.get_ssl_options())

    def get_ssl_options(self):
        """May be overridden by subclasses to select SSL options.

        By default includes a self-signed testing certificate.
        """
        # Testing keys were generated with:
        # openssl req -new -keyout tornado/test/test.key -out tornado/test/test.crt -nodes -days 3650 -x509
        module_dir = os.path.dirname(__file__)
        return dict(
            certfile=os.path.join(module_dir, 'test', 'test.crt'),
            keyfile=os.path.join(module_dir, 'test', 'test.key'))

    def get_protocol(self):
        return 'https'


def gen_test(func=None, timeout=None):
    """Testing equivalent of ``@gen.coroutine``, to be applied to test methods.

    ``@gen.coroutine`` cannot be used on tests because the `.IOLoop` is not
    already running.  ``@gen_test`` should be applied to test methods
    on subclasses of `AsyncTestCase`.

    Example::

        class MyTest(AsyncHTTPTestCase):
            @gen_test
            def test_something(self):
                response = yield gen.Task(self.fetch('/'))

    By default, ``@gen_test`` times out after 5 seconds. The timeout may be
    overridden globally with the ``ASYNC_TEST_TIMEOUT`` environment variable,
    or for each test with the ``timeout`` keyword argument::

        class MyTest(AsyncHTTPTestCase):
            @gen_test(timeout=10)
            def test_something_slow(self):
                response = yield gen.Task(self.fetch('/'))

    .. versionadded:: 3.1
       The ``timeout`` argument and ``ASYNC_TEST_TIMEOUT`` environment
       variable.

    .. versionchanged:: 4.0
       The wrapper now passes along ``*args, **kwargs`` so it can be used
       on functions with arguments.
    """
    if timeout is None:
        timeout = get_async_test_timeout()

    def wrap(f):
        # Stack up several decorators to allow us to access the generator
        # object itself.  In the innermost wrapper, we capture the generator
        # and save it in an attribute of self.  Next, we run the wrapped
        # function through @gen.coroutine.  Finally, the coroutine is
        # wrapped again to make it synchronous with run_sync.
        #
        # This is a good case study arguing for either some sort of
        # extensibility in the gen decorators or cancellation support.
        @functools.wraps(f)
        def pre_coroutine(self, *args, **kwargs):
            result = f(self, *args, **kwargs)
            if isinstance(result, GeneratorType):
                self._test_generator = result
            else:
                self._test_generator = None
            return result

        coro = gen.coroutine(pre_coroutine)

        @functools.wraps(coro)
        def post_coroutine(self, *args, **kwargs):
            try:
                return self.io_loop.run_sync(
                    functools.partial(coro, self, *args, **kwargs),
                    timeout=timeout)
            except TimeoutError as e:
                # run_sync raises an error with an unhelpful traceback.
                # If we throw it back into the generator the stack trace
                # will be replaced by the point where the test is stopped.
                self._test_generator.throw(e)
                # In case the test contains an overly broad except clause,
                # we may get back here.  In this case re-raise the original
                # exception, which is better than nothing.
                raise
        return post_coroutine

    if func is not None:
        # Used like:
        #     @gen_test
        #     def f(self):
        #         pass
        return wrap(func)
    else:
        # Used like @gen_test(timeout=10)
        return wrap


# Without this attribute, nosetests will try to run gen_test as a test
# anywhere it is imported.
gen_test.__test__ = False


class LogTrapTestCase(unittest.TestCase):
    """A test case that captures and discards all logging output
    if the test passes.

    Some libraries can produce a lot of logging output even when
    the test succeeds, so this class can be useful to minimize the noise.
    Simply use it as a base class for your test case.  It is safe to combine
    with AsyncTestCase via multiple inheritance
    (``class MyTestCase(AsyncHTTPTestCase, LogTrapTestCase):``)

    This class assumes that only one log handler is configured and
    that it is a `~logging.StreamHandler`.  This is true for both
    `logging.basicConfig` and the "pretty logging" configured by
    `tornado.options`.  It is not compatible with other log buffering
    mechanisms, such as those provided by some test runners.

    .. deprecated:: 4.1
       Use the unittest module's ``--buffer`` option instead, or `.ExpectLog`.
    """
    def run(self, result=None):
        logger = logging.getLogger()
        if not logger.handlers:
            logging.basicConfig()
        handler = logger.handlers[0]
        if (len(logger.handlers) > 1 or
                not isinstance(handler, logging.StreamHandler)):
            # Logging has been configured in a way we don't recognize,
            # so just leave it alone.
            super(LogTrapTestCase, self).run(result)
            return
        old_stream = handler.stream
        try:
            handler.stream = StringIO()
            gen_log.info("RUNNING TEST: " + str(self))
            old_error_count = len(result.failures) + len(result.errors)
            super(LogTrapTestCase, self).run(result)
            new_error_count = len(result.failures) + len(result.errors)
            if new_error_count != old_error_count:
                old_stream.write(handler.stream.getvalue())
        finally:
            handler.stream = old_stream


class ExpectLog(logging.Filter):
    """Context manager to capture and suppress expected log output.

    Useful to make tests of error conditions less noisy, while still
    leaving unexpected log entries visible.  *Not thread safe.*

    The attribute ``logged_stack`` is set to true if any exception
    stack trace was logged.

    Usage::

        with ExpectLog('tornado.application', "Uncaught exception"):
            error_response = self.fetch("/some_page")

    .. versionchanged:: 4.3
       Added the ``logged_stack`` attribute.
    """
    def __init__(self, logger, regex, required=True):
        """Constructs an ExpectLog context manager.

        :param logger: Logger object (or name of logger) to watch.  Pass
            an empty string to watch the root logger.
        :param regex: Regular expression to match.  Any log entries on
            the specified logger that match this regex will be suppressed.
        :param required: If true, an exeption will be raised if the end of
            the ``with`` statement is reached without matching any log entries.
        """
        if isinstance(logger, basestring_type):
            logger = logging.getLogger(logger)
        self.logger = logger
        self.regex = re.compile(regex)
        self.required = required
        self.matched = False
        self.logged_stack = False

    def filter(self, record):
        if record.exc_info:
            self.logged_stack = True
        message = record.getMessage()
        if self.regex.match(message):
            self.matched = True
            return False
        return True

    def __enter__(self):
        self.logger.addFilter(self)
        return self

    def __exit__(self, typ, value, tb):
        self.logger.removeFilter(self)
        if not typ and self.required and not self.matched:
            raise Exception("did not get expected log message")


def main(**kwargs):
    """A simple test runner.

    This test runner is essentially equivalent to `unittest.main` from
    the standard library, but adds support for tornado-style option
    parsing and log formatting.

    The easiest way to run a test is via the command line::

        python -m tornado.testing tornado.test.stack_context_test

    See the standard library unittest module for ways in which tests can
    be specified.

    Projects with many tests may wish to define a test script like
    ``tornado/test/runtests.py``.  This script should define a method
    ``all()`` which returns a test suite and then call
    `tornado.testing.main()`.  Note that even when a test script is
    used, the ``all()`` test suite may be overridden by naming a
    single test on the command line::

        # Runs all tests
        python -m tornado.test.runtests
        # Runs one test
        python -m tornado.test.runtests tornado.test.stack_context_test

    Additional keyword arguments passed through to ``unittest.main()``.
    For example, use ``tornado.testing.main(verbosity=2)``
    to show many test details as they are run.
    See http://docs.python.org/library/unittest.html#unittest.main
    for full argument list.
    """
    from tornado.options import define, options, parse_command_line

    define('exception_on_interrupt', type=bool, default=True,
           help=("If true (default), ctrl-c raises a KeyboardInterrupt "
                 "exception.  This prints a stack trace but cannot interrupt "
                 "certain operations.  If false, the process is more reliably "
                 "killed, but does not print a stack trace."))

    # support the same options as unittest's command-line interface
    define('verbose', type=bool)
    define('quiet', type=bool)
    define('failfast', type=bool)
    define('catch', type=bool)
    define('buffer', type=bool)

    argv = [sys.argv[0]] + parse_command_line(sys.argv)

    if not options.exception_on_interrupt:
        signal.signal(signal.SIGINT, signal.SIG_DFL)

    if options.verbose is not None:
        kwargs['verbosity'] = 2
    if options.quiet is not None:
        kwargs['verbosity'] = 0
    if options.failfast is not None:
        kwargs['failfast'] = True
    if options.catch is not None:
        kwargs['catchbreak'] = True
    if options.buffer is not None:
        kwargs['buffer'] = True

    if __name__ == '__main__' and len(argv) == 1:
        print("No tests specified", file=sys.stderr)
        sys.exit(1)
    try:
        # In order to be able to run tests by their fully-qualified name
        # on the command line without importing all tests here,
        # module must be set to None.  Python 3.2's unittest.main ignores
        # defaultTest if no module is given (it tries to do its own
        # test discovery, which is incompatible with auto2to3), so don't
        # set module if we're not asking for a specific test.
        if len(argv) > 1:
            unittest.main(module=None, argv=argv, **kwargs)
        else:
            unittest.main(defaultTest="all", argv=argv, **kwargs)
    except SystemExit as e:
        if e.code == 0:
            gen_log.info('PASS')
        else:
            gen_log.error('FAIL')
        raise

if __name__ == '__main__':
    main()


### tornado/netutil.py
#!/usr/bin/env python
#
# Copyright 2011 Facebook
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

"""Miscellaneous network utility code."""

from __future__ import absolute_import, division, print_function, with_statement

import errno
import os
import sys
import socket
import stat

from tornado.concurrent import dummy_executor, run_on_executor
from tornado.ioloop import IOLoop
from tornado.platform.auto import set_close_exec
from tornado.util import u, Configurable, errno_from_exception

try:
    import ssl
except ImportError:
    # ssl is not available on Google App Engine
    ssl = None

try:
    import certifi
except ImportError:
    # certifi is optional as long as we have ssl.create_default_context.
    if ssl is None or hasattr(ssl, 'create_default_context'):
        certifi = None
    else:
        raise

try:
    xrange  # py2
except NameError:
    xrange = range  # py3

if hasattr(ssl, 'match_hostname') and hasattr(ssl, 'CertificateError'):  # python 3.2+
    ssl_match_hostname = ssl.match_hostname
    SSLCertificateError = ssl.CertificateError
elif ssl is None:
    ssl_match_hostname = SSLCertificateError = None
else:
    import backports.ssl_match_hostname
    ssl_match_hostname = backports.ssl_match_hostname.match_hostname
    SSLCertificateError = backports.ssl_match_hostname.CertificateError

if hasattr(ssl, 'SSLContext'):
    if hasattr(ssl, 'create_default_context'):
        # Python 2.7.9+, 3.4+
        # Note that the naming of ssl.Purpose is confusing; the purpose
        # of a context is to authentiate the opposite side of the connection.
        _client_ssl_defaults = ssl.create_default_context(
            ssl.Purpose.SERVER_AUTH)
        _server_ssl_defaults = ssl.create_default_context(
            ssl.Purpose.CLIENT_AUTH)
    else:
        # Python 3.2-3.3
        _client_ssl_defaults = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
        _client_ssl_defaults.verify_mode = ssl.CERT_REQUIRED
        _client_ssl_defaults.load_verify_locations(certifi.where())
        _server_ssl_defaults = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
        if hasattr(ssl, 'OP_NO_COMPRESSION'):
            # Disable TLS compression to avoid CRIME and related attacks.
            # This constant wasn't added until python 3.3.
            _client_ssl_defaults.options |= ssl.OP_NO_COMPRESSION
            _server_ssl_defaults.options |= ssl.OP_NO_COMPRESSION

elif ssl:
    # Python 2.6-2.7.8
    _client_ssl_defaults = dict(cert_reqs=ssl.CERT_REQUIRED,
                                ca_certs=certifi.where())
    _server_ssl_defaults = {}
else:
    # Google App Engine
    _client_ssl_defaults = dict(cert_reqs=None,
                                ca_certs=None)
    _server_ssl_defaults = {}

# ThreadedResolver runs getaddrinfo on a thread. If the hostname is unicode,
# getaddrinfo attempts to import encodings.idna. If this is done at
# module-import time, the import lock is already held by the main thread,
# leading to deadlock. Avoid it by caching the idna encoder on the main
# thread now.
u('foo').encode('idna')

# These errnos indicate that a non-blocking operation must be retried
# at a later time.  On most platforms they're the same value, but on
# some they differ.
_ERRNO_WOULDBLOCK = (errno.EWOULDBLOCK, errno.EAGAIN)

if hasattr(errno, "WSAEWOULDBLOCK"):
    _ERRNO_WOULDBLOCK += (errno.WSAEWOULDBLOCK,)

# Default backlog used when calling sock.listen()
_DEFAULT_BACKLOG = 128


def bind_sockets(port, address=None, family=socket.AF_UNSPEC,
                 backlog=_DEFAULT_BACKLOG, flags=None):
    """Creates listening sockets bound to the given port and address.

    Returns a list of socket objects (multiple sockets are returned if
    the given address maps to multiple IP addresses, which is most common
    for mixed IPv4 and IPv6 use).

    Address may be either an IP address or hostname.  If it's a hostname,
    the server will listen on all IP addresses associated with the
    name.  Address may be an empty string or None to listen on all
    available interfaces.  Family may be set to either `socket.AF_INET`
    or `socket.AF_INET6` to restrict to IPv4 or IPv6 addresses, otherwise
    both will be used if available.

    The ``backlog`` argument has the same meaning as for
    `socket.listen() <socket.socket.listen>`.

    ``flags`` is a bitmask of AI_* flags to `~socket.getaddrinfo`, like
    ``socket.AI_PASSIVE | socket.AI_NUMERICHOST``.
    """
    sockets = []
    if address == "":
        address = None
    if not socket.has_ipv6 and family == socket.AF_UNSPEC:
        # Python can be compiled with --disable-ipv6, which causes
        # operations on AF_INET6 sockets to fail, but does not
        # automatically exclude those results from getaddrinfo
        # results.
        # http://bugs.python.org/issue16208
        family = socket.AF_INET
    if flags is None:
        flags = socket.AI_PASSIVE
    bound_port = None
    for res in set(socket.getaddrinfo(address, port, family, socket.SOCK_STREAM,
                                      0, flags)):
        af, socktype, proto, canonname, sockaddr = res
        if (sys.platform == 'darwin' and address == 'localhost' and
                af == socket.AF_INET6 and sockaddr[3] != 0):
            # Mac OS X includes a link-local address fe80::1%lo0 in the
            # getaddrinfo results for 'localhost'.  However, the firewall
            # doesn't understand that this is a local address and will
            # prompt for access (often repeatedly, due to an apparent
            # bug in its ability to remember granting access to an
            # application). Skip these addresses.
            continue
        try:
            sock = socket.socket(af, socktype, proto)
        except socket.error as e:
            if errno_from_exception(e) == errno.EAFNOSUPPORT:
                continue
            raise
        set_close_exec(sock.fileno())
        if os.name != 'nt':
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        if af == socket.AF_INET6:
            # On linux, ipv6 sockets accept ipv4 too by default,
            # but this makes it impossible to bind to both
            # 0.0.0.0 in ipv4 and :: in ipv6.  On other systems,
            # separate sockets *must* be used to listen for both ipv4
            # and ipv6.  For consistency, always disable ipv4 on our
            # ipv6 sockets and use a separate ipv4 socket when needed.
            #
            # Python 2.x on windows doesn't have IPPROTO_IPV6.
            if hasattr(socket, "IPPROTO_IPV6"):
                sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)

        # automatic port allocation with port=None
        # should bind on the same port on IPv4 and IPv6
        host, requested_port = sockaddr[:2]
        if requested_port == 0 and bound_port is not None:
            sockaddr = tuple([host, bound_port] + list(sockaddr[2:]))

        sock.setblocking(0)
        sock.bind(sockaddr)
        bound_port = sock.getsockname()[1]
        sock.listen(backlog)
        sockets.append(sock)
    return sockets

if hasattr(socket, 'AF_UNIX'):
    def bind_unix_socket(file, mode=0o600, backlog=_DEFAULT_BACKLOG):
        """Creates a listening unix socket.

        If a socket with the given name already exists, it will be deleted.
        If any other file with that name exists, an exception will be
        raised.

        Returns a socket object (not a list of socket objects like
        `bind_sockets`)
        """
        sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        set_close_exec(sock.fileno())
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.setblocking(0)
        try:
            st = os.stat(file)
        except OSError as err:
            if errno_from_exception(err) != errno.ENOENT:
                raise
        else:
            if stat.S_ISSOCK(st.st_mode):
                os.remove(file)
            else:
                raise ValueError("File %s exists and is not a socket", file)
        sock.bind(file)
        os.chmod(file, mode)
        sock.listen(backlog)
        return sock


def add_accept_handler(sock, callback, io_loop=None):
    """Adds an `.IOLoop` event handler to accept new connections on ``sock``.

    When a connection is accepted, ``callback(connection, address)`` will
    be run (``connection`` is a socket object, and ``address`` is the
    address of the other end of the connection).  Note that this signature
    is different from the ``callback(fd, events)`` signature used for
    `.IOLoop` handlers.

    .. versionchanged:: 4.1
       The ``io_loop`` argument is deprecated.
    """
    if io_loop is None:
        io_loop = IOLoop.current()

    def accept_handler(fd, events):
        # More connections may come in while we're handling callbacks;
        # to prevent starvation of other tasks we must limit the number
        # of connections we accept at a time.  Ideally we would accept
        # up to the number of connections that were waiting when we
        # entered this method, but this information is not available
        # (and rearranging this method to call accept() as many times
        # as possible before running any callbacks would have adverse
        # effects on load balancing in multiprocess configurations).
        # Instead, we use the (default) listen backlog as a rough
        # heuristic for the number of connections we can reasonably
        # accept at once.
        for i in xrange(_DEFAULT_BACKLOG):
            try:
                connection, address = sock.accept()
            except socket.error as e:
                # _ERRNO_WOULDBLOCK indicate we have accepted every
                # connection that is available.
                if errno_from_exception(e) in _ERRNO_WOULDBLOCK:
                    return
                # ECONNABORTED indicates that there was a connection
                # but it was closed while still in the accept queue.
                # (observed on FreeBSD).
                if errno_from_exception(e) == errno.ECONNABORTED:
                    continue
                raise
            callback(connection, address)
    io_loop.add_handler(sock, accept_handler, IOLoop.READ)


def is_valid_ip(ip):
    """Returns true if the given string is a well-formed IP address.

    Supports IPv4 and IPv6.
    """
    if not ip or '\x00' in ip:
        # getaddrinfo resolves empty strings to localhost, and truncates
        # on zero bytes.
        return False
    try:
        res = socket.getaddrinfo(ip, 0, socket.AF_UNSPEC,
                                 socket.SOCK_STREAM,
                                 0, socket.AI_NUMERICHOST)
        return bool(res)
    except socket.gaierror as e:
        if e.args[0] == socket.EAI_NONAME:
            return False
        raise
    return True


class Resolver(Configurable):
    """Configurable asynchronous DNS resolver interface.

    By default, a blocking implementation is used (which simply calls
    `socket.getaddrinfo`).  An alternative implementation can be
    chosen with the `Resolver.configure <.Configurable.configure>`
    class method::

        Resolver.configure('tornado.netutil.ThreadedResolver')

    The implementations of this interface included with Tornado are

    * `tornado.netutil.BlockingResolver`
    * `tornado.netutil.ThreadedResolver`
    * `tornado.netutil.OverrideResolver`
    * `tornado.platform.twisted.TwistedResolver`
    * `tornado.platform.caresresolver.CaresResolver`
    """
    @classmethod
    def configurable_base(cls):
        return Resolver

    @classmethod
    def configurable_default(cls):
        return BlockingResolver

    def resolve(self, host, port, family=socket.AF_UNSPEC, callback=None):
        """Resolves an address.

        The ``host`` argument is a string which may be a hostname or a
        literal IP address.

        Returns a `.Future` whose result is a list of (family,
        address) pairs, where address is a tuple suitable to pass to
        `socket.connect <socket.socket.connect>` (i.e. a ``(host,
        port)`` pair for IPv4; additional fields may be present for
        IPv6). If a ``callback`` is passed, it will be run with the
        result as an argument when it is complete.
        """
        raise NotImplementedError()

    def close(self):
        """Closes the `Resolver`, freeing any resources used.

        .. versionadded:: 3.1

        """
        pass


class ExecutorResolver(Resolver):
    """Resolver implementation using a `concurrent.futures.Executor`.

    Use this instead of `ThreadedResolver` when you require additional
    control over the executor being used.

    The executor will be shut down when the resolver is closed unless
    ``close_resolver=False``; use this if you want to reuse the same
    executor elsewhere.

    .. versionchanged:: 4.1
       The ``io_loop`` argument is deprecated.
    """
    def initialize(self, io_loop=None, executor=None, close_executor=True):
        self.io_loop = io_loop or IOLoop.current()
        if executor is not None:
            self.executor = executor
            self.close_executor = close_executor
        else:
            self.executor = dummy_executor
            self.close_executor = False

    def close(self):
        if self.close_executor:
            self.executor.shutdown()
        self.executor = None

    @run_on_executor
    def resolve(self, host, port, family=socket.AF_UNSPEC):
        # On Solaris, getaddrinfo fails if the given port is not found
        # in /etc/services and no socket type is given, so we must pass
        # one here.  The socket type used here doesn't seem to actually
        # matter (we discard the one we get back in the results),
        # so the addresses we return should still be usable with SOCK_DGRAM.
        addrinfo = socket.getaddrinfo(host, port, family, socket.SOCK_STREAM)
        results = []
        for family, socktype, proto, canonname, address in addrinfo:
            results.append((family, address))
        return results


class BlockingResolver(ExecutorResolver):
    """Default `Resolver` implementation, using `socket.getaddrinfo`.

    The `.IOLoop` will be blocked during the resolution, although the
    callback will not be run until the next `.IOLoop` iteration.
    """
    def initialize(self, io_loop=None):
        super(BlockingResolver, self).initialize(io_loop=io_loop)


class ThreadedResolver(ExecutorResolver):
    """Multithreaded non-blocking `Resolver` implementation.

    Requires the `concurrent.futures` package to be installed
    (available in the standard library since Python 3.2,
    installable with ``pip install futures`` in older versions).

    The thread pool size can be configured with::

        Resolver.configure('tornado.netutil.ThreadedResolver',
                           num_threads=10)

    .. versionchanged:: 3.1
       All ``ThreadedResolvers`` share a single thread pool, whose
       size is set by the first one to be created.
    """
    _threadpool = None
    _threadpool_pid = None

    def initialize(self, io_loop=None, num_threads=10):
        threadpool = ThreadedResolver._create_threadpool(num_threads)
        super(ThreadedResolver, self).initialize(
            io_loop=io_loop, executor=threadpool, close_executor=False)

    @classmethod
    def _create_threadpool(cls, num_threads):
        pid = os.getpid()
        if cls._threadpool_pid != pid:
            # Threads cannot survive after a fork, so if our pid isn't what it
            # was when we created the pool then delete it.
            cls._threadpool = None
        if cls._threadpool is None:
            from concurrent.futures import ThreadPoolExecutor
            cls._threadpool = ThreadPoolExecutor(num_threads)
            cls._threadpool_pid = pid
        return cls._threadpool


class OverrideResolver(Resolver):
    """Wraps a resolver with a mapping of overrides.

    This can be used to make local DNS changes (e.g. for testing)
    without modifying system-wide settings.

    The mapping can contain either host strings or host-port pairs.
    """
    def initialize(self, resolver, mapping):
        self.resolver = resolver
        self.mapping = mapping

    def close(self):
        self.resolver.close()

    def resolve(self, host, port, *args, **kwargs):
        if (host, port) in self.mapping:
            host, port = self.mapping[(host, port)]
        elif host in self.mapping:
            host = self.mapping[host]
        return self.resolver.resolve(host, port, *args, **kwargs)


# These are the keyword arguments to ssl.wrap_socket that must be translated
# to their SSLContext equivalents (the other arguments are still passed
# to SSLContext.wrap_socket).
_SSL_CONTEXT_KEYWORDS = frozenset(['ssl_version', 'certfile', 'keyfile',
                                   'cert_reqs', 'ca_certs', 'ciphers'])


def ssl_options_to_context(ssl_options):
    """Try to convert an ``ssl_options`` dictionary to an
    `~ssl.SSLContext` object.

    The ``ssl_options`` dictionary contains keywords to be passed to
    `ssl.wrap_socket`.  In Python 2.7.9+, `ssl.SSLContext` objects can
    be used instead.  This function converts the dict form to its
    `~ssl.SSLContext` equivalent, and may be used when a component which
    accepts both forms needs to upgrade to the `~ssl.SSLContext` version
    to use features like SNI or NPN.
    """
    if isinstance(ssl_options, dict):
        assert all(k in _SSL_CONTEXT_KEYWORDS for k in ssl_options), ssl_options
    if (not hasattr(ssl, 'SSLContext') or
            isinstance(ssl_options, ssl.SSLContext)):
        return ssl_options
    context = ssl.SSLContext(
        ssl_options.get('ssl_version', ssl.PROTOCOL_SSLv23))
    if 'certfile' in ssl_options:
        context.load_cert_chain(ssl_options['certfile'], ssl_options.get('keyfile', None))
    if 'cert_reqs' in ssl_options:
        context.verify_mode = ssl_options['cert_reqs']
    if 'ca_certs' in ssl_options:
        context.load_verify_locations(ssl_options['ca_certs'])
    if 'ciphers' in ssl_options:
        context.set_ciphers(ssl_options['ciphers'])
    if hasattr(ssl, 'OP_NO_COMPRESSION'):
        # Disable TLS compression to avoid CRIME and related attacks.
        # This constant wasn't added until python 3.3.
        context.options |= ssl.OP_NO_COMPRESSION
    return context


def ssl_wrap_socket(socket, ssl_options, server_hostname=None, **kwargs):
    """Returns an ``ssl.SSLSocket`` wrapping the given socket.

    ``ssl_options`` may be either an `ssl.SSLContext` object or a
    dictionary (as accepted by `ssl_options_to_context`).  Additional
    keyword arguments are passed to ``wrap_socket`` (either the
    `~ssl.SSLContext` method or the `ssl` module function as
    appropriate).
    """
    context = ssl_options_to_context(ssl_options)
    if hasattr(ssl, 'SSLContext') and isinstance(context, ssl.SSLContext):
        if server_hostname is not None and getattr(ssl, 'HAS_SNI'):
            # Python doesn't have server-side SNI support so we can't
            # really unittest this, but it can be manually tested with
            # python3.2 -m tornado.httpclient https://sni.velox.ch
            return context.wrap_socket(socket, server_hostname=server_hostname,
                                       **kwargs)
        else:
            return context.wrap_socket(socket, **kwargs)
    else:
        return ssl.wrap_socket(socket, **dict(context, **kwargs))

```
--- END FILE ---

Please first localize the bug based on the issue statement, and then generate *SEARCH/REPLACE* edits to fix the issue.

Every *SEARCH/REPLACE* edit must use this format:
1. The file path
2. The start of search block: <<<<<<< SEARCH
3. A contiguous chunk of lines to search for in the existing source code
4. The dividing line: =======
5. The lines to replace into the source code
6. The end of the replace block: >>>>>>> REPLACE

Here is an example:

```python
### mathweb/flask/app.py
<<<<<<< SEARCH
from flask import Flask
=======
import math
from flask import Flask
>>>>>>> REPLACE
```
</solution>

Please note that the *SEARCH/REPLACE* edit REQUIRES PROPER INDENTATION. If you would like to add the line '        print(x)', you must fully write that out, with all those spaces before the code!
Wrap each *SEARCH/REPLACE* edit in a code block as shown in the example above. If you have multiple *SEARCH/REPLACE* edits, use a separate code block for each one.
user