Qfk"dZddlZddlZddlZddlZddlmZddlmZddl m Z m Z gdZ GddejZGd d ejZGd d eZGd deZGddZGddeeeZGddeeeZdZdZGddeZGddeZGddeZGddeZGdd eZGd!d"eZGd#d$ZGd%d&eeZGd'd(eeZ Gd)d*eeZ!Gd+d,eZ"y)-z4Utilities for with-statement contexts. See PEP 343.N)dequewraps) MethodType GenericAlias)asynccontextmanagercontextmanagerclosing nullcontextAbstractContextManagerAbstractAsyncContextManagerAsyncExitStackContextDecorator ExitStackredirect_stdoutredirect_stderrsuppressaclosingchdircZeZdZdZeeZdZejdZ edZ y)r z,An abstract base class for context managers.c|Sz0Return `self` upon entering the runtime context.selfs 1/opt/alt/python312/lib64/python3.12/contextlib.py __enter__z AbstractContextManager.__enter__s cyz9Raise any exception triggered within the runtime context.Nrrexc_type exc_value tracebacks r__exit__zAbstractContextManager.__exit__srcL|turtj|ddStS)Nrr%)r _collections_abc_check_methodsNotImplementedclsCs r__subclasshook__z'AbstractContextManager.__subclasshook__ s& ( (#221k:N NrN) __name__ __module__ __qualname____doc__ classmethodr__class_getitem__rabcabstractmethodr%r-rrrr r sC6#L1 rr cZeZdZdZeeZdZejdZ edZ y)r z9An abstract base class for asynchronous context managers.cK|Swrrrs r __aenter__z&AbstractAsyncContextManager.__aenter__-s  c Kywr rr!s r __aexit__z%AbstractAsyncContextManager.__aexit__1s cL|turtj|ddStS)Nr8r;)r r'r(r)r*s rr-z,AbstractAsyncContextManager.__subclasshook__6s- - -#221l3>@ @rN) r.r/r0r1r2rr3r8r4r5r;r-rrrr r 'sCC#L1 rr ceZdZdZdZdZy)rzJA base class or mixin that enables context managers to work as decorators.c|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. rrs r _recreate_cmzContextDecorator._recreate_cmAs  rc2tfd}|S)Nc`j5|i|cdddS#1swYyxYwNr@argskwdsfuncrs rinnerz(ContextDecorator.__call__..innerNs-""$T*T* + +$$s$-rrrHrIs`` r__call__zContextDecorator.__call__Ms t +  + rNr.r/r0r1r@rKrrrrr>sP rrceZdZdZdZdZy)AsyncContextDecoratorzPA base class or mixin that enables async context managers to work as decorators.c|S)z-Return a recreated instance of self. rrs rr@z"AsyncContextDecorator._recreate_cmXs  rc2tfd}|S)NcKj4d{|i|d{cdddd{S7%77 #1d{7swYyxYwwrCrDrEs rrIz-AsyncContextDecorator.__call__..inner^sL((**!40400 1 1 1*0 1***sPA>A AAA AAAAAA A AArrJs`` rrKzAsyncContextDecorator.__call__]s t 1  1 rNrLrrrrNrNUsV rrNceZdZdZdZdZy)_GeneratorContextManagerBasezBShared functionality for @contextmanager and @asynccontextmanager.c||i||_|||c|_|_|_t |dd}|t |j }||_y)Nr1)genrHrFrGgetattrtyper1)rrHrFrGdocs r__init__z%_GeneratorContextManagerBase.__init__hsR&&*.d' 49didIt, ;t*$$C rcd|j|j|j|jSrC) __class__rHrFrGrs rr@z)_GeneratorContextManagerBase._recreate_cmvs#~~diiDII>>rN)r.r/r0r1rYr@rrrrSrSesL?rrSceZdZdZdZdZy)_GeneratorContextManagerz%Helper for @contextmanager decorator.cl|`|`|` t|jS#t $r t ddwxYwNzgenerator didn't yield)rFrGrHnextrU StopIteration RuntimeErrorrs rrz"_GeneratorContextManager.__enter__sD Ity$) C> ! C78d B Cs3cB|" t|j td||} |jj | td#|jjwxYw#t$rYywxYw#t$r}||ucYd}~Sd}~wt$r@}||ur ||_Yd}~yt|tr|j|ur ||_Yd}~yd}~wt$r}||ur||_Yd}~yd}~wwxYw#|jjwxYw)Ngenerator didn't stopFz#generator didn't stop after throw()) r`rUrbcloserathrow __traceback__ isinstance __cause__ BaseExceptionrtypvaluer$excs rr%z!_GeneratorContextManager.__exit__s ; %TXX%&'>??}# u%F !"#HIIWHHNN$ !  ! (%'' %<(1C% um4 .*3E'  e#$-!  siA5 AB DA25 BB C? BC? C?" C2%CC C?) C::C?DN)r.r/r0r1rr%rrrr]r]}s 0C7!rr]ceZdZdZdZdZy)_AsyncGeneratorContextManagerz*Helper for @asynccontextmanager decorator.cK|`|`|` t|jd{S7#t $r t ddwxYwwr_)rFrGrHanextrUStopAsyncIterationrbrs rr8z(_AsyncGeneratorContextManager.__aenter__sM Ity$) Ctxx( ((! C78d B Cs#A)')A)?AcK|* t|jd{ td||} |jj |d{ td7I#|jjd{7wxYw#t$rYywxYw7I#t$r}||ucYd}~Sd}~wt$rF}||ur ||_Yd}~yt|ttfr|j|ur ||_Yd}~yd}~wt$r}||ur||_Yd}~yd}~wwxYw#|jjd{7wxYww)NrdFz$generator didn't stop after athrow()) rrrUrbaclosersathrowrgrhrarirjrks rr;z'_AsyncGeneratorContextManager.__aexit__s@ ; ,DHHo%%,&'>??}# hhooe,,,F ("#IJJe&((//+++ &  -% (%'' %<(1C% u}6H&IJ .*3E'  e#$-! hhoo'''sEBA(B A* EB#B!B# D'(B*BB  BE BEBE!B## D$,B5/D$0E5 D$ D E+D<ED D$ DED$$E'E EE  EN)r.r/r0r1r8r;rrrrprps 5C7(rrpc.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: ct||SrC)r]rFrGrHs rhelperzcontextmanager..helper+s'dD99rrrHrzs` rr r s 6 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: ct||SrC)rprys rrzz#asynccontextmanager..helperLs,T4>>rrr{s` rrr1s 6 4[?? Mrc"eZdZdZdZdZdZy)r a2Context 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||_yrCthingrrs rrYzclosing.__init__c  rc|jSrCrrs rrzclosing.__enter__es zzrc8|jjyrC)rrerexc_infos rr%zclosing.__exit__gs rNr.r/r0r1rYrr%rrrr r Rs rr c"eZdZdZdZdZdZy)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||_yrCrrs rrYzaclosing.__init__}rrc"K|jSwrCrrs rr8zaclosing.__aenter__szz cTK|jjd{y7wrC)rrurs rr;zaclosing.__aexit__sjj!!!s (&(N)r.r/r0r1rYr8r;rrrrrks""rrc"eZdZdZdZdZdZy)_RedirectStreamNc ||_g|_yrC) _new_target _old_targets)r new_targets rrYz_RedirectStream.__init__s%rc|jjtt|jt t|j|j |j SrC)rappendrVsys_streamsetattrrrs rrz_RedirectStream.__enter__sC   dll!;<T\\4#3#34rcjtt|j|jj yrC)rrrrpop)rexctypeexcinstexctbs rr%z_RedirectStream.__exit__s!T\\4#4#4#8#8#:;r)r.r/r0rrYrr%rrrrrsG  ._exit_wrappers d #d #rrrrFrGrs``` r_create_cb_wrapperz!_BaseExitStack._create_cb_wrappers $rc"t|_yrC)r_exit_callbacksrs rrYz_BaseExitStack.__init__s $wrcft|}|j|_t|_|S)z@Preserve the context stack by transferring it to a new instance.)rWrr)r new_stacks rpop_allz_BaseExitStack.pop_alls-DJL $($8$8 !$wrct|} |j}|j|||S#t$r|j |Y|SwxYw)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). )rWr% _push_cm_exitAttributeError_push_exit_callbackrexit_cb_type exit_methods rpushz_BaseExitStack.pushsZ: 2"++K   t[ 1  +  $ $T *  +s -A  A ct|} |j}|j}||}|j|||S#t$r(t d|j d|j ddwxYw)zEnters the supplied context manager. If successful, also pushes its __exit__ method as a callback and returns the result of the __enter__ method. '.z6' object does not support the context manager protocolN)rWrr%r TypeErrorr/r0rrrr+_enter_exitresults r enter_contextz_BaseExitStack.enter_contexts2h S]]FLLE 2u%  Sa/q1A1A0BCGHINR S Ss A1A2cb|j|g|i|}||_|j||S)z\Registers an arbitrary callback and arguments. Cannot suppress exceptions. )r __wrapped__rrrrFrGrs rrz_BaseExitStack.callbacks= 0//H4H4H %- !   /rcL|j||}|j|dy)z;Helper to correctly register callbacks to __exit__ methods.TN)rrrrrrs rrz_BaseExitStack._push_cm_exits$11"g>    5rc>|jj||fyrC)rr)rris_syncs rrz"_BaseExitStack._push_exit_callback$s ##Wh$78rN)T)r.r/r0r1 staticmethodrrrYrrrrrrrrrrrsN8'' '(& 6 9rrc"eZdZdZdZdZdZy)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|SrCrrs rrzExitStack.__enter__4s rc |ddu}tjd fd}d}d}|jr6|jj\}} ||rd}d}d}|jr6|r |dj} |d|xr|S#tj}||d|dd}|}YUxYw#t $r  |d_wxYw)NrcL |j}|||ury|ur ||_y|}#rC __context__new_excold_exc exc_context frame_excs r_fix_exception_contextz2ExitStack.__exit__.._fix_exception_context=B%11 &+*@)+#*G &rFTNNNrrrrrrj) r exc_details received_excrsuppressed_exc pending_raisercbnew_exc_details fixed_ctxrs @rr%zExitStack.__exit__7s"1~T1 LLN1%  *  ""..224KGR .{#%)N$)M"4K""  (N66 !!n$.. ."%,,.&q'9;q>J $ - ! -6 A* s B.B6)B36C c*|jdddyz%Immediately unwind the context stack.N)r%rs rrezExitStack.closehs dD$'rN)r.r/r0r1rr%rerrrrr)s//b(rrcZeZdZdZedZedZdZdZdZ dZ dZ d Z d Z y ) 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. ct||SrCrrs r_create_async_exit_wrapperz)AsyncExitStack._create_async_exit_wrapper{rrcfd}|S)Nc2Kid{y7wrCrrs rrz>AsyncExitStack._create_async_cb_wrapper.._exit_wrappersD)D) ) )s rrs``` r_create_async_cb_wrapperz'AsyncExitStack._create_async_cb_wrappers *rcKt|} |j}|j}||d{}|j|||S#t$r(t d|j d|j ddwxYw7Lw)zEnters the supplied async context manager. If successful, also pushes its __aexit__ method as a callback and returns the result of the __aenter__ method. rrzC' object does not support the asynchronous context manager protocolN)rWr8r;rrr/r0_push_async_cm_exitrs renter_async_contextz"AsyncExitStack.enter_async_contexts 2h '^^FMME bz!   U+  'a/q1A1A0BCTU"& ' '"s% BA BA?B 1A<<Bct|} |j}|j|||S#t$r|j |dY|SwxYw)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)rWr;rrrrs rpush_async_exitzAsyncExitStack.push_async_exits\: 8",,K  $ $T; 7  2  $ $T5 1  2s -A  A cd|j|g|i|}||_|j|d|S)zfRegisters an arbitrary coroutine function and arguments. Cannot suppress exceptions. F)rrrrs rpush_async_callbackz"AsyncExitStack.push_async_callbacks? 655hNNN %- !   6rcFK|jdddd{y7wr)r;rs rruzAsyncExitStack.aclosesnnT4...s !!cL|j||}|j|dy)zLHelper to correctly register coroutine function to __aexit__ method.FN)rrrs rrz"AsyncExitStack._push_async_cm_exits&77GD    6rcK|SwrCrrs rr8zAsyncExitStack.__aenter__s  r9c K|ddu}tjd fd}d}d}|jrH|jj\}} |r||}n ||d{}|rd}d}d}|jrH|r |dj} |d|xr|S76#tj} || d|dd}| }YWxYw#t $r  |d_wxYww)NrrcL |j}|||ury|ur ||_y|}#rCrrs rrz8AsyncExitStack.__aexit__.._fix_exception_contextrrFTrr) rrrrrrrr cb_suppressrrrs @rr;zAsyncExitStack.__aexit__s"1~T1 LLN1%  *  ""..224KGR ."$k"2K(*K(8"8K%)N$)M"4K""$  (N66 !!n$..+#9  ."%,,.&q'9;q>J $ - ! -6 A* sNAC$B%B& B2 C$?C$C C$B)C C$ C!!C$N)r.r/r0r1rrrrrrrurr8r;rrrrrnsR '' $$ /7 3/rrc0eZdZdZddZdZdZdZdZy) r aOContext 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||_yrC enter_result)rrs rrYznullcontext.__init__s (rc|jSrCr rs rrznullcontext.__enter__s   rcyrCrrexcinfos rr%znullcontext.__exit__ rrc"K|jSwrCr rs rr8znullcontext.__aenter__ s   rc KywrCrrs rr;znullcontext.__aexit__s  r<rC) r.r/r0r1rYrr%r8r;rrrr r s )! ! rr c"eZdZdZdZdZdZy)rzHNon thread-safe context manager to change the current working directory.c ||_g|_yrC)path_old_cwd)rrs rrYzchdir.__init__s  rc|jjtjtj|j yrC)rrosgetcwdrrrs rrzchdir.__enter__s* RYY[) rc^tj|jjyrC)rrrrrs rr%zchdir.__exit__s ""$%rNrrrrrrsR&rr)#r1r4rrr' collectionsr functoolsrtypesrr__all__ABCr r objectrrNrSr]rpr rr rrrrrrrrr rrrrr#s9: * SWW,#''.v. F  ??0G! G!RG( G(TBB$2"*"4<,<$ o  o $%$NQ9Q9jA( 6A(JH/^%@H/V (*E 6 & " &r