# License for the specific language governing permissions and limitations
# under the License.
-'''StackContext allows applications to maintain threadlocal-like state
+"""StackContext allows applications to maintain threadlocal-like state
that follows execution as it moves to other execution contexts.
The motivating examples are to eliminate the need for explicit
persist across asynchronous calls, create a new `StackContext` (or
`ExceptionStackContext`), and make your asynchronous calls in a ``with``
block that references your `StackContext`.
-'''
+"""
from __future__ import absolute_import, division, print_function, with_statement
class StackContext(object):
- '''Establishes the given context as a StackContext that will be transferred.
+ """Establishes the given context as a StackContext that will be transferred.
Note that the parameter is a callable that returns a context
manager, not the context itself. That is, where for a
deactivating a context does not affect any instances of that
context that are currently pending). This is an advanced feature
and not necessary in most applications.
- '''
+ """
def __init__(self, context_factory, _active_cell=None):
self.context_factory = context_factory
self.active_cell = _active_cell or [True]
class ExceptionStackContext(object):
- '''Specialization of StackContext for exception handling.
+ """Specialization of StackContext for exception handling.
The supplied exception_handler function will be called in the
event of an uncaught exception in this context. The semantics are
If the exception handler returns true, the exception will be
consumed and will not be propagated to other exception handlers.
- '''
+ """
def __init__(self, exception_handler, _active_cell=None):
self.exception_handler = exception_handler
self.active_cell = _active_cell or [True]
class NullContext(object):
- '''Resets the StackContext.
+ """Resets the StackContext.
Useful when creating a shared resource on demand (e.g. an AsyncHTTPClient)
where the stack that caused the creating is not relevant to future
operations.
- '''
+ """
def __enter__(self):
self.old_contexts = _state.contexts
_state.contexts = ()
def wrap(fn):
- '''Returns a callable object that will restore the current StackContext
+ """Returns a callable object that will restore the current StackContext
when executed.
Use this whenever saving a callback to be executed later in a
different execution context (either in a different thread or
asynchronously in the same thread).
- '''
+ """
if fn is None or fn.__class__ is _StackContextWrapper:
return fn
# functools.wraps doesn't appear to work on functools.partial objects
super(AsyncTestCase, self).tearDown()
def get_new_ioloop(self):
- '''Creates a new IOLoop for this test. May be overridden in
+ """Creates a new IOLoop for this test. May be overridden in
subclasses for tests that require a specific IOLoop (usually
the singleton).
- '''
+ """
return IOLoop()
def _handle_exception(self, typ, value, tb):
self.__rethrow()
def stop(self, _arg=None, **kwargs):
- '''Stops the ioloop, causing one pending (or future) call to wait()
+ """Stops the ioloop, causing one pending (or future) call to wait()
to return.
Keyword arguments or a single positional argument passed to stop() are
saved and will be returned by wait().
- '''
+ """
assert _arg is None or not kwargs
self.__stop_args = kwargs or _arg
if self.__running:
class AsyncHTTPTestCase(AsyncTestCase):
- '''A test case that starts up an HTTP server.
+ """A test case that starts up an HTTP server.
Subclasses must override get_app(), which returns the
tornado.web.Application (or other HTTPServer callback) to be tested.
self.http_client.fetch(self.get_url('/'), self.stop)
response = self.wait()
# test contents of response
- '''
+ """
def setUp(self):
super(AsyncHTTPTestCase, self).setUp()
sock, port = bind_unused_port()