U ifۿ@sdZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl mZddl mZddl mZddl mZddl mZdd l mZdd l mZdd l mZdd l mZdd l mZddlmZdZe jdkredddZGdddejZGdddej Z!Gdddej"ej#Z$Gdddej%Z&GdddZ'ddZ(Gd d!d!e'Z)Gd"d#d#e)Z*Gd$d%d%e)Z+Gd&d'd'e'Z,Gd(d)d)e'Z-Gd*d+d+ej.Z/eZ0e/Z1dS),z2Selector event loop for Unix with signal handling.N) base_events)base_subprocess) constants) coroutines)events) exceptions)futures)selector_events)tasks) transports)logger)SelectorEventLoopAbstractChildWatcherSafeChildWatcherFastChildWatcherMultiLoopChildWatcherThreadedChildWatcherDefaultEventLoopPolicyZwin32z+Signals are not really supported on WindowscCsdS)zDummy signal handler.N)signumframerr8/opt/alt/python38/lib64/python3.8/asyncio/unix_events.py_sighandler_noop*srcseZdZdZd)fdd ZfddZddZd d Zd d Zd dZ ddZ d*ddZ d+ddZ d,ddZ ddZd-dddddddZd.dddddddd Zd!d"Zd#d$Zd%d&Zd'd(ZZS)/_UnixSelectorEventLoopzdUnix event loop. Adds signal handling and UNIX Domain Socket support to SelectorEventLoop. Ncst|i|_dSN)super__init___signal_handlers)selfselector __class__rrr5s z_UnixSelectorEventLoop.__init__csZtts.t|jD]}||qn(|jrVtjd|dt |d|j dS)NzClosing the loop z@ on interpreter shutdown stage, skipping signal handlers removalsource) rclosesys is_finalizinglistrremove_signal_handlerwarningswarnResourceWarningclearrsigr!rrr%9s z_UnixSelectorEventLoop.closecCs|D]}|sq||qdSr)_handle_signal)rdatarrrr_process_self_dataGsz)_UnixSelectorEventLoop._process_self_datac GsLt|st|rtd|||zt|j Wn2t t fk rt}zt t |W5d}~XYnXt|||d}||j|<zt|tt|dWnt k rF}zz|j|=|jsztdWn4t t fk r}ztd|W5d}~XYnX|jtjkr4t d|dnW5d}~XYnXdS)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()NFset_wakeup_fd(-1) failed: %ssig  cannot be caught)rZ iscoroutineZiscoroutinefunction TypeError _check_signalZ _check_closedsignal set_wakeup_fdZ_csockfileno ValueErrorOSError RuntimeErrorstrrZHandlerr siginterruptr infoerrnoEINVAL)rr/callbackargsexchandleZnexcrrradd_signal_handlerNs2    z)_UnixSelectorEventLoop.add_signal_handlercCs8|j|}|dkrdS|jr*||n ||dS)z2Internal helper that is the actual signal handler.N)rgetZ _cancelledr)Z_add_callback_signalsafe)rr/rGrrrr0{s   z%_UnixSelectorEventLoop._handle_signalc Cs||z |j|=Wntk r,YdSX|tjkr@tj}ntj}zt||WnBtk r}z$|jtj krt d|dnW5d}~XYnX|jszt dWn2t tfk r}zt d|W5d}~XYnXdS)zwRemove a handler for a signal. UNIX only. Return True if a signal handler was removed, False if not. Fr5r6Nr3r4T)r8rKeyErrorr9SIGINTdefault_int_handlerSIG_DFLr=rBrCr>r:r<r rA)rr/handlerrFrrrr)s(    z,_UnixSelectorEventLoop.remove_signal_handlercCs6t|tstd||tkr2td|dS)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 zinvalid signal number N) isinstanceintr7r9 valid_signalsr<r.rrrr8s  z$_UnixSelectorEventLoop._check_signalcCst|||||Sr)_UnixReadPipeTransportrpipeprotocolwaiterextrarrr_make_read_pipe_transportsz0_UnixSelectorEventLoop._make_read_pipe_transportcCst|||||Sr)_UnixWritePipeTransportrSrrr_make_write_pipe_transportsz1_UnixSelectorEventLoop._make_write_pipe_transportc st} | std|} t||||||||f| |d| } | | |j| z| IdHWnDt t fk rYn,t k r| | IdHYnXW5QRX| S)NzRasyncio.get_child_watcher() is not activated, subprocess support is not installed.)rVrW)rget_child_watcher is_activer> create_future_UnixSubprocessTransportadd_child_handlerZget_pid_child_watcher_callback SystemExitKeyboardInterrupt BaseExceptionr%Z_wait) rrUrEshellstdinstdoutstderrbufsizerWkwargswatcherrVtransprrr_make_subprocess_transports8   z1_UnixSelectorEventLoop._make_subprocess_transportcCs||j|dSr)call_soon_threadsafeZ_process_exited)rpid returncoderkrrrr`sz._UnixSelectorEventLoop._child_watcher_callback)sslsockserver_hostnamessl_handshake_timeoutc s |dkst|tst|r,|dkrLtdn |dk r.cb)Zadd_done_callback)rrrqrrrrrsz6_UnixSelectorEventLoop._sock_add_cancellation_callback)N)NN)NN)N)N)N)__name__ __module__ __qualname____doc__rr%r2rHr0r)r8rXrZrlr`rrrrrr __classcell__rrr!rr/sH -       . CFrcseZdZdZdfdd ZddZddZd d Zd d Zd dZ ddZ ddZ ddZ e jfddZdddZddZddZZS) rRiNcst|||jd<||_||_||_||_d|_d|_ t |jj }t |st |st |sd|_d|_d|_tdt |jd|j|jj||j|jj|j|j|dk r|jtj|ddS)NrTFz)Pipe transport is for pipes/sockets only.)rr_extra_loop_piper;_fileno _protocol_closing_pausedrxrrrS_ISFIFOrS_ISCHRr< set_blocking call_soonconnection_made _add_reader _read_readyr _set_result_unless_cancelled)rrrTrUrVrWmoder!rrrs:      z_UnixReadPipeTransport.__init__cCs|jjg}|jdkr |dn|jr0|d|d|jt|jdd}|jdk r|dk rt ||jt j }|r|dq|dn |jdk r|dn |dd d |S) Nclosedclosingfd= _selectorpollingidleopen<{}> )r"rrappendrrgetattrrr _test_selector_event selectorsZ EVENT_READformatjoin)rrAr rrrr__repr__s(         z_UnixReadPipeTransport.__repr__c Cszt|j|j}WnDttfk r,Yntk rX}z||dW5d}~XYn^X|rl|j |nJ|j rt d|d|_|j |j|j |jj|j |jddS)Nz"Fatal read error on pipe transport%r was closed by peerT)rxreadrmax_sizerrr= _fatal_errorrZ data_receivedr get_debugr rAr_remove_readerrZ eof_received_call_connection_lost)rr1rFrrrrs  z"_UnixReadPipeTransport._read_readycCs>|js |jrdSd|_|j|j|jr:td|dS)NTz%r pauses reading)rrrrrrr debugrrrr pause_readings   z$_UnixReadPipeTransport.pause_readingcCsB|js |jsdSd|_|j|j|j|jr>td|dS)NFz%r resumes reading) rrrrrrrr rrrrrresume_readings   z%_UnixReadPipeTransport.resume_readingcCs ||_dSrrrrUrrr set_protocol sz#_UnixReadPipeTransport.set_protocolcCs|jSrrrrrr get_protocolsz#_UnixReadPipeTransport.get_protocolcCs|jSrrrrrr is_closingsz!_UnixReadPipeTransport.is_closingcCs|js|ddSr)r_closerrrrr%sz_UnixReadPipeTransport.closecCs,|jdk r(|d|t|d|jdSNzunclosed transport r#rr,r%r_warnrrr__del__s z_UnixReadPipeTransport.__del__Fatal error on pipe transportcCsZt|tr4|jtjkr4|jrLtjd||ddn|j||||j d| |dSNz%r: %sTexc_info)message exceptionrrU) rOr=rBZEIOrrr rcall_exception_handlerrrrrFrrrrrs z#_UnixReadPipeTransport._fatal_errorcCs(d|_|j|j|j|j|dSNT)rrrrrrrrFrrrr-sz_UnixReadPipeTransport._closecCs4z|j|W5|jd|_d|_d|_XdSrrr%rrZconnection_lostrrrrr2s  z,_UnixReadPipeTransport._call_connection_lost)NN)r)rrrrrrrrrrrrr%r*r+rrrrrrrr!rrRs rRcseZdZd%fdd ZddZddZdd Zd d Zd d ZddZ ddZ ddZ ddZ ddZ ddZejfddZddZd&dd Zd'd!d"Zd#d$ZZS)(rYNc st||||jd<||_||_||_t|_d|_ d|_ t |jj }t|}t|}t|} |s|s| sd|_d|_d|_tdt |jd|j|jj|| s|rtjds|j|jj|j|j|dk r|jtj|ddS)NrTrFz?Pipe transport is only for pipes, sockets and character devicesZaix)rrrrr;rr bytearray_buffer _conn_lostrrxrrrrrrr<rrrrr&platform startswithrrr r) rrrTrUrVrWrZis_charZis_fifoZ is_socketr!rrr?s:        z _UnixWritePipeTransport.__init__cCs|jjg}|jdkr |dn|jr0|d|d|jt|jdd}|jdk r|dk rt ||jt j }|r|dn |d| }|d|n |jdk r|dn |dd d |S) Nrrrrrrzbufsize=rrr)r"rrrrrrrr rrZ EVENT_WRITEget_write_buffer_sizerr)rrAr rrhrrrrds,         z _UnixWritePipeTransport.__repr__cCs t|jSr)lenrrrrrr|sz-_UnixWritePipeTransport.get_write_buffer_sizecCs6|jrtd||jr*|tn|dS)Nr)rrr rArrBrokenPipeErrorrrrrrs   z#_UnixWritePipeTransport._read_readyc CsRt|tttfstt|t|tr.t|}|s6dS|jsB|jrj|jtj krXt d|jd7_dS|j s8zt |j|}Wntttfk rd}YnZttfk rYnBtk r}z$|jd7_||dWYdSd}~XYnX|t|kr dS|dkr&t||d}|j|j|j|j |7_ |dS)Nz=pipe closed by peer or os.write(pipe, data) raised exception.rr#Fatal write error on pipe transport)rObytesr memoryviewrwreprrrrZ!LOG_THRESHOLD_FOR_CONNLOST_WRITESr warningrrxwriterrrrarbrcrrrZ _add_writer _write_readyZ_maybe_pause_protocol)rr1nrFrrrrs8      z_UnixWritePipeTransport.writec Cs|jstdzt|j|j}Wnttfk r:Ynttfk rRYnt k r}z6|j |j d7_ |j |j||dW5d}~XYnhX|t|jkr|j |j |j||jr|j |j|ddS|dkr |jd|=dS)NzData should not be emptyrrr)rrwrxrrrrrarbrcr-rr_remove_writerrrZ_maybe_resume_protocolrrr)rrrFrrrrs,    z$_UnixWritePipeTransport._write_readycCsdSrrrrrr can_write_eofsz%_UnixWritePipeTransport.can_write_eofcCsB|jr dS|jstd|_|js>|j|j|j|jddSr) rrrwrrrrrrrrrr write_eofs z!_UnixWritePipeTransport.write_eofcCs ||_dSrrrrrrrsz$_UnixWritePipeTransport.set_protocolcCs|jSrrrrrrrsz$_UnixWritePipeTransport.get_protocolcCs|jSrrrrrrrsz"_UnixWritePipeTransport.is_closingcCs|jdk r|js|dSr)rrr rrrrr%sz_UnixWritePipeTransport.closecCs,|jdk r(|d|t|d|jdSrrrrrrrs z_UnixWritePipeTransport.__del__cCs|ddSr)rrrrrabortsz_UnixWritePipeTransport.abortrcCsNt|tr(|jr@tjd||ddn|j||||jd||dSr) rOr=rrr rrrrrrrrrs  z$_UnixWritePipeTransport._fatal_errorcCsFd|_|jr|j|j|j|j|j|j|j|dSr) rrrr rr-rrrrrrrrs  z_UnixWritePipeTransport._closecCs4z|j|W5|jd|_d|_d|_XdSrrrrrrrs  z-_UnixWritePipeTransport._call_connection_lost)NN)r)N)rrrrrrrrrr r rrrr%r*r+rr rrrrrrr!rrY<s"% #   rYc@seZdZddZdS)r^c Ksd}|tjkrt\}}zPtj|f||||d|d||_|dk rh|t|d|d|j_ d}W5|dk r||XdS)NF)rdrerfrgZuniversal_newlinesrhwb) buffering) subprocessPIPErzZ socketpairr%Popen_procrdetachre) rrErdrerfrgrhriZstdin_wrrr_start s.  z_UnixSubprocessTransport._startN)rrrrrrrrr^ sr^c@sHeZdZdZddZddZddZdd Zd d Zd d Z ddZ 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. NNotImplementedErrorrrnrDrErrrr_9s 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.Nrrrnrrrremove_child_handlerDsz)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. Nrrrrrr attach_loopLsz AbstractChildWatcher.attach_loopcCs tdS)zlClose the watcher. This must be called to make sure that any underlying resource is freed. Nrrrrrr%VszAbstractChildWatcher.closecCs tdS)zReturn ``True`` if the watcher is active and is used by the event loop. Return True if the watcher is installed and ready to handle process exit notifications. Nrrrrrr\]szAbstractChildWatcher.is_activecCs tdS)zdEnter the watcher's context and allow starting new processes This function must return selfNrrrrr __enter__fszAbstractChildWatcher.__enter__cCs tdS)zExit the watcher's contextNrrabcrrr__exit__lszAbstractChildWatcher.__exit__N) rrrrr_rrr%r\rr!rrrrr"s   rcCs2t|rt| St|r*t|S|SdSr)rx WIFSIGNALEDWTERMSIG WIFEXITED WEXITSTATUS)statusrrr_compute_returncodeqs     r'c@sDeZdZddZddZddZddZd d Zd d Zd dZ dS)BaseChildWatchercCsd|_i|_dSr)r _callbacksrrrrrszBaseChildWatcher.__init__cCs|ddSr)rrrrrr%szBaseChildWatcher.closecCs|jdk o|jSr)rZ is_runningrrrrr\szBaseChildWatcher.is_activecCs tdSrr)r expected_pidrrr _do_waitpidszBaseChildWatcher._do_waitpidcCs tdSrrrrrr_do_waitpid_allsz BaseChildWatcher._do_waitpid_allcCs~|dkst|tjst|jdk r<|dkr<|jrrr%rr!rrr%s  zFastChildWatcher.closec Cs0|j |jd7_|W5QRSQRXdS)Nr)r=r?rrrrrszFastChildWatcher.__enter__c Cs^|jB|jd8_|js"|js0W5QRdSt|j}|jW5QRXtd|dS)Nrz5Caught subprocesses termination from unknown pids: %s)r=r?r>r?r-r r)rrrr Zcollateral_victimsrrrr!s  zFastChildWatcher.__exit__c Gst|jstd|jFz|j|}Wn.tk rT||f|j|<YW5QRdSXW5QRX|||f|dS)NzMust use the context manager)r?rwr=r>r;rJr))rrnrDrErorrrr_'sz"FastChildWatcher.add_child_handlercCs*z|j|=WdStk r$YdSXdSr1r2rrrrr5s z%FastChildWatcher.remove_child_handlerc Csztdtj\}}Wntk r,YdSX|dkr:dSt|}|jz|j|\}}WnNtk r|j r||j |<|j rt d||YW5QRqd}YnX|j rt d||W5QRX|dkrt d||q|||f|qdS)Nr3rz,unknown process %s exited with returncode %sr6z8Caught subprocess termination from unknown pid: %d -> %d)rxr8r9r:r'r=r)r;rJr?r>rrr rr)rrnr&rorDrErrrr,<s@    z FastChildWatcher._do_waitpid_all) rrrrrr%rr!r_rr,rrrr!rrs  rc@sheZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZdS)ra~A watcher that doesn't require running loop in the main thread. This implementation registers a SIGCHLD signal handler on instantiation (which may conflict with other code that install own handler for this signal). The solution is safe but it has a significant overhead when handling a big number of processes (*O(n)* each time a SIGCHLD is received). cCsi|_d|_dSr)r)_saved_sighandlerrrrrrzszMultiLoopChildWatcher.__init__cCs |jdk Sr)r@rrrrr\~szMultiLoopChildWatcher.is_activecCsT|j|jdkrdSttj}||jkr:tdnttj|jd|_dS)Nz+SIGCHLD handler was changed by outside code) r)r-r@r9 getsignalr.r/r r)rrNrrrr%s     zMultiLoopChildWatcher.closecCs|SrrrrrrrszMultiLoopChildWatcher.__enter__cCsdSrrrexc_typeZexc_valZexc_tbrrrr!szMultiLoopChildWatcher.__exit__cGs&t}|||f|j|<||dSr)rget_running_loopr)r+)rrnrDrErrrrr_sz'MultiLoopChildWatcher.add_child_handlercCs*z|j|=WdStk r$YdSXdSr1r2rrrrrs z*MultiLoopChildWatcher.remove_child_handlercCsN|jdk rdSttj|j|_|jdkrsz6ThreadedChildWatcher._join_threads..N)r(rIvaluesr)rthreadsrOrrrrJsz"ThreadedChildWatcher._join_threadscCs|SrrrrrrrszThreadedChildWatcher.__enter__cCsdSrrrBrrrr!szThreadedChildWatcher.__exit__cCs6ddt|jD}|r2||jdt|ddS)NcSsg|]}|r|qSr)rKrMrrrrP sz0ThreadedChildWatcher.__del__..z0 has registered but not finished child processesr#)r(rIrQr"r,)rrrRrrrrs  zThreadedChildWatcher.__del__cGsFt}tj|jdt|j||||fdd}||j|<|dS)Nzwaitpid-T)targetnamerErL) rrDr<ZThreadr+nextrHrIstart)rrnrDrErrOrrrr_s  z&ThreadedChildWatcher.add_child_handlercCsdSrrrrrrrsz)ThreadedChildWatcher.remove_child_handlercCsdSrrrrrrrsz ThreadedChildWatcher.attach_loopcCs|dks tzt|d\}}Wn(tk rH|}d}td|Yn Xt|}|rhtd||| rtd||n|j |||f||j |dS)Nrr4r5r6rE) rwrxr8r:r rr'rrrFrmrIr;)rrr*rDrErnr&rorrrr+"s(  z ThreadedChildWatcher._do_waitpidN)rrrrrr\r%rJrr!r*r+rr_rrr+rrrrrs  rcsHeZdZdZeZfddZddZfddZdd Z d d Z Z S) _UnixDefaultEventLoopPolicyz:UNIX event loop policy with a watcher for child processes.cstd|_dSr)rr_watcherrr!rrrAs z$_UnixDefaultEventLoopPolicy.__init__c CsHtj8|jdkr:t|_tttjr:|j|j j W5QRXdSr) rr=rXrrOr<current_thread _MainThreadr_localrrrrr _init_watcherEs z)_UnixDefaultEventLoopPolicy._init_watchercs6t||jdk r2tttjr2|j|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_looprXrOr<rYrZrrr!rrr]Ms   z*_UnixDefaultEventLoopPolicy.set_event_loopcCs|jdkr||jS)z~Get the watcher for child processes. If not yet set, a ThreadedChildWatcher object is automatically created. N)rXr\rrrrr[[s z-_UnixDefaultEventLoopPolicy.get_child_watchercCs4|dkst|tst|jdk r*|j||_dS)z$Set the watcher for child processes.N)rOrrwrXr%)rrjrrrset_child_watcheres  z-_UnixDefaultEventLoopPolicy.set_child_watcher) rrrrrZ _loop_factoryrr\r]r[r^rrrr!rrW=s   rW)2rrBrrGrxrr9rzrrr&r<r*rrrrrrr r r r logr __all__r ImportErrorrZBaseSelectorEventLooprZ ReadTransportrRZ_FlowControlMixinZWriteTransportrYZBaseSubprocessTransportr^rr'r(rrrrZBaseDefaultEventLoopPolicyrWrrrrrrs`             NO5Ji}Y3