3 \ @s dZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z ddl m Z ddl mZddl mZdd l mZdd l mZdd l mZdd l mZdd l mZddlmZddlmZdddddgZejdkredddZy ejZWnek r,ddZYnXGdddejZ e!edrVddZ"nddl#Z#d dZ"Gd!d"d"ej$Z%Gd#d$d$ej&ej'Z(e!ed%rej)Z*nddl#Z#d&d'Z*Gd(d)d)e j+Z,Gd*ddZ-Gd+d,d,e-Z.Gd-dde.Z/Gd.dde.Z0Gd/d0d0ej1Z2e Z3e2Z4dS)1z2Selector event loop for Unix with signal handling.N) base_events)base_subprocess)compat) constants) coroutines)events)futures)selector_events) selectors) transports) coroutine)loggerSelectorEventLoopAbstractChildWatcherSafeChildWatcherFastChildWatcherDefaultEventLoopPolicyZwin32z+Signals are not really supported on WindowscCsdS)zDummy signal handler.N)signumframerr#/usr/lib64/python3.6/unix_events.py_sighandler_noop%srcCs|S)Nr)pathrrr.srcseZdZdZd"fdd ZddZfddZd d Zd d Zd dZ ddZ ddZ d#ddZ d$ddZ ed%ddZddZeddddddZed&ddddd d!ZZS)'_UnixSelectorEventLoopzdUnix event loop. Adds signal handling and UNIX Domain Socket support to SelectorEventLoop. Ncstj|i|_dS)N)super__init___signal_handlers)selfselector) __class__rrr7s z_UnixSelectorEventLoop.__init__cCstjS)N)socketZ socketpair)rrrr _socketpair;sz"_UnixSelectorEventLoop._socketpaircs^tjtjs2xFt|jD]}|j|qWn(|jrZtjd|dt |d|jj dS)NzClosing the loop z@ on interpreter shutdown stage, skipping signal handlers removal)source) rclosesys is_finalizinglistrremove_signal_handlerwarningswarnResourceWarningclear)rsig)r!rrr%>s z_UnixSelectorEventLoop.closecCs"x|D]}|sq|j|qWdS)N)_handle_signal)rdatarrrr_process_self_dataLs z)_UnixSelectorEventLoop._process_self_datac+GsHtj|stj|rtd|j||jytj|jj Wn2t t fk rt}zt t |WYdd}~XnXtj|||}||j|<ytj|ttj|dWnt k rB}zz|j|=|jsytjdWn4t t fk r}ztjd|WYdd}~XnX|jtjkr0t dj|nWYdd}~XnXdS)zAdd a handler for a signal. UNIX only. Raise ValueError if the signal number is invalid or uncatchable. Raise RuntimeError if there is a problem setting up the handler. z3coroutines cannot be used with add_signal_handler()NFrzset_wakeup_fd(-1) failed: %szsig {} cannot be caught)rZ iscoroutineZiscoroutinefunction TypeError _check_signalZ _check_closedsignal set_wakeup_fdZ_csockfileno ValueErrorOSError RuntimeErrorstrrZHandlerr siginterruptrinfoerrnoEINVALformat)rr.callbackargsexchandleZnexcrrradd_signal_handlerSs0     z)_UnixSelectorEventLoop.add_signal_handlercCs8|jj|}|dkrdS|jr*|j|n |j|dS)z2Internal helper that is the actual signal handler.N)rgetZ _cancelledr)Z_add_callback_signalsafe)rr.rDrrrr/s   z%_UnixSelectorEventLoop._handle_signalc&Cs|j|y |j|=Wntk r*dSX|tjkr>tj}ntj}ytj||Wn@tk r}z$|jtj krt dj |nWYdd}~XnX|jsytj dWn2t tfk r}ztjd|WYdd}~XnXdS)zwRemove a handler for a signal. UNIX only. Return True if a signal handler was removed, False if not. Fzsig {} cannot be caughtNrzset_wakeup_fd(-1) failed: %sTr2)r4rKeyErrorr5SIGINTdefault_int_handlerSIG_DFLr9r>r?r:r@r6r8rr=)rr.ZhandlerrCrrrr)s(    z,_UnixSelectorEventLoop.remove_signal_handlercCsHt|tstdj|d|ko,tjknsDtdj|tjdS)zInternal helper to validate a signal. Raise ValueError if the signal number is invalid or uncatchable. Raise RuntimeError if there is a problem setting up the handler. zsig must be an int, not {!r}rzsig {} out of range(1, {})N) isinstanceintr3r@r5NSIGr8)rr.rrrr4s  z$_UnixSelectorEventLoop._check_signalcCst|||||S)N)_UnixReadPipeTransport)rpipeprotocolwaiterextrarrr_make_read_pipe_transportsz0_UnixSelectorEventLoop._make_read_pipe_transportcCst|||||S)N)_UnixWritePipeTransport)rrOrPrQrRrrr_make_write_pipe_transportsz1_UnixSelectorEventLoop._make_write_pipe_transportc kstj} |j} t||||||||f| |d| } | j| j|j| y| EdHWn&tk r~} z | }WYdd} ~ XnXd}|dk r| j| j EdH|WdQRX| S)N)rQrR) rget_child_watcherZ create_future_UnixSubprocessTransportadd_child_handlerZget_pid_child_watcher_callback Exceptionr%Z_wait)rrPrBshellstdinstdoutstderrbufsizerRkwargswatcherrQtransprCerrrrr_make_subprocess_transports$     z1_UnixSelectorEventLoop._make_subprocess_transportcCs|j|j|dS)N)Zcall_soon_threadsafeZ_process_exited)rpid returncoderbrrrrYsz._UnixSelectorEventLoop._child_watcher_callback)sslsockserver_hostnamec cs|dkst|tst|r,|dkr|dkrBtd|jtjks`tj|j rntdj|tj||g} |j||jd |j|||| | S) Nz*ssl argument must be an SSLContext or Nonez3path and sock can not be specified at the same timerz2Unable to check or remove stale UNIX socket %r: %rzAddress {!r} is already in usez-path was not specified, and no sock specifiedz2A UNIX Domain Stream Socket was expected, got {!r}F)rrv)rKboolr3r8_fspathr"rkrlstatS_ISSOCKosst_moderemoveFileNotFoundErrorr9rerrorZbindr%r>Z EADDRINUSEr@rnrrorpZServerZlistenrmZ_start_serving) rrqrrhrurgrcrCmsgZserverrrrcreate_unix_serversP         z)_UnixSelectorEventLoop.create_unix_server)N)NN)NN)N)N)__name__ __module__ __qualname____doc__rr#r%r1rEr/r)r4rSrUr rdrYrsr __classcell__rr)r!rr1s, -      %r set_blockingcCstj|ddS)NF)r{r)fdrrr_set_nonblockingBsrcCs,tj|tj}|tjB}tj|tj|dS)N)fcntlZF_GETFLr{ O_NONBLOCKZF_SETFL)rflagsrrrrGs cseZdZdZd fdd ZddZdd Zd d Zd d ZddZ ddZ ddZ ddZ e jrhddZd!ddZddZddZZS)"rNiNcstj|||jd<||_||_|j|_||_d|_t j |jj }t j |pbt j|pbt j|s~d|_d|_d|_tdt|j|jj|jj||jj|jj|j|j|dk r|jjtj|ddS)NrOFz)Pipe transport is for pipes/sockets only.)rr_extra_loop_piper7_fileno _protocol_closingr{fstatr|ryS_ISFIFOrzS_ISCHRr8r call_soonconnection_made _add_reader _read_readyr _set_result_unless_cancelled)rlooprOrPrQrRmode)r!rrrQs,          z_UnixReadPipeTransport.__init__cCs|jjg}|jdkr |jdn|jr0|jd|jd|jt|jdd}|jdk r|dk rtj ||jt j }|r|jdq|jdn |jdk r|jdn |jddd j |S) Nclosedclosingzfd=%s _selectorpollingidleopenz<%s> ) r!rrappendrrgetattrrr _test_selector_eventr Z EVENT_READjoin)rr=r rrrr__repr__ns$          z_UnixReadPipeTransport.__repr__cCsytj|j|j}WnDttfk r,Yntk rX}z|j|dWYdd}~Xn^X|rl|jj |nJ|j j rt j d|d|_|j j|j|j j|jj|j j|jddS)Nz"Fatal read error on pipe transportz%r was closed by peerT)r{readrmax_sizeBlockingIOErrorInterruptedErrorr9 _fatal_errorrZ data_receivedr get_debugrr=r_remove_readerrZ eof_received_call_connection_lost)rr0rCrrrrs  z"_UnixReadPipeTransport._read_readycCs|jj|jdS)N)rrr)rrrr pause_readingsz$_UnixReadPipeTransport.pause_readingcCs|jj|j|jdS)N)rrrr)rrrrresume_readingsz%_UnixReadPipeTransport.resume_readingcCs ||_dS)N)r)rrPrrr set_protocolsz#_UnixReadPipeTransport.set_protocolcCs|jS)N)r)rrrr get_protocolsz#_UnixReadPipeTransport.get_protocolcCs|jS)N)r)rrrr is_closingsz!_UnixReadPipeTransport.is_closingcCs|js|jddS)N)r_close)rrrrr%sz_UnixReadPipeTransport.closecCs,|jdk r(tjd|t|d|jjdS)Nzunclosed transport %r)r$)rr*r+r,r%)rrrr__del__s  z_UnixReadPipeTransport.__del__Fatal error on pipe transportcCsZt|tr4|jtjkr4|jjrLtjd||ddn|jj||||j d|j |dS)Nz%r: %sT)exc_info)message exceptionrrrP) rKr9r>ZEIOrrrdebugcall_exception_handlerrr)rrCrrrrrs  z#_UnixReadPipeTransport._fatal_errorcCs(d|_|jj|j|jj|j|dS)NT)rrrrrr)rrCrrrrsz_UnixReadPipeTransport._closec Cs4z|jj|Wd|jjd|_d|_d|_XdS)N)rconnection_lostrr%r)rrCrrrrs  z,_UnixReadPipeTransport._call_connection_losti)NN)r)rrrrrrrrrrrrr%rPY34rrrrrrr)r!rrNMs rNcseZdZd%fdd ZddZddZdd Zd d Zd d ZddZ ddZ ddZ ddZ ddZ ddZejr|ddZddZd&dd Zd'd!d"Zd#d$ZZS)(rTNc stj||||jd<||_|j|_||_t|_d|_ d|_ t j |jj }tj|}tj|}tj|} |px|px| sd|_d|_d|_tdt|j|jj|jj|| s|rtjjd r|jj|jj|j|j|dk r|jjtj|ddS)NrOrFz?Pipe transport is only for pipes, sockets and character devicesaix)rrrrr7rr bytearray_buffer _conn_lostrr{rr|ryrrrzr8rrrrr&platform startswithrrr r) rrrOrPrQrRrZis_charZis_fifoZ is_socket)r!rrrs2          z _UnixWritePipeTransport.__init__cCs|jjg}|jdkr |jdn|jr0|jd|jd|jt|jdd}|jdk r|dk rtj ||jt j }|r|jdn |jd|j }|jd|n |jdk r|jdn |jdd d j |S) Nrrzfd=%srrrz bufsize=%srz<%s>r)r!rrrrrrrr rr Z EVENT_WRITEget_write_buffer_sizer)rr=r rr_rrrrs(          z _UnixWritePipeTransport.__repr__cCs t|jS)N)lenr)rrrrrsz-_UnixWritePipeTransport.get_write_buffer_sizecCs6|jjrtjd||jr*|jtn|jdS)Nz%r was closed by peer)rrrr=rrBrokenPipeError)rrrrrs   z#_UnixWritePipeTransport._read_readycCs0t|tttfstt|t|tr.t|}|s6dS|jsB|jrj|jtj krXt j d|jd7_dS|j syt j|j|}WnTttfk rd}Yn:tk r}z|jd7_|j|ddSd}~XnX|t|krdS|dkrt||d}|jj|j|j|j |7_ |jdS)Nz=pipe closed by peer or os.write(pipe, data) raised exception.rrz#Fatal write error on pipe transport)rKbytesr memoryviewrjreprrrrZ!LOG_THRESHOLD_FOR_CONNLOST_WRITESrwarningrr{writerrrrZrrrZ _add_writer _write_readyZ_maybe_pause_protocol)rr0nrCrrrrs4       z_UnixWritePipeTransport.writecCs|jstdytj|j|j}Wnjttfk r:Yntk r}z8|jj|j d7_ |j j |j|j |dWYdd}~XnfX|t |jkr|jj|j j |j|j|jr|j j|j|jddS|dkr|jd|=dS)NzData should not be emptyrz#Fatal write error on pipe transportr)rrjr{rrrrrZr-rr_remove_writerrrZ_maybe_resume_protocolrrr)rrrCrrrr>s(   z$_UnixWritePipeTransport._write_readycCsdS)NTr)rrrr can_write_eofXsz%_UnixWritePipeTransport.can_write_eofcCsB|jr dS|jstd|_|js>|jj|j|jj|jddS)NT) rrrjrrrrrr)rrrr write_eof[s z!_UnixWritePipeTransport.write_eofcCs ||_dS)N)r)rrPrrrrdsz$_UnixWritePipeTransport.set_protocolcCs|jS)N)r)rrrrrgsz$_UnixWritePipeTransport.get_protocolcCs|jS)N)r)rrrrrjsz"_UnixWritePipeTransport.is_closingcCs|jdk r|j r|jdS)N)rrr)rrrrr%msz_UnixWritePipeTransport.closecCs,|jdk r(tjd|t|d|jjdS)Nzunclosed transport %r)r$)rr*r+r,r%)rrrrrvs  z_UnixWritePipeTransport.__del__cCs|jddS)N)r)rrrrabort|sz_UnixWritePipeTransport.abortFatal error on pipe transportcCsPt|tjr*|jjrBtjd||ddn|jj||||jd|j |dS)Nz%r: %sT)r)rrrrrP) rKrZ_FATAL_ERROR_IGNORErrrrrrr)rrCrrrrrs   z$_UnixWritePipeTransport._fatal_errorcCsFd|_|jr|jj|j|jj|jj|j|jj|j|dS)NT) rrrrrr-rrr)rrCrrrrs  z_UnixWritePipeTransport._closec Cs4z|jj|Wd|jjd|_d|_d|_XdS)N)rrrr%r)rrCrrrrs  z-_UnixWritePipeTransport._call_connection_lost)NN)r)N)rrrrrrrrrrrrrrr%rrrrrrrrrr)r!rrTs$% !   rTset_inheritablecCsNttdd}tj|tj}|s4tj|tj||Bntj|tj||@dS)NZ FD_CLOEXECr)rrZF_GETFDZF_SETFD)rZ inheritableZ cloexec_flagoldrrr_set_inheritables  rc@seZdZddZdS)rWc Ksvd}|tjkr*|jj\}}t|jdtj|f||||d|d||_|dk rr|jt |j d|d|j_ dS)NF)r[r\r]r^Zuniversal_newlinesr_wb) buffering) subprocessPIPErr#rr7Popen_procr%rdetachr\) rrBr[r\r]r^r_r`Zstdin_wrrr_starts  z_UnixSubprocessTransport._startN)rrrrrrrrrWsrWc@s@eZdZdZddZddZddZdd 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 tdS)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)rrerArBrrrrXs z&AbstractChildWatcher.add_child_handlercCs tdS)zRemoves the handler for process 'pid'. The function returns True if the handler was successfully removed, False if there was nothing to remove.N)r)rrerrrremove_child_handlersz)AbstractChildWatcher.remove_child_handlercCs tdS)zAttach 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)rrrrr attach_loopsz AbstractChildWatcher.attach_loopcCs tdS)zlClose the watcher. This must be called to make sure that any underlying resource is freed. N)r)rrrrr%szAbstractChildWatcher.closecCs tdS)zdEnter the watcher's context and allow starting new processes This function must return selfN)r)rrrr __enter__szAbstractChildWatcher.__enter__cCs tdS)zExit the watcher's contextN)r)rabcrrr__exit__ szAbstractChildWatcher.__exit__N) rrrrrXrrr%rrrrrrrs  c@sDeZdZddZddZddZddZd d Zd d Zd dZ dS)BaseChildWatchercCsd|_i|_dS)N)r _callbacks)rrrrrszBaseChildWatcher.__init__cCs|jddS)N)r)rrrrr%szBaseChildWatcher.closecCs tdS)N)r)r expected_pidrrr _do_waitpidszBaseChildWatcher._do_waitpidcCs tdS)N)r)rrrr_do_waitpid_allsz BaseChildWatcher._do_waitpid_allcCs~|dkst|tjst|jdk r<|dkr<|jr %dr2)r{rrrrrrrrGrrrrrrr)rrerrfrArBrrrrs6      z FastChildWatcher._do_waitpid_all) rrrrrr%rrrXrrrrr)r!rrs   csHeZdZdZeZfddZddZfddZdd Z d d Z Z S) _UnixDefaultEventLoopPolicyz:UNIX event loop policy with a watcher for child processes.cstjd|_dS)N)rr_watcher)r)r!rrr s z$_UnixDefaultEventLoopPolicy.__init__c CsHtj8|jdkr:t|_ttjtjr:|jj|j j WdQRXdS)N) rrrrrKrcurrent_thread _MainThreadr_localr)rrrr _init_watchers  z)_UnixDefaultEventLoopPolicy._init_watchercs6tj||jdk r2ttjtjr2|jj|dS)zSet 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)rset_event_looprrKrrrr)rr)r!rrrs  z*_UnixDefaultEventLoopPolicy.set_event_loopcCs|jdkr|j|jS)zzGet the watcher for child processes. If not yet set, a SafeChildWatcher object is automatically created. N)rr)rrrrrV&s z-_UnixDefaultEventLoopPolicy.get_child_watchercCs4|dkst|tst|jdk r*|jj||_dS)z$Set the watcher for child processes.N)rKrrjrr%)rrarrrset_child_watcher0s  z-_UnixDefaultEventLoopPolicy.set_child_watcher) rrrrrZ _loop_factoryrrrrVrrrr)r!rrs   r)5rr>r{r5r"ryrr&rr*rrrrrrr r r r r logr__all__r ImportErrorrfspathrxAttributeErrorZBaseSelectorEventLooprhasattrrrZ ReadTransportrNZ_FlowControlMixinZWriteTransportrTrrZBaseSubprocessTransportrWrrrrZBaseDefaultEventLoopPolicyrrrrrrrsn                O  F=On2