grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026B �×Ó]Üä•ã @s^dZddlZddlZddlZddlmZddlmZddddd d d d d dddg ZGdd „d ej ƒZ Gdd „d ej ƒZ Gdd „d e ƒZ Gdd„dƒZGdd„dee e ƒZGdd„dee ƒZdd„Zdd„ZGdd„de ƒZGdd„de ƒZGdd„deƒZGd d„deƒZGd!d„de ƒZGd"d#„d#ƒZGd$d „d ee ƒZGd%d „d ee ƒZGd&d„de ƒZdS)'z4Utilities for with-statement contexts. See PEP 343.éN)Údeque)ÚwrapsÚasynccontextmanagerÚcontextmanagerÚclosingÚ nullcontextÚAbstractContextManagerÚAbstractAsyncContextManagerÚAsyncExitStackÚContextDecoratorÚ ExitStackÚredirect_stdoutÚredirect_stderrÚsuppressc@s2eZdZdZdd„Zejdd„ƒZedd„ƒZ dS) rz,An abstract base class for context managers.cCs|S)z0Return `self` upon entering the runtime context.©)Úselfrrú//opt/alt/python37/lib64/python3.7/contextlib.pyÚ __enter__sz AbstractContextManager.__enter__cCsdS)z9Raise any exception triggered within the runtime context.Nr)rÚexc_typeÚ exc_valueÚ tracebackrrrÚ__exit__szAbstractContextManager.__exit__cCs|tkrt |dd¡StS)Nrr)rÚ_collections_abcÚ_check_methodsÚNotImplemented)ÚclsÚCrrrÚ__subclasshook__sz'AbstractContextManager.__subclasshook__N) Ú__name__Ú __module__Ú __qualname__Ú__doc__rÚabcÚabstractmethodrÚ classmethodrrrrrrsc@s2eZdZdZdd„Zejdd„ƒZedd„ƒZ dS) r z9An abstract base class for asynchronous context managers.cÃs|S)z0Return `self` upon entering the runtime context.r)rrrrÚ __aenter__&sz&AbstractAsyncContextManager.__aenter__cÃsdS)z9Raise any exception triggered within the runtime context.Nr)rrrrrrrÚ __aexit__*sz%AbstractAsyncContextManager.__aexit__cCs|tkrt |dd¡StS)Nr%r&)r rrr)rrrrrr/sz,AbstractAsyncContextManager.__subclasshook__N) rrr r!r%r"r#r&r$rrrrrr "sc@s eZdZdZdd„Zdd„ZdS)r zJA base class or mixin that enables context managers to work as decorators.cCs|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)rrrrÚ _recreate_cm:s zContextDecorator._recreate_cmcstˆƒ‡‡fdd„ƒ}|S)Nc sˆ ¡� ˆ||ŽSQRXdS)N)r')ÚargsÚkwds)ÚfuncrrrÚinnerGs z(ContextDecorator.__call__..inner)r)rr*r+r)r*rrÚ__call__FszContextDecorator.__call__N)rrr r!r'r,rrrrr 7s c@seZdZdZdd„ZdS)Ú_GeneratorContextManagerBasezBShared functionality for @contextmanager and @asynccontextmanager.cCsJ|||Ž|_||||_|_|_t|ddƒ}|dkr@t|ƒj}||_dS)Nr!)Úgenr*r(r)ÚgetattrÚtyper!)rr*r(r)ÚdocrrrÚ__init__Qs    z%_GeneratorContextManagerBase.__init__N)rrr r!r2rrrrr-Nsr-c@s(eZdZdZdd„Zdd„Zdd„ZdS) Ú_GeneratorContextManagerz%Helper for @contextmanager decorator.cCs| |j|j|j¡S)N)Ú __class__r*r(r))rrrrr'esz%_GeneratorContextManager._recreate_cmcCs:|`|`|`y t|jƒStk r4tdƒd‚YnXdS)Nzgenerator didn't yield)r(r)r*Únextr.Ú StopIterationÚ RuntimeError)rrrrrks   z"_GeneratorContextManager.__enter__c Csð|dkr6yt|jƒWntk r*dSXtdƒ‚n¶|dkrD|ƒ}y|j |||¡WnŠtk r~}z||k Sd}~XYnftk rÂ}z(||kršdS|tkr°|j|kr°dS‚Wdd}~XYn"t ¡d|krÜdS‚YnXtdƒ‚dS)NFzgenerator didn't stopéz#generator didn't stop after throw())r5r.r6r7ÚthrowÚ __cause__ÚsysÚexc_info)rr0ÚvaluerÚexcrrrrts.  z!_GeneratorContextManager.__exit__N)rrr r!r'rrrrrrr3`s r3c@s eZdZdZdd„Zdd„ZdS)Ú_AsyncGeneratorContextManagerz Helper for @asynccontextmanager.cÃs4y|j ¡IdHStk r.tdƒd‚YnXdS)Nzgenerator didn't yield)r.Ú __anext__ÚStopAsyncIterationr7)rrrrr%¨sz(_AsyncGeneratorContextManager.__aenter__c Ãs|dkrrrrr&®s.    z'_AsyncGeneratorContextManager.__aexit__N)rrr r!r%r&rrrrr?¤sr?cstˆƒ‡fdd„ƒ}|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: cs tˆ||ƒS)N)r3)r(r))r*rrÚhelperíszcontextmanager..helper)r)r*rFr)r*rrÒscstˆƒ‡fdd„ƒ}|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: cs tˆ||ƒS)N)r?)r(r))r*rrrFsz#asynccontextmanager..helper)r)r*rFr)r*rrósc@s(eZdZdZdd„Zdd„Zdd„ZdS) 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() cCs ||_dS)N)Úthing)rrGrrrr2%szclosing.__init__cCs|jS)N)rG)rrrrr'szclosing.__enter__cGs|j ¡dS)N)rGÚclose)rr<rrrr)szclosing.__exit__N)rrr r!r2rrrrrrrsc@s(eZdZdZdd„Zdd„Zdd„ZdS)Ú_RedirectStreamNcCs||_g|_dS)N)Ú _new_targetÚ _old_targets)rÚ new_targetrrrr21sz_RedirectStream.__init__cCs*|j tt|jƒ¡tt|j|jƒ|jS)N)rKÚappendr/r;Ú_streamÚsetattrrJ)rrrrr6sz_RedirectStream.__enter__cCstt|j|j ¡ƒdS)N)rOr;rNrKÚpop)rÚexctypeÚexcinstÚexctbrrrr;sz_RedirectStream.__exit__)rrr rNr2rrrrrrrI-srIc@seZdZdZdZdS)r aAContext manager for temporarily redirecting stdout to another file. # How to send help() to stderr with redirect_stdout(sys.stderr): help(dir) # How to write help() to a file with open('help.txt', 'w') as f: with redirect_stdout(f): help(pow) ÚstdoutN)rrr r!rNrrrrr ?s c@seZdZdZdZdS)rzCContext manager for temporarily redirecting stderr to another file.ÚstderrN)rrr r!rNrrrrrOsc@s(eZdZdZdd„Zdd„Zdd„ZdS) ra?Context manager to suppress specified exceptions After the exception is suppressed, execution proceeds with the next statement following the with statement. with suppress(FileNotFoundError): os.remove(somefile) # Execution still resumes here if the file was already removed cGs ||_dS)N)Ú _exceptions)rÚ exceptionsrrrr2`szsuppress.__init__cCsdS)Nr)rrrrrcszsuppress.__enter__cCs|dk ot||jƒS)N)Ú issubclassrV)rrQrRrSrrrrfs zsuppress.__exit__N)rrr r!r2rrrrrrrUs c@sbeZdZdZedd„ƒZedd„ƒZdd„Zdd „Zd d „Z d d „Z dd„Z dd„Z ddd„Z dS)Ú_BaseExitStackz.A base class for ExitStack and AsyncExitStack.cs‡‡fdd„}|S)Ncsˆˆ|||ƒS)Nr)rr>Útb)ÚcmÚcm_exitrrÚ _exit_wrapperxsz:_BaseExitStack._create_exit_wrapper.._exit_wrapperr)r[r\r]r)r[r\rÚ_create_exit_wrappervsz#_BaseExitStack._create_exit_wrappercsˆ^‰‰‡‡‡fdd„}|S)NcsˆˆˆŽdS)Nr)rr>rZ)r(Úcallbackr)rrr]sz8_BaseExitStack._create_cb_wrapper.._exit_wrapperr)r(r)r]r)r(r_r)rÚ_create_cb_wrapper|sz!_BaseExitStack._create_cb_wrappercCs tƒ|_dS)N)rÚ_exit_callbacks)rrrrr2ƒsz_BaseExitStack.__init__cCst|ƒƒ}|j|_tƒ|_|S)z@Preserve the context stack by transferring it to a new instance.)r0rar)rÚ new_stackrrrÚpop_all†s z_BaseExitStack.pop_allcCsBt|ƒ}y |j}Wntk r0| |¡YnX| ||¡|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). )r0rÚAttributeErrorÚ_push_exit_callbackÚ _push_cm_exit)rÚexitÚ_cb_typeÚ exit_methodrrrÚpush�s   z_BaseExitStack.pushcCs(t|ƒ}|j}| |¡}| ||¡|S)z«Enters the supplied context manager. If successful, also pushes its __exit__ method as a callback and returns the result of the __enter__ method. )r0rrrf)rr[Ú_cm_typeÚ_exitÚresultrrrÚ enter_context¡s   z_BaseExitStack.enter_contextcOs|t|ƒdkr|^}}}n>|s&tdƒ‚n0d|krB| d¡}|^}}ntdt|ƒdƒ‚|j|f|ž|Ž}||_| |¡|S)z\Registers an arbitrary callback and arguments. Cannot suppress exceptions. ézBdescriptor 'callback' of '_BaseExitStack' object needs an argumentr_z8callback expected at least 1 positional argument, got %dr8)ÚlenÚ TypeErrorrPr`Ú __wrapped__re)r(r)rr_r]rrrr_¯s      z_BaseExitStack.callbackcCs"| ||¡}||_| |d¡dS)z;Helper to correctly register callbacks to __exit__ methods.TN)r^Ú__self__re)rr[r\r]rrrrfÈs z_BaseExitStack._push_cm_exitTcCs|j ||f¡dS)N)rarM)rr_Úis_syncrrrreÎsz"_BaseExitStack._push_exit_callbackN)T)rrr r!Ú staticmethodr^r`r2rcrjrnr_rfrerrrrrYss  rYc@s(eZdZdZdd„Zdd„Zdd„ZdS) r a‹Context 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. cCs|S)Nr)rrrrrÞszExitStack.__enter__c sÒ|ddk }t ¡d‰‡fdd„}d}d}x`|jrŒ|j ¡\}}y||ŽrXd}d}d}Wq.t ¡}||d|dƒd}|}Yq.Xq.W|rÊy|dj} |d‚Wn tk rÈ| |d_‚YnX|oÐ|S)Nrr8cs8x,|j}||krdS|dks$|ˆkr&P|}qW||_dS)N)Ú __context__)Únew_excÚold_excÚ exc_context)Ú frame_excrrÚ_fix_exception_contextçsz2ExitStack.__exit__.._fix_exception_contextFT)NNN)r;r<rarPrvrD) rÚ exc_detailsÚ received_excr{Úsuppressed_excÚ pending_raisertÚcbÚnew_exc_detailsÚ fixed_ctxr)rzrrás2      zExitStack.__exit__cCs| ddd¡dS)z%Immediately unwind the context stack.N)r)rrrrrHszExitStack.closeN)rrr r!rrrHrrrrr Ós 1c@s`eZdZdZedd„ƒZedd„ƒZdd„Zdd „Zd d „Z d d „Z dd„Z dd„Z dd„Z dS)r a×Async 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. cs‡‡fdd„}|S)Nc“sˆˆ|||ƒIdHS)Nr)rr>rZ)r[r\rrr]'sz@AsyncExitStack._create_async_exit_wrapper.._exit_wrapperr)r[r\r]r)r[r\rÚ_create_async_exit_wrapper%sz)AsyncExitStack._create_async_exit_wrappercsˆ^‰‰‡‡‡fdd„}|S)Nc“sˆˆˆŽIdHdS)Nr)rr>rZ)r(r_r)rrr].sz>AsyncExitStack._create_async_cb_wrapper.._exit_wrapperr)r(r)r]r)r(r_r)rÚ_create_async_cb_wrapper+sz'AsyncExitStack._create_async_cb_wrappercÃs.t|ƒ}|j}| |¡IdH}| ||¡|S)z³Enters the supplied async context manager. If successful, also pushes its __aexit__ method as a callback and returns the result of the __aenter__ method. N)r0r&r%Ú_push_async_cm_exit)rr[rkrlrmrrrÚenter_async_context2s  z"AsyncExitStack.enter_async_contextcCsDt|ƒ}y |j}Wn tk r2| |d¡YnX| ||¡|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)r0r&rdrer…)rrgrhrirrrÚpush_async_exit>s  zAsyncExitStack.push_async_exitcOs~t|ƒdkr|^}}}n>|s&tdƒ‚n0d|krB| d¡}|^}}ntdt|ƒdƒ‚|j|f|ž|Ž}||_| |d¡|S)zfRegisters an arbitrary coroutine function and arguments. Cannot suppress exceptions. rozMdescriptor 'push_async_callback' of 'AsyncExitStack' object needs an argumentr_zCpush_async_callback expected at least 1 positional argument, got %dr8F)rprqrPr„rrre)r(r)rr_r]rrrÚpush_async_callbackPs      z"AsyncExitStack.push_async_callbackcÃs| ddd¡IdHdS)z%Immediately unwind the context stack.N)r&)rrrrÚacloseiszAsyncExitStack.aclosecCs"| ||¡}||_| |d¡dS)zLHelper to correctly register coroutine function to __aexit__ method.FN)rƒrsre)rr[r\r]rrrr…ms z"AsyncExitStack._push_async_cm_exitcÃs|S)Nr)rrrrr%tszAsyncExitStack.__aenter__c ‡sê|ddk }t ¡d‰‡fdd„}d}d}xx|jr¤|j ¡\}}y0|rR||Ž}n||ŽIdH}|rpd}d}d}Wq.t ¡} || d|dƒd}| }Yq.Xq.W|rây|dj} |d‚Wn tk rà| |d_‚YnX|oè|S)Nrr8cs8x,|j}||krdS|dks$|ˆkr&P|}qW||_dS)N)rv)rwrxry)rzrrr{}sz8AsyncExitStack.__aexit__.._fix_exception_contextFT)NNN)r;r<rarPrvrD) rr|r}r{r~rrtr€Ú cb_suppressr�r‚r)rzrr&ws8       zAsyncExitStack.__aexit__N)rrr r!rurƒr„r†r‡rˆr‰r…r%r&rrrrr s    c@s*eZdZdZd dd„Zdd„Zdd„ZdS) 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 NcCs ||_dS)N)Ú enter_result)rr‹rrrr2¸sznullcontext.__init__cCs|jS)N)r‹)rrrrr»sznullcontext.__enter__cGsdS)Nr)rÚexcinforrrr¾sznullcontext.__exit__)N)rrr r!r2rrrrrrr­s  )r!r"r;rÚ collectionsrÚ functoolsrÚ__all__ÚABCrr Úobjectr r-r3r?rrrrIr rrrYr r rrrrrÚs<    B -!!`E