"""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.
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):
"""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):
`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):
.. 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()
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())
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
@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):
@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")
@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")
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):
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):
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()
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):