internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: tornadoweb/tornado

Issue:
Ignore ENOPROTOOPT errors from SO_REUSEADDR or SO_ERROR
Some systems, for example Hurd doesn't support SO_REUSEADDR.


Discussion:

Resolving patch:
diff --git a/tornado/iostream.py b/tornado/iostream.py
--- a/tornado/iostream.py
+++ b/tornado/iostream.py
@@ -1410,7 +1410,13 @@ def close_callback():
         return future
 
     def _handle_connect(self):
-        err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
+        try:
+            err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
+        except socket.error as e:
+            # Hurd doesn't allow SO_ERROR for loopback sockets because all
+            # errors for such sockets are reported synchronously.
+            if errno_from_exception(e) == errno.ENOPROTOOPT:
+                err = 0
         if err != 0:
             self.error = socket.error(err, os.strerror(err))
             # IOLoop implementations may vary: some of them return
diff --git a/tornado/netutil.py b/tornado/netutil.py
--- a/tornado/netutil.py
+++ b/tornado/netutil.py
@@ -138,7 +138,12 @@ def bind_sockets(port, address=None, family=socket.AF_UNSPEC,
             raise
         set_close_exec(sock.fileno())
         if os.name != 'nt':
-            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+            try:
+                sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+            except socket.error as e:
+                if errno_from_exception(e) != errno.ENOPROTOOPT:
+                    # Hurd doesn't support SO_REUSEADDR.
+                    raise
         if reuse_port:
             sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
         if af == socket.AF_INET6:
@@ -180,7 +185,12 @@ def bind_unix_socket(file, mode=0o600, backlog=_DEFAULT_BACKLOG):
         """
         sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
         set_close_exec(sock.fileno())
-        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+        try:
+            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+        except socket.error as e:
+            if errno_from_exception(e) != errno.ENOPROTOOPT:
+                # Hurd doesn't support SO_REUSEADDR
+                raise
         sock.setblocking(0)
         try:
             st = os.stat(file)


Test patch:
Repository: tornadoweb/tornado

Issue:
IOLoop doc example code doesn't work correctly
The [example TCP server shown in the IOLoop documentation](http://www.tornadoweb.org/en/stable/ioloop.html#ioloop-objects) doesn't work correctly with Python 3.7 (and probably earlier versions).

This is because the `handle_connection` function is declared `async`, however is never `awaited` or otherwise scheduled, therefore doesn't actually run when a connection is established.

Upon running the snippet and connecting with `nc 127.0.0.1 8888`, the following warning is output:

    ioloop_example.py:23: RuntimeWarning: coroutine 'handle_connection' was never awaited


Discussion:

Resolving patch:
diff --git a/tornado/ioloop.py b/tornado/ioloop.py
--- a/tornado/ioloop.py
+++ b/tornado/ioloop.py
@@ -67,7 +67,6 @@ class IOLoop(Configurable):
         import socket
 
         import tornado.ioloop
-        from tornado import gen
         from tornado.iostream import IOStream
 
         async def handle_connection(connection, address):
@@ -84,7 +83,8 @@ def connection_ready(sock, fd, events):
                         raise
                     return
                 connection.setblocking(0)
-                handle_connection(connection, address)
+                io_loop = tornado.ioloop.IOLoop.current()
+                io_loop.spawn_callback(handle_connection, connection, address)
 
         if __name__ == '__main__':
             sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)


Test patch:
Repository: tornadoweb/tornado

Issue:
Update release notes and set version to 5.0b1



Discussion:

Resolving patch:
diff --git a/docs/conf.py b/docs/conf.py
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -1,14 +1,14 @@
 # Ensure we get the local copy of tornado instead of what's on the standard path
 import os
 import sys
-import time
+
 sys.path.insert(0, os.path.abspath(".."))
 import tornado
 
 master_doc = "index"
 
 project = "Tornado"
-copyright = "2009-%s, The Tornado Authors" % time.strftime("%Y")
+copyright = "The Tornado Authors"
 
 version = release = tornado.version
 
@@ -20,8 +20,8 @@
     "sphinx.ext.viewcode",
 ]
 
-primary_domain = 'py'
-default_role = 'py:obj'
+primary_domain = "py"
+default_role = "py:obj"
 
 autodoc_member_order = "bysource"
 autoclass_content = "both"
@@ -42,14 +42,12 @@
 coverage_ignore_classes = [
     # tornado.gen
     "Runner",
-
     # tornado.web
     "ChunkedTransferEncoding",
     "GZipContentEncoding",
     "OutputTransform",
     "TemplateModule",
     "url",
-
     # tornado.websocket
     "WebSocketProtocol",
     "WebSocketProtocol13",
@@ -60,32 +58,36 @@
     # various modules
     "doctests",
     "main",
-
     # tornado.escape
     # parse_qs_bytes should probably be documented but it's complicated by
     # having different implementations between py2 and py3.
     "parse_qs_bytes",
-
     # tornado.gen
     "Multi",
 ]
 
-html_favicon = 'favicon.ico'
+html_favicon = "favicon.ico"
 
 latex_documents = [
-    ('index', 'tornado.tex', 'Tornado Documentation', 'The Tornado Authors', 'manual', False),
+    (
+        "index",
+        "tornado.tex",
+        "Tornado Documentation",
+        "The Tornado Authors",
+        "manual",
+        False,
+    )
 ]
 
-intersphinx_mapping = {
-    'python': ('https://docs.python.org/3.6/', None),
-}
+intersphinx_mapping = {"python": ("https://docs.python.org/3.6/", None)}
 
-on_rtd = os.environ.get('READTHEDOCS', None) == 'True'
+on_rtd = os.environ.get("READTHEDOCS", None) == "True"
 
 # On RTD we can't import sphinx_rtd_theme, but it will be applied by
 # default anyway.  This block will use the same theme when building locally
 # as on RTD.
 if not on_rtd:
     import sphinx_rtd_theme
-    html_theme = 'sphinx_rtd_theme'
+
+    html_theme = "sphinx_rtd_theme"
     html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]


Test patch:
Repository: tornadoweb/tornado

Issue:
tornado.queues.Queue.get timeout is inconsistent with queue.Queue.get
From the `queue.Queue.get` docstring

> If 'timeout' is a non-negative number, it blocks at most 'timeout' seconds and raises the Empty exception if no item was available within that time.

From the `tornado.queues.Queue.get` docstring

> ``timeout`` may be a number denoting a time (on the same scale as `tornado.ioloop.IOLoop.time`, normally `time.time`), or a `datetime.timedelta` object for a deadline relative to the current time.

I found this confusing.  I wanted to verify that it was intentional.  


Discussion:
`asyncio.Queue`, on the other hand, doesn't accept an optional timeout argument (one would likely wrap the `get` call with `asyncio.wait_for` in that case). I'm not sure if Tornado 6.0 is going to even keep its own `Queue` implementation, but if so, it would probably make more sense to align with `asyncio.Queue` rather than `queue.Queue`.

At any rate, having three mostly equivalent `Queue` classes that all behave differently with respect to timing out is a bit awkward.
The difference is unfortunate but deliberate (well, mostly. I don't think we thought about it much at the time but this is the decision I would have made if I had), since it makes the timeouts consistent with those used elsewhere in Tornado, and satisfies a philosophical preference for absolute deadlines instead of timeouts. 

I'm not planning to remove the tornado locks and queues modules in 6.0 (although they'll just be in maintenance mode and are unlikely to receive any new development). They're almost redundant, but there are a few differences in the interfaces so I can't just make the tornado classes an alias for the asyncio ones and I'm not willing to create the kind of backwards-compatibility headache that would come with removing them. 

This difference should be called out in the docs, but I don't think any other changes would be appropriate. 

Resolving patch:
diff --git a/tornado/queues.py b/tornado/queues.py
--- a/tornado/queues.py
+++ b/tornado/queues.py
@@ -232,6 +232,16 @@ def get(self, timeout: Union[float, datetime.timedelta] = None) -> Awaitable[_T]
         scale as `tornado.ioloop.IOLoop.time`, normally `time.time`), or a
         `datetime.timedelta` object for a deadline relative to the
         current time.
+
+        .. note::
+
+           The ``timeout`` argument of this method differs from that
+           of the standard library's `queue.Queue.get`. That method
+           interprets numeric values as relative timeouts; this one
+           interprets them as absolute deadlines and requires
+           ``timedelta`` objects for relative timeouts (consistent
+           with other timeouts in Tornado).
+
         """
         future = Future()  # type: Future[_T]
         try:


Test patch:
Repository: tornadoweb/tornado

Issue:
Excessive logging at warning when content-encoding header is present
tornado prints following log in warning which floods our logs 

'httputil.py - 755 - Unsupported Content-Encoding: deflate`

ideally it should be info or debug 

https://github.com/tornadoweb/tornado/blob/975e9168560cc03f6210d0d3aab10c870bd47080/tornado/httputil.py#L780


Discussion:
I think this should probably either be removed completely or moved into the `ifs` below so it only fires if you send a content-type we support with a content-encoding that we don't. Making it silent is probably best. 

Resolving patch:
diff --git a/tornado/httputil.py b/tornado/httputil.py
--- a/tornado/httputil.py
+++ b/tornado/httputil.py
@@ -777,10 +777,12 @@ def parse_body_arguments(
     and ``files`` parameters are dictionaries that will be updated
     with the parsed contents.
     """
-    if headers and "Content-Encoding" in headers:
-        gen_log.warning("Unsupported Content-Encoding: %s", headers["Content-Encoding"])
-        return
     if content_type.startswith("application/x-www-form-urlencoded"):
+        if headers and "Content-Encoding" in headers:
+            gen_log.warning(
+                "Unsupported Content-Encoding: %s", headers["Content-Encoding"]
+            )
+            return
         try:
             uri_arguments = parse_qs_bytes(native_str(body), keep_blank_values=True)
         except Exception as e:
@@ -790,6 +792,11 @@ def parse_body_arguments(
             if values:
                 arguments.setdefault(name, []).extend(values)
     elif content_type.startswith("multipart/form-data"):
+        if headers and "Content-Encoding" in headers:
+            gen_log.warning(
+                "Unsupported Content-Encoding: %s", headers["Content-Encoding"]
+            )
+            return
         try:
             fields = content_type.split(";")
             for field in fields:


Test patch:
Repository: tornadoweb/tornado

Issue:
WebsocketHandler.set_nodelay results in assertion failure
In previous versions of Torando, WebsocketHandler.set_nodelay could be called from open to set the nodelay option.  In Tornado 6.0, this results in an assertion failure:

  File ".../tornado/websocket.py", line 561, in set_nodelay
    assert self.stream is not None



Discussion:

Resolving patch:
diff --git a/tornado/websocket.py b/tornado/websocket.py
--- a/tornado/websocket.py
+++ b/tornado/websocket.py
@@ -558,8 +558,8 @@ def set_nodelay(self, value: bool) -> None:
 
         .. versionadded:: 3.1
         """
-        assert self.stream is not None
-        self.stream.set_nodelay(value)
+        assert self.ws_connection is not None
+        self.ws_connection.set_nodelay(value)
 
     def on_connection_close(self) -> None:
         if self.ws_connection:
@@ -714,6 +714,10 @@ def start_pinging(self) -> None:
     async def _receive_frame_loop(self) -> None:
         raise NotImplementedError()
 
+    @abc.abstractmethod
+    def set_nodelay(self, x: bool) -> None:
+        raise NotImplementedError()
+
 
 class _PerMessageDeflateCompressor(object):
     def __init__(
@@ -1345,6 +1349,9 @@ def periodic_ping(self) -> None:
         self.write_ping(b"")
         self.last_ping = now
 
+    def set_nodelay(self, x: bool) -> None:
+        self.stream.set_nodelay(x)
+
 
 class WebSocketClientConnection(simple_httpclient._HTTPConnection):
     """WebSocket client connection.


Test patch:
diff --git a/tornado/test/websocket_test.py b/tornado/test/websocket_test.py
--- a/tornado/test/websocket_test.py
+++ b/tornado/test/websocket_test.py
@@ -198,6 +198,12 @@ async def open(self):
         raise Exception("boom")
 
 
+class NoDelayHandler(TestWebSocketHandler):
+    def open(self):
+        self.set_nodelay(True)
+        self.write_message("hello")
+
+
 class WebSocketBaseTestCase(AsyncHTTPTestCase):
     @gen.coroutine
     def ws_connect(self, path, **kwargs):
@@ -258,6 +264,7 @@ def get_app(self):
                 ),
                 ("/error_in_open", ErrorInOpenHandler),
                 ("/error_in_async_open", ErrorInAsyncOpenHandler),
+                ("/nodelay", NoDelayHandler),
             ],
             template_loader=DictLoader({"message.html": "<b>{{ message }}</b>"}),
         )
@@ -562,6 +569,12 @@ def test_error_in_async_open(self):
             res = yield ws.read_message()
         self.assertIsNone(res)
 
+    @gen_test
+    def test_nodelay(self):
+        ws = yield self.ws_connect("/nodelay")
+        res = yield ws.read_message()
+        self.assertEqual(res, "hello")
+
 
 class NativeCoroutineOnMessageHandler(TestWebSocketHandler):
     def initialize(self, **kwargs):
Repository: tornadoweb/tornado

Issue:
LogFormatter DEFAULT_COLORS needs a logging.CRITICAL entry
https://github.com/tornadoweb/tornado/blob/c447875a1058f4768c2995ddd9ebb0eaddd3f32e/tornado/log.py#L108-L113

Currently `ERROR` will log red, but `CRITICAL` will log in white.  Curses `COLOR_MAGENTA` (5) is probably the best option.


Discussion:
Workaround:

```
colors = LogFormatter.DEFAULT_COLORS
colors[logging.CRITICAL] = 5  # Magenta

fmt = LogFormatter(fmt=...., colors=colors)
```

Resolving patch:
diff --git a/tornado/log.py b/tornado/log.py
--- a/tornado/log.py
+++ b/tornado/log.py
@@ -110,6 +110,7 @@ class LogFormatter(logging.Formatter):
         logging.INFO: 2,  # Green
         logging.WARNING: 3,  # Yellow
         logging.ERROR: 1,  # Red
+        logging.CRITICAL: 5,  # Magenta
     }
 
     def __init__(


Test patch:
Repository: tornadoweb/tornado

Issue:
tornado.platform.twisted disappeared but did not explain its replacement
Many years' worth of documentation explains that folks can do

```
from tornado.platform.twisted import install
reactor = install()
```

I can see that in https://github.com/tornadoweb/tornado/commit/004de9c301cc4c2dae4d8f2507af1851d0c9763a#diff-77b5a8a33248ef0bcafbc1bb71e9f013 Twisted integration was removed, since we can all depend on the stdlib loop APIs.  This is great, but it also breaks a bunch of Jupyter notebooks, tutorials, etc.

Could you be convinced to replace all those sprawling APIs with something like this:

```python3
def install():
    from twisted.internet.asyncioreactor import install
    install()
    from twisted.internet import reactor
    reactor.startRunning()
    return reactor
```

possibly with a `warnings.warn` explaining that users could just call these APIs directly, if that's the desired end-state?


Discussion:
To be honest I didn't realize anyone but me ever used this module, until your message to async-sig a few days ago :)  I can't remember the last time anyone asked any questions about it, so I assumed that indicated a lack of interest.

I also thought that `install()` had stopped working on python 3 in Tornado 5.0 (without anyone complaining) so it was safe to remove the python 2-only functionality in Tornado 6 (this is why I failed to add deprecation warnings in addition to the doc comments). But now that I think about it some more, this was wrong. TwistedIOLoop was no longer usable in Tornado 5+Python 3, but `TornadoReactor` and `install()` would (I think) still work. 

If there's demand for it, I think I'm fine with keeping `tornado.platform.twisted.install` around indefinitely using `asyncioreactor` instead of `TornadoReactor`. 
Thanks @bdarnell !  That would definitely work for me.

Resolving patch:
diff --git a/tornado/platform/twisted.py b/tornado/platform/twisted.py
--- a/tornado/platform/twisted.py
+++ b/tornado/platform/twisted.py
@@ -104,6 +104,30 @@ def resolve(
         return result
 
 
+def install() -> None:
+    """Install ``AsyncioSelectorReactor`` as the default Twisted reactor.
+
+    .. deprecated:: 5.1
+
+       This function is provided for backwards compatibility; code
+       that does not require compatibility with older versions of
+       Tornado should use
+       ``twisted.internet.asyncioreactor.install()`` directly.
+
+    .. versionchanged:: 6.0.3
+
+       In Tornado 5.x and before, this function installed a reactor
+       based on the Tornado ``IOLoop``. When that reactor
+       implementation was removed in Tornado 6.0.0, this function was
+       removed as well. It was restored in Tornado 6.0.3 using the
+       ``asyncio`` reactor instead.
+
+    """
+    from twisted.internet.asyncioreactor import install
+
+    install()
+
+
 if hasattr(gen.convert_yielded, "register"):
 
     @gen.convert_yielded.register(Deferred)  # type: ignore


Test patch:
Repository: tornadoweb/tornado

Issue:
reverse_url() unescapes regex inconsistently
If you have a rule with an escaped regex character coming after a group in parentheses, like:
```python
URLSpec('/project/([0-9]+)/export/codebook\\.csv', ExportCodebookCsv)
```
it will correctly match `/project/1/export/codebook.csv`, but will reverse to `/project/1/export/codebook\.csv`.

Of course, if you remove the backslash:
```python
URLSpec('/project/([0-9]+)/export/codebook.csv', ExportCodebookCsv)
```
it will reverse to the wanted `/project/1/export/codebook.csv` but then it will also match `/project/1/export/codebookXcsv` (the dot matches any character).

This is because `re_unescape()` is only called for the first fragment, not the following ones; see no unescaping line 630:
https://github.com/tornadoweb/tornado/blob/ff985fe509513325462441e017f1ec31dda737c1/tornado/routing.py#L626-L638

See also #1619


Discussion:

Resolving patch:
diff --git a/tornado/routing.py b/tornado/routing.py
--- a/tornado/routing.py
+++ b/tornado/routing.py
@@ -627,7 +627,13 @@ def _find_groups(self) -> Tuple[Optional[str], Optional[int]]:
             if ")" in fragment:
                 paren_loc = fragment.index(")")
                 if paren_loc >= 0:
-                    pieces.append("%s" + fragment[paren_loc + 1 :])
+                    try:
+                        unescaped_fragment = re_unescape(fragment[paren_loc + 1 :])
+                    except ValueError:
+                        # If we can't unescape part of it, we can't
+                        # reverse this url.
+                        return (None, None)
+                    pieces.append("%s" + unescaped_fragment)
             else:
                 try:
                     unescaped_fragment = re_unescape(fragment)


Test patch:
diff --git a/tornado/test/web_test.py b/tornado/test/web_test.py
--- a/tornado/test/web_test.py
+++ b/tornado/test/web_test.py
@@ -3107,6 +3107,10 @@ def test_reverse_arguments(self):
         self.assertEqual(
             "/api/v1/foo/bar", url(r"^/api/v1/foo/(\w+)$", None).reverse("bar")
         )
+        self.assertEqual(
+            "/api.v1/foo/5/icon.png",
+            url(r"/api\.v1/foo/([0-9]+)/icon\.png", None).reverse(5),
+        )
 
 
 class RedirectHandlerTest(WebTestCase):
Repository: tornadoweb/tornado

Issue:
Documentation mismatch for parent process exit in tornado.process.fork_processes
Docstring mismatches with parent process exit on success in `tornado.process.fork_processes`:

> In the parent process, ``fork_processes`` returns None if all child processes have exited normally...

The behavior was changed in 4c23ba7e7.



Discussion:

Resolving patch:
diff --git a/tornado/process.py b/tornado/process.py
--- a/tornado/process.py
+++ b/tornado/process.py
@@ -110,9 +110,8 @@ def fork_processes(
     number between 0 and ``num_processes``.  Processes that exit
     abnormally (due to a signal or non-zero exit status) are restarted
     with the same id (up to ``max_restarts`` times).  In the parent
-    process, ``fork_processes`` returns None if all child processes
-    have exited normally, but will otherwise only exit by throwing an
-    exception.
+    process, ``fork_processes`` calls ``sys.exit(0)`` after all child
+    processes have exited normally.
 
     max_restarts defaults to 100.
 


Test patch:
Repository: tornadoweb/tornado

Issue:
Should the Blog example use bcrypt's checkpw instead of ==
bcrypt 3.1.0 added `checkpw` which (I believe) is the recommended way to compare passwords to the hashed password now. 
- `checkpw` code [here](https://github.com/pyca/bcrypt/blob/master/src/bcrypt/__init__.py#L97)
- tornado's blog example using `==` [here](https://github.com/tornadoweb/tornado/blob/stable/demos/blog/blog.py#L211)

The current example looks like this:

``` python
    @gen.coroutine
    def post(self):
        # [...]
        hashed_password = yield executor.submit(
            bcrypt.hashpw, 
            tornado.escape.utf8(self.get_argument("password")),
            tornado.escape.utf8(author.hashed_password))
        if hashed_password == author.hashed_password:
            self.set_secure_cookie("blogdemo_user", str(author.id))
            self.redirect(self.get_argument("next", "/"))
        else:
            self.render("login.html", error="incorrect password")
```

But maybe it should look like this?

``` python
    @gen.coroutine
    def post(self):
        # [...]
        password_equal = yield executor.submit(
            bcrypt.checkpw, 
            tornado.escape.utf8(self.get_argument("password")),
            tornado.escape.utf8(author.hashed_password))
        if password_equal:
            self.set_secure_cookie("blogdemo_user", str(author.id))
            self.redirect(self.get_argument("next", "/"))
        else:
            self.render("login.html", error="incorrect password")
```



Discussion:

Resolving patch:
diff --git a/demos/blog/blog.py b/demos/blog/blog.py
--- a/demos/blog/blog.py
+++ b/demos/blog/blog.py
@@ -266,14 +266,13 @@ async def post(self):
         except NoResultError:
             self.render("login.html", error="email not found")
             return
-        hashed_password = await tornado.ioloop.IOLoop.current().run_in_executor(
+        password_equal = await tornado.ioloop.IOLoop.current().run_in_executor(
             None,
-            bcrypt.hashpw,
+            bcrypt.checkpw,
             tornado.escape.utf8(self.get_argument("password")),
             tornado.escape.utf8(author.hashed_password),
         )
-        hashed_password = tornado.escape.to_unicode(hashed_password)
-        if hashed_password == author.hashed_password:
+        if password_equal:
             self.set_secure_cookie("blogdemo_user", str(author.id))
             self.redirect(self.get_argument("next", "/"))
         else:


Test patch: