idldZddlZddlZddlZddlZddlmZddlmZddl m Z  ddl m Z n#e $rGddZ YnwxYwgd Z e d gz Z Gd d ejZGd dejZGddeZGddeZGddZGddeeeZGddeeeZdZdZGddeZGddeZGdd eZGd!d"eZGd#d$eZGd%d&eZGd'd(ZGd)d*eeZ Gd+d,eeZ!Gd-d.eeZ"Gd/d e Z#dS)0zAcontextlib2 - backports and enhancements to the contextlib moduleN)dequewraps MethodType) GenericAliasceZdZdS)rN)__name__ __module__ __qualname__I/opt/cloudlinux/venv/lib/python3.11/site-packages/contextlib2/__init__.pyrrs rr) asynccontextmanagercontextmanagerclosing nullcontextAbstractContextManagerAbstractAsyncContextManagerAsyncExitStackContextDecorator ExitStackredirect_stdoutredirect_stderrsuppressaclosing ContextStackcdeZdZdZeeZdZej dZ edZ dS)rz,An abstract base class for context managers.c|Sz0Return `self` upon entering the runtime context.r selfs r __enter__z AbstractContextManager.__enter__"s rcdSz9Raise any exception triggered within the runtime context.Nr r"exc_type exc_value tracebacks r__exit__zAbstractContextManager.__exit__&s trcN|turtj|ddStS)Nr#r*)r_collections_abc_check_methodsNotImplementedclsCs r__subclasshook__z'AbstractContextManager.__subclasshook__+s) ( ( (#21k:NN NrN) r r r __doc__ classmethodr__class_getitem__r#abcabstractmethodr*r2r rrrrss66# L11 [rrcdeZdZdZeeZdZej dZ edZ dS)rz9An abstract base class for asynchronous context managers.c K|Sr r r!s r __aenter__z&AbstractAsyncContextManager.__aenter__8s  rc KdSr%r r&s r __aexit__z%AbstractAsyncContextManager.__aexit__<s trcN|turtj|ddStS)Nr:r<)rr,r-r.r/s rr2z,AbstractAsyncContextManager.__subclasshook__As3 - - -#21l3>@@ @rN) r r r r3r4rr5r:r6r7r<r2r rrrr2ssCC# L11 [rrc$eZdZdZdZdZdZdS)rzJA base class or mixin that enables context managers to work as decorators.c^tjdt|S)aReturns the context manager used to actually wrap the call to the decorated function. The default implementation just returns *self*. Overriding this method allows otherwise one-shot context managers like _GeneratorContextManager to support use as decorators via implicit recreation. DEPRECATED: refresh_cm was never added to the standard library's ContextDecorator API z2refresh_cm was never added to the standard library)warningswarnDeprecationWarning _recreate_cmr!s r refresh_cmzContextDecorator.refresh_cmLs0  J( * * *  """rc|S)a6Return a recreated instance of self. Allows an otherwise one-shot context manager like _GeneratorContextManager to support use as a decorator via implicit recreation. This is a private interface just for _GeneratorContextManager. See issue #11647 for details. r r!s rrCzContextDecorator._recreate_cm]s  rc@tfd}|S)Ncp5|i|cdddS#1swxYwYdSNrCargskwdsfuncr"s rinnerz(ContextDecorator.__call__..innerjs""$$ + +tT*T** + + + + + + + + + + + + + + + + + +s +//rr"rMrNs`` r__call__zContextDecorator.__call__is9 t + + + + +  + rN)r r r r3rDrCrPr rrrrIsGPP###"   rrceZdZdZdZdZdS)AsyncContextDecoratorzPA base class or mixin that enables async context managers to work as decorators.c|S)z-Return a recreated instance of self. r r!s rrCz"AsyncContextDecorator._recreate_cmts  rc@tfd}|S)NcK4d{V|i|d{Vcdddd{VS#1d{VswxYwYdSrHrIrJs rrNz-AsyncContextDecorator.__call__..innerzs((** 1 1 1 1 1 1 1 1!T40400000000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1s? A  A rrOs`` rrPzAsyncContextDecorator.__call__ys9 t 1 1 1 1 1  1 rN)r r r r3rCrPr rrrRrRqs8VV rrRceZdZdZdZdS)_GeneratorContextManagerBasezBShared functionality for @contextmanager and @asynccontextmanager.c||i||_|||c|_|_|_t |dd}|t |j}||_dS)Nr3)genrMrKrLgetattrtyper3)r"rMrKrLdocs r__init__z%_GeneratorContextManagerBase.__init__s]4&&&*.d' 49didIt,, ;t**$C rN)r r r r3r]r rrrWrWs)LLrrWc$eZdZdZdZdZdZdS)_GeneratorContextManagerz%Helper for @contextmanager decorator.cN||j|j|jSrH __class__rMrKrLr!s rrCz%_GeneratorContextManager._recreate_cm ~~diDI>>>rct|`|`|` t|jS#t $rt ddwxYwNzgenerator didn't yield)rKrLrMnextrY StopIteration RuntimeErrorr!s rr#z"_GeneratorContextManager.__enter__sU Ity$) C>> ! C C C788d B Cs7c|5 t|jtd#t$rYdSwxYw| |} |j|||nj#t$r}||ucYd}~Sd}~wt$r(}||urYd}~dS|tur|j|urYd}~dSd}~wt jd|urYdSxYwtd)Ngenerator didn't stopFz#generator didn't stop after throw())rfrYrhrgthrow __cause__sysexc_info)r"r[valuer)excs rr*z!_GeneratorContextManager.__exit__sC < <TX##:;;;!   uu  } tUI6666  ( ( (%'''''''   %<< 55555=((S]e-C-C 55555 <>>!$-- 55DEE EsH' 55A"" C ,A5/C 5 C B% B%$B%% C C N)r r r r3rCr#r*r rrr_r_sQ0/??? CCC-F-F-F-F-Frr_c$eZdZdZdZdZdZdS)_AsyncGeneratorContextManagerz Helper for @asynccontextmanager.cN||j|j|jSrHrar!s rrCz*_AsyncGeneratorContextManager._recreate_cmrcrcK |jd{VS#t$rtddwxYwre)rY __anext__StopAsyncIterationrhr!s rr:z(_AsyncGeneratorContextManager.__aenter__sb C++-------- -! C C C788d B Cs#>cK|@ |jd{Vtd#t$rYdSwxYw| |} |j|||d{Vtd#t$r}||ucYd}~Sd}~wt$r;}||urYd}~dSt |t tfr|j|urYd}~dSd}~wt$r}||urYd}~dSd}~wwxYw)Nrjz$generator didn't stop after athrow()F) rYrvrhrwathrow isinstancergrm BaseException)r"typrpr)rqs rr<z'_AsyncGeneratorContextManager.__aexit__s ; <h((*********##:;;;&     } hooc5)<<<<<<<<<"#IJJJ% ( ( (%'''''''   %<< 55555em5G%HII%}--$uuuuu    e##$##### sQ4 AA1B C6 BC6 C6#C-%CC C6&C11C6N)r r r r3rCr:r<r rrrsrssL+*??? CCC !!!!!rrsc<tfd}|S)a@contextmanager decorator. Typical usage: @contextmanager def some_generator(): try: yield finally: This makes this: with some_generator() as : equivalent to this: try: = finally: c&t||SrH)r_rKrLrMs rhelperzcontextmanager..helper's'dD999rrrMrs` rrr s36 4[[::::[: Mrc<tfd}|S)a@asynccontextmanager decorator. Typical usage: @asynccontextmanager async def some_async_generator(): try: yield finally: This makes this: async with some_async_generator() as : equivalent to this: try: = finally: c&t||SrH)rsrs rrz#asynccontextmanager..helperHs,T4>>>rrrs` rrr-s36 4[[????[? Mrc$eZdZdZdZdZdZdS)ra2Context to automatically close something at the end of a block. Code like this: with closing(.open()) as f: is equivalent to this: f = .open() try: finally: f.close() c||_dSrHthingr"rs rr]zclosing.__init___  rc|jSrHrr!s rr#zclosing.__enter__as zrc8|jdSrH)rcloser"ros rr*zclosing.__exit__cs rNr r r r3r]r#r*r rrrrNsK rrc$eZdZdZdZdZdZdS)raAsync context manager for safely finalizing an asynchronously cleaned-up resource such as an async generator, calling its ``aclose()`` method. Code like this: async with aclosing(.fetch()) as agen: is equivalent to this: agen = .fetch() try: finally: await agen.aclose() c||_dSrHrrs rr]zaclosing.__init__yrrcK|jSrHrr!s rr:zaclosing.__aenter__{szrcHK|jd{VdSrH)raclosers rr<zaclosing.__aexit__}s2j!!!!!!!!!!!rN)r r r r3r]r:r<r rrrrgsK""""""rrc$eZdZdZdZdZdZdS)_RedirectStreamNc"||_g|_dSrH) _new_target _old_targets)r" new_targets rr]z_RedirectStream.__init__s%rc|jtt|jt t|j|j|jSrH)rappendrZrn_streamsetattrrr!s rr#z_RedirectStream.__enter__sE   dl!;!;<<<T\4#3444rcjtt|j|jdSrH)rrnrrpopr"exctypeexcinstexctbs rr*z_RedirectStream.__exit__s+T\4#4#8#8#:#:;;;;;r)r r r rr]r#r*r rrrrsFG    <<<<._exit_wrappers Hd #d # # # # #rr rKrLrrs`` @r_create_cb_wrapperz!_BaseExitStack._create_cb_wrappers94 $ $ $ $ $ $ $rc,t|_dSrH)r_exit_callbacksr!s rr]z_BaseExitStack.__init__s$wwrcrt|}|j|_t|_|S)z@Preserve the context stack by transferring it to a new instance.)r[rr)r" new_stacks rpop_allz_BaseExitStack.pop_alls0DJJLL $($8 !$wwrct|} |j}|||n%#t$r||YnwxYw|S)aRegisters a callback with the standard __exit__ method signature. Can suppress exceptions the same way __exit__ method can. Also accepts any object with an __exit__ method (registering a call to the method instead of the object itself). )r[r* _push_cm_exitAttributeError_push_exit_callbackr"exit_cb_type exit_methods rpushz_BaseExitStack.pushsu:: 2"+K   t[ 1 1 1 1  + + +  $ $T * * * * * +  s/AAct|}|j}||}||||S)zEnters the supplied context manager. If successful, also pushes its __exit__ method as a callback and returns the result of the __enter__ method. )r[r*r#rr"r_cm_type_exitresults r enter_contextz_BaseExitStack.enter_contextsE88!##B'' 2u%%% rc, |^}}}n_#t$rR}t|dd}d|}t |dd}~wwxYw|j|g|Ri|}||_|||S)z\Registers an arbitrary callback and arguments. Cannot suppress exceptions. ("Not enough positional arguments {}N) ValueErrorstr partitionformat TypeErrorr __wrapped__rrKrLr"rrq exc_detailsmsgrs rrz_BaseExitStack.callbacks  +$( !D(TT + + +c((,,S11!4K6==kJJCC..d * +0/H4HHH4HH %- !   /// A$A AA$c^|||}||ddS)z;Helper to correctly register callbacks to __exit__ methods.TN)rrr"rrrs rrz_BaseExitStack._push_cm_exits311"g>>    55555rTc>|j||fdSrH)rr)r"ris_syncs rrz"_BaseExitStack._push_exit_callbacks$ ##Wh$788888rN)T)r r r r3 staticmethodrrr]rrrrrrr rrrrs88''\'\'''(   (666 999999rrc$eZdZdZdZdZdZdS)raContext manager for dynamic management of a stack of exit callbacks. For example: with ExitStack() as stack: files = [stack.enter_context(open(fname)) for fname in filenames] # All opened files will automatically be closed at the end of # the with statement, even if attempts to open files later # in the list raise an exception. c|SrHr r!s rr#zExitStack.__enter__+s rc |ddu}tjd fd}d}d}|jrj|j\}}|sJ ||rd}d}d}n6#tj}||d|dd}|}YnxYw|jj|r2 |dj} |d#t $r| |d_wxYw|o|S)NrrkcF |j}||urdS||urn|}||_dSrH __context__new_excold_exc exc_context frame_excs r_fix_exception_contextz2ExitStack.__exit__.._fix_exception_context4M &%1 '))F&+*B*B% &#*G   rFTNNNrnrorrrr{) r"r received_excrsuppressed_exc pending_raisercbnew_exc_details fixed_ctxrs @rr*zExitStack.__exit__.sA"1~T1 LNN1%  * * * * *  " ..2244KGRNNN .2{#5%)N$)M"4K ."%,..&&q'9;q>JJJ $ - " .   (N6 !!n$    -6 A* ..s A""1B"B77Cc4|ddddSz%Immediately unwind the context stack.N)r*r!s rrzExitStack.close_s dD$'''''rN)r r r r3r#r*rr rrrr sL//////b(((((rrcheZdZdZedZedZdZdZdZ dZ dZ d Z d Z d S) raAsync context manager for dynamic management of a stack of exit callbacks. For example: async with AsyncExitStack() as stack: connections = [await stack.enter_async_context(get_connection()) for i in range(5)] # All opened connections will automatically be released at the # end of the async with statement, even if attempts to open a # connection later in the list raise an exception. c"t||SrHrrs r_create_async_exit_wrapperz)AsyncExitStack._create_async_exit_wrapperrrrc"^fd}|S)Nc(Kid{VdSrHr rs rrz>AsyncExitStack._create_async_cb_wrapper.._exit_wrapperzs4(D)D)) ) ) ) ) ) ) ) ) )rr rs`` @r_create_async_cb_wrapperz'AsyncExitStack._create_async_cb_wrappervs94 * * * * * * *rcKt|}|j}||d{V}||||S)zEnters the supplied async context manager. If successful, also pushes its __aexit__ method as a callback and returns the result of the __aenter__ method. N)r[r<r:_push_async_cm_exitrs renter_async_contextz"AsyncExitStack.enter_async_context~s[ 88"**2........   U+++ rct|} |j}|||n&#t$r||dYnwxYw|S)a#Registers a coroutine function with the standard __aexit__ method signature. Can suppress exceptions the same way __aexit__ method can. Also accepts any object with an __aexit__ method (registering a call to the method instead of the object itself). F)r[r<rrrrs rpush_async_exitzAsyncExitStack.push_async_exitsw:: 8",K  $ $T; 7 7 7 7  2 2 2  $ $T5 1 1 1 1 1 2  s/ AAc. |^}}}n_#t$rR}t|dd}d|}t |dd}~wwxYw|j|g|Ri|}||_||d|S)zfRegisters an arbitrary coroutine function and arguments. Cannot suppress exceptions. rrrNF)rrrrrrrrrs rpush_async_callbackz"AsyncExitStack.push_async_callbacks  +$( !D(TT + + +c((,,S11!4K6==kJJCC..d * +65hNNNNNN %- !   666rcDK|dddd{VdSr)r<r!s rrzAsyncExitStack.acloses4nnT4...........rc^|||}||ddS)zLHelper to correctly register coroutine function to __aexit__ method.FN)rrrs rrz"AsyncExitStack._push_async_cm_exits577GDD    66666rc K|SrHr r!s rr:zAsyncExitStack.__aenter__s  rc K|ddu}tjd fd}d}d}|jrv|j\}} |r||}n ||d{V}|rd}d}d}n6#tj} || d|dd}| }YnxYw|jv|r2 |dj} |d#t $r| |d_wxYw|o|S)NrrkcF |j}||urdS||urn|}||_dSrHrrs rrz8AsyncExitStack.__aexit__.._fix_exception_contextrrFTrr) r"rrrrrrr cb_suppressrrrs @rr<zAsyncExitStack.__aexit__si"1~T1 LNN1%  * * * * *  " ..2244KGR .9"$"k"2KK(*K(8"8"8"8"8"8"8K5%)N$)M"4K ."%,..&&q'9;q>JJJ $ - #" .$   (N6 !!n$    -6 A* ..sA001B#0CCN)r r r r3rrrrr r rrr:r<r rrrres  ''\'\   $(///777 3/3/3/3/3/rrc2eZdZdZddZdZdZdZdZdS) raOContext manager that does no additional processing. Used as a stand-in for a normal context manager, when a particular block of code is only sometimes used with a normal context manager: cm = optional_cm if condition else nullcontext() with cm: # Perform operation, using optional_cm if condition is True Nc||_dSrH enter_result)r"rs rr]znullcontext.__init__s(rc|jSrHrr!s rr#znullcontext.__enter__s   rcdSrHr r"excinfos rr*znullcontext.__exit__rrcK|jSrHrr!s rr:znullcontext.__aenter__s  rc KdSrHr rs rr<znullcontext.__aexit__ s  rrH) r r r r3r]r#r*r:r<r rrrrsn))))!!!   !!!     rrc4eZdZdZfdZdZdZdZxZS)rz+Backwards compatibility alias for ExitStackctjdttt|dS)Nz*ContextStack has been renamed to ExitStack)r@rArBsuperrr])r"rbs rr]zContextStack.__init__s? B( * * * lD!!**,,,,,rc,||SrH)r)r"rs r register_exitzContextStack.register_exitsyy"""rc$|j|g|Ri|SrH)r)r"rrKrLs rregisterzContextStack.registers$t}X5555555rc*|SrH)rr!s rpreservezContextStack.preserves||~~r) r r r r3r]r r"r$ __classcell__)rbs@rrrsk55----- ###666r)$r3r6rnr@r, collectionsr functoolsrtypesrr ImportError__all__ABCrrobjectrrRrWr_rsrrrrrrrrrrrrrr rrr-sbGG  """""""               I I I N SW*#'.%%%%%v%%%P     F           $AFAFAFAFAF;5/AFAFAFH22222$@$?$9222jBB$2"""""*"""4<<<<<,<<<$     o    o MMMMM%MMM