grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026î   fw-ã@sØdZddlZddlmZddlmZddddd d gZGd d„deƒZGd d „d eƒZ dd„Z Gdd„deƒZ Gdd „d ƒZ Gdd „d ƒZ Gdd„deƒZdS)z4Utilities for with-statement contexts. See PEP 343.éN)Údeque)ÚwrapsÚcontextmanagerÚclosingÚContextDecoratorÚ ExitStackÚredirect_stdoutÚ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/python34/lib64/python3.4/contextlib.pyÚ _recreate_cms zContextDecorator._recreate_cmcs%tˆƒ‡‡fdd†ƒ}|S)Nc s$ˆjƒ�ˆ||ŽSWdQXdS)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__r rr r r r r s  c@sFeZdZdZdd„Zdd„Zdd„Zdd „Zd S) Ú_GeneratorContextManagerz%Helper for @contextmanager decorator.cCsl|||Ž|_||||_|_|_t|ddƒ}|dkr_t|ƒj}n||_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 r 3sz%_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__cCsÜ|dkrEyt|jƒWntk r5dSYqØXtdƒ‚n“|dkr]|ƒ}ny&|jj|||ƒtdƒ‚WnRtk r´}z||k SWYdd}~Xn$tjƒd|k rÓ‚nYnXdS)Nzgenerator didn't stopz#generator didn't stop after throw()é)rrrr ÚthrowÚsysÚexc_info)r rÚvalueÚ tracebackÚexcr r r Ú__exit__?s      z!_GeneratorContextManager.__exit__N)rrrrrr r!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 r`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)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) ra@Context 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) cCs||_g|_dS)N)Ú _new_targetÚ _old_targets)r Ú new_targetr r r r§s zredirect_stdout.__init__cCs&|jjtjƒ|jt_|jS)N)r.Úappendr$Ústdoutr-)r r r r r!¬s zredirect_stdout.__enter__cCs|jjƒt_dS)N)r.Úpopr$r1)r ÚexctypeÚexcinstÚexctbr r r r)±szredirect_stdout.__exit__N)rrrrrr!r)r 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)Ú issubclassr6)r r3r4r5r 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)rr9r)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_wrapperìsz.ExitStack._push_cm_exit.._exit_wrapperN)Ú__self__Úpush)r r=r>r?r )r=r>r Ú _push_cm_exitês 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)ÚAttributeErrorr9r0rB)r ÚexitÚ_cb_typeÚ exit_methodr r r rAñ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 r? sz)ExitStack.callback.._exit_wrapper)Ú __wrapped__rA)r rIrrr?r )rrIrr rIs  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!rB)r r=Ú_cm_typeÚ_exitÚresultr r r Ú enter_contexts   zExitStack.enter_contextcCs|jdddƒdS)z$Immediately unwind the context stackN)r))r r r r r,szExitStack.closecCs|S)Nr )r r r r r!#szExitStack.__enter__c s |ddk }tjƒd‰‡fdd†}d}d}xy|jr¹|jjƒ}y%||Œr}d}d}d}nWqAtjƒ}||d|dƒd}|}YqAXqAW|ry|dj}|d‚Wqtk rÿ||d_‚YqXn|o |S)Nrr"csOx?|j}||krdS|dks4|ˆkr8Pn|}qW||_dS)N)Ú __context__)Únew_excÚold_excÚ exc_context)Ú frame_excr r Ú_fix_exception_context,s   z2ExitStack.__exit__.._fix_exception_contextFT)NNN)r$r%r9r2rOÚ BaseException) r r<Ú received_excrTÚsuppressed_excÚ pending_raiseÚcbÚnew_exc_detailsÚ fixed_ctxr )rSr r)&s2        zExitStack.__exit__N) rrrrrr;rBrArIrNr,r!r)r r r r rÔs       )rr$Ú collectionsrÚ functoolsrÚ__all__Úobjectrrrrrr rr r r r Ús   > "