HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.29
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //lib64/python3.6/site-packages/zmq/eventloop/minitornado/__pycache__/stack_context.cpython-36.pyc
3

VS�_m3�@s�dZddlmZmZmZmZddlZddlZddlm	Z	Gdd�de
�ZGdd	�d	ej�Z
e
�ZGd
d�de�ZGdd
�d
e�ZGdd�de�Zdd�Zdd�Zdd�Zdd�ZdS)a	`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`.
�)�absolute_import�division�print_function�with_statementN�)�raise_exc_infoc@seZdZdS)�StackContextInconsistentErrorN)�__name__�
__module__�__qualname__�rr�%/usr/lib64/python3.6/stack_context.pyrNsrc@seZdZdd�ZdS)�_StatecCst�df|_dS)N)�tuple�contexts)�selfrrr
�__init__Ssz_State.__init__N)r	r
rrrrrr
rRsrc@s@eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dS)�StackContextaEstablishes 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.
    cCs||_g|_d|_dS)NT)�context_factoryr�active)rrrrr
rlszStackContext.__init__cCs
d|_dS)NF)r)rrrr
�_deactivateqszStackContext._deactivatecCs |j�}|jj|�|j�dS)N)rr�append�	__enter__)r�contextrrr
�enteruszStackContext.entercCs|jj�}|j|||�dS)N)r�pop�__exit__)r�type�value�	tracebackrrrr
�exitzs
zStackContext.exitc	CsPtj|_|jd|f|f|_|jt_y|j�Wn|jt_�YnX|jS)Nr)�_stater�old_contexts�new_contextsrr)rrrr
r�szStackContext.__enter__cCs@z|j|||�Wdtj}|jt_||jk	r4td��d|_XdS)NzWstack_context inconsistency (may be caused by yield within a "with StackContext" block))r r!rr"r#r)rrrr�final_contextsrrr
r�s
zStackContext.__exit__N)
r	r
r�__doc__rrrr rrrrrr
rXs
rc@s8eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
�ExceptionStackContextaASpecialization 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.
    cCs||_d|_dS)NT)�exception_handlerr)rr'rrr
r�szExceptionStackContext.__init__cCs
d|_dS)NF)r)rrrr
r�sz!ExceptionStackContext._deactivatecCs|dk	r|j|||�SdS)N)r')rrrrrrr
r �szExceptionStackContext.exitcCs&tj|_|jd|f|_|jt_|jS)Nr)r!rr"r#r)rrrr
r�szExceptionStackContext.__enter__cCsHz|dk	r|j|||�SWdtj}|jt_||jk	r<td��d|_XdS)NzWstack_context inconsistency (may be caused by yield within a "with StackContext" block))r'r!rr"r#r)rrrrr$rrr
r�s
zExceptionStackContext.__exit__N)	r	r
rr%rrr rrrrrr
r&�sr&c@s eZdZdZdd�Zdd�ZdS)�NullContextz�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.
    cCstj|_t�dft_dS)N)r!rr"r)rrrr
r�szNullContext.__enter__cCs|jt_dS)N)r"r!r)rrrrrrr
r�szNullContext.__exit__N)r	r
rr%rrrrrr
r(�sr(cCs�tdd�|dD��}|d}x|dk	r<|jr<|jd}q W|}xB|dk	r�|jd}x&|dk	r||jrhP|j|_|jd}qXW|}qDW||fS)z*Remove deactivated handlers from the chaincSsg|]}|jr|�qSr)r)�.0�hrrr
�
<listcomp>�sz'_remove_deactivated.<locals>.<listcomp>rrN)rrr")rZstack_contexts�headZctx�parentrrr
�_remove_deactivated�s


r.csj�dkst�d�r�Stjg��ddrR�ddrR��fdd�}d|_|S��fdd	�}d|_|S)
a
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).
    N�_wrappedrrcs(ztj}�dt_�||�S|t_XdS)Nr)r!r)�args�kwargs�
current_state)�cap_contexts�fnrr
�null_wrappers


zwrap.<locals>.null_wrapperTcsPd}�z<tj}t�d��d<}|t_d}d}d}|d}x@|D]8}	y|	j�|d7}WqBtj�}|	jd}YqBXqBW|dkr�y�||�}Wntj�}|d}YnX|dk	r�t||�}nhxR|dk�r|d8}||}
y|
j|�Wq�tj�}|
jd}PYq�Xq�Wd}|dk	�r.t||�}|dk�r@t	|�Wd|t_X|S)Nrr)NNN)NNN)
r!rr.r�sys�exc_infor"�_handle_exceptionr r)r0r1�retr2r�exc�topZlast_ctx�stack�n�c)r3r4rr
�wrappedsP




zwrap.<locals>.wrapped)�hasattrr!rr/)r4r5r?r)r3r4r
�wrap�s	CrAc	CsDx>|dk	r>y|j|�rd}Wntj�}YnX|jd}qW|S)Nr)NNN)r r6r7r")�tailr:rrr
r8`s

r8c	Cs|�|�SQRXdS)a�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
    Nr)r�funcrrr
�run_with_stack_contextmsrD)r%Z
__future__rrrrr6Z	threading�utilr�	ExceptionrZlocalrr!�objectrr&r(r.rAr8rDrrrr
�<module>DsL0c