ó ­sWc@s°dZddlZddlZddlmZddlmZddddd d d gZed g7Zdefd „ƒYZ de fd„ƒYZ d„Z defd„ƒYZ defd„ƒYZ d e fd„ƒYZd e fd„ƒYZd efd„ƒYZejddkZer7d„Zd„Znd„Zd„ZyddlmZWnek rveZn Xd„Zdefd„ƒYZd efd „ƒYZdS(!sAcontextlib2 - backports and enhancements to the contextlib moduleiÿÿÿÿN(tdeque(twrapstcontextmanagertclosingtContextDecoratort ExitStacktredirect_stdouttredirect_stderrtsuppresst ContextStackcBs)eZdZd„Zd„Zd„ZRS(sJA base class or mixin that enables context managers to work as decorators.cCstjdtƒ|jƒS(sÁReturns the context manager used to actually wrap the call to the decorated function. The default implementation just returns *self*. Overriding this method allows otherwise one-shot context managers like _GeneratorContextManager to support use as decorators via implicit recreation. DEPRECATED: refresh_cm was never added to the standard library's ContextDecorator API s2refresh_cm was never added to the standard library(twarningstwarntDeprecationWarningt _recreate_cm(tself((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyt refresh_cms cCs|S(s6Return 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. ((R((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyR "s cs"tˆƒ‡‡fd†ƒ}|S(Ncs$ˆjƒˆ||ŽSWdQXdS(N(R (targstkwds(tfuncR(s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pytinner/s (R(RRR((RRs</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyt__call__.s(t__name__t __module__t__doc__RR R(((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyRs  t_GeneratorContextManagercBs2eZdZd„Zd„Zd„Zd„ZRS(s%Helper for @contextmanager decorator.cCsl|||Ž|_||||_|_|_t|ddƒ}|dkr_t|ƒj}n||_dS(NR(tgenRRRtgetattrtNonettypeR(RRRRtdoc((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyt__init__9s  cCs|j|j|j|jƒS(N(t __class__RRR(R((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyR GscCs5yt|jƒSWntk r0tdƒ‚nXdS(Nsgenerator didn't yield(tnextRt StopIterationt RuntimeError(R((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyt __enter__Ms cCs|dkrAyt|jƒWntk r1dSXtdƒ‚n»|dkrY|ƒ}ny&|jj|||ƒtdƒ‚Wnztk r›}||k Stk rÙ}||krºtStrÓ|j|krÓtS‚n#t j ƒd|k rü‚qünXdS(Nsgenerator didn't stops#generator didn't stop after throw()i( RR RR!R"tthrowtFalset_HAVE_EXCEPTION_CHAININGt __cause__tsystexc_info(RRtvaluet tracebacktexc((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyt__exit__Ss,      (RRRRR R#R-(((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyR6s    cstˆƒ‡fd†ƒ}|S(sÜ@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(R(s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pythelperšs(R(RR.((Rs</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyR~scBs)eZdZd„Zd„Zd„ZRS(s2Context 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(tthing(RR/((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyR±scCs|jS(N(R/(R((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyR#³scGs|jjƒdS(N(R/tclose(RR)((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyR-µs(RRRRR#R-(((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyR s  t_RedirectStreamcBs)eZdZd„Zd„Zd„ZRS(cCs||_g|_dS(N(t _new_targett _old_targets(Rt new_target((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyR½s cCs9|jjtt|jƒƒtt|j|jƒ|jS(N(R3tappendRR(t_streamtsetattrR2(R((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyR#ÂscCs tt|j|jjƒƒdS(N(R7R(R6R3tpop(Rtexctypetexcinsttexctb((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyR-ÇsN(RRRR6RR#R-(((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyR1¹s  cBseZdZdZRS(sAContext 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) tstdout(RRRR6(((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyRËs cBseZdZdZRS(sCContext manager for temporarily redirecting stderr to another file.tstderr(RRRR6(((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyRÛscBs)eZdZd„Zd„Zd„ZRS(s?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(t _exceptions(Rt exceptions((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyRìscCsdS(N((R((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyR#ïscCs|dk ot||jƒS(N(Rt issubclassR>(RR9R:R;((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyR-òs (RRRRR#R-(((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyRás   iics‡fd†}|S(NcsOx?|j}||krdS|dks4|ˆkr8Pn|}qW||_dS(N(t __context__R(tnew_exctold_exct exc_context(t frame_exc(s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyt_fix_exception_contexts   ((RERF((REs</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyt_make_context_fixers cCsCy|dj}|d‚Wn!tk r>||d_‚nXdS(Ni(RAt BaseException(t exc_detailst fixed_ctx((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyt_reraise_with_existing_contexts    cCsd„S(NcSsdS(N(R(RBRC((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pytt((RE((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyRGscBs|\}}}ddUdS(Ns!raise exc_type, exc_value, exc_tb((RItexc_typet exc_valuetexc_tb((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyRK"s(t InstanceTypecCs#t|ƒ}|tkr|jS|S(N(RRQR(tobjtobj_type((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyt _get_type.s  cBs_eZdZd„Zd„Zd„Zd„Zd„Zd„Zd„Z d„Z d „Z RS( s‹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(Rt_exit_callbacks(R((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyRAscCs+t|ƒƒ}|j|_tƒ|_|S(s?Preserve the context stack by transferring it to a new instance(RRUR(Rt new_stack((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pytpop_allDs  cs,‡‡fd†}ˆ|_|j|ƒdS(s:Helper to correctly register callbacks to __exit__ methodscs ˆˆ|ŒS(N((RI(tcmtcm_exit(s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyt _exit_wrapperMsN(t__self__tpush(RRXRYRZ((RXRYs</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyt _push_cm_exitKs cCsQt|ƒ}y |j}Wn!tk r<|jj|ƒnX|j||ƒ|S(sRegisters 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) (RTR-tAttributeErrorRUR5R](Rtexitt_cb_typet exit_method((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyR\Rs   cs/‡‡‡fd†}ˆ|_|j|ƒˆS(s\Registers an arbitrary callback and arguments. Cannot suppress exceptions. csˆˆˆŽdS(N((RNR,ttb(RtcallbackR(s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyRZks(t __wrapped__R\(RRcRRRZ((RRcRs</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyRcfs  cCs8t|ƒ}|j}|j|ƒ}|j||ƒ|S(sªEnters the supplied context manager If successful, also pushes its __exit__ method as a callback and returns the result of the __enter__ method. (RTR-R#R](RRXt_cm_typet_exittresult((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyt enter_contextss   cCs|jdddƒdS(s$Immediately unwind the context stackN(R-R(R((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyR0€scCs|S(N((R((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyR#„sc GsÐ|ddk }tjƒd}t|ƒ}t}t}xx|jr²|jjƒ}y%||Œrwt}t}d}nWq;tjƒ}||d|dƒt}|}q;Xq;W|rÆt|ƒn|oÏ|S(Nii(NNN( RR(R)RGR%RUR8tTrueRK( RRIt received_excRERFtsuppressed_exct pending_raisetcbtnew_exc_details((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyR-‡s(      ( RRRRRWR]R\RcRhR0R#R-(((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyR5s       cBs2eZdZd„Zd„Zd„Zd„ZRS(s+Backwards compatibility alias for ExitStackcCs'tjdtƒtt|ƒjƒdS(Ns*ContextStack has been renamed to ExitStack(R R R tsuperR R(R((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyR¨s cCs |j|ƒS(N(R\(RRc((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyt register_exit­scOs|j|||ŽS(N(Rc(RRcRR((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pytregister°scCs |jƒS(N(RW(R((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pytpreserve³s(RRRRRpRqRr(((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyR ¥s    (RR(R t collectionsRt functoolsRt__all__tobjectRRRRR1RRRt version_infoR&RGRKttypesRQt ImportErrorRRTRR (((s</opt/alt/python27/lib/python2.7/site-packages/contextlib2.pyts8    (H "      p