grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026 û“qc£0ã@sdZddlZddlmZddlmZddddd d d gZGd d„deƒZGd d„deƒZ dd„Z Gdd„deƒZ Gdd„dƒZ Gdd „d e ƒZ Gdd „d e ƒZGdd „d ƒZGdd„deƒZdS)z4Utilities for with-statement contexts. See PEP 343.éN)Údeque)ÚwrapsÚcontextmanagerÚclosingÚContextDecoratorÚ ExitStackÚredirect_stdoutÚredirect_stderrÚsuppressc@s.eZdZdZdd„Zdd„ZdS)rzJA 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. ©)Úselfr r ú//opt/alt/python35/lib64/python3.5/contextlib.pyÚ _recreate_cms zContextDecorator._recreate_cmcs%tˆƒ‡‡fdd†ƒ}|S)Nc s%ˆjƒ�ˆ||ŽSWdQRXdS)N)r)ÚargsÚkwds)Úfuncr r r Úinners z(ContextDecorator.__call__..inner)r)r rrr )rr r Ú__call__s!zContextDecorator.__call__N)Ú__name__Ú __module__Ú __qualname__Ú__doc__rrr r r r r s  c@sFeZdZdZdd„Zdd„Zdd„Zdd „Zd S) Ú_GeneratorContextManagerz%Helper for @contextmanager decorator.cCsi|||Ž|_||||_|_|_t|ddƒ}|dkr\t|ƒj}||_dS)Nr)ÚgenrrrÚgetattrÚtyper)r rrrÚdocr r r Ú__init__%s  z!_GeneratorContextManager.__init__cCs|j|j|j|jƒS)N)Ú __class__rrr)r r r r r3sz%_GeneratorContextManager._recreate_cmc Cs9yt|jƒSWn!tk r4tdƒd‚YnXdS)Nzgenerator didn't yield)ÚnextrÚ StopIterationÚ RuntimeError)r r r r Ú __enter__9s z"_GeneratorContextManager.__enter__cCs0|dkrEyt|jƒWntk r5dSYq,Xtdƒ‚nç|dkrZ|ƒ}y|jj|||ƒWn©tk r¥}z||k SWYdd}~Xn{tk rû}z7||krÇdS|tkræ|j|krædS‚WYdd}~Xn%tjƒd|krdS‚YnXtdƒ‚dS)NFzgenerator didn't stopéz#generator didn't stop after throw())rrr r!ÚthrowÚ __cause__ÚsysÚexc_info)r rÚvalueÚ tracebackÚexcr r r Ú__exit__?s.      z!_GeneratorContextManager.__exit__N)rrrrrrr"r+r r r r r"s    rcs"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: cstˆ||ƒS)N)r)rr)rr r Úhelper‡szcontextmanager..helper)r)rr,r )rr rksc@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)r r-r r r ržszclosing.__init__cCs|jS)N)r-)r r r r r" szclosing.__enter__cGs|jjƒdS)N)r-Úclose)r r'r r r r+¢szclosing.__exit__N)rrrrrr"r+r r r r r�s   c@s:eZdZdZdd„Zdd„Zdd„ZdS)Ú_RedirectStreamNcCs||_g|_dS)N)Ú _new_targetÚ _old_targets)r Ú new_targetr r r rªs z_RedirectStream.__init__cCs9|jjtt|jƒƒtt|j|jƒ|jS)N)r1Úappendrr&Ú_streamÚsetattrr0)r r r r r"¯sz_RedirectStream.__enter__cCs tt|j|jjƒƒdS)N)r5r&r4r1Úpop)r ÚexctypeÚexcinstÚexctbr r r r+´sz_RedirectStream.__exit__)rrrr4rr"r+r r r r r/¦s   r/c@seZdZdZdZdS)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)rrrrr4r r r r r¸s c@seZdZdZdZdS)r zCContext manager for temporarily redirecting stderr to another file.ÚstderrN)rrrrr4r r r r r Ès c@s:eZdZdZdd„Zdd„Zdd„ZdS) r a?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 Ú exceptionsr r r rÙszsuppress.__init__cCsdS)Nr )r r r r r"Üszsuppress.__enter__cCs|dk ot||jƒS)N)Ú issubclassr<)r r7r8r9r r r r+ßs zsuppress.__exit__N)rrrrrr"r+r r r r r Îs   c@s‚eZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dd„Z dd„Z dS)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 cCstƒ|_dS)N)rÚ_exit_callbacks)r r r r rùszExitStack.__init__cCs+t|ƒƒ}|j|_tƒ|_|S)z?Preserve the context stack by transferring it to a new instance)rr?r)r Ú new_stackr r r Úpop_allüs  zExitStack.pop_allcs/‡‡fdd†}ˆ|_|j|ƒdS)z:Helper to correctly register callbacks to __exit__ methodscs ˆˆ|ŒS)Nr )Ú exc_details)ÚcmÚcm_exitr r Ú _exit_wrappersz.ExitStack._push_cm_exit.._exit_wrapperN)Ú__self__Úpush)r rCrDrEr )rCrDr Ú _push_cm_exits zExitStack._push_cm_exitc CsRt|ƒ}y |j}Wn"tk r=|jj|ƒYnX|j||ƒ|S)aRegisters a callback with the standard __exit__ method signature Can suppress exceptions the same way __exit__ methods can. Also accepts any object with an __exit__ method (registering a call to the method instead of the object itself) )rr+ÚAttributeErrorr?r3rH)r ÚexitÚ_cb_typeÚ exit_methodr r r rG s   zExitStack.pushcs2‡‡‡fdd†}ˆ|_|j|ƒˆS)z\Registers an arbitrary callback and arguments. Cannot suppress exceptions. csˆˆˆŽdS)Nr )Úexc_typer*Útb)rÚcallbackrr r rE#sz)ExitStack.callback.._exit_wrapper)Ú __wrapped__rG)r rOrrrEr )rrOrr rOs  zExitStack.callbackcCs8t|ƒ}|j}|j|ƒ}|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. )rr+r"rH)r rCÚ_cm_typeÚ_exitÚresultr r r Ú enter_context+s   zExitStack.enter_contextcCs|jdddƒdS)z$Immediately unwind the context stackN)r+)r r r r r.8szExitStack.closecCs|S)Nr )r r r r r"<szExitStack.__enter__c s|ddk }tjƒd‰‡fdd†}d}d}xv|jr¶|jjƒ}y"||Œrzd}d}d}WqAtjƒ}||d|dƒd}|}YqAXqAW|rýy|dj}|d‚Wn"tk rü||d_‚YnX|o|S)Nrr#csLx<|j}||krdS|dks4|ˆkr5P|}qW||_dS)N)Ú __context__)Únew_excÚold_excÚ exc_context)Ú frame_excr r Ú_fix_exception_contextEs   z2ExitStack.__exit__.._fix_exception_contextFT)NNN)r&r'r?r6rUÚ BaseException) r rBÚ received_excrZÚsuppressed_excÚ pending_raiseÚcbÚnew_exc_detailsÚ fixed_ctxr )rYr r+?s2       zExitStack.__exit__N) rrrrrrArHrGrOrTr.r"r+r r r r rís       )rr&Ú collectionsrÚ functoolsrÚ__all__Úobjectrrrrr/rr r rr r r r Ús  I "