with ``await``.
- The ``callback`` argument to `.run_on_executor` is deprecated and will
be removed in 6.0.
-- `.return_future` is deprecated. The wrapped function will no longer
- accept ``callback`` arguments in Tornado 6.0, although the decorator
- itself will not be removed.
+- `.return_future` is deprecated and will be removed in 6.0.
`tornado.gen`
~~~~~~~~~~~~~
import uuid
import warnings
-from tornado.concurrent import (Future, return_future, chain_future,
- future_set_exc_info,
+from tornado.concurrent import (Future, _non_deprecated_return_future,
+ future_set_exc_info, chain_future,
future_set_result_unless_cancelled)
from tornado import gen
from tornado import httpclient
* ``_OPENID_ENDPOINT``: the identity provider's URI.
"""
- @return_future
+ @_non_deprecated_return_future
def authenticate_redirect(self, callback_uri=None,
ax_attrs=["name", "email", "language", "username"],
callback=None):
Subclasses must also override the `_oauth_get_user_future` and
`_oauth_consumer_token` methods.
"""
- @return_future
+ @_non_deprecated_return_future
def authorize_redirect(self, callback_uri=None, extra_params=None,
http_client=None, callback=None):
"""Redirects the user to obtain OAuth authorization for this service.
"""
raise NotImplementedError()
- @return_future
+ @_non_deprecated_return_future
def _oauth_get_user_future(self, access_token, callback):
"""Subclasses must override this to get basic information about the
user.
* ``_OAUTH_AUTHORIZE_URL``: The service's authorization url.
* ``_OAUTH_ACCESS_TOKEN_URL``: The service's access token url.
"""
- @return_future
+ @_non_deprecated_return_future
def authorize_redirect(self, redirect_uri=None, client_id=None,
client_secret=None, extra_params=None,
callback=None, scope=None, response_type="code"):
_OAUTH_NO_CALLBACKS = False
_TWITTER_BASE_URL = "https://api.twitter.com/1.1"
- @return_future
+ @_non_deprecated_return_future
def authenticate_redirect(self, callback_uri=None, callback=None):
"""Just like `~OAuthMixin.authorize_redirect`, but
auto-redirects if authorized.
.. deprecated:: 5.1
- New code should use coroutines directly instead of wrapping
- callback-based code with this decorator.
+ This decorator will be removed in Tornado 6.0. New code should
+ use coroutines directly instead of wrapping callback-based code
+ with this decorator. Interactions with non-Tornado
+ callback-based code should be managed explicitly to avoid
+ relying on the `.ExceptionStackContext` built into this
+ decorator.
"""
+ warnings.warn("@return_future is deprecated, use coroutines instead",
+ DeprecationWarning)
+ return _non_deprecated_return_future(f)
+
+
+def _non_deprecated_return_future(f):
+ # Allow auth.py to use this decorator without triggering
+ # deprecation warnings. This will go away once auth.py has removed
+ # its legacy interfaces in 6.0.
replacer = ArgReplacer(f, 'callback')
@functools.wraps(f)
class ReturnFutureTest(AsyncTestCase):
- @return_future
- def sync_future(self, callback):
- callback(42)
+ with ignore_deprecation():
+ @return_future
+ def sync_future(self, callback):
+ callback(42)
- @return_future
- def async_future(self, callback):
- self.io_loop.add_callback(callback, 42)
+ @return_future
+ def async_future(self, callback):
+ self.io_loop.add_callback(callback, 42)
- @return_future
- def immediate_failure(self, callback):
- 1 / 0
+ @return_future
+ def immediate_failure(self, callback):
+ 1 / 0
- @return_future
- def delayed_failure(self, callback):
- self.io_loop.add_callback(lambda: 1 / 0)
+ @return_future
+ def delayed_failure(self, callback):
+ self.io_loop.add_callback(lambda: 1 / 0)
- @return_future
- def return_value(self, callback):
- # Note that the result of both running the callback and returning
- # a value (or raising an exception) is unspecified; with current
- # implementations the last event prior to callback resolution wins.
- return 42
+ @return_future
+ def return_value(self, callback):
+ # Note that the result of both running the callback and returning
+ # a value (or raising an exception) is unspecified; with current
+ # implementations the last event prior to callback resolution wins.
+ return 42
- @return_future
- def no_result_future(self, callback):
- callback()
+ @return_future
+ def no_result_future(self, callback):
+ callback()
def test_immediate_failure(self):
with self.assertRaises(ZeroDivisionError):
future.result()
def test_kw_only_callback(self):
- @return_future
- def f(**kwargs):
- kwargs['callback'](42)
+ with ignore_deprecation():
+ @return_future
+ def f(**kwargs):
+ kwargs['callback'](42)
future = f()
self.assertEqual(future.result(), 42)
class DecoratorCapClient(BaseCapClient):
- @return_future
- def capitalize(self, request_data, callback):
- logging.debug("capitalize")
- self.request_data = request_data
- self.stream = IOStream(socket.socket())
- self.stream.connect(('127.0.0.1', self.port),
- callback=self.handle_connect)
- self.callback = callback
+ with ignore_deprecation():
+ @return_future
+ def capitalize(self, request_data, callback):
+ logging.debug("capitalize")
+ self.request_data = request_data
+ self.stream = IOStream(socket.socket())
+ self.stream.connect(('127.0.0.1', self.port),
+ callback=self.handle_connect)
+ self.callback = callback
def handle_connect(self):
logging.debug("handle_connect")
self.io_loop.add_callback(functools.partial(
self.delay_callback, iterations - 1, callback, arg))
- @return_future
- def async_future(self, result, callback):
- self.io_loop.add_callback(callback, result)
+ with ignore_deprecation():
+ @return_future
+ def async_future(self, result, callback):
+ self.io_loop.add_callback(callback, result)
@gen.coroutine
def async_exception(self, e):
yield gen.moment
raise gen.Return(arg)
- @return_future
- def async_future(self, result, callback):
- self.io_loop.add_callback(callback, result)
+ with ignore_deprecation():
+ @return_future
+ def async_future(self, result, callback):
+ self.io_loop.add_callback(callback, result)
@gen.coroutine
def async_exception(self, e):