3 ”ReBã @s¸dZddlZddlZddlZddlmZddlmZddddd d d d d g Zedg7Zej dd…d/krnej Z nej de fdfiƒZ dd„Zdd„ZGdd„de ƒZGdd „d e ƒZGdd„deƒZdd„ZGdd„de ƒZGdd„de ƒZGd d „d eƒZGd!d „d eƒZGd"d „d e ƒZej ddkZe�r@d#d$„Zd%d&„Znd'd$„Zd(d&„Zydd)lmZWnek �rzeZ Yn Xd*d+„Z Gd,d „d e ƒZ!Gd-d„de!ƒZ"Gd.d„deƒZ#dS)0zAcontextlib2 - backports and enhancements to the contextlib moduleéN)Údeque)ÚwrapsÚcontextmanagerÚclosingÚ nullcontextÚAbstractContextManagerÚContextDecoratorÚ ExitStackÚredirect_stdoutÚredirect_stderrÚsuppressÚ ContextStackéééÚABCÚ __slots__cCs4||kr dS|j|ƒx|jD]}t||ƒqW|S)N)ÚappendÚ __bases__Ú _classic_mro)ÚCÚresultÚB©rú†/builddir/build/BUILDROOT/alt-python36-pip-20.2.4-5.el8.x86_64/opt/alt/python36/lib/python3.6/site-packages/pip/_vendor/contextlib2.pyrs   rc Gspy |j}Wn"tk r,tt|gƒƒ}YnXx<|D]4}x.|D]"}||jkr>|j|dkr^tSPq>WtSq4WdS)NT)Ú__mro__ÚAttributeErrorÚtuplerÚ__dict__ÚNotImplemented)rÚmethodsÚmroÚmethodrrrrÚ_check_methods$s    r#c@s2eZdZdZdd„Zejdd„ƒZedd„ƒZ dS) rz,An abstract base class for context managers.cCs|S)z0Return `self` upon entering the runtime context.r)ÚselfrrrÚ __enter__8sz 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)z.inner)r)r$r:r;r)r:r$rÚ__call__iszContextDecorator.__call__N)r,r-r.r/r7r6r<rrrrrIs c@s0eZdZdZdd„Zdd„Zdd„Zdd „Zd S) Ú_GeneratorContextManagerz%Helper for @contextmanager decorator.cCsJ|||Ž|_||||_|_|_t|ddƒ}|dkr@t|ƒj}||_dS)Nr/)Úgenr:r8r9ÚgetattrÚtyper/)r$r:r8r9ÚdocrrrÚ__init__ts    z!_GeneratorContextManager.__init__cCs|j|j|j|jƒS)N)Ú __class__r:r8r9)r$rrrr6‚sz%_GeneratorContextManager._recreate_cmc Cs,y t|jƒStk r&tdƒ‚YnXdS)Nzgenerator didn't yield)Únextr>Ú StopIterationÚ RuntimeError)r$rrrr%ˆs z"_GeneratorContextManager.__enter__cCsæ|dkr6yt|jƒWntk r*dSXtdƒ‚n¬|dkrD|ƒ}y|jj|||ƒtdƒ‚Wn€tk r„}z||k Sd}~Xn^tk rÄ}z&||kr dStr²|j|kr²dS‚WYdd}~Xntjƒd|k rÜ‚YnXdS)Nzgenerator didn't stopz#generator didn't stop after throw()Fé) rDr>rErFÚthrowÚ_HAVE_EXCEPTION_CHAININGÚ __cause__ÚsysÚexc_info)r$r@Úvaluer(Úexcrrrr)Žs,  z!_GeneratorContextManager.__exit__N)r,r-r.r/rBr6r%r)rrrrr=qs r=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)r=)r8r9)r:rrÚhelperÕszcontextmanager..helper)r)r:rOr)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)r$rPrrrrBìszclosing.__init__cCs|jS)N)rP)r$rrrr%ïszclosing.__enter__cGs|jjƒdS)N)rPÚclose)r$rLrrrr)òszclosing.__exit__N)r,r-r.r/rBr%r)rrrrrÛsc@s(eZdZdZdd„Zdd„Zdd„ZdS)Ú_RedirectStreamNcCs||_g|_dS)N)Ú _new_targetÚ _old_targets)r$Ú new_targetrrrrBúsz_RedirectStream.__init__cCs*|jjtt|jƒƒtt|j|jƒ|jS)N)rTrr?rKÚ_streamÚsetattrrS)r$rrrr%ÿsz_RedirectStream.__enter__cCstt|j|jjƒƒdS)N)rWrKrVrTÚpop)r$ÚexctypeÚexcinstÚexctbrrrr)sz_RedirectStream.__exit__)r,r-r.rVrBr%r)rrrrrRösrRc@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)r,r-r.r/rVrrrrr s c@seZdZdZdZdS)r zCContext manager for temporarily redirecting stderr to another file.ÚstderrN)r,r-r.r/rVrrrrr sc@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$Ú exceptionsrrrrB)szsuppress.__init__cCsdS)Nr)r$rrrr%,szsuppress.__enter__cCs|dk ot||jƒS)N)Ú issubclassr^)r$rYrZr[rrrr)/s zsuppress.__exit__N)r,r-r.r/rBr%r)rrrrr s cs‡fdd„}|S)Ncs8x,|j}||krdS|dks$|ˆkr&P|}qW||_dS)N)Ú __context__)Únew_excÚold_excÚ exc_context)Ú frame_excrrÚ_fix_exception_context@sz3_make_context_fixer.._fix_exception_contextr)rerfr)rerÚ_make_context_fixer?s rgc Cs<y|dj}|d‚Wn tk r6||d_‚YnXdS)NrG)raÚ BaseException)Ú exc_detailsÚ fixed_ctxrrrÚ_reraise_with_existing_contextOs    rkcCsdd„S)NcSsdS)Nr)rbrcrrrÚ[sz%_make_context_fixer..r)rerrrrgZscCs|\}}}tdƒdS)Nz!raise exc_type, exc_value, exc_tb)Úexec)rir&r'Úexc_tbrrrrk_s )Ú InstanceTypecCst|ƒ}|tkr|jS|S)N)r@rorC)ÚobjZobj_typerrrÚ _get_typeksrqc@sXeZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dd„Z dd„Z dS)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 tƒ|_dS)N)rÚ_exit_callbacks)r$rrrrBszExitStack.__init__cCst|ƒƒ}|j|_tƒ|_|S)z?Preserve the context stack by transferring it to a new instance)r@rrr)r$Ú new_stackrrrÚpop_all‚s zExitStack.pop_allcs"‡‡fdd„}ˆ|_|j|ƒdS)z:Helper to correctly register callbacks to __exit__ methodscsˆˆf|žŽS)Nr)ri)ÚcmÚcm_exitrrÚ _exit_wrapper‹sz.ExitStack._push_cm_exit.._exit_wrapperN)Ú__self__Úpush)r$rurvrwr)rurvrÚ _push_cm_exit‰szExitStack._push_cm_exitc CsDt|ƒ}y |j}Wn tk r2|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) )rqr)rrrrrz)r$ÚexitÚ_cb_typeÚ exit_methodrrrry�s   zExitStack.pushcs$‡‡‡fdd„}ˆ|_|j|ƒˆS)z\Registers an arbitrary callback and arguments. Cannot suppress exceptions. csˆˆˆŽdS)Nr)r&rNÚtb)r8Úcallbackr9rrrw©sz)ExitStack.callback.._exit_wrapper)Ú __wrapped__ry)r$rr8r9rwr)r8rr9rr¤s zExitStack.callbackcCs(t|ƒ}|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. )rqr)r%rz)r$ruÚ_cm_typeÚ_exitrrrrÚ enter_context±s   zExitStack.enter_contextcCs|jdddƒdS)z$Immediately unwind the context stackN)r))r$rrrrQ¾szExitStack.closecCs|S)Nr)r$rrrr%ÂszExitStack.__enter__c Gsš|ddk }tjƒd}t|ƒ}d}d}x\|jr„|jjƒ}y||ŽrPd}d}d}Wq*tjƒ}||d|dƒd}|}Yq*Xq*W|r’t|ƒ|o˜|S)NrrGFT)NNN)rKrLrgrrrXrk) r$riÚ received_excrerfÚsuppressed_excÚ pending_raiseÚcbÚnew_exc_detailsrrrr)Ås(   zExitStack.__exit__N) r,r-r.r/rBrtrzryrrƒrQr%r)rrrrr ss   cs8eZdZdZ‡fdd„Zdd„Zdd„Zdd „Z‡ZS) r z+Backwards compatibility alias for ExitStackcstjdtƒtt|ƒjƒdS)Nz*ContextStack has been renamed to ExitStack)r3r4r5Úsuperr rB)r$)rCrrrBçszContextStack.__init__cCs |j|ƒS)N)ry)r$rrrrÚ register_exitìszContextStack.register_exitcOs|j|f|ž|ŽS)N)r)r$rr8r9rrrÚregisterïszContextStack.registercCs|jƒS)N)rt)r$rrrÚpreserveòszContextStack.preserve) r,r-r.r/rBrŠr‹rŒÚ __classcell__rr)rCrr äs  c@s*eZdZdZd dd„Zdd„Zdd„ZdS) raMContext 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)r$rŽrrrrBÿsznullcontext.__init__cCs|jS)N)rŽ)r$rrrr%sznullcontext.__enter__cGsdS)Nr)r$Úexcinforrrr)sznullcontext.__exit__)N)r,r-r.r/rBr%r)rrrrrös )rr)$r/r0rKr3Ú collectionsrÚ functoolsrÚ__all__Ú version_inforÚ_abc_ABCÚABCMetaÚobjectrr#rrr=rrrRr r r rIrgrkÚtypesroÚ ImportErrorr@rqr r rrrrrÚsL     (H"  q