]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
iostream: Deprecate callback argument to connect()
authorBen Darnell <ben@bendarnell.com>
Sun, 22 Apr 2018 04:09:26 +0000 (00:09 -0400)
committerBen Darnell <ben@bendarnell.com>
Mon, 23 Apr 2018 13:08:26 +0000 (09:08 -0400)
tornado/iostream.py
tornado/test/httpserver_test.py
tornado/test/iostream_test.py

index c28dacfba637b4738b4c074b5aaa0960025b7b4c..a302d0eb7e0aab545c14443113e1d2b744bbe944 100644 (file)
@@ -1291,9 +1291,17 @@ class IOStream(BaseIOStream):
            ``ssl_options=dict(cert_reqs=ssl.CERT_NONE)`` or a
            suitably-configured `ssl.SSLContext` to the
            `SSLIOStream` constructor to disable.
+
+        .. deprecated:: 5.1
+
+           The ``callback`` argument is deprecated and will be removed
+           in Tornado 6.0. Use the returned `.Future` instead.
+
         """
         self._connecting = True
         if callback is not None:
+            warnings.warn("callback argument is deprecated, use returned Future instead",
+                          DeprecationWarning)
             self._connect_callback = stack_context.wrap(callback)
             future = None
         else:
@@ -1577,9 +1585,13 @@ class SSLIOStream(IOStream):
 
     def connect(self, address, callback=None, server_hostname=None):
         self._server_hostname = server_hostname
-        # Pass a dummy callback to super.connect(), which is slightly
-        # more efficient than letting it return a Future we ignore.
-        super(SSLIOStream, self).connect(address, callback=lambda: None)
+        # Ignore the result of connect(). If it fails,
+        # wait_for_handshake will raise an error too. This is
+        # necessary for the old semantics of the connect callback
+        # (which takes no arguments). In 6.0 this can be refactored to
+        # be a regular coroutine.
+        fut = super(SSLIOStream, self).connect(address)
+        fut.add_done_callback(lambda f: f.exception())
         return self.wait_for_handshake(callback)
 
     def _handle_connect(self):
index 19bfabb39faffa28bc5d8a814048ad171936c8bf..4bca757a66883c0084376f8a47a416f5e2ea05b8 100644 (file)
@@ -13,7 +13,7 @@ from tornado.log import gen_log
 from tornado.netutil import ssl_options_to_context
 from tornado.simple_httpclient import SimpleAsyncHTTPClient
 from tornado.testing import AsyncHTTPTestCase, AsyncHTTPSTestCase, AsyncTestCase, ExpectLog, gen_test  # noqa: E501
-from tornado.test.util import unittest, skipOnTravis
+from tornado.test.util import unittest, skipOnTravis, ignore_deprecation
 from tornado.web import Application, RequestHandler, asynchronous, stream_request_body
 
 from contextlib import closing
@@ -209,7 +209,8 @@ class HTTPConnectionTest(AsyncHTTPTestCase):
 
     def raw_fetch(self, headers, body, newline=b"\r\n"):
         with closing(IOStream(socket.socket())) as stream:
-            stream.connect(('127.0.0.1', self.get_http_port()), self.stop)
+            with ignore_deprecation():
+                stream.connect(('127.0.0.1', self.get_http_port()), self.stop)
             self.wait()
             stream.write(
                 newline.join(headers +
@@ -389,8 +390,7 @@ class HTTPServerRawTest(AsyncHTTPTestCase):
     def setUp(self):
         super(HTTPServerRawTest, self).setUp()
         self.stream = IOStream(socket.socket())
-        self.stream.connect(('127.0.0.1', self.get_http_port()), self.stop)
-        self.wait()
+        self.io_loop.run_sync(lambda: self.stream.connect(('127.0.0.1', self.get_http_port())))
 
     def tearDown(self):
         self.stream.close()
@@ -618,8 +618,7 @@ class UnixSocketTest(AsyncTestCase):
         self.server = HTTPServer(app)
         self.server.add_socket(sock)
         self.stream = IOStream(socket.socket(socket.AF_UNIX))
-        self.stream.connect(self.sockfile, self.stop)
-        self.wait()
+        self.io_loop.run_sync(lambda: self.stream.connect(self.sockfile))
 
     def tearDown(self):
         self.stream.close()
index 8f3ef1077eadbeb416fc58526679e67a751b40f3..f12fbc190519268d56b7ec5992ab1472ed3de623 100644 (file)
@@ -100,8 +100,9 @@ class TestIOStreamWebMixin(object):
         def connected_callback():
             connected[0] = True
             cond.notify()
-        stream.connect(("127.0.0.1", self.get_http_port()),
-                       callback=connected_callback)
+        with ignore_deprecation():
+            stream.connect(("127.0.0.1", self.get_http_port()),
+                           callback=connected_callback)
         # unlike the previous tests, try to write before the connection
         # is complete.
         written = [False]
@@ -885,7 +886,8 @@ class TestIOStreamMixin(TestReadWriteMixin):
         stream.set_close_callback(self.stop)
         # log messages vary by platform and ioloop implementation
         with ExpectLog(gen_log, ".*", required=False):
-            stream.connect(("127.0.0.1", port), connect_callback)
+            with ignore_deprecation():
+                stream.connect(("127.0.0.1", port), connect_callback)
             self.wait()
         self.assertFalse(self.connect_called)
         self.assertTrue(isinstance(stream.error, socket.error), stream.error)
@@ -909,7 +911,8 @@ class TestIOStreamMixin(TestReadWriteMixin):
         with mock.patch('socket.socket.connect',
                         side_effect=socket.gaierror(errno.EIO, 'boom')):
             with ExpectLog(gen_log, "Connect error"):
-                stream.connect(('localhost', 80), callback=self.stop)
+                with ignore_deprecation():
+                    stream.connect(('localhost', 80), callback=self.stop)
                 self.wait()
                 self.assertIsInstance(stream.error, socket.gaierror)