]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
gen: Deprecate callback argument to coroutines
authorBen Darnell <ben@bendarnell.com>
Sun, 18 Mar 2018 23:12:18 +0000 (19:12 -0400)
committerBen Darnell <ben@bendarnell.com>
Sun, 18 Mar 2018 23:12:18 +0000 (19:12 -0400)
tornado/gen.py
tornado/simple_httpclient.py
tornado/test/gen_test.py
tornado/test/tcpclient_test.py

index d252c5a072dd5ee02d4cdbcebbf07cbd51b90ec1..253f8a5107e4bb2a11dc6da4c3be79f0677da0d5 100644 (file)
@@ -240,7 +240,7 @@ def engine(func):
     return wrapper
 
 
-def coroutine(func, replace_callback=True):
+def coroutine(func):
     """Decorator for asynchronous generators.
 
     Any generator that yields objects from this module must be wrapped
@@ -274,6 +274,10 @@ def coroutine(func, replace_callback=True):
        `.IOLoop.run_sync` for top-level calls, or passing the `.Future`
        to `.IOLoop.add_future`.
 
+    .. deprecated:: 5.1
+
+       The ``callback`` argument is deprecated and will be removed in 6.0.
+       Use the returned awaitable object instead.
     """
     return _make_coroutine_wrapper(func, replace_callback=True)
 
@@ -296,6 +300,8 @@ def _make_coroutine_wrapper(func, replace_callback):
         future = _create_future()
 
         if replace_callback and 'callback' in kwargs:
+            warnings.warn("callback arguments are deprecated, use the returned Future instead",
+                          DeprecationWarning, stacklevel=2)
             callback = kwargs.pop('callback')
             IOLoop.current().add_future(
                 future, lambda future: callback(future.result()))
index 3c192fcd4ad7681dc0acd43bdf4c5b70fa9a868d..1fc3e7075b2083a4c5569e5962895b0a540656b0 100644 (file)
@@ -227,10 +227,10 @@ class _HTTPConnection(httputil.HTTPMessageDelegate):
                 self._timeout = self.io_loop.add_timeout(
                     self.start_time + timeout,
                     stack_context.wrap(functools.partial(self._on_timeout, "while connecting")))
-            self.tcp_client.connect(host, port, af=af,
-                                    ssl_options=ssl_options,
-                                    max_buffer_size=self.max_buffer_size,
-                                    callback=self._on_connect)
+            fut = self.tcp_client.connect(host, port, af=af,
+                                          ssl_options=ssl_options,
+                                          max_buffer_size=self.max_buffer_size)
+            fut.add_done_callback(stack_context.wrap(self._on_connect))
 
     def _get_ssl_options(self, scheme):
         if scheme == "https":
@@ -275,7 +275,8 @@ class _HTTPConnection(httputil.HTTPMessageDelegate):
             self.io_loop.remove_timeout(self._timeout)
             self._timeout = None
 
-    def _on_connect(self, stream):
+    def _on_connect(self, stream_fut):
+        stream = stream_fut.result()
         if self.final_callback is None:
             # final_callback is cleared if we've hit our timeout.
             stream.close()
index 6c75b5a255434e698c740dffbdd2af720028dca3..4ac41140e3f145c5720b89305d3b191271b12dd6 100644 (file)
@@ -821,7 +821,8 @@ class GenCoroutineTest(AsyncTestCase):
             yield gen.Task(self.io_loop.add_callback)
             raise gen.Return(43)
 
-        f2(callback=(yield gen.Callback('cb')))
+        with ignore_deprecation():
+            f2(callback=(yield gen.Callback('cb')))
         results = yield [namespace['f1'](), gen.Wait('cb')]
         self.assertEqual(results, [42, 43])
         self.finished = True
@@ -885,10 +886,11 @@ class GenCoroutineTest(AsyncTestCase):
 
     @gen_test
     def test_pass_callback(self):
-        @gen.coroutine
-        def f():
-            raise gen.Return(42)
-        result = yield gen.Task(f)
+        with ignore_deprecation():
+            @gen.coroutine
+            def f():
+                raise gen.Return(42)
+            result = yield gen.Task(f)
         self.assertEqual(result, 42)
         self.finished = True
 
index ca42cf47ea71c7c797633b97eb92a94d92d944c4..c5a83342937f7ef1ab42973ffabdee4658da73fe 100644 (file)
@@ -77,8 +77,7 @@ class TCPClientTest(AsyncTestCase):
     def skipIfLocalhostV4(self):
         # The port used here doesn't matter, but some systems require it
         # to be non-zero if we do not also pass AI_PASSIVE.
-        Resolver().resolve('localhost', 80, callback=self.stop)
-        addrinfo = self.wait()
+        addrinfo = yield Resolver().resolve('localhost', 80)
         families = set(addr[0] for addr in addrinfo)
         if socket.AF_INET6 not in families:
             self.skipTest("localhost does not resolve to ipv6")