]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Pass the correct file object to IOLoop handler functions.
authorBen Darnell <ben@bendarnell.com>
Sat, 18 Jan 2014 21:04:49 +0000 (16:04 -0500)
committerBen Darnell <ben@bendarnell.com>
Sat, 18 Jan 2014 21:35:33 +0000 (16:35 -0500)
tornado/ioloop.py
tornado/platform/twisted.py
tornado/test/ioloop_test.py

index da3d789eee98f6e0824db092f21529b66448d874..c2c520e3022ad8423b8f70641bf82cd53d473108 100644 (file)
@@ -741,7 +741,8 @@ class PollIOLoop(IOLoop):
                 while self._events:
                     fd, events = self._events.popitem()
                     try:
-                        self._handlers[fd][1](fd, events)
+                        fd_obj, handler_func = self._handlers[fd]
+                        handler_func(fd_obj, events)
                     except (OSError, IOError) as e:
                         if e.args[0] == errno.EPIPE:
                             # Happens when the client closes the connection
@@ -750,6 +751,7 @@ class PollIOLoop(IOLoop):
                             self.handle_callback_exception(self._handlers.get(fd))
                     except Exception:
                         self.handle_callback_exception(self._handlers.get(fd))
+                fd_obj = handler_func = None
 
         finally:
             # reset the stopped flag so another start/stop pair can be issued
index 737032d573e7f822f21c6a84e0510a595f5197f6..5ef4e9b0fbc8b3d38c97ce1f2dbab3d7e929226f 100644 (file)
@@ -378,15 +378,15 @@ class _FD(object):
 
     def doRead(self):
         if not self.lost:
-            self.handler(self.fd, tornado.ioloop.IOLoop.READ)
+            self.handler(self.fileobj, tornado.ioloop.IOLoop.READ)
 
     def doWrite(self):
         if not self.lost:
-            self.handler(self.fd, tornado.ioloop.IOLoop.WRITE)
+            self.handler(self.fileobj, tornado.ioloop.IOLoop.WRITE)
 
     def connectionLost(self, reason):
         if not self.lost:
-            self.handler(self.fd, tornado.ioloop.IOLoop.ERROR)
+            self.handler(self.fileobj, tornado.ioloop.IOLoop.ERROR)
             self.lost = True
 
     def logPrefix(self):
index 382adb41c49592a652b53f1f0aaca53f3c7622dc..3e888d3ddc2c56c7b2ff2afc42e6720dcc926ec8 100644 (file)
@@ -199,6 +199,31 @@ class TestIOLoop(AsyncTestCase):
         io_loop.close(all_fds=True)
         self.assertTrue(socket_wrapper.closed)
 
+    def test_handler_callback_file_object(self):
+        """The handler callback receives the same fd object it passed in."""
+        server_sock, port = bind_unused_port()
+        fds = []
+        def handle_connection(fd, events):
+            fds.append(fd)
+            conn, addr = server_sock.accept()
+            conn.close()
+            self.stop()
+        self.io_loop.add_handler(server_sock, handle_connection, IOLoop.READ)
+        with contextlib.closing(socket.socket()) as client_sock:
+            client_sock.connect(('127.0.0.1', port))
+            self.wait()
+        self.io_loop.remove_handler(server_sock)
+        self.io_loop.add_handler(server_sock.fileno(), handle_connection,
+                                 IOLoop.READ)
+        with contextlib.closing(socket.socket()) as client_sock:
+            client_sock.connect(('127.0.0.1', port))
+            self.wait()
+        self.assertIs(fds[0], server_sock)
+        self.assertIs(fds[1], server_sock.fileno())
+        self.io_loop.remove_handler(server_sock.fileno())
+        server_sock.close()
+
+
 
 # Deliberately not a subclass of AsyncTestCase so the IOLoop isn't
 # automatically set as current.