3 ^j3@sddlZddlZddlZddlmZddlmZdddddd d d gZGd ddejZ Gd dde Z Gddde e Z ddZ Gddde ZGddde ZGdd d eZGdd d eZGdd d e ZGddde ZdS)N)deque)wrapscontextmanagerclosingAbstractContextManagerContextDecorator ExitStackredirect_stdoutredirect_stderrsuppressc@s.eZdZddZejddZeddZdS)rcCs|S)N)selfr r "/usr/lib64/python3.6/contextlib.py __enter__sz AbstractContextManager.__enter__cCsdS)Nr )r exc_type exc_value tracebackr r r__exit__szAbstractContextManager.__exit__cCs|tkrtj|ddStS)Nrr)r_collections_abc_check_methodsNotImplemented)clsCr r r__subclasshook__sz'AbstractContextManager.__subclasshook__N) __name__ __module__ __qualname__rabcabstractmethodr classmethodrr r r rr sc@seZdZddZddZdS)rcCs|S)Nr )r r r r _recreate_cm$s zContextDecorator._recreate_cmcstfdd}|S)Nc sj ||SQRXdS)N)r )argskwds)funcr r rinner1s z(ContextDecorator.__call__..inner)r)r r#r$r )r#r r__call__0szContextDecorator.__call__N)rrrr r%r r r rr!s c@s,eZdZddZddZddZddZd S) _GeneratorContextManagercCsJ||||_||||_|_|_t|dd}|dkr@t|j}||_dS)N__doc__)genr#r!r"getattrtyper')r r#r!r"docr r r__init__;s    z!_GeneratorContextManager.__init__cCs|j|j|j|jS)N) __class__r#r!r")r r r rr Isz%_GeneratorContextManager._recreate_cmc Cs.y t|jStk r(tddYnXdS)Nzgenerator didn't yield)nextr( StopIteration RuntimeError)r r r rrOs z"_GeneratorContextManager.__enter__cCs|dkr6yt|jWntk r*dSXtdn|dkrD|}y|jj|||Wntk r|}z||k Sd}~Xnftk r}z*||krdS|tkr|j|krdSWYdd}~Xn"tjd|krdSYnXtddS)NFzgenerator didn't stopz#generator didn't stop after throw())r.r(r/r0throw __cause__sysexc_info)r r*valuerexcr r rrUs. z!_GeneratorContextManager.__exit__N)rrrr,r rrr r r rr&8sr&cstfdd}|S)Ncs t||S)N)r&)r!r")r#r rhelperszcontextmanager..helper)r)r#r8r )r#rrsc@s$eZdZddZddZddZdS)rcCs ||_dS)N)thing)r r9r r rr,szclosing.__init__cCs|jS)N)r9)r r r rrszclosing.__enter__cGs|jjdS)N)r9close)r r5r r rrszclosing.__exit__N)rrrr,rrr r r rrsc@s(eZdZdZddZddZddZdS)_RedirectStreamNcCs||_g|_dS)N) _new_target _old_targets)r new_targetr r rr,sz_RedirectStream.__init__cCs*|jjtt|jtt|j|j|jS)N)r=appendr)r4_streamsetattrr<)r r r rrsz_RedirectStream.__enter__cCstt|j|jjdS)N)rAr4r@r=pop)r exctypeexcinstexctbr r rrsz_RedirectStream.__exit__)rrrr@r,rrr r r rr;sr;c@seZdZdZdS)r stdoutN)rrrr@r r r rr s c@seZdZdZdS)r stderrN)rrrr@r r r rr sc@s$eZdZddZddZddZdS)r cGs ||_dS)N) _exceptions)r exceptionsr r rr,szsuppress.__init__cCsdS)Nr )r r r rrszsuppress.__enter__cCs|dk ot||jS)N) issubclassrH)r rCrDrEr r rrs zsuppress.__exit__N)rrrr,rrr r r rr s c@sLeZdZddZddZddZddZd d Zd d Zd dZ ddZ dS)rcCs t|_dS)N)r_exit_callbacks)r r r rr,szExitStack.__init__cCst|}|j|_t|_|S)N)r*rKr)r new_stackr r rpop_alls zExitStack.pop_allcs"fdd}|_|j|dS)Ncsf|S)Nr ) exc_details)cmcm_exitr r _exit_wrappersz.ExitStack._push_cm_exit.._exit_wrapper)__self__push)r rOrPrQr )rOrPr _push_cm_exitszExitStack._push_cm_exitc CsDt|}y |j}Wn tk r2|jj|YnX|j|||S)N)r*rAttributeErrorrKr?rT)r exit_cb_type exit_methodr r rrS s   zExitStack.pushcs$fdd}|_|j|S)NcsdS)Nr )rr7tb)r!callbackr"r rrQ9sz)ExitStack.callback.._exit_wrapper) __wrapped__rS)r rZr!r"rQr )r!rZr"rrZ4s zExitStack.callbackcCs(t|}|j}|j|}|j|||S)N)r*rrrT)r rO_cm_type_exitresultr r r enter_contextAs   zExitStack.enter_contextcCs|jddddS)N)r)r r r rr:NszExitStack.closec s|ddk }tjdfdd}d}d}x\|jr|jj}y||rTd}d}d}Wq.tj}||d|dd}|}Yq.Xq.W|ry|dj}|dWn tk r||d_YnX|o|S)Nrr1cs8x,|j}||krdS|dks$|kr&P|}qW||_dS)N) __context__)new_excold_exc exc_context) frame_excr r_fix_exception_contextXsz2ExitStack.__exit__.._fix_exception_contextFT)NNN)r4r5rKrBr` BaseException) r rN received_excresuppressed_exc pending_raisecbnew_exc_details fixed_ctxr )rdrrRs2       zExitStack.__exit__N) rrrr,rMrTrSrZr_r:rr r r rrs   )rr4r collectionsr functoolsr__all__ABCrobjectrr&rrr;r r r rr r r rs"  I"