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 mZddlmZddlmZdddddgZejdkrüedƒ‚dd„Zy ejZWnek �r,dd„ZYnXGdd„dejƒZ e!edƒ�rVdd„Z"nddl#Z#d d„Z"Gd!d"„d"ej$ƒZ%Gd#d$„d$ej&ej'ƒZ(e!ed%ƒ�r¢ej)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.ƒZ0Gd/d0„d0ej1ƒZ2e Z3e2Z4dS)1z2Selector 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ÚDefaultEventLoopPolicyZwin32z+Signals are not really supported on WindowscCsdS)zDummy signal handler.N©)ÚsignumÚframerrú+/usr/lib64/python3.6/asyncio/unix_events.pyÚ_sighandler_noop%srcCs|S)Nr)ÚpathrrrÚ.srcs¶eZdZdZd"‡fdd„ Zdd„Z‡fdd„Zd d „Zd d „Zd d„Z dd„Z dd„Z d#dd„Z d$dd„Z ed%dd„ƒZdd„Zeddddœdd„ƒZed&ddddœd d!„ƒZ‡ZS)'Ú_UnixSelectorEventLoopzdUnix event loop. Adds signal handling and UNIX Domain Socket support to SelectorEventLoop. Ncstƒj|ƒi|_dS)N)ÚsuperÚ__init__Ú_signal_handlers)ÚselfÚselector)Ú __class__rrr7s z_UnixSelectorEventLoop.__init__cCstjƒS)N)ÚsocketZ socketpair)rrrrÚ _socketpair;sz"_UnixSelectorEventLoop._socketpaircs^tƒjƒtjƒs2xFt|jƒD]}|j|ƒqWn(|jrZtjd|›d�t |d�|jj ƒdS)NzClosing the loop z@ on interpreter shutdown stage, skipping signal handlers removal)Úsource) rÚcloseÚsysÚ is_finalizingÚlistrÚremove_signal_handlerÚwarningsÚwarnÚResourceWarningÚclear)rÚsig)r!rrr%>s z_UnixSelectorEventLoop.closecCs"x|D]}|sq|j|ƒqWdS)N)Ú_handle_signal)rÚdatarrrrÚ_process_self_dataLs z)_UnixSelectorEventLoop._process_self_datac+GsHtj|ƒstj|ƒrtdƒ‚|j|ƒ|jƒytj|jj ƒƒWn2t t fk rt}zt t |ƒƒ‚WYdd}~XnXtj|||ƒ}||j|<ytj|tƒtj|dƒWn˜t k �rB}zz|j|=|j�sytjdƒWn4t t fk �r}ztjd|ƒWYdd}~XnX|jtjk�r0t dj|ƒƒ‚n‚WYdd}~XnXdS)zÃAdd 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_closedÚsignalÚ set_wakeup_fdZ_csockÚfilenoÚ ValueErrorÚOSErrorÚ RuntimeErrorÚstrrZHandlerrÚ siginterruptrÚinfoÚerrnoÚEINVALÚformat)rr.ÚcallbackÚargsÚexcÚhandleZnexcrrrÚadd_signal_handlerSs0     z)_UnixSelectorEventLoop.add_signal_handlercCs8|jj|ƒ}|dkrdS|jr*|j|ƒn |j|ƒdS)z2Internal helper that is the actual signal handler.N)rÚgetZ _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 kr‚t dj |ƒƒ‚n‚WYdd}~XnX|jsÞytj dƒWn2t 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)r4rÚKeyErrorr5ÚSIGINTÚdefault_int_handlerÚSIG_DFLr9r>r?r:r@r6r8rr=)rr.ZhandlerrCrrrr)Šs(    z,_UnixSelectorEventLoop.remove_signal_handlercCsHt|tƒstdj|ƒƒ‚d|ko,tjknsDtdj|tjƒƒ‚dS)zÁInternal 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)Ú isinstanceÚintr3r@r5ÚNSIGr8)rr.rrrr4ªs  z$_UnixSelectorEventLoop._check_signalcCst|||||ƒS)N)Ú_UnixReadPipeTransport)rÚpipeÚprotocolÚwaiterÚextrarrrÚ_make_read_pipe_transport·sz0_UnixSelectorEventLoop._make_read_pipe_transportcCst|||||ƒS)N)Ú_UnixWritePipeTransport)rrOrPrQrRrrrÚ_make_write_pipe_transport»sz1_UnixSelectorEventLoop._make_write_pipe_transportc ks´tjƒ�¢} |jƒ} t||||||||f| |dœ| —Ž} | j| jƒ|j| ƒy| EdHWn&tk r~} z | }WYdd} ~ XnXd}|dk r¦| jƒ| j ƒEdH|‚WdQRX| S)N)rQrR) rÚget_child_watcherZ create_futureÚ_UnixSubprocessTransportÚadd_child_handlerZget_pidÚ_child_watcher_callbackÚ Exceptionr%Z_wait)rrPrBÚshellÚstdinÚstdoutÚstderrÚbufsizerRÚkwargsÚwatcherrQÚtransprCÚerrrrrÚ_make_subprocess_transport¿s$     z1_UnixSelectorEventLoop._make_subprocess_transportcCs|j|j|ƒdS)N)Zcall_soon_threadsafeZ_process_exited)rÚpidÚ returncoderbrrrrYÝsz._UnixSelectorEventLoop._child_watcher_callback)ÚsslÚsockÚserver_hostnamec cs|dkst|tƒst‚|r,|dkr|dk�rBtdƒ‚|jtjk�s`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 timerúz2Unable 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)rKÚboolr3r8Ú_fspathr"rkrlÚstatÚS_ISSOCKÚosÚst_modeÚremoveÚFileNotFoundErrorr9rÚerrorZbindr%r>Z EADDRINUSEr@rnrrorpZServerZlistenrmZ_start_serving) rrqrrhrurgrcrCÚmsgZserverrrrÚcreate_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|dƒdS)NF)r{r‡)ÚfdrrrÚ_set_nonblockingBsr‰cCs,tj|tjƒ}|tjB}tj|tj|ƒdS)N)ÚfcntlZF_GETFLr{Ú O_NONBLOCKZF_SETFL)rˆÚflagsrrrr‰Gs csŠeZdZdZd ‡fdd„ Zdd„Zdd „Zd d „Zd d „Zdd„Z dd„Z dd„Z dd„Z e jrhdd„Zd!dd„Zdd„Zdd„Z‡ZS)"rNéiNcsÐtƒj|ƒ||jd<||_||_|jƒ|_||_d|_t j |jƒj }t j |ƒpbt j|ƒpbt j|ƒs~d|_d|_d|_tdƒ‚t|jƒ|jj|jj|ƒ|jj|jj|j|jƒ|dk rÌ|jjtj|dƒdS)NrOFz)Pipe transport is for pipes/sockets only.)rrÚ_extraÚ_loopÚ_piper7Ú_filenoÚ _protocolÚ_closingr{Úfstatr|ryÚS_ISFIFOrzÚS_ISCHRr8r‰Ú call_soonÚconnection_madeÚ _add_readerÚ _read_readyr Ú_set_result_unless_cancelled)rÚlooprOrPrQrRÚmode)r!rrrQs,          z_UnixReadPipeTransport.__init__cCs¼|jjg}|jdkr |jdƒn|jr0|jdƒ|jd|jƒt|jddƒ}|jdk rŽ|dk rŽtj ||jt j ƒ}|r‚|jdƒq®|jdƒn |jdk r¤|jdƒn |jdƒdd j |ƒS) NÚclosedÚclosingzfd=%sÚ _selectorÚpollingÚidleÚopenz<%s>ú ) r!r‚r�Úappendr“r‘Úgetattrr�r Ú_test_selector_eventr Z EVENT_READÚjoin)rr=r r¡rrrÚ__repr__ns$          z_UnixReadPipeTransport.__repr__cCsºytj|j|jƒ}WnDttfk r,YnŠtk rX}z|j|dƒWYdd}~Xn^X|rl|jj |ƒnJ|j j ƒr‚t j d|ƒd|_|j j|jƒ|j j|jjƒ|j j|jdƒdS)Nz"Fatal read error on pipe transportz%r was closed by peerT)r{Úreadr‘Úmax_sizeÚBlockingIOErrorÚInterruptedErrorr9Ú _fatal_errorr’Z data_receivedr�Ú get_debugrr=r“Ú_remove_readerr—Z eof_receivedÚ_call_connection_lost)rr0rCrrrrš„s  z"_UnixReadPipeTransport._read_readycCs|jj|jƒdS)N)r�r°r‘)rrrrÚ pause_reading–sz$_UnixReadPipeTransport.pause_readingcCs|jj|j|jƒdS)N)r�r™r‘rš)rrrrÚresume_reading™sz%_UnixReadPipeTransport.resume_readingcCs ||_dS)N)r’)rrPrrrÚ set_protocolœsz#_UnixReadPipeTransport.set_protocolcCs|jS)N)r’)rrrrÚ get_protocolŸsz#_UnixReadPipeTransport.get_protocolcCs|jS)N)r“)rrrrÚ is_closing¢sz!_UnixReadPipeTransport.is_closingcCs|js|jdƒdS)N)r“Ú_close)rrrrr%¥sz_UnixReadPipeTransport.closecCs,|jdk r(tjd|t|d�|jjƒdS)Nzunclosed transport %r)r$)r�r*r+r,r%)rrrrÚ__del__­s  z_UnixReadPipeTransport.__del__úFatal error on pipe transportcCsZt|tƒr4|jtjkr4|jjƒrLtjd||dd�n|jj||||j dœƒ|j |ƒdS)Nz%r: %sT)Úexc_info)ÚmessageÚ exceptionrrrP) rKr9r>ZEIOr�r¯rÚdebugÚcall_exception_handlerr’r·)rrCr»rrrr®³s  z#_UnixReadPipeTransport._fatal_errorcCs(d|_|jj|jƒ|jj|j|ƒdS)NT)r“r�r°r‘r—r±)rrCrrrr·Ász_UnixReadPipeTransport._closec Cs4z|jj|ƒWd|jjƒd|_d|_d|_XdS)N)r’Úconnection_lostr�r%r�)rrCrrrr±Æs  z,_UnixReadPipeTransport._call_connection_losti)NN)r¹)r‚rƒr„r«rr©ršr²r³r´rµr¶r%rÚPY34r¸r®r·r±r†rr)r!rrNMs rNcs¨eZdZd%‡fdd„ Zdd„Zdd„Zdd „Zd d „Zd d „Zdd„Z dd„Z dd„Z dd„Z dd„Z dd„Zejr|dd„Zdd„Zd&dd „Zd'd!d"„Zd#d$„Z‡ZS)(rTNc sütƒj||ƒ||jd<||_|jƒ|_||_tƒ|_d|_ d|_ t j |jƒj }tj|ƒ}tj|ƒ}tj|ƒ} |px|px| s”d|_d|_d|_tdƒ‚t|jƒ|jj|jj|ƒ| sÆ|rÞtjjdƒ rÞ|jj|jj|j|jƒ|dk rø|jjtj|dƒdS)NrOrFz?Pipe transport is only for pipes, sockets and character devicesÚaix)rrrŽr�r7r‘r’Ú bytearrayÚ_bufferÚ _conn_lostr“r{r”r|ryr–r•rzr8r‰r�r—r˜r&ÚplatformÚ startswithr™ršr r›) rrœrOrPrQrRr�Zis_charZis_fifoZ is_socket)r!rrrÓs2          z _UnixWritePipeTransport.__init__cCsÒ|jjg}|jdkr |jdƒn|jr0|jdƒ|jd|jƒt|jddƒ}|jdk r¤|dk r¤tj ||jt j ƒ}|r‚|jdƒn |jdƒ|j ƒ}|jd|ƒn |jdk rº|jdƒn |jdƒd d j |ƒS) NržrŸzfd=%sr r¡r¢z bufsize=%sr£z<%s>r¤)r!r‚r�r¥r“r‘r¦r�r r§r Z EVENT_WRITEÚget_write_buffer_sizer¨)rr=r r¡r_rrrr©øs(          z _UnixWritePipeTransport.__repr__cCs t|jƒS)N)ÚlenrÃ)rrrrrÇsz-_UnixWritePipeTransport.get_write_buffer_sizecCs6|jjƒrtjd|ƒ|jr*|jtƒƒn|jƒdS)Nz%r was closed by peer)r�r¯rr=rÃr·ÚBrokenPipeError)rrrrršs   z#_UnixWritePipeTransport._read_readycCs0t|tttfƒstt|ƒƒ‚t|tƒr.t|ƒ}|s6dS|jsB|jrj|jtj krXt j dƒ|jd7_dS|j �syt j|j|ƒ}WnTttfk r d}Yn:tk rØ}z|jd7_|j|dƒdSd}~XnX|t|ƒkrêdS|dk�rt|ƒ|d…}|jj|j|jƒ|j |7_ |jƒdS)Nz=pipe closed by peer or os.write(pipe, data) raised exception.rrz#Fatal write error on pipe transport)rKÚbytesrÂÚ memoryviewrjÚreprrÄr“rZ!LOG_THRESHOLD_FOR_CONNLOST_WRITESrÚwarningrÃr{Úwriter‘r¬r­rZr®rÈr�Z _add_writerÚ _write_readyZ_maybe_pause_protocol)rr0ÚnrCrrrrÎs4       z_UnixWritePipeTransport.writecCsö|jstdƒ‚ytj|j|jƒ}Wnjttfk r:Yn¸tk rŒ}z8|jjƒ|j d7_ |j j |jƒ|j |dƒWYdd}~XnfX|t |jƒkrÞ|jjƒ|j j |jƒ|jƒ|jrÚ|j j|jƒ|jdƒdS|dkrò|jd|…=dS)NzData should not be emptyrz#Fatal write error on pipe transportr)rÃrjr{rÎr‘r¬r­rZr-rÄr�Ú_remove_writerr®rÈZ_maybe_resume_protocolr“r°r±)rrÐrCrrrrÏ>s(   z$_UnixWritePipeTransport._write_readycCsdS)NTr)rrrrÚ can_write_eofXsz%_UnixWritePipeTransport.can_write_eofcCsB|jr dS|jst‚d|_|js>|jj|jƒ|jj|jdƒdS)NT) r“r�rjrÃr�r°r‘r—r±)rrrrÚ write_eof[s z!_UnixWritePipeTransport.write_eofcCs ||_dS)N)r’)rrPrrrr´dsz$_UnixWritePipeTransport.set_protocolcCs|jS)N)r’)rrrrrµgsz$_UnixWritePipeTransport.get_protocolcCs|jS)N)r“)rrrrr¶jsz"_UnixWritePipeTransport.is_closingcCs|jdk r|j r|jƒdS)N)r�r“rÓ)rrrrr%msz_UnixWritePipeTransport.closecCs,|jdk r(tjd|t|d�|jjƒdS)Nzunclosed transport %r)r$)r�r*r+r,r%)rrrrr¸vs  z_UnixWritePipeTransport.__del__cCs|jdƒdS)N)r·)rrrrÚabort|sz_UnixWritePipeTransport.abortúFatal error on pipe transportcCsPt|tjƒr*|jjƒrBtjd||dd�n|jj||||jdœƒ|j |ƒdS)Nz%r: %sT)rº)r»r¼rrrP) rKrZ_FATAL_ERROR_IGNOREr�r¯rr½r¾r’r·)rrCr»rrrr®s   z$_UnixWritePipeTransport._fatal_errorcCsFd|_|jr|jj|jƒ|jjƒ|jj|jƒ|jj|j|ƒdS)NT) r“rÃr�rÑr‘r-r°r—r±)rrCrrrr·�s  z_UnixWritePipeTransport._closec Cs4z|jj|ƒWd|jjƒd|_d|_d|_XdS)N)r’r¿r�r%r�)rrCrrrr±•s  z-_UnixWritePipeTransport._call_connection_lost)NN)rÕ)N)r‚rƒr„rr©rÇršrÎrÏrÒrÓr´rµr¶r%rrÀr¸rÔr®r·r±r†rr)r!rrTÐs$% !   rTÚset_inheritablecCsNttddƒ}tj|tjƒ}|s4tj|tj||Bƒntj|tj||@ƒdS)NZ FD_CLOEXECr)r¦rŠZF_GETFDZF_SETFD)rˆZ inheritableZ cloexec_flagÚoldrrrÚ_set_inheritable¥s  rØc@seZdZdd„ZdS)rWc Ksvd}|tjkr*|jjƒ\}}t|jƒdƒtj|f||||d|dœ|—Ž|_|dk rr|jƒt |j ƒd|d�|j_ dS)NF)r[r\r]r^Zuniversal_newlinesr_Úwb)Ú buffering) Ú subprocessÚPIPEr�r#rØr7ÚPopenÚ_procr%r£Údetachr\) rrBr[r\r]r^r_r`Zstdin_wrrrÚ_start±s  z_UnixSubprocessTransport._startN)r‚rƒr„ràrrrrrW¯srWc@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)rrerArBrrrrXß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á)rrerrrÚ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á)rrœrrrÚ 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) r‚rƒr„r…rXrârãr%rärèrrrrrÈs  c@sDeZdZdd„Zdd„Zdd„Zdd„Zd d „Zd d „Zd d„Z dS)ÚBaseChildWatchercCsd|_i|_dS)N)r�Ú _callbacks)rrrrrszBaseChildWatcher.__init__cCs|jdƒdS)N)rã)rrrrr%szBaseChildWatcher.closecCs tƒ‚dS)N)rá)rÚ expected_pidrrrÚ _do_waitpidszBaseChildWatcher._do_waitpidcCs tƒ‚dS)N)rá)rrrrÚ_do_waitpid_allsz BaseChildWatcher._do_waitpid_allcCs~|dkst|tjƒst‚|jdk r<|dkr<|jr %dr2)r{rørùrúrörýrêrûrGrÿrþr�r¯rr½rÍ)rrerõrfrArBrrrríÝs6      z FastChildWatcher._do_waitpid_all) r‚rƒr„r…rr%rärèrXrârír†rr)r!rršs   csHeZdZdZeZ‡fdd„Zdd„Z‡fdd„Zdd „Z d d „Z ‡Z S) Ú_UnixDefaultEventLoopPolicyz:UNIX event loop policy with a watcher for child processes.cstƒjƒd|_dS)N)rrÚ_watcher)r)r!rrr s z$_UnixDefaultEventLoopPolicy.__init__c CsHtj�8|jdkr:tƒ|_ttjƒtjƒr:|jj|j j ƒWdQRXdS)N) rrýrrrKrüÚcurrent_threadÚ _MainThreadrãÚ_localr�)rrrrÚ _init_watchers  z)_UnixDefaultEventLoopPolicy._init_watchercs6tƒj|ƒ|jdk r2ttjƒtjƒr2|jj|ƒdS)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_looprrKrürrrã)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|tƒst‚|jdk r*|jjƒ||_dS)z$Set the watcher for child processes.N)rKrrjrr%)rrarrrÚset_child_watcher0s  z-_UnixDefaultEventLoopPolicy.set_child_watcher) r‚rƒr„r…rZ _loop_factoryrrrrVrr†rr)r!rrs   r)5r…r>r{r5r"ryrÛr&rür*Úrrrrrrr r r r r ÚlogrÚ__all__rÅÚ ImportErrorrÚfspathrxÚAttributeErrorZBaseSelectorEventLooprÚhasattrr‰rŠZ ReadTransportrNZ_FlowControlMixinZWriteTransportrTrÖrØZBaseSubprocessTransportrWrrérrZBaseDefaultEventLoopPolicyrrrrrrrÚsn                O  F=On2