From: Ben Darnell Date: Sun, 18 Mar 2018 23:12:18 +0000 (-0400) Subject: gen: Deprecate callback argument to coroutines X-Git-Tag: v5.1.0b1~36^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=48073bf40593a2be0badb9ecb062eec993e4bfc3;p=thirdparty%2Ftornado.git gen: Deprecate callback argument to coroutines --- diff --git a/tornado/gen.py b/tornado/gen.py index d252c5a07..253f8a510 100644 --- a/tornado/gen.py +++ b/tornado/gen.py @@ -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())) diff --git a/tornado/simple_httpclient.py b/tornado/simple_httpclient.py index 3c192fcd4..1fc3e7075 100644 --- a/tornado/simple_httpclient.py +++ b/tornado/simple_httpclient.py @@ -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() diff --git a/tornado/test/gen_test.py b/tornado/test/gen_test.py index 6c75b5a25..4ac41140e 100644 --- a/tornado/test/gen_test.py +++ b/tornado/test/gen_test.py @@ -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 diff --git a/tornado/test/tcpclient_test.py b/tornado/test/tcpclient_test.py index ca42cf47e..c5a833429 100644 --- a/tornado/test/tcpclient_test.py +++ b/tornado/test/tcpclient_test.py @@ -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")