From: Ben Darnell Date: Sun, 18 Mar 2018 23:49:50 +0000 (-0400) Subject: gen: Deprecate YieldPoint and its subclasses X-Git-Tag: v5.1.0b1~36^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5c6ae8de94db84afad34aae56fc9d9f7f2dbb8c1;p=thirdparty%2Ftornado.git gen: Deprecate YieldPoint and its subclasses --- diff --git a/tornado/gen.py b/tornado/gen.py index 253f8a510..989174264 100644 --- a/tornado/gen.py +++ b/tornado/gen.py @@ -527,8 +527,13 @@ class YieldPoint(object): """Base class for objects that may be yielded from the generator. .. deprecated:: 4.0 - Use `Futures <.Future>` instead. + Use `Futures <.Future>` instead. This class and all its subclasses + will be removed in 6.0 """ + def __init__(self): + warnings.warn("YieldPoint is deprecated, use Futures instead", + DeprecationWarning) + def start(self, runner): """Called by the runner after the generator has yielded. @@ -565,9 +570,11 @@ class Callback(YieldPoint): is given it will be returned by `Wait`. .. deprecated:: 4.0 - Use `Futures <.Future>` instead. + Use `Futures <.Future>` instead. This class will be removed in 6.0. """ def __init__(self, key): + warnings.warn("gen.Callback is deprecated, use Futures instead", + DeprecationWarning) self.key = key def start(self, runner): @@ -585,9 +592,11 @@ class Wait(YieldPoint): """Returns the argument passed to the result of a previous `Callback`. .. deprecated:: 4.0 - Use `Futures <.Future>` instead. + Use `Futures <.Future>` instead. This class will be removed in 6.0. """ def __init__(self, key): + warnings.warn("gen.Wait is deprecated, use Futures instead", + DeprecationWarning) self.key = key def start(self, runner): @@ -609,9 +618,11 @@ class WaitAll(YieldPoint): `WaitAll` is equivalent to yielding a list of `Wait` objects. .. deprecated:: 4.0 - Use `Futures <.Future>` instead. + Use `Futures <.Future>` instead. This class will be removed in 6.0. """ def __init__(self, keys): + warnings.warn("gen.WaitAll is deprecated, use gen.multi instead", + DeprecationWarning) self.keys = keys def start(self, runner): @@ -659,7 +670,12 @@ class YieldFuture(YieldPoint): .. versionchanged:: 5.0 The ``io_loop`` argument (deprecated since version 4.1) has been removed. + + .. deprecated:: 5.1 + This class will be removed in 6.0. """ + warnings.warn("YieldFuture is deprecated, use Futures instead", + DeprecationWarning) self.future = future self.io_loop = IOLoop.current() @@ -775,9 +791,11 @@ class MultiYieldPoint(YieldPoint): remains as an alias for the equivalent `multi` function. .. deprecated:: 4.3 - Use `multi` instead. + Use `multi` instead. This class will be removed in 6.0. """ def __init__(self, children, quiet_exceptions=()): + warnings.warn("MultiYieldPoint is deprecated, use Futures instead", + DeprecationWarning) self.keys = None if isinstance(children, dict): self.keys = list(children.keys()) diff --git a/tornado/test/gen_test.py b/tornado/test/gen_test.py index 4ac41140e..c831f3bd0 100644 --- a/tornado/test/gen_test.py +++ b/tornado/test/gen_test.py @@ -823,7 +823,7 @@ class GenCoroutineTest(AsyncTestCase): with ignore_deprecation(): f2(callback=(yield gen.Callback('cb'))) - results = yield [namespace['f1'](), gen.Wait('cb')] + results = yield [namespace['f1'](), gen.Wait('cb')] self.assertEqual(results, [42, 43]) self.finished = True @@ -935,46 +935,48 @@ class GenCoroutineTest(AsyncTestCase): @gen_test def test_replace_context_exception(self): - # Test exception handling: exceptions thrown into the stack context - # can be caught and replaced. - # Note that this test and the following are for behavior that is - # not really supported any more: coroutines no longer create a - # stack context automatically; but one is created after the first - # YieldPoint (i.e. not a Future). - @gen.coroutine - def f2(): - (yield gen.Callback(1))() - yield gen.Wait(1) - self.io_loop.add_callback(lambda: 1 / 0) - try: - yield gen.Task(self.io_loop.add_timeout, - self.io_loop.time() + 10) - except ZeroDivisionError: - raise KeyError() + with ignore_deprecation(): + # Test exception handling: exceptions thrown into the stack context + # can be caught and replaced. + # Note that this test and the following are for behavior that is + # not really supported any more: coroutines no longer create a + # stack context automatically; but one is created after the first + # YieldPoint (i.e. not a Future). + @gen.coroutine + def f2(): + (yield gen.Callback(1))() + yield gen.Wait(1) + self.io_loop.add_callback(lambda: 1 / 0) + try: + yield gen.Task(self.io_loop.add_timeout, + self.io_loop.time() + 10) + except ZeroDivisionError: + raise KeyError() - future = f2() - with self.assertRaises(KeyError): - yield future - self.finished = True + future = f2() + with self.assertRaises(KeyError): + yield future + self.finished = True @gen_test def test_swallow_context_exception(self): - # Test exception handling: exceptions thrown into the stack context - # can be caught and ignored. - @gen.coroutine - def f2(): - (yield gen.Callback(1))() - yield gen.Wait(1) - self.io_loop.add_callback(lambda: 1 / 0) - try: - yield gen.Task(self.io_loop.add_timeout, - self.io_loop.time() + 10) - except ZeroDivisionError: - raise gen.Return(42) + with ignore_deprecation(): + # Test exception handling: exceptions thrown into the stack context + # can be caught and ignored. + @gen.coroutine + def f2(): + (yield gen.Callback(1))() + yield gen.Wait(1) + self.io_loop.add_callback(lambda: 1 / 0) + try: + yield gen.Task(self.io_loop.add_timeout, + self.io_loop.time() + 10) + except ZeroDivisionError: + raise gen.Return(42) - result = yield f2() - self.assertEqual(result, 42) - self.finished = True + result = yield f2() + self.assertEqual(result, 42) + self.finished = True @gen_test def test_moment(self): @@ -1099,32 +1101,30 @@ class GenSequenceHandler(RequestHandler): @asynchronous @gen.engine def get(self): - self.io_loop = self.request.connection.stream.io_loop - self.io_loop.add_callback((yield gen.Callback("k1"))) - yield gen.Wait("k1") - self.write("1") - self.io_loop.add_callback((yield gen.Callback("k2"))) - yield gen.Wait("k2") - self.write("2") - # reuse an old key - self.io_loop.add_callback((yield gen.Callback("k1"))) - yield gen.Wait("k1") - self.finish("3") + # The outer ignore_deprecation applies at definition time. + # We need another for serving time. + with ignore_deprecation(): + self.io_loop = self.request.connection.stream.io_loop + self.io_loop.add_callback((yield gen.Callback("k1"))) + yield gen.Wait("k1") + self.write("1") + self.io_loop.add_callback((yield gen.Callback("k2"))) + yield gen.Wait("k2") + self.write("2") + # reuse an old key + self.io_loop.add_callback((yield gen.Callback("k1"))) + yield gen.Wait("k1") + self.finish("3") class GenCoroutineSequenceHandler(RequestHandler): @gen.coroutine def get(self): - self.io_loop = self.request.connection.stream.io_loop - self.io_loop.add_callback((yield gen.Callback("k1"))) - yield gen.Wait("k1") + yield gen.moment self.write("1") - self.io_loop.add_callback((yield gen.Callback("k2"))) - yield gen.Wait("k2") + yield gen.moment self.write("2") - # reuse an old key - self.io_loop.add_callback((yield gen.Callback("k1"))) - yield gen.Wait("k1") + yield gen.moment self.finish("3") @@ -1132,16 +1132,11 @@ class GenCoroutineUnfinishedSequenceHandler(RequestHandler): @asynchronous @gen.coroutine def get(self): - self.io_loop = self.request.connection.stream.io_loop - self.io_loop.add_callback((yield gen.Callback("k1"))) - yield gen.Wait("k1") + yield gen.moment self.write("1") - self.io_loop.add_callback((yield gen.Callback("k2"))) - yield gen.Wait("k2") + yield gen.moment self.write("2") - # reuse an old key - self.io_loop.add_callback((yield gen.Callback("k1"))) - yield gen.Wait("k1") + yield gen.moment # just write, don't finish self.write("3") diff --git a/tornado/test/stack_context_test.py b/tornado/test/stack_context_test.py index 7ddd818a5..4afe2a86f 100644 --- a/tornado/test/stack_context_test.py +++ b/tornado/test/stack_context_test.py @@ -6,11 +6,12 @@ from tornado.log import app_log from tornado.stack_context import (StackContext, wrap, NullContext, StackContextInconsistentError, ExceptionStackContext, run_with_stack_context, _state) from tornado.testing import AsyncHTTPTestCase, AsyncTestCase, ExpectLog, gen_test -from tornado.test.util import unittest, ignore_deprecation +from tornado.test.util import unittest from tornado.web import asynchronous, Application, RequestHandler import contextlib import functools import logging +import warnings class TestRequestHandler(RequestHandler): @@ -61,6 +62,13 @@ class StackContextTest(AsyncTestCase): def setUp(self): super(StackContextTest, self).setUp() self.active_contexts = [] + self.warning_catcher = warnings.catch_warnings() + self.warning_catcher.__enter__() + warnings.simplefilter('ignore', DeprecationWarning) + + def tearDown(self): + self.warning_catcher.__exit__(None, None, None) + super(StackContextTest, self).tearDown() @contextlib.contextmanager def context(self, name): @@ -215,16 +223,15 @@ class StackContextTest(AsyncTestCase): self.wait() def test_yield_in_with(self): - with ignore_deprecation(): - @gen.engine - def f(): - self.callback = yield gen.Callback('a') - with StackContext(functools.partial(self.context, 'c1')): - # This yield is a problem: the generator will be suspended - # and the StackContext's __exit__ is not called yet, so - # the context will be left on _state.contexts for anything - # that runs before the yield resolves. - yield gen.Wait('a') + @gen.engine + def f(): + self.callback = yield gen.Callback('a') + with StackContext(functools.partial(self.context, 'c1')): + # This yield is a problem: the generator will be suspended + # and the StackContext's __exit__ is not called yet, so + # the context will be left on _state.contexts for anything + # that runs before the yield resolves. + yield gen.Wait('a') with self.assertRaises(StackContextInconsistentError): f() @@ -245,15 +252,14 @@ class StackContextTest(AsyncTestCase): def test_yield_in_with_exception_stack_context(self): # As above, but with ExceptionStackContext instead of StackContext. - with ignore_deprecation(): - @gen.engine - def f(): - with ExceptionStackContext(lambda t, v, tb: False): - yield gen.Task(self.io_loop.add_callback) - - with self.assertRaises(StackContextInconsistentError): - f() - self.wait() + @gen.engine + def f(): + with ExceptionStackContext(lambda t, v, tb: False): + yield gen.Task(self.io_loop.add_callback) + + with self.assertRaises(StackContextInconsistentError): + f() + self.wait() @gen_test def test_yield_outside_with_exception_stack_context(self):