
    dW                        d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dlm
Z
 ddlmZ ej        rd dlmZ d d	lmZ d d
lmZ  ej        d          Z ej        dej        dej        f                   Zdej        d         ddfdZ G d d          Z G d dej        e                   Z G d d          Z G d d          Z G d de          ZdedefdZdedefd Z  G d! d"ej        e                   Z!dS )#    N)
ContextVarpartial)update_wrapper)
attrgetter   )ClosingIterator)StartResponse)WSGIApplication)WSGIEnvironmentTF.)boundlocal)Local
LocalStackreturnc                 .    |                                   dS )zRelease the data for the current context in a :class:`Local` or
    :class:`LocalStack` without using a :class:`LocalManager`.

    This should not be needed for modern use cases, and may be removed
    in the future.

    .. versionadded:: 0.6.1
    N)__release_local__)r   s    .lib/python3.11/site-packages/werkzeug/local.pyrelease_localr      s     
    c                   6   e Zd ZdZdZ	 ddej        eej        e	ej
        f                           ddfdZdej        ej        e	ej
        f                  fdZddd	e	d
ej        e	         ddfdZddZd	e	dej
        fdZd	e	dej
        ddfdZd	e	ddfdZdS )r   aq  Create a namespace of context-local data. This wraps a
    :class:`ContextVar` containing a :class:`dict` value.

    This may incur a performance penalty compared to using individual
    context vars, as it has to copy data to avoid mutating the dict
    between nested contexts.

    :param context_var: The :class:`~contextvars.ContextVar` to use as
        storage for this local. If not given, one will be created.
        Context vars not created at the global scope may interfere with
        garbage collection.

    .. versionchanged:: 2.0
        Uses ``ContextVar`` instead of a custom storage implementation.
    )	__storageNcontext_varr   c                     | t          dt          |            d          }t                              | d|           d S )Nzwerkzeug.Local<	>.storage_Local__storage)r   idobject__setattr__selfr   s     r   __init__zLocal.__init__4   sM      	L
 %%Jr$xx%J%J%JKKK4!2K@@@@@r   c                 t    t          | j                            i                                                     S N)iterr   getitemsr#   s    r   __iter__zLocal.__iter__@   s,    DN&&r**0022333r   unbound_messagenamer-   
LocalProxyc                &    t          | ||          S )zCreate a :class:`LocalProxy` that access an attribute on this
        local namespace.

        :param name: Proxy this attribute.
        :param unbound_message: The error message that the proxy will
            show if the attribute isn't set.
        r,   r/   r#   r.   r-   s      r   __call__zLocal.__call__C   s     $oFFFFr   c                 :    | j                             i            d S r&   )r   setr*   s    r   r   zLocal.__release_local__O   s    2r   c                 l    | j                             i           }||v r||         S t          |          r&   )r   r(   AttributeErrorr#   r.   valuess      r   __getattr__zLocal.__getattr__R   s;    ##B''6> 	 $<T"""r   valuec                     | j                             i                                           }|||<   | j                             |           d S r&   )r   r(   copyr5   )r#   r.   r;   r9   s       r   r!   zLocal.__setattr__Z   sH    ##B'',,..t6"""""r   c                     | j                             i           }||v r3|                                }||= | j                             |           d S t	          |          r&   )r   r(   r=   r5   r7   r8   s      r   __delattr__zLocal.__delattr___   sa    ##B''6> 	'[[]]FtNv&&&&& &&&r   r&   r   N)__name__
__module____qualname____doc__	__slots__tOptionalr   DictstrAnyr$   IteratorTupler+   r3   r   r:   r!   r?    r   r   r   r   !   su          I IM
A 
A:jQU
1C&DE
A	
A 
A 
A 
A4!*QWS!%Z%89 4 4 4 4 @D
G 
G 
G
G-.Z_
G	
G 
G 
G 
G   # # # # # ## #AE #d # # # #
' ' ' ' ' ' ' 'r   r   c                   ,   e Zd ZdZdZddej        eej        e	                           ddfdZ
ddZde	dej        e	         fd	Zdej        e	         fd
Zedej        e	         fd            Z	 ddddej        e         dej        e         ddfdZdS )r   a  Create a stack of context-local data. This wraps a
    :class:`ContextVar` containing a :class:`list` value.

    This may incur a performance penalty compared to using individual
    context vars, as it has to copy data to avoid mutating the list
    between nested contexts.

    :param context_var: The :class:`~contextvars.ContextVar` to use as
        storage for this local. If not given, one will be created.
        Context vars not created at the global scope may interfere with
        garbage collection.

    .. versionchanged:: 2.0
        Uses ``ContextVar`` instead of a custom storage implementation.

    .. versionadded:: 0.6.1
    )_storageNr   r   c                 X    | t          dt          |            d          }|| _        d S )Nzwerkzeug.LocalStack<r   )r   r   rO   r"   s     r   r$   zLocalStack.__init__   s7     	Q
 %%OBtHH%O%O%OPPK#r   c                 :    | j                             g            d S r&   )rO   r5   r*   s    r   r   zLocalStack.__release_local__   s    "r   objc                     | j                             g                                           }|                    |           | j                             |           |S )z'Add a new item to the top of the stack.)rO   r(   r=   appendr5   )r#   rR   stacks      r   pushzLocalStack.push   sP    !!"%%**,,S%   r   c                     | j                             g           }t          |          dk    rdS |d         }| j                             |dd                    |S )zjRemove the top item from the stack and return it. If the
        stack is empty, return ``None``.
        r   N)rO   r(   lenr5   )r#   rU   rvs      r   popzLocalStack.pop   s\     !!"%%u::? 	42Y%*%%%	r   c                 p    | j                             g           }t          |          dk    rdS |d         S )z[The topmost item on the stack.  If the stack is empty,
        `None` is returned.
        r   NrX   )rO   r(   rY   )r#   rU   s     r   topzLocalStack.top   s:    
 !!"%%u::? 	4Ryr   r,   r.   r-   r/   c                &    t          | ||          S )a@  Create a :class:`LocalProxy` that accesses the top of this
        local stack.

        :param name: If given, the proxy access this attribute of the
            top item, rather than the item itself.
        :param unbound_message: The error message that the proxy will
            show if the stack is empty.
        r,   r1   r2   s      r   r3   zLocalStack.__call__   s     $oFFFFr   r&   r@   )rA   rB   rC   rD   rE   rF   rG   r   Listr   r$   r   rV   r[   propertyr]   rI   r3   rM   r   r   r   r   j   sD        $ I$ $AJz!&)/D$E $QU $ $ $ $    afQi    QZ]     	QZ] 	 	 	 X	 '+GRVG G GJsOG@A
3G	G G G G G Gr   r   c            
           e Zd ZdZdZ	 ddej        ej        ee	ej
        ej        ee	f                  f                  ddfdZddZdd
ZddZdefdZdS )LocalManagera>  Manage releasing the data for the current context in one or more
    :class:`Local` and :class:`LocalStack` objects.

    This should not be needed for modern use cases, and may be removed
    in the future.

    :param locals: A local or list of locals to manage.

    .. versionchanged:: 2.0
        ``ident_func`` is deprecated and will be removed in Werkzeug
         2.1.

    .. versionchanged:: 0.7
        The ``ident_func`` parameter was added.

    .. versionchanged:: 0.6.1
        The :func:`release_local` function can be used instead of a
        manager.
    )localsNrc   r   c                     |	g | _         d S t          |t                    r
|g| _         d S t          |          | _         d S r&   )rc   
isinstancer   list)r#   rc   s     r   r$   zLocalManager.__init__   sG      	'DKKK&& 	'!(DKKKv,,DKKKr   c                 8    | j         D ]}t          |           dS )zRelease the data in the locals for this context. Call this at
        the end of each request or use :meth:`make_middleware`.
        N)rc   r   )r#   r   s     r   cleanupzLocalManager.cleanup   s0     [ 	! 	!E%    	! 	!r   appr   c                 N     dddddt           j        t                   f fd}|S )zWrap a WSGI application so that local data is released
        automatically after the response has been sent for a request.
        environr   start_responser
   r   c                 B    t           | |          j                  S r&   )r	   rh   )rk   rl   ri   r#   s     r   applicationz1LocalManager.make_middleware.<locals>.application   s#     #33w#?#?NNNr   )rF   Iterablebytes)r#   ri   rn   s   `` r   make_middlewarezLocalManager.make_middleware   sX    
	O&	O8G	OZ	O 	O 	O 	O 	O 	O 	O
 r   funcc                 H    t          |                     |          |          S )zLike :meth:`make_middleware` but used as a decorator on the
        WSGI application function.

        .. code-block:: python

            @manager.middleware
            def application(environ, start_response):
                ...
        )r   rq   )r#   rr   s     r   
middlewarezLocalManager.middleware   s"     d22488$???r   c                 \    dt          |           j         dt          | j                   dS )N<z storages: >)typerA   rY   rc   r*   s    r   __repr__zLocalManager.__repr__   s.    F4::&FF3t{3C3CFFFFr   r&   r@   )ri   r   r   r   )rr   r   r   r   )rA   rB   rC   rD   rE   rF   rG   Unionr   r   ro   r$   rh   rq   rt   rI   ry   rM   r   r   rb   rb      s         ( I 	' '
GE:qz!'%:K2L'MMN
'
 
' ' ' '! ! ! !
 
 
 

@ 
@ 
@ 
@G# G G G G G Gr   rb   c                   (   e Zd ZdZdZ	 	 	 	 ddej        ej                 dej        ej                 dej        ej                 de	d	df
d
Z
ddded	dfdZddddej        e         d	ej        fdZd	efdZdddej        dej        d	ej        fdZdS )_ProxyLookupa  Descriptor that handles proxied attribute lookup for
    :class:`LocalProxy`.

    :param f: The built-in function this attribute is accessed through.
        Instead of looking up the special method, the function call
        is redone on the object.
    :param fallback: Return this function if the proxy is unbound
        instead of raising a :exc:`RuntimeError`.
    :param is_attr: This proxied name is an attribute, not a function.
        Call the fallback immediately to get the value.
    :param class_value: Value to return when accessed from the
        ``LocalProxy`` class directly. Used for ``__doc__`` so building
        docs still works.
    )bind_ffallbackis_attrclass_valuer.   NFfr~   r   r   r   c                     t          d          r!dddt          j        dt          j        ffd}n%!dddt          j        dt          j        ffd}nd }|| _        || _        || _        || _        d S )N__get__instancer/   rR   r   c                 J                         |t          |                    S r&   r   rx   r   rR   r   s     r   r}   z%_ProxyLookup.__init__.<locals>.bind_f  s    yyd3ii000r   c                 $    t          |          S r&   r   r   s     r   r}   z%_ProxyLookup.__init__.<locals>.bind_f%  s    q#&r   )hasattrrF   rJ   Callabler}   r~   r   r   )r#   r   r~   r   r   r}   s    `    r   r$   z_ProxyLookup.__init__  s     1i   	1 1AE 1aj 1 1 1 1 1 1 1  	' 'AE 'aj ' ' ' ' ' ' '
 F &r   ownerr/   r.   c                     || _         d S r&   r.   )r#   r   r.   s      r   __set_name__z_ProxyLookup.__set_name__1  s    			r   r   c                 F   || j         | j         S | S 	 |                                }nH# t          $ r; | j         | j                            ||          }| j        r |            cY S |cY S w xY w| j        |                     ||          S t          || j                  S r&   )	r   _get_current_objectRuntimeErrorr~   r   r   r}   getattrr.   )r#   r   r   rR   r~   s        r   r   z_ProxyLookup.__get__4  s     	 (''K	..00CC 	 	 	} },,Xu==H| "  xzz!!!OOO	 ; 	.;;x---sDI&&&s   ) >A.)A.-A.c                     d| j          S )Nzproxy r   r*   s    r   ry   z_ProxyLookup.__repr__O  s    #	###r   argskwargsc                 T     |                      |t          |                    |i |S )zSupport calling unbound methods from the class. For example,
        this happens with ``copy.copy``, which does
        ``type(x).__copy__(x)``. ``type(x)`` can't be proxied, so it
        returns the proxy type and descriptor.
        r   )r#   r   r   r   s       r   r3   z_ProxyLookup.__call__R  s-     6t||Hd8nn55tFvFFFr   )NNNFr&   )rA   rB   rC   rD   rE   rF   rG   r   rJ   boolr$   rI   r   rx   r   ry   r3   rM   r   r   r|   r|     sW         II %)+/)- :aj! *QZ( Z&	
  
   <, c d    ' ' 'QZ5E 'QRQV ' ' ' '6$# $ $ $ $G Gae Gqu GQRQV G G G G G Gr   r|   c                   x     e Zd ZdZdZ	 ddej        ej                 dej        ej                 ddf fdZ xZ	S )		_ProxyIOpzLook up an augmented assignment method on a proxied object. The
    method is wrapped to return the proxy instead of the object.
    rM   Nr   r~   r   c                     t                                          |           dddt          j        dt          j        ffd}|| _        d S )Nr   r/   rR   r   c                      dt           j        dt           j        ddf fd}|                    |t          |                    S )Nr#   otherr   r/   c                       | |           S r&   rM   )r#   r   r   r   s     r   i_opz0_ProxyIOp.__init__.<locals>.bind_f.<locals>.i_oph  s    $r   )rF   rJ   r   rx   )r   rR   r   r   s   `  r   r}   z"_ProxyIOp.__init__.<locals>.bind_fg  s\     15    <               <<T#YY///r   )superr$   rF   rJ   r   r}   )r#   r   r~   r}   	__class__s    `  r   r$   z_ProxyIOp.__init__b  sd     	H%%%	0\ 	0 	0!* 	0 	0 	0 	0 	0 	0 r   )NN)
rA   rB   rC   rD   rE   rF   rG   r   r$   __classcell__)r   s   @r   r   r   [  s          I TX AJ':;*QZ:P	         r   r   opc                      dt           j        dt           j        dt           j        f fd}t          j        t          |          S )z5Swap the argument order to turn an l-op into an r-op.rR   r   r   c                      ||           S r&   rM   )rR   r   r   s     r   r_opz_l_to_r_op.<locals>.r_opt  s    r%~~r   )rF   rJ   castr   )r   r   s   ` r   
_l_to_r_opr   q  sN    !%  !%       6!T??r   oc                     | S r&   rM   )r   s    r   	_identityr   z  s    Hr   c            
       ,   e Zd ZU dZdZej        g ef         ed<   	 	 ddddej	        e
e         eee         ej        g ef         f         dej        e         dej        e         d	dfd
Z eed d          Z ed d          Z eed           Z ee          Z ee          Z e            Z eej                  Z eej                  Z eej                  Z eej                  Z  eej!                  Z" eej#                  Z$ ee%          Z& ee'd           Z( ee)          Z* ee+          Z, ee-          Z. ee/d           Z0 ed d          Z1 ed           Z2 ed           Z3 ed           Z4 ee5          Z6 eej7                  Z8 eej9                  Z: eej;                  Z< eej=                  Z> ee?          Z@ eeA          ZB eeC          ZD eejE                  ZF eejG                  ZH eejI                  ZJ eejK                  ZL eejM                  ZN eejO                  ZP eejQ                  ZR eejS                  ZT eeU          ZV eeW          ZX eejY                  ZZ eej[                  Z\ eej]                  Z^ eej_                  Z` eeja                  Zb e ecejG                            Zd e ecejI                            Ze e ecejK                            Zf e ecejM                            Zg e ecejO                            Zh e ecejQ                            Zi e ecejS                            Zj e eceU                    Zk e eceW                    Zl e ecejY                            Zm e ecej[                            Zn e ecej]                            Zo e ecej_                            Zp e eceja                            Zq erejs                  Zt ereju                  Zv erejw                  Zx erejy                  Zz erej{                  Z| erej}                  Z~ erej                  Z erej                  Z erej                  Z erej                  Z erej                  Z erej                  Z erej                  Z eej                  Z eej                  Z ee          Z eej                  Z ee          Z ee          Z ee          Z eej                  Z ee          Z eej                  Z eej                  Z eej                  Z e            Z e            Z e            Z e            Z e            Z e            Z e            Z eej                  Z eej                  ZdS )r/   as	  A proxy to the object bound to a context-local object. All
    operations on the proxy are forwarded to the bound object. If no
    object is bound, a ``RuntimeError`` is raised.

    :param local: The context-local object that provides the proxied
        object.
    :param name: Proxy this attribute from the proxied object.
    :param unbound_message: The error message to show if the
        context-local object is unbound.

    Proxy a :class:`~contextvars.ContextVar` to make it easier to
    access. Pass a name to proxy that attribute.

    .. code-block:: python

        _request_var = ContextVar("request")
        request = LocalProxy(_request_var)
        session = LocalProxy(_request_var, "session")

    Proxy an attribute on a :class:`Local` namespace by calling the
    local with the attribute name:

    .. code-block:: python

        data = Local()
        user = data("user")

    Proxy the top item on a :class:`LocalStack` by calling the local.
    Pass a name to proxy that attribute.

    .. code-block::

        app_stack = LocalStack()
        current_app = app_stack()
        g = app_stack("g")

    Pass a function to proxy the return value from that function. This
    was previously used to access attributes of local objects before
    that was supported directly.

    .. code-block:: python

        session = LocalProxy(lambda: request.session)

    ``__repr__`` and ``__class__`` are proxied, so ``repr(x)`` and
    ``isinstance(x, cls)`` will look like the proxied object. Use
    ``issubclass(type(x), LocalProxy)`` to check if an object is a
    proxy.

    .. code-block:: python

        repr(user)  # <User admin>
        isinstance(user, User)  # True
        issubclass(type(user), LocalProxy)  # True

    .. versionchanged:: 2.2.2
        ``__wrapped__`` is set when wrapping an object, not only when
        wrapping a function, to prevent doctest from failing.

    .. versionchanged:: 2.2
        Can proxy a ``ContextVar`` or ``LocalStack`` directly.

    .. versionchanged:: 2.2
        The ``name`` parameter can be used with any proxied object, not
        only ``Local``.

    .. versionchanged:: 2.2
        Added the ``unbound_message`` parameter.

    .. versionchanged:: 2.0
        Updated proxied attributes and methods to reflect the current
        data model.

    .. versionchanged:: 0.6.1
        The class can be instantiated with a callable.
    )	__wrappedr   r   Nr,   r   r.   r-   r   c                2   |t           nt          |          dt          t                    r!|t	          d          dt
          ffd}nt          t                    rdt
          ffd}nct          t                    rdt
          ffd}n>t                    rdt
          ffd}n t	          dt                     d	          t                              | d
           t                              | d|           d S )Nzobject is not boundz2'name' is required when proxying a 'Local' object.r   c                  X    	             S # t           $ r t                    d w xY wr&   )r7   r   )get_namer   r-   s   r   r   z0LocalProxy.__init__.<locals>._get_current_object  sF    B#8E??*% B B B&77TABs   
 )c                  J    j         } | t                     |           S r&   )r]   r   rR   r   r   r-   s    r   r   z0LocalProxy.__init__.<locals>._get_current_object  s.    i 8&777x}}$r   c                      	                                  } n# t          $ r t                    d w xY w |           S r&   )r(   LookupErrorr   r   s    r   r   z0LocalProxy.__init__.<locals>._get_current_object  sW    B))++CC" B B B&77TAB  x}}$s    3c                  *                             S r&   rM   )r   r   s   r   r   z0LocalProxy.__init__.<locals>._get_current_object  s    x(((r   zDon't know how to proxy 'z'._LocalProxy__wrappedr   )r   r   re   r   	TypeErrorr   r   r   callablerx   r    r!   )r#   r   r.   r-   r   r   s    ` ` @r   r$   zLocalProxy.__init__  s     	( HH!$''H 	43OeU## $	I V TUUUB B B B B B B B B B z** 	I% % % % % % % % % % z** 	I% % % % % % % % % % e__ 	I) ) ) ) ) ) ) ) ) GUGGGHHH4!7???4!68KLLLLLr   c                 *    t          |           j        S r&   )rx   rD   r*   s    r   <lambda>zLocalProxy.<lambda>  s    4::3E r   T)r   r~   r   c                     | j         S r&   )r   r*   s    r   r   zLocalProxy.<lambda>  s	    d7 r   )r~   r   c                 2    dt          |           j         dS )Nrv   z	 unbound>)rx   rA   r*   s    r   r   zLocalProxy.<lambda>  s    $FT

(;$F$F$F r   )r~   c                     dS )NFrM   r*   s    r   r   zLocalProxy.<lambda>$  s     r   c                     g S r&   rM   r*   s    r   r   zLocalProxy.<lambda>)  s    b r   c                      t          |           S r&   )rx   r*   s    r   r   zLocalProxy.<lambda>5  s    d4jj r   c                 "    t          ||           S r&   )re   r#   r   s     r   r   zLocalProxy.<lambda>7      E49P9P r   c                 "    t          ||           S r&   )
issubclassr   s     r   r   zLocalProxy.<lambda>8  r   r   c                      | |i |S r&   rM   )r#   r   r   s      r   r   zLocalProxy.<lambda>:  s    $$:O:O:O r   r&   )rA   rB   rC   rD   rE   rF   r   r   __annotations__rz   r   r   r   rG   rI   r$   r|   __wrapped__reprry   __str__rp   	__bytes__
__format__operatorlt__lt__le__le__eq__eq__ne__ne__gt__gt__ge__ge__hash__hash__r   __bool__r   r:   setattrr!   delattrr?   dir__dir__r   __instancecheck____subclasscheck__r3   rY   __len__length_hint__length_hint__getitem__getitem__setitem__setitem__delitem__delitem__r'   r+   next__next__reversed__reversed__contains__contains__add__add__sub__sub__mul__mul__matmul
__matmul__truediv__truediv__floordiv__floordiv__mod__mod__divmod
__divmod__pow__pow__lshift
__lshift__rshift
__rshift__and___and__xor__xor__or___or__r   __radd____rsub____rmul____rmatmul____rtruediv____rfloordiv____rmod____rdivmod____rpow____rlshift____rrshift____rand____rxor____ror__r   iadd__iadd__isub__isub__imul__imul__imatmul__imatmul__itruediv__itruediv__	ifloordiv__ifloordiv__imod__imod__ipow__ipow__ilshift__ilshift__irshift__irshift__iand__iand__ixor__ixor__ior__ior__neg__neg__pos__pos__abs__abs__invert
__invert__complex__complex__int__int__float	__float__index	__index__round	__round__mathtrunc	__trunc__floor	__floor__ceil__ceil__	__enter____exit__	__await__	__aiter__	__anext__
__aenter__	__aexit__r=   __copy__deepcopy__deepcopy__rM   r   r   r/   r/   ~  s        K KZ 5IBE**** !%6M
 ,06M 6M 6Mwz!}eZ]AJr1u<MMN6M jo6M
 C6M 
6M 6M 6M 6Mp l&E&Et  G ,77  K |FF  H l3GU##IJ\(+&&F\(+&&F\(+&&F\(+&&F\(+&&F\(+&&F|D!!H|D+=+=>>>H,w''K,w''K,w''Kl3999G (($  I %%P%PQQ$%P%PQQ|OOPPHl3G"l8#788O,x/00K,x/00K,x/00K|D!!H|D!!H<))L< 122Ll8<((Gl8<((Gl8<((Gho..J,x/00K< 122Ll8<((Gf%%Jl3Gho..Jho..Jl8=))Gl8<((G\(,''F|JJx|4455H|JJx|4455H|JJx|4455H,zz(/::;;K<

8+; < <==L LH,=!>!>??M|JJx|4455H,zz&1122K|JJsOO,,H,zz(/::;;K,zz(/::;;K|JJx}5566H|JJx|4455Hl::hl3344Gy''Hy''Hy''H)H,--K9X.//LIh011My''Hy''H)H,--K)H,--Ky''Hy''Hi%%Gl8<((Gl8<((Gl3Gho..J,w''Kl3GU##IX^,,IU##ITZ((ITZ((I|DI&&HI|~~HIIIJI|DI&&H<..LLLr   r/   )"r=   rF  r   typingrF   contextvarsr   	functoolsr   r   r   wsgir	   TYPE_CHECKING_typeshed.wsgir
   r   r   TypeVarr   r   rJ   r   rz   r   r   Genericr   rb   r|   r   r   r   r/   rM   r   r   <module>r_     s          " " " " " "       $ $ $ $ $ $       ! ! ! ! ! !? /,,,,,,............AIcNNAIcCJ/000	!67 	D 	 	 	 	F' F' F' F' F' F' F' F'RMG MG MG MG MG1 MG MG MG`DG DG DG DG DG DG DG DGNWG WG WG WG WG WG WG WGt       ,1      q    D/ D/ D/ D/ D/1 D/ D/ D/ D/ D/r   