
    ǄgW                       d dl mZ 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dZ G d d      Z G d dej6                  e         Z G d d      Z G d d      Z G d de      Zd dZ d!dZ! G d dej6                  e         Z"y)"    )annotationsN)
ContextVarpartial)update_wrapper)
attrgetter   )ClosingIterator)StartResponse)WSGIApplication)WSGIEnvironmentTF.)boundc                $    | j                          y)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__)locals    V/home/mcse/projects/flask_80/flask-venv/lib/python3.12/site-packages/werkzeug/local.pyrelease_localr      s     
    c                  ^    e Zd ZdZdZdddZddZdd	 	 	 	 	 ddZddZdd	Z	dd
Z
ddZy)Localaq  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.
    )	__storageNc                f    |t        dt        |        d      }t        j                  | d|       y )Nzwerkzeug.Local<	>.storage_Local__storage)r   idobject__setattr__selfcontext_vars     r   __init__zLocal.__init__6   s4    
 %r$xj	%JKK4!2K@r   c                f    t        | j                  j                  i       j                               S N)iterr   getitemsr!   s    r   __iter__zLocal.__iter__@   s%    DNN&&r*00233r   unbound_messagec                   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+   
LocalProxyr!   namer,   s      r   __call__zLocal.__call__C   s     $oFFr   c                :    | j                   j                  i        y r%   )r   setr)   s    r   r   zLocal.__release_local__O   s    2r   c                `    | j                   j                  i       }||v r||   S t        |      r%   )r   r'   AttributeErrorr!   r1   valuess      r   __getattr__zLocal.__getattr__R   s2    ##B'6>$<T""r   c                    | j                   j                  i       j                         }|||<   | j                   j                  |       y r%   )r   r'   copyr4   )r!   r1   valuer8   s       r   r   zLocal.__setattr__Z   s:    ##B',,.t6"r   c                    | j                   j                  i       }||v r/|j                         }||= | j                   j                  |       y t	        |      r%   )r   r'   r;   r4   r6   r7   s      r   __delattr__zLocal.__delattr___   sL    ##B'6>[[]FtNNv& &&r   r%   )r"   z#ContextVar[dict[str, t.Any]] | NonereturnNone)r?   zt.Iterator[tuple[str, t.Any]])r1   strr,   
str | Noner?   LocalProxy[t.Any]r?   r@   )r1   rA   r?   t.Any)r1   rA   r<   rE   r?   r@   )r1   rA   r?   r@   )__name__
__module____qualname____doc__	__slots__r#   r*   r2   r   r9   r   r>    r   r   r   r   #   sS      IA4 ;?
G
G-7
G	
G##
'r   r   c                  d    e Zd ZdZdZdddZddZddZddZe	dd       Z
	 ddd		 	 	 	 	 dd
Zy)
LocalStacka  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
    )_storageNc                F    |t        dt        |        d      }|| _        y )Nzwerkzeug.LocalStack<r   )r   r   rN   r    s     r   r#   zLocalStack.__init__   s)    
 %';BtH:Y%OPK#r   c                :    | j                   j                  g        y r%   )rN   r4   r)   s    r   r   zLocalStack.__release_local__   s    "r   c                    | j                   j                  g       j                         }|j                  |       | j                   j	                  |       |S )z'Add a new item to the top of the stack.)rN   r'   r;   appendr4   )r!   objstacks      r   pushzLocalStack.push   sB    !!"%**,S% r   c                    | j                   j                  g       }t        |      dk(  ry|d   }| j                   j                  |dd        |S )zjRemove the top item from the stack and return it. If the
        stack is empty, return ``None``.
        r   N)rN   r'   lenr4   )r!   rT   rvs      r   popzLocalStack.pop   sL     !!"%u:?2Y%*%	r   c                `    | j                   j                  g       }t        |      dk(  ry|d   S )z[The topmost item on the stack.  If the stack is empty,
        `None` is returned.
        r   NrW   )rN   r'   rX   )r!   rT   s     r   topzLocalStack.top   s0    
 !!"%u:?Ry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+   r.   r0   s      r   r2   zLocalStack.__call__   s     $oFFr   r%   )r"   zContextVar[list[T]] | Noner?   r@   rD   )rS   r   r?   zlist[T])r?   zT | None)r1   rB   r,   rB   r?   rC   )rF   rG   rH   rI   rJ   r#   r   rU   rZ   propertyr\   r2   rK   r   r   rM   rM   j   se    $ I$ 	 	 "&GHLGG;EG	Gr   rM   c                  F    e Zd ZdZdZ	 d		 	 	 d
dZddZddZddZddZ	y)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.1
        The ``ident_func`` was removed.

    .. 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.
    )localsNc                j    |g | _         y t        |t              r	|g| _         y t        |      | _         y r%   )ra   
isinstancer   list)r!   ra   s     r   r#   zLocalManager.__init__   s.    
 >DK&!(DKv,DKr   c                <    | j                   D ]  }t        |        y)zRelease the data in the locals for this context. Call this at
        the end of each request or use :meth:`make_middleware`.
        N)ra   r   )r!   r   s     r   cleanupzLocalManager.cleanup   s     [[ 	!E% 	!r   c                $     	 	 	 	 	 	 d fd}|S )zWrap a WSGI application so that local data is released
        automatically after the response has been sent for a request.
        c                >    t         | |      j                        S r%   )r
   rf   )environstart_responseappr!   s     r   applicationz1LocalManager.make_middleware.<locals>.application   s     #3w#?NNr   )ri   r   rj   r   r?   zt.Iterable[bytes]rK   )r!   rk   rl   s   `` r   make_middlewarezLocalManager.make_middleware   s,    
	O$	O6C	O	O
 r   c                8    t        | j                  |      |      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   rm   )r!   funcs     r   
middlewarezLocalManager.middleware   s     d2248$??r   c                `    dt        |       j                   dt        | j                         dS )N<z storages: >)typerF   rX   ra   r)   s    r   __repr__zLocalManager.__repr__   s,    4:&&'{3t{{3C2DAFFr   r%   )ra   zJNone | (Local | LocalStack[t.Any] | t.Iterable[Local | LocalStack[t.Any]])r?   r@   rD   )rk   r   r?   r   )ro   r   r?   r   r?   rA   )
rF   rG   rH   rI   rJ   r#   rf   rm   rp   ru   rK   r   r   r`   r`      sE    & I
 QU
'N
' 
	
'!

@Gr   r`   c                  j    e Zd ZdZdZ	 	 	 	 d		 	 	 	 	 	 	 	 	 d
dZddZdddZddZ	 	 	 	 	 	 	 	 ddZ	y)_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_valuer1   Nc                    t        d      r	 	 	 	 	 	 dfd}n	 	 	 	 	 	 dfd}nd }|| _        || _        || _        || _        y )N__get__c                :    j                  |t        |            S r%   r~   rt   instancerS   fs     r   ry   z%_ProxyLookup.__init__.<locals>.bind_f  s     yyd3i00r   c                    t        |      S r%   r   r   s     r   ry   z%_ProxyLookup.__init__.<locals>.bind_f%  s     q#&r   r   rC   rS   rE   r?   zt.Callable[..., t.Any])hasattrry   rz   r|   r{   )r!   r   rz   r|   r{   ry   s    `    r   r#   z_ProxyLookup.__init__  st     1i 1+1271'1
 ]'+'27''' F &r   c                    || _         y r%   r1   )r!   ownerr1   s      r   __set_name__z_ProxyLookup.__set_name__3  s	    	r   c                b   || j                   | j                   S | S 	 |j                         }| j                  | j                  ||      S t        || j                        S # t        $ rC | j                   | j                  j	                  ||      }| j
                  r	 |       cY S |cY S w xY wr%   )	r|   _get_current_objectRuntimeErrorrz   r~   r{   ry   getattrr1   )r!   r   r   rS   rz   s        r   r~   z_ProxyLookup.__get__6  s    +'''K	..0C ;;";;x--sDII&&!  	}}$}},,Xu=H||  z!O	s   A" "AB.)B.-B.c                     d| j                    S )Nzproxy r   r)   s    r   ru   z_ProxyLookup.__repr__Q  s    		{##r   c                D     | j                  |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   argskwargss       r   r2   z_ProxyLookup.__call__T  s%     6t||Hd8n5tFvFFr   )NNNF)
r   t.Callable[..., t.Any] | Nonerz   -t.Callable[[LocalProxy[t.Any]], t.Any] | Noner|   zt.Any | Noner{   boolr?   r@   )r   rC   r1   rA   r?   r@   r%   )r   rC   r   ztype | Noner?   rE   rv   )r   rC   r   rE   r   rE   r?   rE   )
rF   rG   rH   rI   rJ   r#   r   r~   ru   r2   rK   r   r   rx   rx      s     II ,0BF$( (  @  "	 
   
 D'6$G)G27GCHG	Gr   rx   c                  8     e Zd ZdZdZ	 	 d	 	 	 	 	 d 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.
    rK   c                B    t         |   |       dfd}|| _        y )Nc                J     d fd}|j                  |t        |            S )Nc                     | |       S r%   rK   )r!   otherr   r   s     r   i_opz0_ProxyIOp.__init__.<locals>.bind_f.<locals>.i_opn  s    $r   )r!   rE   r   rE   r?   rC   r   )r   rS   r   r   s   `  r   ry   z"_ProxyIOp.__init__.<locals>.bind_fm  s      <<T#Y//r   r   )superr#   ry   )r!   r   rz   ry   	__class__s    `  r   r#   z_ProxyIOp.__init__f  s"    
 	H%	0 r   )NN)r   r   rz   r   r?   r@   )rF   rG   rH   rI   rJ   r#   __classcell__)r   s   @r   r   r   _  s>     I ,0BF( @ 
	 r   r   c                D     d fd}t        j                  t        |      S )z5Swap the argument order to turn an l-op into an r-op.c                     ||       S r%   rK   )rS   r   ops     r   r_opz_l_to_r_op.<locals>.r_opz  s    %~r   )rS   rE   r   rE   r?   rE   )tcastr   )r   r   s   ` r   
_l_to_r_opr   w  s     66!T?r   c                    | S r%   rK   )os    r   	_identityr     s    Hr   c                  `   e Zd ZU dZdZded<   	 	 ddd	 	 	 	 	 	 	 d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j2                        Z eej6                        Z ee      Z eed       Z  ee!      Z" ee#      Z$ ee%      Z& ee'd       Z( ed d	      Z) ed       Z* ed       Z+ ed       Z, ee-      Z. eej^                        Z0 eejb                        Z2 eejf                        Z4 eejj                        Z6 ee7      Z8 ee9      Z: ee;      Z< eejz                        Z> eej~                        Z@ eej                        ZB eej                        ZD eej                        ZF eej                        ZH eej                        ZJ eej                        ZL eeM      ZN eeO      ZP eej                        ZR eej                        ZT eej                        ZV eej                        ZX eej                        ZZ e e[ej~                              Z\ e e[ej                              Z] e e[ej                              Z^ e e[ej                              Z_ e e[ej                              Z` e e[ej                              Za e e[ej                              Zb e e[eM            Zc e e[eO            Zd e e[ej                              Ze e e[ej                              Zf e e[ej                              Zg e e[ej                              Zh e e[ej                              Zi ejej                        Zl ejej                        Zn ejej                        Zp ejej                        Zr ejej                        Zt ejej                        Zv ejej                        Zx ejej                        Zz ejej                        Z| ejej                        Z~ ejej                        Z ejej                        Z eje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ej0                        Z eej4                        Z eej8                        Z e       Z e       Z e       Z e       Z e       Z e       Z e       Z eejJ                        Z eejN                        Zy)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   zt.Callable[[], T]r   Nr+   c                  |t         nt        |      dt        t              r|t	        d      dfd}n]t        t
              r	dfd}nDt        t              r	dfd}n+t              rdfd}nt	        dt               d      t        j                  | d	       t        j                  | d
|       y )Nzobject is not boundz2'name' is required when proxying a 'Local' object.c                 H    	         S # t         $ r t              d w xY wr%   )r6   r   )get_namer   r,   s   r   r   z0LocalProxy.__init__.<locals>._get_current_object  s1    B#E?*% B&7TABs    !c                 F    j                   } | t               |       S r%   )r\   r   rS   r   r   r,   s    r   r   z0LocalProxy.__init__.<locals>._get_current_object  s&    ii;&77}$r   c                 h    	 j                         }  |       S # t        $ r t              d w xY wr%   )r'   LookupErrorr   r   s    r   r   z0LocalProxy.__init__.<locals>._get_current_object  s@    B))+C  }$ # B&7TABs    1c                                     S r%   rK   )r   r   s   r   r   z0LocalProxy.__init__.<locals>._get_current_object  s    ((r   zDon't know how to proxy 'z'._LocalProxy__wrappedr   )r?   r   )r   r   rc   r   	TypeErrorrM   r   callablert   r   r   )r!   r   r1   r,   r   r   s    ` ` @r   r#   zLocalProxy.__init__  s     < H!$'H"3OeU#| TUUB B z*% % z*% % e_) 7U}BGHH4!7?4!68KLr   c                ,    t        |       j                  S r%   )rt   rI   r)   s    r   <lambda>zLocalProxy.<lambda>  s    4:3E3E r   T)r|   rz   r{   c                    | j                   S r%   )r   r)   s    r   r   zLocalProxy.<lambda>  s    d77 r   )rz   r{   c                4    dt        |       j                   dS )Nrr   z	 unbound>)rt   rF   r)   s    r   r   zLocalProxy.<lambda>  s    aT
(;(;'<I$F r   )rz   c                     y)NFrK   r)   s    r   r   zLocalProxy.<lambda>+  s    r   c                    g S r%   rK   r)   s    r   r   zLocalProxy.<lambda>0  s    b r   c                    t        |       S r%   )rt   r)   s    r   r   zLocalProxy.<lambda>;  s
    4: r   c                    t        ||       S r%   )rc   r!   r   s     r   r   zLocalProxy.<lambda><      E49P r   c                    t        ||       S r%   )
issubclassr   s     r   r   zLocalProxy.<lambda>=  r   r   c                     | |i |S r%   rK   )r!   r   r   s      r   r   zLocalProxy.<lambda>?  s    $:O:O r   r%   )r   z9ContextVar[T] | Local | LocalStack[T] | t.Callable[[], T]r1   rB   r,   rB   r?   r@   )rF   rG   rH   rI   rJ   __annotations__r#   rx   __wrapped__reprru   rA   __str__bytes	__bytes__
__format__operatorlt__lt__le__le__eq__eq__ne__ne__gt__gt__ge__ge__hash__hash__r   __bool__r   r9   setattrr   delattrr>   dir__dir__r   __instancecheck____subclasscheck__r2   rX   __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__rK   r   r   r/   r/     sV   KZ 5I**  6M
 '+6MH6M 6M
 $6M 
6Mp &EtG 7K
 FH 3GU#IJ(++&F(++&F(++&F(++&F(++&F(++&FD!HD+=>Hw'Kw'Kw'K39G &=tLI$%PQ$%PQOPH3G"8#7#78Ox//0Kx//0Kx//0KD!HD!H)L 1 12L8<<(G8<<(G8<<(Ghoo.Jx//0K 1 12L8<<(Gf%J3Ghoo.Jhoo.J8==)G8<<(G(,,'FJx||45HJx||45HJx||45Hz(//:;K
8+;+; <=L H,=,=!>?MJx||45Hz&12KJsO,Hz(//:;Kz(//:;KJx}}56HJx||45H:hll34G'H'H'HH,,-KX../Lh001M'H'HH,,-KH,,-K'H'H%G8<<(G8<<(G3Ghoo.Jw'K3GU#IX^^,IU#ITZZ(ITZZ(IDII&HI~HIIIJIDII&H.Lr   r/   )r   zLocal | LocalStack[t.Any]r?   r@   )r   r   r?   r   )r   r   r?   r   )#
__future__r   r;   rF  r   typingr   contextvarsr   	functoolsr   r   r   wsgir
   TYPE_CHECKING_typeshed.wsgir   r   r   TypeVarr   CallableAnyr   r   r   GenericrM   r`   rx   r   r   r   r/   rK   r   r   <module>rb     s    "     "  $  !??,..AIIcNAIIcCJ/0	D' D'NMG1 MG`BG BGJ]G ]G@ 0C/1 C/r   