%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib64/python2.7/site-packages/tornado/
Upload File :
Create Path :
Current File : //lib64/python2.7/site-packages/tornado/stack_context.pyc

�
��L]c@�s�dZddlmZmZmZmZddlZddlZddlm	Z	de
fd��YZdejfd��YZ
e
�Zd	efd
��YZdefd��YZd
efd��YZd�Zd�Zd�Zd�ZdS(s	`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
``async_callback`` wrappers (as in `tornado.web.RequestHandler`), and to
allow some additional context to be kept for logging.

This is slightly magic, but it's an extension of the idea that an
exception handler is a kind of stack-local state and when that stack
is suspended and resumed in a new context that state needs to be
preserved.  `StackContext` shifts the burden of restoring that state
from each call site (e.g.  wrapping each `.AsyncHTTPClient` callback
in ``async_callback``) to the mechanisms that transfer control from
one context to another (e.g. `.AsyncHTTPClient` itself, `.IOLoop`,
thread pools, etc).

Example usage::

    @contextlib.contextmanager
    def die_on_error():
        try:
            yield
        except Exception:
            logging.error("exception in asynchronous operation",exc_info=True)
            sys.exit(1)

    with StackContext(die_on_error):
        # Any exception thrown here *or in callback and its descendants*
        # will cause the process to exit instead of spinning endlessly
        # in the ioloop.
        http_client.fetch(url, callback)
    ioloop.start()

Most applications shouldn't have to work with `StackContext` directly.
Here are a few rules of thumb for when it's necessary:

* If you're writing an asynchronous library that doesn't rely on a
  stack_context-aware library like `tornado.ioloop` or `tornado.iostream`
  (for example, if you're writing a thread pool), use
  `.stack_context.wrap()` before any asynchronous operations to capture the
  stack context from where the operation was started.

* If you're writing an asynchronous library that has some shared
  resources (such as a connection pool), create those shared resources
  within a ``with stack_context.NullContext():`` block.  This will prevent
  ``StackContexts`` from leaking from one request to another.

* If you want to write something like an exception handler that will
  persist across asynchronous calls, create a new `StackContext` (or
  `ExceptionStackContext`), and make your asynchronous calls in a ``with``
  block that references your `StackContext`.
i(tabsolute_importtdivisiontprint_functiontwith_statementN(traise_exc_infotStackContextInconsistentErrorcB�seZRS((t__name__t
__module__(((s;/usr/lib64/python2.7/site-packages/tornado/stack_context.pyRMst_StatecB�seZd�ZRS(cC�st�df|_dS(N(ttupletNonetcontexts(tself((s;/usr/lib64/python2.7/site-packages/tornado/stack_context.pyt__init__Rs(RRR
(((s;/usr/lib64/python2.7/site-packages/tornado/stack_context.pyRQstStackContextcB�sDeZdZd�Zd�Zd�Zd�Zd�Zd�ZRS(sEstablishes 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
    non-transferable context manager you would say::

      with my_context():

    StackContext takes the function itself rather than its result::

      with StackContext(my_context):

    The result of ``with StackContext() as cb:`` is a deactivation
    callback.  Run this callback when the StackContext is no longer
    needed to ensure that it is not propagated any further (note that
    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.
    cC�s||_g|_t|_dS(N(tcontext_factoryRtTruetactive(RR((s;/usr/lib64/python2.7/site-packages/tornado/stack_context.pyR
ks		cC�s
t|_dS(N(tFalseR(R((s;/usr/lib64/python2.7/site-packages/tornado/stack_context.pyt_deactivatepscC�s*|j�}|jj|�|j�dS(N(RRtappendt	__enter__(Rtcontext((s;/usr/lib64/python2.7/site-packages/tornado/stack_context.pytentertscC�s&|jj�}|j|||�dS(N(Rtpopt__exit__(Rttypetvaluet	tracebackR((s;/usr/lib64/python2.7/site-packages/tornado/stack_context.pytexityscC�sctj|_|jd|f|f|_|jt_y|j�Wn|jt_�nX|jS(Ni(t_stateRtold_contextstnew_contextsRR(R((s;/usr/lib64/python2.7/site-packages/tornado/stack_context.pyR�scC�s[z|j|||�Wdtj}|jt_||jk	rMtd��nd|_XdS(NsWstack_context inconsistency (may be caused by yield within a "with StackContext" block)(RRRRR RR
(RRRRtfinal_contexts((s;/usr/lib64/python2.7/site-packages/tornado/stack_context.pyR�s	(	RRt__doc__R
RRRRR(((s;/usr/lib64/python2.7/site-packages/tornado/stack_context.pyRWs					
tExceptionStackContextcB�s;eZdZd�Zd�Zd�Zd�Zd�ZRS(sASpecialization 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
    similar to a try/finally clause, and intended use cases are to log
    an error, close a socket, or similar cleanup actions.  The
    ``exc_info`` triple ``(type, value, traceback)`` will be passed to the
    exception_handler function.

    If the exception handler returns true, the exception will be
    consumed and will not be propagated to other exception handlers.
    cC�s||_t|_dS(N(texception_handlerRR(RR$((s;/usr/lib64/python2.7/site-packages/tornado/stack_context.pyR
�s	cC�s
t|_dS(N(RR(R((s;/usr/lib64/python2.7/site-packages/tornado/stack_context.pyR�scC�s#|dk	r|j|||�SdS(N(R
R$(RRRR((s;/usr/lib64/python2.7/site-packages/tornado/stack_context.pyR�scC�s5tj|_|jd|f|_|jt_|jS(Ni(RRRR R(R((s;/usr/lib64/python2.7/site-packages/tornado/stack_context.pyR�scC�sgz#|dk	r"|j|||�SWdtj}|jt_||jk	rYtd��nd|_XdS(NsWstack_context inconsistency (may be caused by yield within a "with StackContext" block)(R
R$RRRR R(RRRRR!((s;/usr/lib64/python2.7/site-packages/tornado/stack_context.pyR�s	(RRR"R
RRRR(((s;/usr/lib64/python2.7/site-packages/tornado/stack_context.pyR#�s				tNullContextcB�s eZdZd�Zd�ZRS(s�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.
    cC�s"tj|_t�dft_dS(N(RRRR	R
(R((s;/usr/lib64/python2.7/site-packages/tornado/stack_context.pyR�scC�s|jt_dS(N(RRR(RRRR((s;/usr/lib64/python2.7/site-packages/tornado/stack_context.pyR�s(RRR"RR(((s;/usr/lib64/python2.7/site-packages/tornado/stack_context.pyR%�s	cC�s�tg|dD]}|jr|^q�}|d}x'|dk	r_|jr_|jd}q9W|}x\|dk	r�|jd}x6|dk	r�|jr�Pn|j|_|jd}q�W|}qiW||fS(s*Remove deactivated handlers from the chainiiN(R	RR
R(Rthtstack_contextstheadtctxtparent((s;/usr/lib64/python2.7/site-packages/tornado/stack_context.pyt_remove_deactivated�s,

	
c�s��dkst�d�r�Stjg��ddrh�ddrh��fd�}t|_|S��fd�}t|_|S(s
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).
    t_wrappediic�s8z'tj}�dt_�||�SWd|t_XdS(Ni(RR(targstkwargst
current_state(tcap_contextstfn(s;/usr/lib64/python2.7/site-packages/tornado/stack_context.pytnull_wrappers
	
c�s�d}z�tj}t�d��d<}|t_d}d}d}|d}xI|D]A}	y|	j�|d7}WqVtj�}|	jd}qVXqVW|dkr�y�||�}Wq�tj�}|d}q�Xn|dk	r�t||�}n�x_|dkrV|d8}||}
y|
j	|�Wq�tj�}|
jd}Pq�Xq�Wd}|dk	r{t||�}n|dkr�t
|�nWd|t_X|S(Nii(NNN(NNN(R
RRR+Rtsystexc_infoRt_handle_exceptionRR(R-R.tretR/Rtexcttoptlast_ctxtstacktntc(R0R1(s;/usr/lib64/python2.7/site-packages/tornado/stack_context.pytwrappedsP		





	
N(R
thasattrRRRR,(R1R2R=((R0R1s;/usr/lib64/python2.7/site-packages/tornado/stack_context.pytwrap�s		C	cC�sVxO|dk	rQy|j|�r*d}nWntj�}nX|jd}qW|S(Ni(NNN(R
RR3R4R(ttailR7((s;/usr/lib64/python2.7/site-packages/tornado/stack_context.pyR5_s
cC�s|�|�SWdQXdS(s�Run a coroutine ``func`` in the given `StackContext`.

    It is not safe to have a ``yield`` statement within a ``with StackContext``
    block, so it is difficult to use stack context with `.gen.coroutine`.
    This helper function runs the function in the correct context while
    keeping the ``yield`` and ``with`` statements syntactically separate.

    Example::

        @gen.coroutine
        def incorrect():
            with StackContext(ctx):
                # ERROR: this will raise StackContextInconsistentError
                yield other_coroutine()

        @gen.coroutine
        def correct():
            yield run_with_stack_context(StackContext(ctx), other_coroutine)

    .. versionadded:: 3.1
    N((Rtfunc((s;/usr/lib64/python2.7/site-packages/tornado/stack_context.pytrun_with_stack_contextls(R"t
__future__RRRRR3t	threadingttornado.utilRt	ExceptionRtlocalRRtobjectRR#R%R+R?R5RB(((s;/usr/lib64/python2.7/site-packages/tornado/stack_context.pyt<module>Cs"	L0		c	

Zerion Mini Shell 1.0