U .eB @sdZddlZddlZddlZddlmZddlmZddddd d d d d g Zedg7Zej dddkrnej Z ne de fddiZ ddZddZGddde ZGdd d e ZGdddeZddZGddde ZGddde ZGd d d eZGd!d d eZGd"d d e Zej dd#kZer@d$d%Zd&d'Znd(d%Zd)d'Zzdd*lmZWnek rzeZ Yn Xd+d,Z Gd-d d e Z!Gd.dde!Z"Gd/ddeZ#dS)0zAcontextlib2 - backports and enhancements to the contextlib moduleN)dequewrapscontextmanagerclosing nullcontextAbstractContextManagerContextDecorator ExitStackredirect_stdoutredirect_stderrsuppress ContextStack)ABC __slots__cCs0||kr dS|||jD]}t||q|SN)append __bases__ _classic_mro)CresultBrr;/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__AttributeErrortupler__dict__NotImplemented)rmethodsmromethodrrrr_check_methods$s    r%c@s2eZdZdZddZejddZeddZ dS) rz,An abstract base class for context managers.cCs|S)z0Return `self` upon entering the runtime context.rselfrrr __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|ddStS)z.innerr)r'r?r@rr>r__call__iszContextDecorator.__call__N)r/r0r1r2r:r9rArrrrr Is c@s0eZdZdZddZddZddZdd Zd S) _GeneratorContextManagerz%Helper for @contextmanager decorator.cCsJ||||_||||_|_|_t|dd}|dkr@t|j}||_dS)Nr2)genr?r<r=getattrtyper2)r'r?r<r=docrrr__init__ts    z!_GeneratorContextManager.__init__cCs||j|j|jSr) __class__r?r<r=r&rrrr9sz%_GeneratorContextManager._recreate_cmcCs.z t|jWStk r(tdYnXdS)Nzgenerator didn't yield)nextrC StopIteration RuntimeErrorr&rrrr(s z"_GeneratorContextManager.__enter__c Cs|dkr8zt|jWntk r,YdSXtdn|dkrF|}z|j|||tdWntk r}z||k WYSd}~XYnjtk r}z0||krWY"dStr|j|krWY dSW5d}~XYntd|k rYnXdS)Nzgenerator didn't stopz#generator didn't stop after throw()F) rIrCrJrKthrow_HAVE_EXCEPTION_CHAINING __cause__sysexc_info)r'rEvaluer+excrrrr,s,    z!_GeneratorContextManager.__exit__N)r/r0r1r2rGr9r(r,rrrrrBqs rBcstfdd}|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?rrhelperszcontextmanager..helperr)r?rUrrTrrsc@s(eZdZdZddZddZddZdS) 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 ||_dSrthing)r'rWrrrrGszclosing.__init__cCs|jSrrVr&rrrr(szclosing.__enter__cGs|jdSr)rWclose)r'rQrrrr,szclosing.__exit__Nr/r0r1r2rGr(r,rrrrrsc@s(eZdZdZddZddZddZdS)_RedirectStreamNcCs||_g|_dSr) _new_target _old_targets)r' new_targetrrrrGsz_RedirectStream.__init__cCs*|jtt|jtt|j|j|jSr)r\rrDrP_streamsetattrr[r&rrrr(sz_RedirectStream.__enter__cCstt|j|jdSr)r_rPr^r\popr'exctypeexcinstexctbrrrr,sz_RedirectStream.__exit__)r/r0r1r^rGr(r,rrrrrZsrZc@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) stdoutNr/r0r1r2r^rrrrr s c@seZdZdZdZdS)r zCContext manager for temporarily redirecting stderr to another file.stderrNrfrrrrr sc@s(eZdZdZddZddZddZdS) 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||jSr) issubclassrhrarrrr,/s zsuppress.__exit__NrYrrrrr s rcsfdd}|S)Ncs4|j}||krdS|dks*|kr$q*|}q||_dSr) __context__)new_excold_exc exc_context frame_excrr_fix_exception_context@sz3_make_context_fixer.._fix_exception_contextr)rprqrror_make_context_fixer?s rrcCs<z|dj}|dWn tk r6||d_YnXdS)NrL)rk BaseException) exc_details fixed_ctxrrr_reraise_with_existing_contextOs    rvcCsddS)NcSsdSrr)rlrmrrr[z%_make_context_fixer..rrorrrrrZscCs|\}}}tddS)Nz!raise exc_type, exc_value, exc_tb)exec)rtr)r*Zexc_tbrrrrv_s ) InstanceTypecCst|}|tkr|jS|Sr)rErzrH)objZobj_typerrr _get_typeksr|c@sXeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ dS)r aContext 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_stackrrrpop_alls zExitStack.pop_allcs"fdd}|_||dS)z:Helper to correctly register callbacks to __exit__ methodscsf|Srr)rtcmcm_exitrr _exit_wrappersz.ExitStack._push_cm_exit.._exit_wrapperN)__self__push)r'rrrrrr _push_cm_exitszExitStack._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_methodrrrrs   zExitStack.pushcs$fdd}|_||S)z\Registers an arbitrary callback and arguments. Cannot suppress exceptions. csdSrr)r)rStbr<callbackr=rrrsz)ExitStack.callback.._exit_wrapper) __wrapped__r)r'rr<r=rrrrrs zExitStack.callbackcCs(t|}|j}||}||||S)zEnters 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_contexts   zExitStack.enter_contextcCs|ddddS)z$Immediately unwind the context stackN)r,r&rrrrXszExitStack.closecCs|Srrr&rrrr(szExitStack.__enter__c Gs|ddk }td}t|}d}d}|jr|j}z||rNd}d}d}Wq(t}||d|dd}|}Yq(Xq(|rt||o|S)NrrLFT)NNN)rPrQrrr}r`rv) r'rt received_excrprqsuppressed_exc pending_raisecbnew_exc_detailsrrrr,s(    zExitStack.__exit__N) r/r0r1r2rGrrrrrrXr(r,rrrrr ss   cs8eZdZdZfddZddZddZdd ZZS) rz+Backwards compatibility alias for ExitStackcstdttt|dS)Nz*ContextStack has been renamed to ExitStack)r6r7r8superrrGr&rHrrrGszContextStack.__init__cCs ||Sr)r)r'rrrr register_exitszContextStack.register_exitcOs|j|f||Sr)r)r'rr<r=rrrregisterszContextStack.registercCs|Sr)rr&rrrpreserveszContextStack.preserve) r/r0r1r2rGrrr __classcell__rrrrrs  c@s*eZdZdZd ddZddZddZdS) 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'rrrrrGsznullcontext.__init__cCs|jSrrr&rrrr(sznullcontext.__enter__cGsdSrr)r'excinforrrr,sznullcontext.__exit__)NrYrrrrrs )$r2r3rPr6 collectionsr functoolsr__all__ version_inforZ_abc_ABCABCMetaobjectrr%rr rBrrrZr r r rNrrrvtypesrz ImportErrorrEr|r rrrrrrsT    (H"  q