î j f؆ã@sÂdZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z ddl m Z ddl mZddl mZdd l mZdd l mZdd l mZdd l mZdd l mZddlmZddlmZdddddgZejdkreedƒ‚ndd„ZGdd„dejƒZeedƒr¨dd„Znddl Z dd„ZGdd „d ej!ƒZ"Gd!d"„d"ej#ej$ƒZ%eed#ƒrej&Z'nddl Z d$d%„Z'Gd&d'„d'e j(ƒZ)Gd(d„dƒZ*Gd)d*„d*e*ƒZ+Gd+d„de+ƒZ,Gd,d„de+ƒZ-Gd-d.„d.ej.ƒZ/eZ0e/Z1dS)/z2Selector event loop for Unix with signal handling.éNé)Ú base_events)Úbase_subprocess)Úcompat)Ú constants)Ú coroutines)Úevents)Úfutures)Úselector_events)Ú selectors)Ú transports)Ú coroutine)ÚloggerÚSelectorEventLoopÚAbstractChildWatcherÚSafeChildWatcherÚFastChildWatcherÚDefaultEventLoopPolicyÚwin32z+Signals are not really supported on WindowscCsdS)zDummy signal handler.N©)ÚsignumZframerrú8/opt/alt/python34/lib64/python3.4/asyncio/unix_events.pyÚ_sighandler_noop%src seZdZdZd‡fdd†Zdd„Z‡fdd†Zd d „Zd d „Zd d„Z dd„Z dd„Z dddd„Z dddd„Z eddd„ƒZdd„Zedddddddd„ƒZedddd d!ddd"d#„ƒZ‡S)$Ú_UnixSelectorEventLoopzdUnix event loop. Adds signal handling and UNIX Domain Socket support to SelectorEventLoop. Ncstƒj|ƒi|_dS)N)ÚsuperÚ__init__Ú_signal_handlers)ÚselfZselector)Ú __class__rrr0sz_UnixSelectorEventLoop.__init__cCs tjƒS)N)ÚsocketZ socketpair)rrrrÚ _socketpair4sz"_UnixSelectorEventLoop._socketpaircs8tƒjƒx$t|jƒD]}|j|ƒqWdS)N)rÚcloseÚlistrÚremove_signal_handler)rÚsig)rrrr!7s z_UnixSelectorEventLoop.closecCs.x'|D]}|sqn|j|ƒqWdS)N)Ú_handle_signal)rÚdatarrrrÚ_process_self_data<s z)_UnixSelectorEventLoop._process_self_datac+Gs¢tj|ƒstj|ƒr-tdƒ‚n|j|ƒ|jƒytj|jj ƒƒWn=t t fk r�}zt t |ƒƒ‚WYdd}~XnXtj|||ƒ}||j|ú ) rrmr{Úappendr„r|r Ú_test_selector_eventrzÚ _selectorr Z EVENT_READÚjoin)rr3r�rrrÚ__repr__Es   z_UnixReadPipeTransport.__repr__cCsòytj|j|jƒ}WnLttfk r6Yn¸tk rj}z|j|dƒWYdd}~Xn„X|r„|jj |ƒnj|j j ƒr¦t j d|ƒnd|_|j j|jƒ|j j|jjƒ|j j|jdƒdS)Nz"Fatal read error on pipe transportz%r was closed by peerT)rrÚreadr|Úmax_sizeÚBlockingIOErrorÚInterruptedErrorr/Ú _fatal_errorrƒZ data_receivedrzÚ get_debugrr3r„Ú remove_readerr…Z eof_receivedÚ_call_connection_lost)rr&r9rrrrˆXs# z"_UnixReadPipeTransport._read_readycCs|jj|jƒdS)N)rzr›r|)rrrrÚ pause_readingjsz$_UnixReadPipeTransport.pause_readingcCs|jj|j|jƒdS)N)rzr‡r|rˆ)rrrrÚresume_readingmsz%_UnixReadPipeTransport.resume_readingcCs|jS)N)r„)rrrrÚ is_closingpsz!_UnixReadPipeTransport.is_closingcCs|js|jdƒndS)N)r„Ú_close)rrrrr!ss z_UnixReadPipeTransport.closecCs7|jdk r3tjd|tƒ|jjƒndS)Nzunclosed transport %r)r{ÚwarningsÚwarnÚResourceWarningr!)rrrrÚ__del__{sz_UnixReadPipeTransport.__del__zFatal error on pipe transportcCs�t|tƒrO|jtjkrO|jjƒr~tjd||ddƒq~n/|jji|d6|d6|d6|j d6ƒ|j |ƒdS)Nz%r: %sÚexc_infoTÚmessageÚ exceptionrfrF) rAr/r4ZEIOrzršrÚdebugÚcall_exception_handlerrƒr )rr9r¦rrrr™€s! z#_UnixReadPipeTransport._fatal_errorcCs6d|_|jj|jƒ|jj|j|ƒdS)NT)r„rzr›r|r…rœ)rr9rrrr Žs z_UnixReadPipeTransport._closec CsDz|jj|ƒWd|jjƒd|_d|_d|_XdS)N)rƒÚconnection_lostr{r!rz)rr9rrrrœ“s    z,_UnixReadPipeTransport._call_connection_losti)rmrnror–rr”rˆr�ržrŸr!rÚPY34r¤r™r rœrr)rrrD*s         rDcsâeZdZdd‡fdd†Zdd„Zdd„Zdd „Zd d „Zd d „Zdd„Z dd„Z dd„Z dd„Z e jr¨dd„Zndd„Zddd„Zddd„Zdd „Z‡S)!rJNcsCtƒj||ƒ||jd<||_|jƒ|_tj|jƒj}t j |ƒ}|p}t j |ƒp}t j |ƒs�t dƒ‚nt|jƒ||_g|_d|_d|_|jj|jj|ƒ|sòtjjdƒ r|jj|jj|j|jƒn|dk r?|jjtj|dƒndS)NrEz?Pipe transport is only for pipes, sockets and character devicesrFÚaix)rrryr{r-r|rrr}r~rr�r€r‚r.rtrƒÚ_bufferÚ _conn_lostr„rzr…r†ÚsysÚplatformÚ startswithr‡rˆr r‰)rrLrErFrGrHrŠZ is_socket)rrrr s,        z _UnixWritePipeTransport.__init__cCsì|jjg}|jdkr.|jdƒn|jrG|jdƒn|jd|jƒ|jdk rÎtj|jj |jt j ƒ}|r¡|jdƒn |jdƒ|j ƒ}|jd|ƒn |jdƒddj |ƒS) Nr‹rŒzfd=%sr�rŽz bufsize=%sz<%s>r�)rrmr{r�r„r|r r‘rzr’r Z EVENT_WRITEÚget_write_buffer_sizer“)rr3r�rVrrrr”Ás"     z _UnixWritePipeTransport.__repr__cCstdd„|jDƒƒS)Ncss|]}t|ƒVqdS)N)Úlen)Ú.0r&rrrú Øsz@_UnixWritePipeTransport.get_write_buffer_size..)Úsumr­)rrrrr²×sz-_UnixWritePipeTransport.get_write_buffer_sizecCsL|jjƒr"tjd|ƒn|jr>|jtƒƒn |jƒdS)Nz%r was closed by peer)rzršrr3r­r ÚBrokenPipeError)rrrrrˆÚs  z#_UnixWritePipeTransport._read_readycCsht|tƒrt|ƒ}n|s(dS|js:|jro|jtjkr\tjdƒn|jd7_dS|j sJyt j |j |ƒ}Wnet tfk r±d}YnHtk rø}z(|jd7_|j|dƒdSWYdd}~XnX|t|ƒkrdS|dkr.||d…}n|jj|j |jƒn|j j|ƒ|jƒdS)Nz=pipe closed by peer or os.write(pipe, data) raised exception.rrz#Fatal write error on pipe transport)rAÚ bytearrayÚ memoryviewr®r„rZ!LOG_THRESHOLD_FOR_CONNLOST_WRITESrÚwarningr­rrÚwriter|r—r˜rQr™r³rzZ add_writerÚ _write_readyr�Z_maybe_pause_protocol)rr&Únr9rrrr»ãs2   z_UnixWritePipeTransport.writecCsUdj|jƒ}|jjƒytj|j|ƒ}Wn~ttfk rb|jj|ƒYnït k r¸}z7|j d7_ |j j |jƒ|j |dƒWYdd}~Xn™X|t|ƒkr"|j j |jƒ|jƒ|j r|jr|j j|jƒ|jdƒndS|dkrA||d…}n|jj|ƒdS)Nórz#Fatal write error on pipe transportr)r“r­Úclearrrr»r|r—r˜r�rQr®rzÚ remove_writerr™r³Z_maybe_resume_protocolr„r›rœ)rr&r½r9rrrr¼s( #  z$_UnixWritePipeTransport._write_readycCsdS)NTr)rrrrÚ can_write_eof sz%_UnixWritePipeTransport.can_write_eofcCsO|jr dSd|_|jsK|jj|jƒ|jj|jdƒndS)NT)r„r­rzr›r|r…rœ)rrrrÚ write_eof#s    z!_UnixWritePipeTransport.write_eofcCs|jS)N)r„)rrrrrŸ,sz"_UnixWritePipeTransport.is_closingcCs*|jdk r&|j r&|jƒndS)N)r{r„rÂ)rrrrr!/sz_UnixWritePipeTransport.closecCs7|jdk r3tjd|tƒ|jjƒndS)Nzunclosed transport %r)r{r¡r¢r£r!)rrrrr¤8sz_UnixWritePipeTransport.__del__cCs|jdƒdS)N)r )rrrrÚabort=sz_UnixWritePipeTransport.abortzFatal error on pipe transportcCsƒt|ttfƒrC|jjƒrrtjd||ddƒqrn/|jji|d6|d6|d6|jd6ƒ|j |ƒdS)Nz%r: %sr¥Tr¦r§rfrF) rAr·ÚConnectionResetErrorrzršrr¨r©rƒr )rr9r¦rrrr™@s z$_UnixWritePipeTransport._fatal_errorcCsbd|_|jr(|jj|jƒn|jjƒ|jj|jƒ|jj|j|ƒdS)NT) r„r­rzrÀr|r¿r›r…rœ)rr9rrrr Ns    z_UnixWritePipeTransport._closec CsDz|jj|ƒWd|jjƒd|_d|_d|_XdS)N)rƒrªr{r!rz)rr9rrrrœVs    z-_UnixWritePipeTransport._call_connection_lost)rmrnrorr”r²rˆr»r¼rÁrÂrŸr!rr«r¤rÃr™r rœrr)rrrJ�s !   !      rJÚset_inheritablecCsittddƒ}tj|tjƒ}|sJtj|tj||Bƒntj|tj||@ƒdS)NZ FD_CLOEXECr)ÚgetattrruZF_GETFDZF_SETFD)rsZ inheritableZ cloexec_flagÚoldrrrÚ_set_inheritablefs rÈc@seZdZdd„ZdS)rNc Ks·d}|tjkr@|jjƒ\}}t|jƒdƒntj|d|d|d|d|ddd||�|_|dk r³|jƒt |j ƒdd |ƒ|j_ ndS) NFrRrSrTrUZuniversal_newlinesrVÚwbÚ buffering) Ú subprocessÚPIPErzr rÈr-ÚPopenÚ_procr!ÚopenÚdetachrS) rr8rRrSrTrUrVrWZstdin_wrrrÚ_startrs  z_UnixSubprocessTransport._startN)rmrnrorÑrrrrrNps rNc@s^eZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dS)raHAbstract base class for monitoring child processes. Objects derived from this class monitor a collection of subprocesses and report their termination or interruption by a signal. New callbacks are registered with .add_child_handler(). Starting a new process must be done within a 'with' block to allow the watcher to suspend its activity until the new process if fully registered (this is needed to prevent a race condition in some implementations). Example: with watcher: proc = subprocess.Popen("sleep 1") watcher.add_child_handler(proc.pid, callback) Notes: Implementations of this class must be thread-safe. Since child watcher objects may catch the SIGCHLD signal and call waitpid(-1), there should be only one active object per process. cGs tƒ‚dS)aRegister a new child handler. Arrange for callback(pid, returncode, *args) to be called when process 'pid' terminates. Specifying another callback for the same process replaces the previous handler. Note: callback() must be thread-safe. N)ÚNotImplementedError)rr\r7r8rrrrO s z&AbstractChildWatcher.add_child_handlercCs tƒ‚dS)z Removes the handler for process 'pid'. The function returns True if the handler was successfully removed, False if there was nothing to remove.N)rÒ)rr\rrrÚremove_child_handler«sz)AbstractChildWatcher.remove_child_handlercCs tƒ‚dS)zÔAttach the watcher to an event loop. If the watcher was previously attached to an event loop, then it is first detached before attaching to the new loop. Note: loop may be None. N)rÒ)rrLrrrÚ attach_loop³sz AbstractChildWatcher.attach_loopcCs tƒ‚dS)zlClose the watcher. This must be called to make sure that any underlying resource is freed. N)rÒ)rrrrr!½szAbstractChildWatcher.closecCs tƒ‚dS)zdEnter the watcher's context and allow starting new processes This function must return selfN)rÒ)rrrrÚ __enter__ÄszAbstractChildWatcher.__enter__cCs tƒ‚dS)zExit the watcher's contextN)rÒ)rÚaÚbÚcrrrÚ__exit__ÊszAbstractChildWatcher.__exit__N) rmrnrorprOrÓrÔr!rÕrÙrrrrr‰s    c@sdeZdZdd„Zdd„Zdd„Zdd„Zd d „Zd d „Zd d„Z dS)ÚBaseChildWatchercCs d|_dS)N)rz)rrrrrÑszBaseChildWatcher.__init__cCs|jdƒdS)N)rÔ)rrrrr!ÔszBaseChildWatcher.closecCs tƒ‚dS)N)rÒ)rÚ expected_pidrrrÚ _do_waitpid×szBaseChildWatcher._do_waitpidcCs tƒ‚dS)N)rÒ)rrrrÚ_do_waitpid_allÚsz BaseChildWatcher._do_waitpid_allcCsa|jdk r%|jjtjƒn||_|dk r]|jtj|jƒ|jƒndS)N)rzr#r+ÚSIGCHLDr;Ú _sig_chldrÝ)rrLrrrrÔÝs   zBaseChildWatcher.attach_loopcCsXy|jƒWnCtk rS}z#|jjidd6|d6ƒWYdd}~XnXdS)Nz$Unknown exception in SIGCHLD handlerr¦r§)rÝrQrzr©)rr9rrrrßës  zBaseChildWatcher._sig_chldcCsAtj|ƒrtj|ƒ Stj|ƒr9tj|ƒS|SdS)N)rrÚ WIFSIGNALEDÚWTERMSIGÚ WIFEXITEDÚ WEXITSTATUS)rÚstatusrrrÚ_compute_returncode÷s  z$BaseChildWatcher._compute_returncodeN) rmrnrorr!rÜrÝrÔrßrårrrrrÚÏs       rÚcs‚eZdZdZ‡fdd†Z‡fdd†Zdd„Zdd „Zd d „Zd d „Z dd„Z dd„Z ‡S)rad'Safe' child watcher implementation. This implementation avoids disrupting other code spawning processes by polling explicitly each process in the SIGCHLD handler instead of calling os.waitpid(-1). This is a safe solution but it has a significant overhead when handling a big number of children (O(n) each time SIGCHLD is raised) cstƒjƒi|_dS)N)rrÚ _callbacks)r)rrrrs zSafeChildWatcher.__init__cs|jjƒtƒjƒdS)N)rær¿rr!)r)rrrr!s zSafeChildWatcher.closecCs|S)Nr)rrrrrÕszSafeChildWatcher.__enter__cCsdS)Nr)rrÖr×rØrrrrÙszSafeChildWatcher.__exit__cGs$||f|j|<|j|ƒdS)N)rærÜ)rr\r7r8rrrrOsz"SafeChildWatcher.add_child_handlerc Cs/y|j|=dSWntk r*dSYnXdS)NTF)rær=)rr\rrrrÓ$s   z%SafeChildWatcher.remove_child_handlercCs+x$t|jƒD]}|j|ƒqWdS)N)r"rærÜ)rr\rrrrÝ+sz SafeChildWatcher._do_waitpid_allcCsytj|tjƒ\}}Wn.tk rO|}d}tjd|ƒYnEX|dkr`dS|j|ƒ}|jjƒr”tj d||ƒny|j j |ƒ\}}Wn:t k rì|jjƒrètjd|ddƒnYnX||||ŒdS)Néÿz8Unknown child process pid %d, will report returncode 255rz$process %s exited with returncode %sz'Child watcher got an unexpected pid: %rr¥T) rrÚwaitpidÚWNOHANGÚChildProcessErrorrrºrårzršr¨ræÚpopr=)rrÛr\rär]r7r8rrrrÜ0s*       zSafeChildWatcher._do_waitpid) rmrnrorprr!rÕrÙrOrÓrÝrÜrr)rrrs      csveZdZdZ‡fdd†Z‡fdd†Zdd„Zdd „Zd d „Zd d „Z dd„Z ‡S)raW'Fast' child watcher implementation. This implementation reaps every terminated processes by calling os.waitpid(-1) directly, possibly breaking other code spawning processes and waiting for their termination. There is no noticeable overhead when handling a big number of children (O(1) each time a child terminates). cs;tƒjƒi|_tjƒ|_i|_d|_dS)Nr)rrræÚ threadingZLockÚ_lockÚ_zombiesÚ_forks)r)rrrr]s    zFastChildWatcher.__init__cs+|jjƒ|jjƒtƒjƒdS)N)rær¿rîrr!)r)rrrr!ds  zFastChildWatcher.closec Cs'|j�|jd7_|SWdQXdS)Nr)rírï)rrrrrÕis zFastChildWatcher.__enter__c Csf|j�G|jd8_|js,|j r0dSt|jƒ}|jjƒWdQXtjd|ƒdS)Nrz5Caught subprocesses termination from unknown pids: %s)rírïrîr1r¿rrº)rrÖr×rØZcollateral_victimsrrrrÙos zFastChildWatcher.__exit__cGsf|j�Gy|jj|ƒ}Wn)tk rK||f|j| %dr()rrrèrérêrårírærër=rïrîrzršrr¨rº)rr\rär]r7r8rrrrÝ‘s6             z FastChildWatcher._do_waitpid_all) rmrnrorprr!rÕrÙrOrÓrÝrr)rrrSs    csdeZdZdZeZ‡fdd†Zdd„Z‡fdd†Zdd „Z d d „Z ‡S) Ú_UnixDefaultEventLoopPolicyz:UNIX event loop policy with a watcher for child processes.cstƒjƒd|_dS)N)rrÚ_watcher)r)rrrrÀs z$_UnixDefaultEventLoopPolicy.__init__c Csctj�T|jdkrYtƒ|_ttjƒtjƒrY|jj|j j ƒqYnWdQXdS)N) rrírñrrArìÚcurrent_threadÚ _MainThreadrÔÚ_localrz)rrrrÚ _init_watcherÄs     z)_UnixDefaultEventLoopPolicy._init_watchercsNtƒj|ƒ|jdk rJttjƒtjƒrJ|jj|ƒndS)zÑSet the event loop. As a side effect, if a child watcher was set before, then calling .set_event_loop() from the main thread will call .attach_loop(loop) on the child watcher. N)rÚset_event_looprñrArìròrórÔ)rrL)rrrröÌsz*_UnixDefaultEventLoopPolicy.set_event_loopcCs#|jdkr|jƒn|jS)zzGet the watcher for child processes. If not yet set, a SafeChildWatcher object is automatically created. N)rñrõ)rrrrrMÚs z-_UnixDefaultEventLoopPolicy.get_child_watchercCs,|jdk r|jjƒn||_dS)z$Set the watcher for child processes.N)rñr!)rrXrrrÚset_child_watcheräsz-_UnixDefaultEventLoopPolicy.set_child_watcher) rmrnrorprZ _loop_factoryrrõrörMr÷rr)rrrð¼s   rð)2rpr4rrr+rrrËr¯rìr¡Úrrrrrrr r r r r ÚlogrÚ__all__r°Ú ImportErrorrZBaseSelectorEventLooprÚhasattrrtruZ ReadTransportrDZ_FlowControlMixinZWriteTransportrJrÅrÈZBaseSubprocessTransportrNrrÚrrZBaseDefaultEventLoopPolicyrðrrrrrrÚs^           ô  s    F6Ni2