U ÀÒ.eBã @s¸dZddlZddlZddlZddlmZddlmZddddd d d d d g Zedg7Zej dd…dkrnej Z ne  de fddi¡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 dd#kZe�r@d$d%„Zd&d'„Znd(d%„Zd)d'„Zzdd*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__©cCs0||kr dS| |¡|jD]}t||ƒq|S©N)ÚappendÚ __bases__Ú _classic_mro)ÚCÚresultÚBrrú;/usr/lib/python3.8/site-packages/pip/_vendor/contextlib2.pyrs    rcGsvz |j}Wn"tk r,tt|gƒƒ}YnX|D]>}|D],}||jkr:|j|dkrbtSq2q:tSq2dS)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.innerr)r'r?r@rr>rÚ__call__iszContextDecorator.__call__N)r/r0r1r2r:r9rArrrrr Is c@s0eZdZdZdd„Zdd„Zdd„Zdd „Zd S) Ú_GeneratorContextManagerz%Helper for @contextmanager decorator.cCsJ|||Ž|_||||_|_|_t|ddƒ}|dkr@t|ƒj}||_dS)Nr2)Úgenr?r<r=ÚgetattrÚtyper2)r'r?r<r=ÚdocrrrÚ__init__ts    z!_GeneratorContextManager.__init__cCs| |j|j|j¡Sr)Ú __class__r?r<r=r&rrrr9‚sz%_GeneratorContextManager._recreate_cmcCs.z t|jƒWStk r(tdƒ‚YnXdS)Nzgenerator didn't yield)ÚnextrCÚ StopIterationÚ RuntimeErrorr&rrrr(ˆs z"_GeneratorContextManager.__enter__c Csþ|dkr8zt|jƒWntk r,YdSXtdƒ‚nÂ|dkrF|ƒ}z|j |||¡tdƒ‚Wn–tk r�}z||k WY¢Sd}~XYnjtk rÜ}z0||kr²WY¢"dStrÊ|j|krÊWY¢ dS‚W5d}~XYnt ¡d|k rô‚YnXdS)Nzgenerator didn't stopz#generator didn't stop after throw()Fé) rIrCrJrKÚthrowÚ_HAVE_EXCEPTION_CHAININGÚ __cause__ÚsysÚexc_info)r'rEÚvaluer+Úexcrrrr,Žs,    z!_GeneratorContextManager.__exit__N)r/r0r1r2rGr9r(r,rrrrrBqs rBcstˆƒ‡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ˆ||ƒSr)rBr;©r?rrÚhelperÕszcontextmanager..helperr)r?rUrrTrr¹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 ||_dSr©Úthing)r'rWrrrrGìszclosing.__init__cCs|jSrrVr&rrrr(ïszclosing.__enter__cGs|j ¡dSr)rWÚclose)r'rQrrrr,òszclosing.__exit__N©r/r0r1r2rGr(r,rrrrrÛsc@s(eZdZdZdd„Zdd„Zdd„ZdS)Ú_RedirectStreamNcCs||_g|_dSr)Ú _new_targetÚ _old_targets)r'Ú new_targetrrrrGúsz_RedirectStream.__init__cCs*|j tt|jƒ¡tt|j|jƒ|jSr)r\rrDrPÚ_streamÚsetattrr[r&rrrr(ÿsz_RedirectStream.__enter__cCstt|j|j ¡ƒdSr)r_rPr^r\Úpop©r'ÚexctypeÚexcinstÚexctbrrrr,sz_RedirectStream.__exit__)r/r0r1r^rGr(r,rrrrrZösrZc@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/r0r1r2r^rrrrr s c@seZdZdZdZdS)r zCContext manager for temporarily redirecting stderr to another file.ÚstderrNrfrrrrr 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 ||_dSr)Ú _exceptions)r'Ú exceptionsrrrrG)szsuppress.__init__cCsdSrrr&rrrr(,szsuppress.__enter__cCs|dk ot||jƒSr)Ú issubclassrhrarrrr,/s zsuppress.__exit__NrYrrrrr s rcs‡fdd„}|S)Ncs4|j}||krdS|dks*|ˆkr$q*|}q||_dSr)Ú __context__)Únew_excÚold_excÚ exc_context©Ú frame_excrrÚ_fix_exception_context@sz3_make_context_fixer.._fix_exception_contextr)rprqrrorÚ_make_context_fixer?s rrcCs<z|dj}|d‚Wn tk r6||d_‚YnXdS)NrL)rkÚ BaseException)Ú exc_detailsÚ fixed_ctxrrrÚ_reraise_with_existing_contextOs    rvcCsdd„S)NcSsdSrr)rlrmrrrÚ[óz%_make_context_fixer..rrorrrrrZscCs|\}}}tdƒdS)Nz!raise exc_type, exc_value, exc_tb)Úexec)rtr)r*Zexc_tbrrrrv_s )Ú InstanceTypecCst|ƒ}|tkr|jS|Sr)rErzrH)ÚobjZobj_typerrrÚ _get_typeksr|c@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ƒ|_dSr)rÚ_exit_callbacksr&rrrrGszExitStack.__init__cCst|ƒƒ}|j|_tƒ|_|S)z?Preserve the context stack by transferring it to a new instance)rEr}r)r'Ú new_stackrrrÚpop_all‚s zExitStack.pop_allcs"‡‡fdd„}ˆ|_| |¡dS)z:Helper to correctly register callbacks to __exit__ methodscsˆˆf|žŽSrr)rt©ÚcmÚcm_exitrrÚ _exit_wrapper‹sz.ExitStack._push_cm_exit.._exit_wrapperN)Ú__self__Úpush)r'r�r‚rƒrr€rÚ _push_cm_exit‰szExitStack._push_cm_exitcCsDt|ƒ}z |j}Wn tk r2|j |¡YnX| ||¡|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) )r|r,rr}rr†)r'ÚexitÚ_cb_typeÚ exit_methodrrrr…�s   zExitStack.pushcs$‡‡‡fdd„}ˆ|_| |¡ˆS)z\Registers an arbitrary callback and arguments. Cannot suppress exceptions. csˆˆˆŽdSrr)r)rSÚtb©r<Úcallbackr=rrrƒ©sz)ExitStack.callback.._exit_wrapper)Ú __wrapped__r…)r'rŒr<r=rƒrr‹rrŒ¤s zExitStack.callbackcCs(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. )r|r,r(r†)r'r�Ú_cm_typeÚ_exitrrrrÚ enter_context±s   zExitStack.enter_contextcCs| ddd¡dS)z$Immediately unwind the context stackN)r,r&rrrrX¾szExitStack.closecCs|Srrr&rrrr(ÂszExitStack.__enter__c Gs–|ddk }t ¡d}t|ƒ}d}d}|jr‚|j ¡}z||ŽrNd}d}d}Wq(t ¡}||d|dƒd}|}Yq(Xq(|rŽt|ƒ|o”|S)NrrLFT)NNN)rPrQrrr}r`rv) r'rtÚ received_excrprqÚsuppressed_excÚ pending_raiseÚcbÚnew_exc_detailsrrrr,Ås(    zExitStack.__exit__N) r/r0r1r2rGrr†r…rŒr�rXr(r,rrrrr ss   cs8eZdZdZ‡fdd„Zdd„Zdd„Zdd „Z‡ZS) rz+Backwards compatibility alias for ExitStackcst dt¡tt|ƒ ¡dS)Nz*ContextStack has been renamed to ExitStack)r6r7r8ÚsuperrrGr&©rHrrrGçsÿzContextStack.__init__cCs | |¡Sr)r…)r'rŒrrrÚ register_exitìszContextStack.register_exitcOs|j|f|ž|ŽSr)rŒ)r'rŒr<r=rrrÚregisterïszContextStack.registercCs| ¡Sr)rr&rrrÚpreserveòszContextStack.preserve) r/r0r1r2rGr˜r™ršÚ __classcell__rrr—rrä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 ||_dSr©Ú enter_result)r'r�rrrrGÿsznullcontext.__init__cCs|jSrrœr&rrrr(sznullcontext.__enter__cGsdSrr)r'Úexcinforrrr,sznullcontext.__exit__)NrYrrrrrös )$r2r3rPr6Ú collectionsrÚ functoolsrÚ__all__Ú version_inforZ_abc_ABCÚABCMetaÚobjectrr%rr rBrrrZr r r rNrrrvÚtypesrzÚ ImportErrorrEr|r rrrrrrÚsT  ý  (H"  q