B ] @sZddlZddlZddlZddlmZddlmZdddddd d d d d ddg ZGdddejZ Gdd d ejZ Gdd d e Z GdddZ Gddde e e ZGddde e ZddZddZGddde ZGddde ZGdd d eZGdddeZGd dde ZGd!d"d"ZGd#d d ee ZGd$d d ee ZGd%dde ZdS)&N)deque)wrapsasynccontextmanagercontextmanagerclosing nullcontextAbstractContextManagerAbstractAsyncContextManagerAsyncExitStackContextDecorator ExitStackredirect_stdoutredirect_stderrsuppressc@s.eZdZddZejddZeddZdS)rcCs|S)N)selfrr-/opt/alt/python37/lib/python3.7/contextlib.py __enter__sz AbstractContextManager.__enter__cCsdS)Nr)rexc_type exc_value tracebackrrr__exit__szAbstractContextManager.__exit__cCs|tkrt|ddStS)Nrr)r_collections_abc_check_methodsNotImplemented)clsCrrr__subclasshook__sz'AbstractContextManager.__subclasshook__N) __name__ __module__ __qualname__rabcabstractmethodr classmethodrrrrrrsc@s.eZdZddZejddZeddZdS)r cs|S)Nr)rrrr __aenter__&sz&AbstractAsyncContextManager.__aenter__csdS)Nr)rrrrrrr __aexit__*sz%AbstractAsyncContextManager.__aexit__cCs|tkrt|ddStS)Nr$r%)r rrr)rrrrrr/sz,AbstractAsyncContextManager.__subclasshook__N) rrr r$r!r"r%r#rrrrrr "sc@seZdZddZddZdS)r cCs|S)Nr)rrrr _recreate_cm:s zContextDecorator._recreate_cmcstfdd}|S)Nc s ||SQRXdS)N)r&)argskwds)funcrrrinnerGs z(ContextDecorator.__call__..inner)r)rr)r*r)r)rr__call__FszContextDecorator.__call__N)rrr r&r+rrrrr 7s c@seZdZddZdS)_GeneratorContextManagerBasecCsJ||||_||||_|_|_t|dd}|dkr@t|j}||_dS)N__doc__)genr)r'r(getattrtyper-)rr)r'r(docrrr__init__Qs    z%_GeneratorContextManagerBase.__init__N)rrr r2rrrrr,Nsr,c@s$eZdZddZddZddZdS)_GeneratorContextManagercCs||j|j|jS)N) __class__r)r'r()rrrrr&esz%_GeneratorContextManager._recreate_cmcCs:|`|`|`y t|jStk r4tddYnXdS)Nzgenerator didn't yield)r'r(r)nextr. StopIteration RuntimeError)rrrrrks   z"_GeneratorContextManager.__enter__c Cs|dkr6yt|jWntk r*dSXtdn|dkrD|}y|j|||Wntk r~}z||k Sd}~XYnftk r}z(||krdS|tkr|j|krdSWdd}~XYn"td|krdSYnXtddS)NFzgenerator didn't stopz#generator didn't stop after throw())r5r.r6r7throw __cause__sysexc_info)rr0valuerexcrrrrts.  z!_GeneratorContextManager.__exit__N)rrr r&rrrrrrr3`s r3c@seZdZddZddZdS)_AsyncGeneratorContextManagercs4y|jIdHStk r.tddYnXdS)Nzgenerator didn't yield)r. __anext__StopAsyncIterationr7)rrrrr$sz(_AsyncGeneratorContextManager.__aenter__c s|dkrrrrr%s.    z'_AsyncGeneratorContextManager.__aexit__N)rrr r$r%rrrrr?sr?cstfdd}|S)Ncs t||S)N)r3)r'r()r)rrhelperszcontextmanager..helper)r)r)rFr)r)rrscstfdd}|S)Ncs t||S)N)r?)r'r()r)rrrFsz#asynccontextmanager..helper)r)r)rFr)r)rrsc@s$eZdZddZddZddZdS)rcCs ||_dS)N)thing)rrGrrrr2%szclosing.__init__cCs|jS)N)rG)rrrrr'szclosing.__enter__cGs|jdS)N)rGclose)rr<rrrr)szclosing.__exit__N)rrr r2rrrrrrrsc@s(eZdZdZddZddZddZdS)_RedirectStreamNcCs||_g|_dS)N) _new_target _old_targets)r new_targetrrrr21sz_RedirectStream.__init__cCs*|jtt|jtt|j|j|jS)N)rKappendr/r;_streamsetattrrJ)rrrrr6sz_RedirectStream.__enter__cCstt|j|jdS)N)rOr;rNrKpop)rexctypeexcinstexctbrrrr;sz_RedirectStream.__exit__)rrr rNr2rrrrrrrI-srIc@seZdZdZdS)r stdoutN)rrr rNrrrrr ?s c@seZdZdZdS)rstderrN)rrr rNrrrrrOsc@s$eZdZddZddZddZdS)rcGs ||_dS)N) _exceptions)r exceptionsrrrr2`szsuppress.__init__cCsdS)Nr)rrrrrcszsuppress.__enter__cCs|dk ot||jS)N) issubclassrV)rrQrRrSrrrrfs zsuppress.__exit__N)rrr r2rrrrrrrUs c@s^eZdZeddZeddZddZddZd d Zd d Z d dZ ddZ dddZ dS)_BaseExitStackcsfdd}|S)Ncs|||S)Nr)rr>tb)cmcm_exitrr _exit_wrapperxsz:_BaseExitStack._create_exit_wrapper.._exit_wrapperr)r[r\r]r)r[r\r_create_exit_wrappervsz#_BaseExitStack._create_exit_wrappercs^fdd}|S)NcsdS)Nr)rr>rZ)r'callbackr(rrr]sz8_BaseExitStack._create_cb_wrapper.._exit_wrapperr)r'r(r]r)r'r_r(r_create_cb_wrapper|sz!_BaseExitStack._create_cb_wrappercCs t|_dS)N)r_exit_callbacks)rrrrr2sz_BaseExitStack.__init__cCst|}|j|_t|_|S)N)r0rar)r new_stackrrrpop_alls z_BaseExitStack.pop_allcCsBt|}y |j}Wntk r0||YnX||||S)N)r0rAttributeError_push_exit_callback _push_cm_exit)rexit_cb_type exit_methodrrrpushs   z_BaseExitStack.pushcCs(t|}|j}||}||||S)N)r0rrrf)rr[_cm_type_exitresultrrr enter_contexts   z_BaseExitStack.enter_contextcOs|t|dkr|^}}}n>|s&tdn0d|krB|d}|^}}ntdt|d|j|f||}||_|||S)NzBdescriptor 'callback' of '_BaseExitStack' object needs an argumentr_z8callback expected at least 1 positional argument, got %dr8)len TypeErrorrPr` __wrapped__re)r'r(rr_r]rrrr_s      z_BaseExitStack.callbackcCs"|||}||_||ddS)NT)r^__self__re)rr[r\r]rrrrfs z_BaseExitStack._push_cm_exitTcCs|j||fdS)N)rarM)rr_is_syncrrrresz"_BaseExitStack._push_exit_callbackN)T) rrr staticmethodr^r`r2rcrjrnr_rfrerrrrrYss  rYc@s$eZdZddZddZddZdS)r cCs|S)Nr)rrrrrszExitStack.__enter__c s|ddk }tdfdd}d}d}x`|jr|j\}}y||rXd}d}d}Wq.t}||d|dd}|}Yq.Xq.W|ry|dj} |dWn tk r| |d_YnX|o|S)Nrr8cs8x,|j}||krdS|dks$|kr&P|}qW||_dS)N) __context__)new_excold_exc exc_context) frame_excrr_fix_exception_contextsz2ExitStack.__exit__.._fix_exception_contextFT)NNN)r;r<rarPrvrD) r exc_details received_excr{suppressed_exc pending_raisertcbnew_exc_details fixed_ctxr)rzrrs2      zExitStack.__exit__cCs|ddddS)N)r)rrrrrHszExitStack.closeN)rrr rrrHrrrrr s 1c@s\eZdZeddZeddZddZddZd d Zd d Z d dZ ddZ ddZ dS)r csfdd}|S)Ncs|||IdHS)Nr)rr>rZ)r[r\rrr]'sz@AsyncExitStack._create_async_exit_wrapper.._exit_wrapperr)r[r\r]r)r[r\r_create_async_exit_wrapper%sz)AsyncExitStack._create_async_exit_wrappercs^fdd}|S)NcsIdHdS)Nr)rr>rZ)r'r_r(rrr].sz>AsyncExitStack._create_async_cb_wrapper.._exit_wrapperr)r'r(r]r)r'r_r(r_create_async_cb_wrapper+sz'AsyncExitStack._create_async_cb_wrappercs.t|}|j}||IdH}||||S)N)r0r%r$_push_async_cm_exit)rr[rkrlrmrrrenter_async_context2s  z"AsyncExitStack.enter_async_contextcCsDt|}y |j}Wn tk r2||dYnX||||S)NF)r0r%rdrer)rrgrhrirrrpush_async_exit>s  zAsyncExitStack.push_async_exitcOs~t|dkr|^}}}n>|s&tdn0d|krB|d}|^}}ntdt|d|j|f||}||_||d|S)NrozMdescriptor 'push_async_callback' of 'AsyncExitStack' object needs an argumentr_zCpush_async_callback expected at least 1 positional argument, got %dr8F)rprqrPrrrre)r'r(rr_r]rrrpush_async_callbackPs      z"AsyncExitStack.push_async_callbackcs|dddIdHdS)N)r%)rrrracloseiszAsyncExitStack.aclosecCs"|||}||_||ddS)NF)rrsre)rr[r\r]rrrrms z"AsyncExitStack._push_async_cm_exitcs|S)Nr)rrrrr$tszAsyncExitStack.__aenter__c s|ddk }tdfdd}d}d}xx|jr|j\}}y0|rR||}n||IdH}|rpd}d}d}Wq.t} || d|dd}| }Yq.Xq.W|ry|dj} |dWn tk r| |d_YnX|o|S)Nrr8cs8x,|j}||krdS|dks$|kr&P|}qW||_dS)N)rv)rwrxry)rzrrr{}sz8AsyncExitStack.__aexit__.._fix_exception_contextFT)NNN)r;r<rarPrvrD) rr|r}r{r~rrtr cb_suppressrrr)rzrr%ws8       zAsyncExitStack.__aexit__N) rrr rurrrrrrrr$r%rrrrr s   c@s&eZdZdddZddZddZdS) rNcCs ||_dS)N) enter_result)rrrrrr2sznullcontext.__init__cCs|jS)N)r)rrrrrsznullcontext.__enter__cGsdS)Nr)rexcinforrrrsznullcontext.__exit__)N)rrr r2rrrrrrrs )r!r;r collectionsr functoolsr__all__ABCrr objectr r,r3r?rrrrIr rrrYr r rrrrrs:    B -!!`E