grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026o ®û|jeã@s~dZddlZddlZddlZddlmZddlmZddlm Z m Z gd¢Z Gdd„dej ƒZ Gd d „d ej ƒZGd d „d eƒZGd d„deƒZGdd„dƒZGdd„dee eƒZGdd„deeeƒZdd„Zdd„ZGdd„de ƒZGdd„deƒZGdd„de ƒZGdd „d eƒZGd!d"„d"eƒZGd#d$„d$e ƒZGd%d&„d&ƒZGd'd(„d(ee ƒZGd)d*„d*eeƒZGd+d,„d,e eƒZ dS)-z4Utilities for with-statement contexts. See PEP 343.éN)Údeque©Úwraps)Ú MethodTypeÚ GenericAlias) ÚasynccontextmanagerÚcontextmanagerÚclosingÚ nullcontextÚAbstractContextManagerÚAbstractAsyncContextManagerÚAsyncExitStackÚContextDecoratorÚ ExitStackÚredirect_stdoutÚredirect_stderrÚsuppressÚaclosingc@ó:eZdZdZeeƒZdd„Zej dd„ƒZ edd„ƒZ dS) r z,An abstract base class for context managers.cCó|S©z0Return `self` upon entering the runtime context.©©Úselfrrú1/opt/alt/python310/lib64/python3.10/contextlib.pyÚ __enter__sz AbstractContextManager.__enter__cCsdS©z9Raise any exception triggered within the runtime context.Nr©rÚexc_typeÚ exc_valueÚ tracebackrrrÚ__exit__ózAbstractContextManager.__exit__cCó|tur t |dd¡StS)Nrr!)r Ú_collections_abcÚ_check_methodsÚNotImplemented©ÚclsÚCrrrÚ__subclasshook__sz'AbstractContextManager.__subclasshook__N) Ú__name__Ú __module__Ú __qualname__Ú__doc__Ú classmethodrÚ__class_getitem__rÚabcÚabstractmethodr!r*rrrrr ó r c@r) r z9An abstract base class for asynchronous context managers.cÃó�|SrrrrrrÚ __aenter__+s€z&AbstractAsyncContextManager.__aenter__cÃs�dSrrrrrrÚ __aexit__/s€z%AbstractAsyncContextManager.__aexit__cCr#)Nr5r6)r r$r%r&r'rrrr*4s ÿz,AbstractAsyncContextManager.__subclasshook__N) r+r,r-r.r/rr0r5r1r2r6r*rrrrr %r3r c@ó eZdZdZdd„Zdd„ZdS)rzJA base class or mixin that enables context managers to work as decorators.cCr)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. rrrrrÚ _recreate_cm?s zContextDecorator._recreate_cmcótˆƒ‡‡fdd„ƒ}|S)Ncs:ˆ ¡�ˆ|i|¤ŽWdƒS1swYdS©N©r8©ÚargsÚkwds©ÚfuncrrrÚinnerLs  $ÿz(ContextDecorator.__call__..innerr©rr@rArr?rÚ__call__KózContextDecorator.__call__N©r+r,r-r.r8rCrrrrr<s rc@r7)ÚAsyncContextDecoratorzPA base class or mixin that enables async context managers to work as decorators.cCr)z-Return a recreated instance of self. rrrrrr8Vr"z"AsyncContextDecorator._recreate_cmcr9)Nc ŸsV�ˆ ¡4IdHšˆ|i|¤ŽIdHWdƒIdHS1IdHs$wYdSr:r;r<r?rrrA\s€0ÿz-AsyncContextDecorator.__call__..innerrrBrr?rrC[rDzAsyncContextDecorator.__call__NrErrrrrFSs rFc@r7)Ú_GeneratorContextManagerBasezBShared functionality for @contextmanager and @asynccontextmanager.cCsN||i|¤Ž|_||||_|_|_t|ddƒ}|dur"t|ƒj}||_dS)Nr.)Úgenr@r=r>ÚgetattrÚtyper.)rr@r=r>ÚdocrrrÚ__init__fs    z%_GeneratorContextManagerBase.__init__cCs| |j|j|j¡Sr:)Ú __class__r@r=r>rrrrr8tsz)_GeneratorContextManagerBase._recreate_cmN)r+r,r-r.rLr8rrrrrGcs rGc@r7)Ú_GeneratorContextManagerz%Helper for @contextmanager decorator.cCs2|`|`|`zt|jƒWStytdƒd‚w©Nzgenerator didn't yield)r=r>r@ÚnextrHÚ StopIterationÚ RuntimeErrorrrrrr‚s    ÿz"_GeneratorContextManager.__enter__c Cs|durz t|jƒWtdƒ‚tyYdSw|dur |ƒ}z |j |||¡Wtdƒ‚tyC}z ||uWYd}~Sd}~wtyk}z||urUWYd}~dSt|tƒrf|j|urfWYd}~dS‚d}~wty‚}z ||urw‚WYd}~dSd}~ww)NFúgenerator didn't stopz#generator didn't stop after throw())rPrHrQrRÚthrowÚ isinstanceÚ __cause__Ú BaseException©rÚtypÚvaluer Úexcrrrr!‹s<  ýÿ á€ÿ €€÷z!_GeneratorContextManager.__exit__N)r+r,r-r.rr!rrrrrN{ó rNc@r7)Ú_AsyncGeneratorContextManagerz*Helper for @asynccontextmanager decorator.cÃs:�|`|`|`z t|jƒIdHWStytdƒd‚wrO)r=r>r@ÚanextrHÚStopAsyncIterationrRrrrrr5Âs€   ÿz(_AsyncGeneratorContextManager.__aenter__c Ãs�|durz t|jƒIdHWtdƒ‚tyYdSw|dur$|ƒ}z|j |||¡IdHWtdƒ‚tyJ}z ||uWYd}~Sd}~wtyt}z||ur\WYd}~dSt|ttfƒro|j|uroWYd}~dS‚d}~wty‹}z ||ur€‚WYd}~dSd}~ww)NFrSz$generator didn't stop after athrow()) r^rHr_rRÚathrowrUrQrVrWrXrrrr6Ës>€ ýÿ ဠÿ €€÷z'_AsyncGeneratorContextManager.__aexit__N)r+r,r-r.r5r6rrrrr]»r\r]cótˆƒ‡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: có tˆ||ƒSr:)rNr<©r@rrÚhelperó zcontextmanager..helperr©r@rdrrcrrüórcra)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: crbr:)r]r<rcrrrd8rez#asynccontextmanager..helperrrfrrcrrrgrc@ó(eZdZdZdd„Zdd„Zdd„ZdS) 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() cCó ||_dSr:©Úthing©rrkrrrrLOó zclosing.__init__cCó|jSr:rjrrrrrQózclosing.__enter__cGs|j ¡dSr:)rkÚclose©rÚexc_inforrrr!Sszclosing.__exit__N©r+r,r-r.rLrr!rrrrr >s  r c@rh) ražAsync 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() cCrir:rjrlrrrrLirmzaclosing.__init__cÃó�|jSr:rjrrrrr5kó€zaclosing.__aenter__cÇs�|j ¡IdHdSr:)rkÚacloserqrrrr6ms€zaclosing.__aexit__N)r+r,r-r.rLr5r6rrrrrWs  rc@s(eZdZdZdd„Zdd„Zdd„ZdS)Ú_RedirectStreamNcCs||_g|_dSr:)Ú _new_targetÚ _old_targets)rÚ new_targetrrrrLus z_RedirectStream.__init__cCs*|j tt|jƒ¡tt|j|jƒ|jSr:)ryÚappendrIÚsysÚ_streamÚsetattrrxrrrrrzsz_RedirectStream.__enter__cCstt|j|j ¡ƒdSr:)r~r|r}ryÚpop©rÚexctypeÚexcinstÚexctbrrrr!sz_RedirectStream.__exit__)r+r,r-r}rLrr!rrrrrwqs  rwc@óeZdZdZdZdS)raAContext 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©r+r,r-r.r}rrrrrƒs rc@r„)rzCContext manager for temporarily redirecting stderr to another file.ÚstderrNr†rrrrr“src@rh) 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 cGrir:)Ú _exceptions)rÚ exceptionsrrrrL¤rmzsuppress.__init__cCódSr:rrrrrr§ózsuppress.__enter__cCs|duo t||jƒSr:)Ú issubclassrˆr€rrrr!ªs zsuppress.__exit__Nrsrrrrr™s   rc@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.cCó t||ƒSr:©r©ÚcmÚcm_exitrrrÚ_create_exit_wrapperºó z#_BaseExitStack._create_exit_wrapperc󇇇fdd„}|S)Ncsˆˆiˆ¤ŽdSr:r©rr[Útb©r=Úcallbackr>rrÚ _exit_wrapperÀsz8_BaseExitStack._create_cb_wrapper.._exit_wrapperr©r™r=r>ršrr˜rÚ_create_cb_wrapper¾óz!_BaseExitStack._create_cb_wrappercCs tƒ|_dSr:)rÚ_exit_callbacksrrrrrLÄs z_BaseExitStack.__init__cCst|ƒƒ}|j|_tƒ|_|S)z@Preserve the context stack by transferring it to a new instance.)rJržr)rÚ new_stackrrrÚpop_allÇs z_BaseExitStack.pop_allcCsBt|ƒ}z|j}Wnty| |¡Y|Sw| ||¡|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). )rJr!Ú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. )rJr!rr£©rr‘Ú_cm_typeÚ_exitÚresultrrrÚ enter_contextâs   z_BaseExitStack.enter_contextcOs,|j|g|¢Ri|¤Ž}||_| |¡|S)z\Registers an arbitrary callback and arguments. Cannot suppress exceptions. )rœÚ __wrapped__r¢©rr™r=r>ršrrrr™ðs z_BaseExitStack.callbackcCó| ||¡}| |d¡dS)z;Helper to correctly register callbacks to __exit__ methods.TN)r“r¢©rr‘r’ršrrrr£ýs z_BaseExitStack._push_cm_exitTcCs|j ||f¡dSr:)ržr{)rr™Úis_syncrrrr¢sz"_BaseExitStack._push_exit_callbackN)T)r+r,r-r.Ú staticmethodr“rœrLr r¨r­r™r£r¢rrrrr�·s   r�c@rh) ra‹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. cCrr:rrrrrrr‹zExitStack.__enter__c sÄ|ddu}t ¡d‰‡fdd„}d}d}|jrE|j ¡\}}z ||Žr+d}d}d}Wnt ¡}||d|dƒd}|}Y|js|r^z |dj} |d‚ty]| |d_‚w|oa|S)Nrécó6 |j}|dus ||urdS|ˆurn|}q||_dSr:©Ú __context__©Únew_excÚold_excÚ exc_context©Ú frame_excrrÚ_fix_exception_contextóù z2ExitStack.__exit__.._fix_exception_contextFT©NNN©r|rrržrr·rW) rÚ exc_detailsÚ received_excr¾Úsuppressed_excÚ pending_raiser²ÚcbÚnew_exc_detailsÚ fixed_ctxrr¼rr!s8   €ó   þzExitStack.__exit__cCs| ddd¡dS©z%Immediately unwind the context stack.N)r!rrrrrpFszExitStack.closeN)r+r,r-r.rr!rprrrrrs   1rc@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. cCrŽr:r�r�rrrÚ_create_async_exit_wrapperYr”z)AsyncExitStack._create_async_exit_wrappercr•)Nc“s�ˆˆiˆ¤ŽIdHdSr:rr–r˜rrrš_s€z>AsyncExitStack._create_async_cb_wrapper.._exit_wrapperrr›rr˜rÚ_create_async_cb_wrapper]r�z'AsyncExitStack._create_async_cb_wrappercÃs0�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)rJr6r5Ú_push_async_cm_exitr©rrrÚenter_async_contextcs € z"AsyncExitStack.enter_async_contextcCsDt|ƒ}z|j}Wnty| |d¡Y|Sw| ||¡|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)rJr6r¡r¢rÌr¤rrrÚpush_async_exitos  û zAsyncExitStack.push_async_exitcOs.|j|g|¢Ri|¤Ž}||_| |d¡|S)zfRegisters an arbitrary coroutine function and arguments. Cannot suppress exceptions. F)rËr®r¢r¯rrrÚpush_async_callback�s z"AsyncExitStack.push_async_callbackcÃs�| ddd¡IdHdSrÉ)r6rrrrrvŽs€zAsyncExitStack.aclosecCr°)zLHelper to correctly register coroutine function to __aexit__ method.FN)rÊr¢r±rrrrÌ’s z"AsyncExitStack._push_async_cm_exitcÃr4r:rrrrrr5˜ó€zAsyncExitStack.__aenter__c ‡sÞ�|ddu}t ¡d‰‡fdd„}d}d}|jrR|j ¡\}}z|r)||Ž}n||ŽIdH}|r8d}d}d}Wnt ¡} || d|dƒd}| }Y|js|rkz |dj} |d‚tyj| |d_‚w|on|S)Nrr´crµr:r¶r¸r¼rrr¾¡r¿z8AsyncExitStack.__aexit__.._fix_exception_contextFTrÀrÁ) rrÂrÃr¾rÄrÅr²rÆÚ cb_suppressrÇrÈrr¼rr6›s@€    €ï   þzAsyncExitStack.__aexit__N)r+r,r-r.r³rÊrËrÍrÎrÏrvrÌr5r6rrrrr Ls     r c@s:eZdZdZd dd„Zdd„Zdd„Zd d „Zd d „ZdS)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 NcCrir:©Ú enter_result)rrÓrrrrLÜrmznullcontext.__init__cCrnr:rÒrrrrrßroznullcontext.__enter__cGrŠr:r©rÚexcinforrrr!âr‹znullcontext.__exit__cÃrtr:rÒrrrrr5åruznullcontext.__aenter__cÇs�dSr:rrÔrrrr6èrÐznullcontext.__aexit__r:) r+r,r-r.rLrr!r5r6rrrrr Ñs  r )!r.r1r|r$Ú collectionsrÚ functoolsrÚtypesrrÚ__all__ÚABCr r ÚobjectrrFrGrNr]rrr rrwrrrr�rr r rrrrÚsH   ý @ýA!!PE