From: Ben Darnell Date: Fri, 27 Apr 2018 14:36:51 +0000 (-0400) Subject: web: Deprecate asynchronous decorator X-Git-Tag: v5.1.0b1~21^2~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c60ab672131522cdcf6ee4d4d5da3f24313a38bb;p=thirdparty%2Ftornado.git web: Deprecate asynchronous decorator --- diff --git a/tornado/test/auth_test.py b/tornado/test/auth_test.py index 0d26a35af..e0fd437b1 100644 --- a/tornado/test/auth_test.py +++ b/tornado/test/auth_test.py @@ -26,17 +26,18 @@ class OpenIdClientLoginHandlerLegacy(RequestHandler, OpenIdMixin): def initialize(self, test): self._OPENID_ENDPOINT = test.get_url('/openid/server/authenticate') - @asynchronous - def get(self): - if self.get_argument('openid.mode', None): - with warnings.catch_warnings(): - warnings.simplefilter('ignore', DeprecationWarning) - self.get_authenticated_user( - self.on_user, http_client=self.settings['http_client']) - return - res = self.authenticate_redirect() - assert isinstance(res, Future) - assert res.done() + with ignore_deprecation(): + @asynchronous + def get(self): + if self.get_argument('openid.mode', None): + with warnings.catch_warnings(): + warnings.simplefilter('ignore', DeprecationWarning) + self.get_authenticated_user( + self.on_user, http_client=self.settings['http_client']) + return + res = self.authenticate_redirect() + assert isinstance(res, Future) + assert res.done() def on_user(self, user): if user is None: @@ -78,16 +79,17 @@ class OAuth1ClientLoginHandlerLegacy(RequestHandler, OAuthMixin): def _oauth_consumer_token(self): return dict(key='asdf', secret='qwer') - @asynchronous - def get(self): - if self.get_argument('oauth_token', None): - with warnings.catch_warnings(): - warnings.simplefilter('ignore', DeprecationWarning) - self.get_authenticated_user( - self.on_user, http_client=self.settings['http_client']) - return - res = self.authorize_redirect(http_client=self.settings['http_client']) - assert isinstance(res, Future) + with ignore_deprecation(): + @asynchronous + def get(self): + if self.get_argument('oauth_token', None): + with warnings.catch_warnings(): + warnings.simplefilter('ignore', DeprecationWarning) + self.get_authenticated_user( + self.on_user, http_client=self.settings['http_client']) + return + res = self.authorize_redirect(http_client=self.settings['http_client']) + assert isinstance(res, Future) def on_user(self, user): if user is None: @@ -226,12 +228,13 @@ class TwitterClientHandler(RequestHandler, TwitterMixin): class TwitterClientLoginHandlerLegacy(TwitterClientHandler): - @asynchronous - def get(self): - if self.get_argument("oauth_token", None): - self.get_authenticated_user(self.on_user) - return - self.authorize_redirect() + with ignore_deprecation(): + @asynchronous + def get(self): + if self.get_argument("oauth_token", None): + self.get_authenticated_user(self.on_user) + return + self.authorize_redirect() def on_user(self, user): if user is None: diff --git a/tornado/test/gen_test.py b/tornado/test/gen_test.py index 8b2f62b97..721d927dd 100644 --- a/tornado/test/gen_test.py +++ b/tornado/test/gen_test.py @@ -1368,7 +1368,6 @@ class GenCoroutineSequenceHandler(RequestHandler): class GenCoroutineUnfinishedSequenceHandler(RequestHandler): - @asynchronous @gen.coroutine def get(self): yield gen.moment diff --git a/tornado/test/httpserver_test.py b/tornado/test/httpserver_test.py index 4bca757a6..5abb7fe3c 100644 --- a/tornado/test/httpserver_test.py +++ b/tornado/test/httpserver_test.py @@ -14,7 +14,7 @@ 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, ignore_deprecation -from tornado.web import Application, RequestHandler, asynchronous, stream_request_body +from tornado.web import Application, RequestHandler, stream_request_body from contextlib import closing import datetime @@ -668,9 +668,11 @@ class KeepAliveTest(AsyncHTTPTestCase): self.write(''.join(chr(i % 256) * 1024 for i in range(512))) class FinishOnCloseHandler(RequestHandler): - @asynchronous + @gen.coroutine def get(self): self.flush() + never_finish = Event() + yield never_finish.wait() def on_connection_close(self): # This is not very realistic, but finishing the request diff --git a/tornado/test/simple_httpclient_test.py b/tornado/test/simple_httpclient_test.py index 09038710d..378c13d34 100644 --- a/tornado/test/simple_httpclient_test.py +++ b/tornado/test/simple_httpclient_test.py @@ -17,6 +17,7 @@ from tornado.httpclient import AsyncHTTPClient from tornado.httputil import HTTPHeaders, ResponseStartLine from tornado.ioloop import IOLoop from tornado.iostream import UnsatisfiableReadError +from tornado.locks import Event from tornado.log import gen_log from tornado.concurrent import Future from tornado.netutil import Resolver, bind_sockets @@ -27,7 +28,7 @@ from tornado.testing import (AsyncHTTPTestCase, AsyncHTTPSTestCase, AsyncTestCas ExpectLog, gen_test) from tornado.test.util import (skipOnTravis, skipIfNoIPv6, refusing_port, skipBefore35, exec_test, ignore_deprecation) -from tornado.web import RequestHandler, Application, asynchronous, url, stream_request_body +from tornado.web import RequestHandler, Application, url, stream_request_body class SimpleHTTPClientCommonTestCase(httpclient_test.HTTPClientCommonTestCase): @@ -42,18 +43,21 @@ class TriggerHandler(RequestHandler): self.queue = queue self.wake_callback = wake_callback - @asynchronous + @gen.coroutine def get(self): logging.debug("queuing trigger") self.queue.append(self.finish) if self.get_argument("wake", "true") == "true": self.wake_callback() + never_finish = Event() + yield never_finish.wait() class HangHandler(RequestHandler): - @asynchronous + @gen.coroutine def get(self): - pass + never_finish = Event() + yield never_finish.wait() class ContentLengthHandler(RequestHandler): diff --git a/tornado/test/stack_context_test.py b/tornado/test/stack_context_test.py index 243ea47f3..61411d2ff 100644 --- a/tornado/test/stack_context_test.py +++ b/tornado/test/stack_context_test.py @@ -18,12 +18,13 @@ class TestRequestHandler(RequestHandler): def __init__(self, app, request): super(TestRequestHandler, self).__init__(app, request) - @asynchronous - def get(self): - logging.debug('in get()') - # call self.part2 without a self.async_callback wrapper. Its - # exception should still get thrown - IOLoop.current().add_callback(self.part2) + with ignore_deprecation(): + @asynchronous + def get(self): + logging.debug('in get()') + # call self.part2 without a self.async_callback wrapper. Its + # exception should still get thrown + IOLoop.current().add_callback(self.part2) def part2(self): logging.debug('in part2()') diff --git a/tornado/test/web_test.py b/tornado/test/web_test.py index 7f053ef06..b318fb176 100644 --- a/tornado/test/web_test.py +++ b/tornado/test/web_test.py @@ -8,6 +8,7 @@ from tornado.httputil import format_timestamp from tornado.ioloop import IOLoop from tornado.iostream import IOStream from tornado import locale +from tornado.locks import Event from tornado.log import app_log, gen_log from tornado.simple_httpclient import SimpleAsyncHTTPClient from tornado.template import DictLoader @@ -369,9 +370,11 @@ class ConnectionCloseHandler(RequestHandler): def initialize(self, test): self.test = test - @asynchronous + @gen.coroutine def get(self): self.test.on_handler_waiting() + never_finish = Event() + yield never_finish.wait() def on_connection_close(self): self.test.on_connection_close() @@ -548,10 +551,11 @@ class OptionalPathHandler(RequestHandler): class FlowControlHandler(RequestHandler): # These writes are too small to demonstrate real flow control, # but at least it shows that the callbacks get run. - @asynchronous - def get(self): - self.write("1") - self.flush(callback=self.step2) + with ignore_deprecation(): + @asynchronous + def get(self): + self.write("1") + self.flush(callback=self.step2) def step2(self): self.write("2") @@ -1805,10 +1809,11 @@ class MultipleExceptionTest(SimpleHandlerTestCase): class Handler(RequestHandler): exc_count = 0 - @asynchronous - def get(self): - IOLoop.current().add_callback(lambda: 1 / 0) - IOLoop.current().add_callback(lambda: 1 / 0) + with ignore_deprecation(): + @asynchronous + def get(self): + IOLoop.current().add_callback(lambda: 1 / 0) + IOLoop.current().add_callback(lambda: 1 / 0) def log_exception(self, typ, value, tb): MultipleExceptionTest.Handler.exc_count += 1 diff --git a/tornado/web.py b/tornado/web.py index 888dc5431..6693f10c5 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -78,6 +78,7 @@ import time import tornado import traceback import types +import warnings from inspect import isclass from io import BytesIO @@ -1702,7 +1703,14 @@ def asynchronous(method): .. versionchanged:: 4.3 Returning anything but ``None`` or a yieldable object from a method decorated with ``@asynchronous`` is an error. Such return values were previously ignored silently. + + .. deprecated:: 5.1 + + This decorator is deprecated and will be removed in Tornado 6.0. + Use coroutines instead. """ + warnings.warn("@asynchronous is deprecated, use coroutines instead", + DeprecationWarning) # Delay the IOLoop import because it's not available on app engine. from tornado.ioloop import IOLoop