a ´i*Êã@sædZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl mZddl mZddl mZddl mZddl mZdd l mZdd l mZdd l mZdd l mZdd l mZddlmZdZe jdkrþedƒ‚dd„ZGdd„dejƒZGdd„dej ƒZ!Gdd„dej"ej#ƒZ$Gdd„dej%ƒZ&Gdd„dƒZ'Gdd„de'ƒZ(d d!„Z)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/ƒZ0eZ1e0Z2dS).z2Selector event loop for Unix with signal handling.éNé)Ú base_events)Úbase_subprocess)Ú constants)Ú coroutines)Úevents)Ú exceptions)Úfutures)Úselector_events)Útasks)Ú transports)Úlogger)ÚSelectorEventLoopÚAbstractChildWatcherÚSafeChildWatcherÚFastChildWatcherÚPidfdChildWatcherÚMultiLoopChildWatcherÚThreadedChildWatcherÚDefaultEventLoopPolicyÚwin32z+Signals are not really supported on WindowscCsdS)zDummy signal handler.N©)ÚsignumÚframerrú+/usr/lib64/python3.9/asyncio/unix_events.pyÚ_sighandler_noop*srcsÊeZdZdZd)‡fdd„ Z‡fdd„Zdd„Zd d „Zd d „Zd d„Z dd„Z d*dd„Z d+dd„Z d,dd„Z dd„Zd-dddddœdd„Zd.ddddddœdd „Zd!d"„Zd#d$„Zd%d&„Zd'd(„Z‡ZS)/Ú_UnixSelectorEventLoopzdUnix event loop. Adds signal handling and UNIX Domain Socket support to SelectorEventLoop. Ncstƒ |¡i|_dS©N)ÚsuperÚ__init__Ú_signal_handlers)ÚselfÚselector©Ú __class__rrr5s z_UnixSelectorEventLoop.__init__csZtƒ ¡t ¡s.t|jƒD]}| |¡qn(|jrVtjd|›dt |d|j  ¡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!Úsigr#rrr'9s üz_UnixSelectorEventLoop.closecCs|D]}|sq| |¡qdSr)Ú_handle_signal)r!ÚdatarrrrÚ_process_self_dataGsz)_UnixSelectorEventLoop._process_self_datac GsRt |¡st |¡rtdƒ‚| |¡| ¡zt |j  ¡¡Wn4t t fyv}zt t |ƒƒ‚WYd}~n d}~00t |||d¡}||j|<zt |t¡t |d¡Wnžt yL}z„|j|=|jszt d¡Wn6t t fy}zt d|¡WYd}~n d}~00|jtjkr6t d|›dƒ‚n‚WYd}~n d}~00dS)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()NFéÿÿÿÿúset_wakeup_fd(-1) failed: %súsig ú cannot be caught)rZ iscoroutineZiscoroutinefunctionÚ TypeErrorÚ _check_signalÚ _check_closedÚsignalÚ set_wakeup_fdZ_csockÚfilenoÚ ValueErrorÚOSErrorÚ RuntimeErrorÚstrrÚHandler rÚ siginterruptr ÚinfoÚerrnoÚEINVAL)r!r1ÚcallbackÚargsÚexcÚhandleZnexcrrrÚadd_signal_handlerNs2 ÿ "  "z)_UnixSelectorEventLoop.add_signal_handlercCs8|j |¡}|durdS|jr*| |¡n | |¡dS)z2Internal helper that is the actual signal handler.N)r ÚgetZ _cancelledr+Z_add_callback_signalsafe)r!r1rKrrrr2{s   z%_UnixSelectorEventLoop._handle_signalc Csè| |¡z |j|=Wnty*YdS0|tjkr>tj}ntj}zt ||¡WnDty˜}z,|jtj kr‚t d|›dƒ‚n‚WYd}~n d}~00|jsäzt  d¡Wn4t tfyâ}zt  d|¡WYd}~n d}~00dS)zwRemove a handler for a signal. UNIX only. Return True if a signal handler was removed, False if not. Fr7r8Nr5r6T)r:r ÚKeyErrorr<ÚSIGINTÚdefault_int_handlerÚSIG_DFLr@rFrGrAr=r?r rE)r!r1ÚhandlerrJrrrr+…s(     "z,_UnixSelectorEventLoop.remove_signal_handlercCs6t|tƒstd|›ƒ‚|t ¡vr2td|›ƒ‚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 zinvalid signal number N)Ú isinstanceÚintr9r<Ú valid_signalsr?r0rrrr:¥s  z$_UnixSelectorEventLoop._check_signalcCst|||||ƒSr)Ú_UnixReadPipeTransport©r!ÚpipeÚprotocolÚwaiterÚextrarrrÚ_make_read_pipe_transport±sz0_UnixSelectorEventLoop._make_read_pipe_transportcCst|||||ƒSr)Ú_UnixWritePipeTransportrWrrrÚ_make_write_pipe_transportµsz1_UnixSelectorEventLoop._make_write_pipe_transportc ËsÌt ¡°} |  ¡stdƒ‚| ¡} t||||||||f| |dœ| ¤Ž} |  |  ¡|j| ¡z| IdHWn@t t fy€‚Yn*t y¨|   ¡|   ¡IdH‚Yn0Wdƒn1s¾0Y| S)NzRasyncio.get_child_watcher() is not activated, subprocess support is not installed.)rZr[)rÚget_child_watcherÚ is_activerAÚ create_futureÚ_UnixSubprocessTransportÚadd_child_handlerZget_pidÚ_child_watcher_callbackÚ SystemExitÚKeyboardInterruptÚ BaseExceptionr'Ú_wait) r!rYrIÚshellÚstdinÚstdoutÚstderrÚbufsizer[ÚkwargsÚwatcherrZÚtransprrrÚ_make_subprocess_transport¹s.  ÿþý ÿ &z1_UnixSelectorEventLoop._make_subprocess_transportcCs| |j|¡dSr)Úcall_soon_threadsafeZ_process_exited)r!ÚpidÚ returncoderprrrrd×sz._UnixSelectorEventLoop._child_watcher_callback)ÚsslÚsockÚserver_hostnameÚssl_handshake_timeoutc Ãs |r|dur6tdƒ‚n |dur&tdƒ‚|dur6tdƒ‚|dur¤|durNtdƒ‚t |¡}t tjtjd¡}z | d¡| ||¡IdHWqä| ¡‚Yqä0n@|dur´tdƒ‚|j tjksÌ|j tjkrÚtd|›ƒ‚| d¡|j |||||d IdH\}}||fS) Nz/you have to pass server_hostname when using sslz+server_hostname is only meaningful with sslú1ssl_handshake_timeout is only meaningful with sslú3path and sock can not be specified at the same timerFzno path and sock were specifiedú.A UNIX Domain Stream Socket was expected, got )rx) r?ÚosÚfspathÚsocketÚAF_UNIXÚ SOCK_STREAMÚ setblockingZ sock_connectr'ÚfamilyÚtypeZ_create_connection_transport) r!Úprotocol_factoryÚpathrurvrwrxÚ transportrYrrrÚcreate_unix_connectionÚsLÿÿÿ     ÿÿ þz-_UnixSelectorEventLoop.create_unix_connectionédT)rvÚbacklogrurxÚ start_servingc ÃsÈt|tƒrtdƒ‚|dur&|s&tdƒ‚|durJ|dur@tdƒ‚t |¡}t tjtj¡}|ddvrÊz t   t  |¡j ¡r„t  |¡WnBt y˜Yn2tyÈ}zt d||¡WYd}~n d}~00z| |¡Wnnty2} z@| ¡| jtjkrd|›d} ttj| ƒd‚n‚WYd} ~ nd} ~ 0| ¡‚Yn0n<|dur\td ƒ‚|jtjksx|jtjkr†td |›ƒ‚| d ¡t ||g||||¡} |rÄ|  ¡t d¡IdH| S) Nz*ssl argument must be an SSLContext or Noneryrzr)rúz2Unable to check or remove stale UNIX socket %r: %rzAddress z is already in usez-path was not specified, and no sock specifiedr{F)rSÚboolr9r?r|r}r~rr€ÚstatÚS_ISSOCKÚst_modeÚremoveÚFileNotFoundErrorr@r ÚerrorÚbindr'rFZ EADDRINUSEr‚rƒrrZServerZ_start_servingr Úsleep) r!r„r…rvr‰rurxrŠÚerrrJÚmsgZserverrrrÚcreate_unix_serversh  ÿ ÿ   ÿ   ÿ ÿÿ  ÿz)_UnixSelectorEventLoop.create_unix_serverc ÃsÖz tjWnty&t d¡‚Yn0z | ¡}Wn4ttjfyh}zt d¡‚WYd}~n d}~00zt |¡j }Wnt y–t d¡‚Yn0|r |n|}|s¬dS|  ¡} |  | d|||||d¡| IdHS)Nzos.sendfile() is not availableznot a regular filer) r|ÚsendfileÚAttributeErrorrÚSendfileNotAvailableErrorr>ÚioÚUnsupportedOperationÚfstatÚst_sizer@raÚ_sock_sendfile_native_impl) r!rvÚfileÚoffsetÚcountr>r•ÚfsizeÚ blocksizeÚfutrrrÚ_sock_sendfile_nativeJs,  ÿ      ÿz,_UnixSelectorEventLoop._sock_sendfile_nativec Cs,| ¡} |dur| |¡| ¡r4| |||¡dS|rd||}|dkrd| |||¡| |¡dSzt | |||¡} WnDttfyÄ|dur |  ||¡|  | |j || ||||||¡ Yndt yj} zŽ|dur| j t jkrt| ƒturtdt jƒ} | | _| } |dkr>t d¡} | |||¡| | ¡n| |||¡| | ¡WYd} ~ nÆd} ~ 0ttfy‚‚Yn¦ty¾} z$| |||¡| | ¡WYd} ~ nrd} ~ 00| dkrä| |||¡| |¡nD|| 7}|| 7}|dur |  ||¡|  | |j || ||||||¡ dS)Nrzsocket is not connectedzos.sendfile call failed)r>Ú remove_writerÚ cancelledÚ_sock_sendfile_update_fileposÚ set_resultr|r˜ÚBlockingIOErrorÚInterruptedErrorÚ_sock_add_cancellation_callbackZ add_writerrŸr@rFZENOTCONNrƒÚConnectionErrorÚ __cause__rršÚ set_exceptionrerfrg)r!r¥Z registered_fdrvr>r¡r¢r¤Ú total_sentÚfdÚsentrJÚnew_excr•rrrrŸasp    þ   ÿ þÿ ÿ       þz1_UnixSelectorEventLoop._sock_sendfile_native_implcCs|dkrt ||tj¡dS©Nr)r|ÚlseekÚSEEK_SET)r!r>r¡r±rrrr©§sz4_UnixSelectorEventLoop._sock_sendfile_update_fileposcs‡‡fdd„}| |¡dS)Ncs&| ¡r"ˆ ¡}|dkr"ˆ |¡dS)Nr5)r¨r>r§)r¥r²©r!rvrrÚcb¬szB_UnixSelectorEventLoop._sock_add_cancellation_callback..cb)Úadd_done_callback)r!r¥rvr¹rr¸rr­«sz6_UnixSelectorEventLoop._sock_add_cancellation_callback)N)NN)NN)N)N)N)Ú__name__Ú __module__Ú __qualname__Ú__doc__rr'r4rLr2r+r:r\r^rqrdr‡r—r¦rŸr©r­Ú __classcell__rrr#rr/sB -   ÿ ÿ þ ÿü .ÿü CFrcsŠeZdZdZd‡fdd„ Zdd„Zdd„Zd d „Zd d „Zd d„Z dd„Z dd„Z dd„Z e jfdd„Zddd„Zdd„Zdd„Z‡ZS) rViNcsÚtƒ |¡||jd<||_||_| ¡|_||_d|_d|_ t   |j¡j }t  |¡s„t  |¡s„t  |¡s„d|_d|_d|_tdƒ‚t  |jd¡|j |jj|¡|j |jj|j|j¡|durÖ|j tj|d¡dS)NrXFz)Pipe transport is for pipes/sockets only.)rrÚ_extraÚ_loopÚ_piper>Ú_filenoÚ _protocolÚ_closingÚ_pausedr|rrrÚS_ISFIFOrŽÚS_ISCHRr?Ú set_blockingÚ call_soonÚconnection_madeÚ _add_readerÚ _read_readyr Ú_set_result_unless_cancelled)r!ÚlooprXrYrZr[Úmoder#rrr¸s6    ÿþ ÿ ÿz_UnixReadPipeTransport.__init__cCsÀ|jjg}|jdur | d¡n|jr0| d¡| d|j›¡t|jddƒ}|jdur|durt  ||jt j ¡}|r„| d¡q°| d¡n |jdur¦| d¡n | d¡d  d   |¡¡S) NÚclosedÚclosingúfd=Ú _selectorÚpollingÚidleÚopenú<{}>ú )r$r»rÂÚappendrÅrÃÚgetattrrÁr Ú_test_selector_eventÚ selectorsÚ EVENT_READÚformatÚjoin)r!rEr"rÕrrrÚ__repr__Ös$     ÿ     z_UnixReadPipeTransport.__repr__c Csºzt |j|j¡}WnDttfy*YnŒtyX}z| |d¡WYd}~nfd}~00|rl|j  |¡nJ|j   ¡r‚t   d|¡d|_|j  |j¡|j  |jj¡|j  |jd¡dS)Nz"Fatal read error on pipe transportú%r was closed by peerT)r|ÚreadrÃÚmax_sizer«r¬r@Ú _fatal_errorrÄZ data_receivedrÁÚ get_debugr rErÅÚ_remove_readerrÊZ eof_receivedÚ_call_connection_lost)r!r3rJrrrrÍës"  z"_UnixReadPipeTransport._read_readycCs>|js |jrdSd|_|j |j¡|j ¡r:t d|¡dS)NTz%r pauses reading)rÅrÆrÁrçrÃrær Údebug©r!rrrÚ pause_readingýs   z$_UnixReadPipeTransport.pause_readingcCsB|js |jsdSd|_|j |j|j¡|j ¡r>t d|¡dS)NFz%r resumes reading) rÅrÆrÁrÌrÃrÍrær rérêrrrÚresume_readings   z%_UnixReadPipeTransport.resume_readingcCs ||_dSr©rÄ©r!rYrrrÚ set_protocol sz#_UnixReadPipeTransport.set_protocolcCs|jSrrírêrrrÚ get_protocolsz#_UnixReadPipeTransport.get_protocolcCs|jSr©rÅrêrrrÚ is_closingsz!_UnixReadPipeTransport.is_closingcCs|js| d¡dSr)rÅÚ_closerêrrrr'sz_UnixReadPipeTransport.closecCs,|jdur(|d|›t|d|j ¡dS©Nzunclosed transport r%©rÂr.r'©r!Ú_warnrrrÚ__del__s z_UnixReadPipeTransport.__del__úFatal error on pipe transportcCsZt|tƒr4|jtjkr4|j ¡rLtjd||ddn|j ||||j dœ¡|  |¡dS©Nz%r: %sT©Úexc_info)ÚmessageÚ exceptionr†rY) rSr@rFZEIOrÁrær réÚcall_exception_handlerrÄró©r!rJrýrrrrås üz#_UnixReadPipeTransport._fatal_errorcCs(d|_|j |j¡|j |j|¡dS©NT)rÅrÁrçrÃrÊrè©r!rJrrrró-sz_UnixReadPipeTransport._closecCsPz,|j |¡W|j ¡d|_d|_d|_n|j ¡d|_d|_d|_0dSr©rÄZconnection_lostrÂr'rÁrrrrrè2s ý z,_UnixReadPipeTransport._call_connection_lost)NN)rù)r»r¼r½rärrárÍrërìrïrðròr'r,r-rørårórèr¿rrr#rrV´s rVcs¨eZdZd%‡fdd„ Zdd„Zdd„Zdd „Zd d „Zd d „Zdd„Z dd„Z dd„Z dd„Z dd„Z dd„Zejfdd„Zdd„Zd&dd „Zd'd!d"„Zd#d$„Z‡ZS)(r]Nc sþtƒ ||¡||jd<||_| ¡|_||_tƒ|_d|_ d|_ t   |j¡j }t |¡}t |¡}t |¡} |s”|s”| s”d|_d|_d|_tdƒ‚t  |jd¡|j |jj|¡| sÈ|ràtj d¡sà|j |jj|j|j¡|durú|j tj|d¡dS)NrXrFz?Pipe transport is only for pipes, sockets and character devicesZaix)rrrÀrÂr>rÃrÄÚ bytearrayÚ_bufferÚ _conn_lostrÅr|rrrrÈrÇrŽr?rÉrÁrÊrËr(ÚplatformÚ startswithrÌrÍr rÎ) r!rÏrXrYrZr[rÐZis_charÚis_fifoÚ is_socketr#rrr?s6       ÿ ÿz _UnixWritePipeTransport.__init__cCsØ|jjg}|jdur | d¡n|jr0| d¡| d|j›¡t|jddƒ}|jdur¨|dur¨t  ||jt j ¡}|r„| d¡n | d¡|  ¡}| d|›¡n |jdur¾| d¡n | d¡d   d  |¡¡S) NrÑrÒrÓrÔrÕrÖzbufsize=r×rØrÙ)r$r»rÂrÚrÅrÃrÛrÁr rÜrÝÚ EVENT_WRITEÚget_write_buffer_sizerßrà)r!rEr"rÕrmrrrráds(     ÿ     z _UnixWritePipeTransport.__repr__cCs t|jƒSr)Úlenrrêrrrr |sz-_UnixWritePipeTransport.get_write_buffer_sizecCs6|j ¡rt d|¡|jr*| tƒ¡n| ¡dS)Nrâ)rÁrær rErróÚBrokenPipeErrorrêrrrrÍs   z#_UnixWritePipeTransport._read_readyc Cs.t|tƒrt|ƒ}|sdS|js&|jrN|jtjkr|j  ¡|j d7_ |j   |j¡|  |d¡WYd}~nnd}~00|t|jƒkræ|j  ¡|j   |j¡| ¡|jrâ|j  |j¡| d¡dS|dkrú|jd|…=dS)Nrrr)r|rrÃrr«r¬rerfrgr/rrÁÚ_remove_writerrår Z_maybe_resume_protocolrÅrçrè)r!rrJrrrr«s* "  z$_UnixWritePipeTransport._write_readycCsdSrrrêrrrÚ can_write_eofÇsz%_UnixWritePipeTransport.can_write_eofcCs8|jr dSd|_|js4|j |j¡|j |jd¡dSr)rÅrrÁrçrÃrÊrèrêrrrÚ write_eofÊs z!_UnixWritePipeTransport.write_eofcCs ||_dSrrírîrrrrïÓsz$_UnixWritePipeTransport.set_protocolcCs|jSrrírêrrrrðÖsz$_UnixWritePipeTransport.get_protocolcCs|jSrrñrêrrrròÙsz"_UnixWritePipeTransport.is_closingcCs|jdur|js| ¡dSr)rÂrÅrrêrrrr'Üsz_UnixWritePipeTransport.closecCs,|jdur(|d|›t|d|j ¡dSrôrõrörrrrøás z_UnixWritePipeTransport.__del__cCs| d¡dSr)rórêrrrÚabortæsz_UnixWritePipeTransport.abortrùcCsNt|tƒr(|j ¡r@tjd||ddn|j ||||jdœ¡| |¡dSrú) rSr@rÁrær rérÿrÄrórrrrråés  üz$_UnixWritePipeTransport._fatal_errorcCsFd|_|jr|j |j¡|j ¡|j |j¡|j |j|¡dSr) rÅrrÁrrÃr/rçrÊrèrrrrró÷s  z_UnixWritePipeTransport._closecCsPz,|j |¡W|j ¡d|_d|_d|_n|j ¡d|_d|_d|_0dSrrrrrrrèÿs ý z-_UnixWritePipeTransport._call_connection_lost)NN)rù)N)r»r¼r½rrár rÍrrrrrïrðròr'r,r-rørrårórèr¿rrr#rr]<s"% #   r]c@seZdZdd„ZdS)rbc Ks¢d}|tjkrt ¡\}}zhtj|f||||d|dœ|¤Ž|_|durh| ¡t| ¡d|d|j_ d}W|durž| ¡| ¡n|durœ| ¡| ¡0dS)NF)rirjrkrlÚuniversal_newlinesrmÚwb)Ú buffering) Ú subprocessÚPIPEr~Ú socketpairÚPopenÚ_procr'r×Údetachrj) r!rIrirjrkrlrmrnZstdin_wrrrÚ_start s,  ÿþþ þz_UnixSubprocessTransport._startN)r»r¼r½r"rrrrrb srbc@sHeZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„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©r!rsrHrIrrrrc9s 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.Nr#©r!rsrrrÚremove_child_handlerDsz)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. Nr#©r!rÏrrrÚ attach_loopLsz AbstractChildWatcher.attach_loopcCs tƒ‚dS)zlClose the watcher. This must be called to make sure that any underlying resource is freed. Nr#rêrrrr'VszAbstractChildWatcher.closecCs tƒ‚dS)zºReturn ``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. Nr#rêrrrr`]szAbstractChildWatcher.is_activecCs tƒ‚dS)zdEnter the watcher's context and allow starting new processes This function must return selfNr#rêrrrÚ __enter__fszAbstractChildWatcher.__enter__cCs tƒ‚dS)zExit the watcher's contextNr#©r!ÚaÚbÚcrrrÚ__exit__lszAbstractChildWatcher.__exit__N) r»r¼r½r¾rcr'r)r'r`r*r/rrrrr"s   rc@sXeZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dd„Z dd„Z dS)ra6Child watcher implementation using Linux's pid file descriptors. This child watcher polls process file descriptors (pidfds) to await child process termination. In some respects, PidfdChildWatcher is a "Goldilocks" child watcher implementation. It doesn't require signals or threads, doesn't interfere with any processes launched outside the event loop, and scales linearly with the number of subprocesses launched by the event loop. The main disadvantage is that pidfds are specific to Linux, and only work on recent (5.3+) kernels. cCsd|_i|_dSr©rÁÚ _callbacksrêrrrr}szPidfdChildWatcher.__init__cCs|Srrrêrrrr*szPidfdChildWatcher.__enter__cCsdSrr)r!Úexc_typeÚ exc_valueÚ exc_tracebackrrrr/„szPidfdChildWatcher.__exit__cCs|jduo|j ¡Sr©rÁZ is_runningrêrrrr`‡szPidfdChildWatcher.is_activecCs| d¡dSr©r)rêrrrr'ŠszPidfdChildWatcher.closecCsd|jdur$|dur$|jr$t dt¡|j ¡D] \}}}|j |¡t |¡q.|j  ¡||_dS©NzCA loop is being detached from a child watcher with pending handlers) rÁr1r,r-ÚRuntimeWarningÚvaluesrçr|r'r/)r!rÏÚpidfdÚ_rrrr)sý   zPidfdChildWatcher.attach_loopcGsZ|j |¡}|dur*|d||f|j|<n,t |¡}|j ||j|¡|||f|j|<dSrµ)r1rMr|Ú pidfd_openrÁrÌÚ_do_wait)r!rsrHrIÚexistingr:rrrrc™s   z#PidfdChildWatcher.add_child_handlercCs~|j |¡\}}}|j |¡zt |d¡\}}Wn"tyTd}t d|¡Yn 0t |ƒ}t  |¡|||g|¢RŽdS)NréÿzJchild process pid %d exit status already read: will report returncode 255) r1ÚpoprÁrçr|ÚwaitpidÚChildProcessErrorr rÚ_compute_returncoder')r!rsr:rHrIr;Ústatusrtrrrr=¢s  ý  zPidfdChildWatcher._do_waitcCsFz|j |¡\}}}Wnty*YdS0|j |¡t |¡dS)NFT)r1r@rNrÁrçr|r')r!rsr:r;rrrr'µs   z&PidfdChildWatcher.remove_child_handlerN) r»r¼r½r¾rr*r/r`r'r)rcr=r'rrrrrqs   rcCs2t |¡rt |¡ St |¡r*t |¡S|SdSr)r|Ú WIFSIGNALEDÚWTERMSIGÚ WIFEXITEDÚ WEXITSTATUS)rDrrrrC¿s     rCc@sDeZdZdd„Zdd„Zdd„Zdd„Zd d „Zd d „Zd d„Z dS)ÚBaseChildWatchercCsd|_i|_dSrr0rêrrrrÏszBaseChildWatcher.__init__cCs| d¡dSrr6rêrrrr'ÓszBaseChildWatcher.closecCs|jduo|j ¡Srr5rêrrrr`ÖszBaseChildWatcher.is_activecCs tƒ‚dSrr#)r!Ú expected_pidrrrÚ _do_waitpidÙszBaseChildWatcher._do_waitpidcCs tƒ‚dSrr#rêrrrÚ_do_waitpid_allÜsz BaseChildWatcher._do_waitpid_allcCsf|jdur$|dur$|jr$t dt¡|jdur<|j tj¡||_|durb| tj|j ¡|  ¡dSr7) rÁr1r,r-r8r+r<ÚSIGCHLDrLÚ _sig_chldrLr(rrrr)ßsý zBaseChildWatcher.attach_loopc Cs^z | ¡WnLttfy$‚Yn6tyX}z|j d|dœ¡WYd}~n d}~00dS)Nú$Unknown exception in SIGCHLD handler)rýrþ)rLrerfrgrÁrÿrrrrrNós þzBaseChildWatcher._sig_chldN) r»r¼r½rr'r`rKrLr)rNrrrrrIÍsrIcsPeZdZdZ‡fdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z ‡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) cs|j ¡tƒ ¡dSr)r1r/rr'rêr#rrr' s zSafeChildWatcher.closecCs|Srrrêrrrr*szSafeChildWatcher.__enter__cCsdSrrr+rrrr/szSafeChildWatcher.__exit__cGs||f|j|<| |¡dSr)r1rKr%rrrrcsz"SafeChildWatcher.add_child_handlercCs(z|j|=WdSty"YdS0dS©NTF©r1rNr&rrrr's  z%SafeChildWatcher.remove_child_handlercCst|jƒD]}| |¡q dSr©r*r1rKr&rrrrL$sz SafeChildWatcher._do_waitpid_allcCsÂzt |tj¡\}}Wn&ty<|}d}t d|¡Yn.0|dkrJdSt|ƒ}|j ¡rjt  d||¡z|j   |¡\}}Wn,t yª|j ¡r¦tjd|ddYn0|||g|¢RŽdS)Nr?ú8Unknown child process pid %d, will report returncode 255rú$process %s exited with returncode %sú'Child watcher got an unexpected pid: %rTrû) r|rAÚWNOHANGrBr rrCrÁrærér1r@rN)r!rJrsrDrtrHrIrrrrK)s0 þ  ÿ  ÿ zSafeChildWatcher._do_waitpid) r»r¼r½r¾r'r*r/rcr'rLrKr¿rrr#rrs rcsTeZdZdZ‡fdd„Z‡fdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z ‡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ƒ ¡t ¡|_i|_d|_dSrµ)rrÚ threadingÚLockÚ_lockÚ_zombiesÚ_forksrêr#rrrVs  zFastChildWatcher.__init__cs"|j ¡|j ¡tƒ ¡dSr)r1r/rZrr'rêr#rrr'\s  zFastChildWatcher.closecCs<|j"|jd7_|WdƒS1s.0YdS)Nr)rYr[rêrrrr*aszFastChildWatcher.__enter__cCst|jN|jd8_|js"|js2WdƒdSt|jƒ}|j ¡Wdƒn1sZ0Yt d|¡dS)Nrz5Caught subprocesses termination from unknown pids: %s)rYr[rZrBr/r r)r!r,r-r.Zcollateral_victimsrrrr/gs  (þzFastChildWatcher.__exit__c Gs||jPz|j |¡}Wn.tyF||f|j|<YWdƒdS0Wdƒn1s\0Y|||g|¢RŽdSr)rYrZr@rNr1)r!rsrHrIrtrrrrcus 2z"FastChildWatcher.add_child_handlercCs(z|j|=WdSty"YdS0dSrPrQr&rrrr'ƒs  z%FastChildWatcher.remove_child_handlerc Cszt dtj¡\}}Wnty*YdS0|dkr8dSt|ƒ}|jŒz|j |¡\}}WnNtyª|j r¢||j |<|j   ¡r’t  d||¡YWdƒqd}Yn0|j   ¡rÄt  d||¡Wdƒn1sØ0Y|durút  d||¡q|||g|¢RŽqdS)Nr5rz,unknown process %s exited with returncode %srTz8Caught subprocess termination from unknown pid: %d -> %d)r|rArVrBrCrYr1r@rNr[rZrÁrær rér)r!rsrDrtrHrIrrrrLŠs:    þ  ÿ"þz FastChildWatcher._do_waitpid_all) r»r¼r½r¾rr'r*r/rcr'rLr¿rrr#rrLs  rc@sheZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dd„Z dd„Z dd„Z dd„ZdS)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)r1Ú_saved_sighandlerrêrrrrÈszMultiLoopChildWatcher.__init__cCs |jduSr)r\rêrrrr`ÌszMultiLoopChildWatcher.is_activecCsT|j ¡|jdurdSt tj¡}||jkr:t d¡nt tj|j¡d|_dS)Nz+SIGCHLD handler was changed by outside code) r1r/r\r<Ú getsignalrMrNr r)r!rRrrrr'Ïs     zMultiLoopChildWatcher.closecCs|Srrrêrrrr*ÛszMultiLoopChildWatcher.__enter__cCsdSrr©r!r2Úexc_valÚexc_tbrrrr/ÞszMultiLoopChildWatcher.__exit__cGs&t ¡}|||f|j|<| |¡dSr)rÚget_running_loopr1rK)r!rsrHrIrÏrrrrcász'MultiLoopChildWatcher.add_child_handlercCs(z|j|=WdSty"YdS0dSrPrQr&rrrr'ès  z*MultiLoopChildWatcher.remove_child_handlercCsN|jdurdSt tj|j¡|_|jdurKsÿz6ThreadedChildWatcher._join_threads..N)r*rfr9rà)r!ÚthreadsrlrrrrgIsz"ThreadedChildWatcher._join_threadscCs|Srrrêrrrr*PszThreadedChildWatcher.__enter__cCsdSrrr^rrrr/SszThreadedChildWatcher.__exit__cCs6dd„t|j ¡ƒDƒ}|r2||j›dt|ddS)NcSsg|]}| ¡r|‘qSr)rhrjrrrrmWsÿz0ThreadedChildWatcher.__del__..z0 has registered but not finished child processesr%)r*rfr9r$r.)r!r÷rnrrrrøVs  þzThreadedChildWatcher.__del__cGsFt ¡}tj|jdt|jƒ›||||fdd}||j|<| ¡dS)Nzwaitpid-T)ÚtargetÚnamerIri) rrarWÚThreadrKÚnextrerfÚstart)r!rsrHrIrÏrlrrrrc^s ý z&ThreadedChildWatcher.add_child_handlercCsdSrrr&rrrr'gsz)ThreadedChildWatcher.remove_child_handlercCsdSrrr(rrrr)msz ThreadedChildWatcher.attach_loopcCs˜zt |d¡\}}Wn&ty:|}d}t d|¡Yn 0t|ƒ}| ¡rZt d||¡| ¡rrt d||¡n|j |||g|¢RŽ|j   |¡dS)Nrr?rSrTrb) r|rArBr rrCrærércrrrfr@)r!rÏrJrHrIrsrDrtrrrrKps$ þ ÿz ThreadedChildWatcher._do_waitpidN)r»r¼r½r¾rr`r'rgr*r/r,r-rørcr'r)rKrrrrr2s  rcsHeZdZdZeZ‡fdd„Zdd„Z‡fdd„Zdd „Z d d „Z ‡Z S) Ú_UnixDefaultEventLoopPolicyz:UNIX event loop policy with a watcher for child processes.cstƒ ¡d|_dSr)rrÚ_watcherrêr#rrrs z$_UnixDefaultEventLoopPolicy.__init__cCs\tjB|jdur:tƒ|_t ¡t ¡ur:|j |jj ¡Wdƒn1sN0YdSr) rrYrurrWÚcurrent_threadÚ main_threadr)Ú_localrÁrêrrrÚ _init_watcher“s  z)_UnixDefaultEventLoopPolicy._init_watchercs6tƒ |¡|jdur2t ¡t ¡ur2|j |¡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_looprurWrvrwr)r(r#rrrzšs   ÿz*_UnixDefaultEventLoopPolicy.set_event_loopcCs|jdur| ¡|jS)z~Get the watcher for child processes. If not yet set, a ThreadedChildWatcher object is automatically created. N)ruryrêrrrr_¨s z-_UnixDefaultEventLoopPolicy.get_child_watchercCs|jdur|j ¡||_dS)z$Set the watcher for child processes.N)rur')r!rorrrÚset_child_watcher²s  z-_UnixDefaultEventLoopPolicy.set_child_watcher) r»r¼r½r¾rZ _loop_factoryrryrzr_r{r¿rrr#rrt‹s   rt)3r¾rFr›rdr|rÝr<r~rrr(rWr,Úrrrrrrr r r r Úlogr Ú__all__rÚ ImportErrorrZBaseSelectorEventLooprZ ReadTransportrVZ_FlowControlMixinZWriteTransportr]ZBaseSubprocessTransportrbrrrCrIrrrrZBaseDefaultEventLoopPolicyrtrrrrrrÚsb             ÿNON5Ji}Y2