4V=jVddlmZddlZddlZddlZddlZddlmZddl m Z ddl m Z ddlm Z ddl mZejrdd lmZdd lmZdd lmZejd Zejd ejdejfZd%dZGddZGddejeZGddZGddZGddeZd&d Z d'd"Z!Gd#d$ejeZ"dS)() annotationsN) ContextVarpartial)update_wrapper) attrgetter)ClosingIterator) StartResponse)WSGIApplication)WSGIEnvironmentTF.)boundlocalLocal | LocalStack[t.Any]returnNonec.|dS)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__)rs .storage_Local__storage)ridobject __setattr__selfrs r__init__zLocal.__init__6sH   %%Jr$xx%J%J%JKKK4!2K@@@@@rt.Iterator[tuple[str, t.Any]]ctt|jiSN)iterr!getitemsr&s r__iter__zLocal.__iter__@s,DN&&r**0022333runbound_messagenamestrr1 str | NoneLocalProxy[t.Any]c&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. r0 LocalProxyr&r2r1s r__call__zLocal.__call__Cs$oFFFFrc:|jidSr*)r!setr.s rrzLocal.__release_local__Os 2rt.Anycl|ji}||vr||St|r*)r!r,AttributeErrorr&r2valuess r __getattr__zLocal.__getattr__Rs8##B'' 6>>$< T"""rvaluec|ji}|||<|j|dSr*)r!r,copyr<)r&r2rCrAs rr$zLocal.__setattr__ZsH##B'',,..t  6"""""rc|ji}||vr3|}||=|j|dSt |r*)r!r,rEr<r?r@s r __delattr__zLocal.__delattr___s^##B'' 6>>[[]]Ft N  v & & & & & && &rr*)rrrr)rr()r2r3r1r4rr5rr)r2r3rr=)r2r3rCr=rr)r2r3rr) __name__ __module__ __qualname____doc__ __slots__r'r/r:rrBr$rGrrrr#s IAAAAA4444;? G G G G G G######## ''''''rrcbeZdZdZdZdddZdd Zdd ZddZe ddZ dddddZ dS) LocalStackaCreate 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 )_storageNrContextVar[list[T]] | NonerrcX| tdt|d}||_dS)Nzwerkzeug.LocalStackDKKK  & & '!(DKKKv,,DKKKrc8|jD]}t|dS)zRelease the data in the locals for this context. Call this at the end of each request or use :meth:`make_middleware`. N)rgr)r&rs rcleanupzLocalManager.cleanups0[ ! !E %  ! !rappr cdfd }|S) zWrap a WSGI application so that local data is released automatically after the response has been sent for a request. environr start_responser rt.Iterable[bytes]cBt||jSr*)r rm)rprqrnr&s r applicationz1LocalManager.make_middleware..applications##33w#?#?NN Nr)rpr rqr rrrrN)r&rnrts`` rmake_middlewarezLocalManager.make_middlewares6  O O O O O O O rfunccHt|||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): ... )rru)r&rvs r middlewarezLocalManager.middlewares"d22488$???rr3c\dt|jdt|jdS)N)typerIr^rgr.s r__repr__zLocalManager.__repr__s.F4::&FF3t{3C3CFFFFrr*)rgrhrrrH)rnr rr )rvr rr rr3) rIrJrKrLrMr'rmrurxr}rNrrrfrfs&I QU ' ' ' ' '!!!!     @ @ @ @GGGGGGrrfcJeZdZdZdZ dddZddZd d!dZd"dZd#dZ dS)$ _ProxyLookupaDescriptor 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_ffallbackis_attr class_valuer2NFft.Callable[..., t.Any] | Noner-t.Callable[[LocalProxy[t.Any]], t.Any] | Noner t.Any | Nonerboolrrctdrd fd }n d fd }nd}||_||_||_||_dS) N__get__instancer5rUr=rt.Callable[..., t.Any]cJ|t|Sr*rr|rrUrs rrz%_ProxyLookup.__init__..bind_fsyyd3ii000rc$t|Sr*rrs rrz%_ProxyLookup.__init__..bind_f%sq#&rrr5rUr=rr)hasattrrrrr)r&rrrrrs ` rr'z_ProxyLookup.__init__s 1i   1 1 1 1 1 1 1 ] ' ' ' ' ' ' 'F   & rownerr5r2r3c||_dSr*r2)r&rr2s r __set_name__z_ProxyLookup.__set_name__3s  rr type | Noner=cF||j|jS|S |}nH#t$r;|j|j||}|jr |cYS|cYSwxYw|j|||St||jSr*) r_get_current_object RuntimeErrorrrrrgetattrr2)r&rrrUrs rrz_ProxyLookup.__get__6s  +''K ..00CC   }$},,Xu==H| " xzz!!!OOO  ; ";;x-- -sDI&&&s)>A.)A.-A.cd|jS)Nzproxy rr.s rr}z_ProxyLookup.__repr__Qs# ###rargskwargscT||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&rrrs rr:z_ProxyLookup.__call__Ts-6t||Hd8nn55tFvFFFr)NNNF) rrrrrrrrrr)rr5r2r3rrr*)rr5rrrr=r~)rr5rr=rr=rr=) rIrJrKrLrMr'rrr}r:rNrrrrs  II,0BF$(     D'''''6$$$$GGGGGGrrc.eZdZdZdZ d d fd ZxZS) _ProxyIOpzLook up an augmented assignment method on a proxied object. The method is wrapped to return the proxy instead of the object. rNNrrrrrrcht|dfd }||_dS) Nrr5rUr=rrcZdfd }||t|S)Nr&r=otherrr5c ||Sr*rN)r&rrrs ri_opz0_ProxyIOp.__init__..bind_f..i_opns$r)r&r=rr=rr5r)rrUrrs` rrz"_ProxyIOp.__init__..bind_fmsC       <<T#YY// /rr)superr'r)r&rrr __class__s ` rr'z_ProxyIOp.__init__fsK H%%% 0 0 0 0 0 0 r)NN)rrrrrr)rIrJrKrLrMr' __classcell__)rs@rrr_s[I,0BFrropcDdfd }tjt|S)z5Swap the argument order to turn an l-op into an r-op.rUr=rrc||Sr*rN)rUrrs rr_opz_l_to_r_op..r_opzsr%~~r)rUr=rr=rr=)tcastr)rrs` r _l_to_r_oprws3 6!T??roc|Sr*rN)rs r _identityrs Hrc eZdZUdZdZded< dddddZeeddZeddZee dZ ee Z ee ZeZeejZeejZeejZeejZeejZeejZeeZeedZ ee!Z"ee#Z$ee%Z&ee'dZ(eddZ)edZ*edZ+edZ,ee-Z.eej/Z0eej1Z2eej3Z4eej5Z6ee7Z8ee9Z:ee;Z<eej=Z>eej?Z@eejAZBeejCZDeejEZFeejGZHeejIZJeejKZLeeMZNeeOZPeejQZReejSZTeejUZVeejWZXeejYZZee[ej?Z\ee[ejAZ]ee[ejCZ^ee[ejEZ_ee[ejGZ`ee[ejIZaee[ejKZbee[eMZcee[eOZdee[ejQZeee[ejSZfee[ejUZgee[ejWZhee[ejYZiejejkZlejejmZnejejoZpejejqZrejejsZtejejuZvejejwZxejejyZzejej{Z|ejej}Z~ejejZejejZejejZeejZeejZeeZeejZeeZeeZeeZeejZeeZeejZeejZeejZeZeZeZeZeZeZeZeejZeejZdS)r8as 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) # 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. ) __wrappedrzt.Callable[[], T]rNr0r9ContextVar[T] | Local | LocalStack[T] | t.Callable[[], T]r2r4r1rrc|tnt|dttr|t dd fd }nstt r d fd }nUtt r d fd }n7trd fd }n t d td t |d t |d |dS)Nzobject is not boundz2'name' is required when proxying a 'Local' object.rrcX S#t$rtdwxYwr*)r?r)get_namerr1srrz0LocalProxy.__init__.._get_current_objectsFB#8E??*%BBB&77TABs )cJj}|t|Sr*)rbrrUrrr1s rrz0LocalProxy.__init__.._get_current_objects+i;&777x}}$rc }n#t$rtdwxYw|Sr*)r, LookupErrorrrs rrz0LocalProxy.__init__.._get_current_objectsWB))++CC"BBB&77TAB x}}$s3c*Sr*rN)rrsrrz0LocalProxy.__init__.._get_current_object sx(((rzDon't know how to proxy 'z'._LocalProxy__wrappedr)rr) rrrjr TypeErrorrPrcallabler|r#r$)r&rr2r1rrs ` ` @rr'zLocalProxy.__init__s < HH!$''H  "3O eU # #$ I| TUUU B B B B B B B B B z * * I % % % % % % % % %z * * I % % % % % % % % %e__ I ) ) ) ) ) ) ) )GU GGGHH H4!7???4!68KLLLLLrc*t|jSr*)r|rLr.s rzLocalProxy.s4::3ErT)rrrc|jSr*)rr.s rrzLocalProxy.s d7r)rrc2dt|jdS)Nrzz unbound>)r|rIr.s rrzLocalProxy.s$FT (;$F$F$Fr)rcdS)NFrNr.s rrzLocalProxy.+srcgSr*rNr.s rrzLocalProxy.0sbrc t|Sr*)r|r.s rrzLocalProxy.;s 4::rc"t||Sr*)rjr&rs rrzLocalProxy.<E49P9Prc"t||Sr*) issubclassrs rrzLocalProxy.=rrc||i|Sr*rN)r&rrs rrzLocalProxy.?s$$:O:O:Orr*)rrr2r4r1r4rr)rIrJrKrLrM__annotations__r'r __wrapped__reprr}r3__str__bytes __bytes__ __format__operatorlt__lt__le__le__eq__eq__ne__ne__gt__gt__ge__ge__hash__hash__r__bool__rrBsetattrr$delattrrGdir__dir__r__instancecheck____subclasscheck__r:r^__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__riadd__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__mathtrunc __trunc__floor __floor__ceil__ceil__ __enter____exit__ __await__ __aiter__ __anext__ __aenter__ __aexit__rE__copy__deepcopy __deepcopy__rNrrr8r8s*KKZ5I**** 6M '+ 6M6M6M6M6M6Mpl&E&EtG,77K | FFHl3G U##IJ \(+ & &F \(+ & &F \(+ & &F \(+ & &F \(+ & &F \(+ & &F|D!!H|D+=+=>>>H,w''K,w''K,w''Kl3999G &=&=tLLLI$ %P%PQQ$ %P%PQQ|OOPPHl3G"l8#788O,x/00K,x/00K,x/00K|D!!H|D!!H<))L< 122Ll8<((Gl8<((Gl8<((Gho..J,x/00K< 122Ll8<((Gf%%Jl3Gho..Jho..Jl8=))Gl8<((G \(, ' 'F|JJx|4455H|JJx|4455H|JJx|4455H,zz(/::;;K< 8+; < <==L LH,=!>!>??M|JJx|4455H,zz&1122K|JJsOO,,H,zz(/::;;K,zz(/::;;K|JJx}5566H|JJx|4455Hl::hl3344Gy''Hy''Hy''H)H,--K9X.//LIh011My''Hy''H)H,--K)H,--Ky''Hy''Hi %%Gl8<((Gl8<((Gl3Gho..J,w''Kl3G U##I X^,,I U##I TZ((I TZ((I|DI&&H I|~~H I I IJ I|DI&&H< ..LLLrr8)rrrr)rrrr)rrrr)# __future__rrErRrtypingr contextvarsr functoolsrrrwsgir TYPE_CHECKING_typeshed.wsgir r r TypeVarrCallableAnyrrrGenericrPrfrrrrr8rNrrrns"""""" """"""$$$$$$!!!!!!?/,,,,,,............ AIcNN AIcCJ/000    D'D'D'D'D'D'D'D'NMGMGMGMGMG1MGMGMG`BGBGBGBGBGBGBGBGJ]G]G]G]G]G]G]G]G@ 0    C/C/C/C/C/1C/C/C/C/C/r