o
     JAf                     @   s  	 d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ dZe	jdkr~eddd Zdd ZG dd dejZG dd dej Z!G dd dej"ej#Z$G dd dej%Z&G dd dZ'G dd  d e'Z(G d!d" d"e'Z)G d#d$ d$e)Z*G d%d& d&e)Z+G d'd( d(e'Z,G d)d* d*e'Z-G d+d, d,ej.Z/eZ0e/Z1dS )-    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 Windowsc                 C   s   	 d S N )signumframer   r   */usr/lib/python3.10/asyncio/unix_events.py_sighandler_noop*   s   r   c                 C   s$   zt | W S  ty   |  Y S w r   )oswaitstatus_to_exitcode
ValueError)statusr   r   r   r   /   s
   r   c                       s   e Zd Z	 d( fdd	Z fddZdd Zdd	 Zd
d Zdd Zdd Z			d)ddZ
		d)ddZ	d(ddZdd Z	d(dddddddZ	d(ddddddddZd d! Zd"d# Zd$d% Zd&d' Z  ZS )*_UnixSelectorEventLoopNc                    s   t  | i | _d S r   )super__init___signal_handlers)selfselector	__class__r   r   r#   ?      
z_UnixSelectorEventLoop.__init__c                    s`   t    t st| jD ]}| | qd S | jr.tjd| dt	| d | j
  d S d S )NzClosing 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'   r   r   r,   C   s   
z_UnixSelectorEventLoop.closec                 C   s   |D ]
}|sq|  | qd S r   )_handle_signal)r%   datar   r   r   r   _process_self_dataQ   s
   z)_UnixSelectorEventLoop._process_self_datac                 G   s0  	 t |st |rtd| | |   z
t| j	  W n t
tfy6 } ztt|d }~ww t||| d }|| j|< zt|t t|d W d S  ty } z8| j|= | jsztd W n t
tfy } ztd| W Y d }~nd }~ww |jtjkrtd| d d }~ww )Nz3coroutines cannot be used with add_signal_handler()Fset_wakeup_fd(-1) failed: %ssig  cannot be caught)r   ZiscoroutineZiscoroutinefunction	TypeError_check_signal_check_closedsignalset_wakeup_fdZ_csockfilenor   OSErrorRuntimeErrorstrr   Handler$   r   siginterruptr   infoerrnoEINVAL)r%   r6   callbackargsexchandleZnexcr   r   r   add_signal_handlerX   s@   


z)_UnixSelectorEventLoop.add_signal_handlerc                 C   s<   	 | j |}|d u rd S |jr| | d S | | d S r   )r$   getZ
_cancelledr0   Z_add_callback_signalsafe)r%   r6   rO   r   r   r   r7      s   z%_UnixSelectorEventLoop._handle_signalc              
   C   s   	 |  | z| j|= W n
 ty   Y dS w |tjkr tj}ntj}zt|| W n tyF } z|jtj	krAt
d| d d }~ww | jsnztd W dS  ttfym } ztd| W Y d }~dS d }~ww dS )NFr<   r=   r:   r;   T)r?   r$   KeyErrorrA   SIGINTdefault_int_handlerSIG_DFLrD   rJ   rK   rE   rB   r   r   rI   )r%   r6   handlerrN   r   r   r   r0      s8   

z,_UnixSelectorEventLoop.remove_signal_handlerc                 C   s8   	 t |tstd||t vrtd| d S )Nzsig must be an int, not zinvalid signal number )
isinstanceintr>   rA   valid_signalsr   r5   r   r   r   r?      s   
z$_UnixSelectorEventLoop._check_signalc                 C      t | ||||S r   )_UnixReadPipeTransportr%   pipeprotocolwaiterextrar   r   r   _make_read_pipe_transport      z0_UnixSelectorEventLoop._make_read_pipe_transportc                 C   rZ   r   )_UnixWritePipeTransportr\   r   r   r   _make_write_pipe_transport   rb   z1_UnixSelectorEventLoop._make_write_pipe_transportc	              
      s   t  U}
|
 std|  }t| |||||||f||d|	}|
| | j| z|I d H  W n t	t
fy?     tyQ   |  | I d H   w W d    |S 1 s]w   Y  |S )NzRasyncio.get_child_watcher() is not activated, subprocess support is not installed.)r_   r`   )r   get_child_watcher	is_activerE   create_future_UnixSubprocessTransportadd_child_handlerZget_pid_child_watcher_callback
SystemExitKeyboardInterruptBaseExceptionr,   _wait)r%   r^   rM   shellstdinstdoutstderrbufsizer`   kwargswatcherr_   transpr   r   r   _make_subprocess_transport   s:   



z1_UnixSelectorEventLoop._make_subprocess_transportc                 C   s   |  | j|j| d S r   )call_soon_threadsafe	call_soonZ_process_exited)r%   pid
returncoderv   r   r   r   rj      s   z._UnixSelectorEventLoop._child_watcher_callback)sslsockserver_hostnamessl_handshake_timeoutc          	         s  |r|d u rt dn|d urt d|d urt d|d urO|d ur(t dt|}ttjtjd}z|d | ||I d H  W n(   |   |d u rWt d|j	tjksc|j
tjkrjt d||d | j|||||d	I d H \}}||fS )
Nz/you have to pass server_hostname when using sslz+server_hostname is only meaningful with ssl1ssl_handshake_timeout is only meaningful with ssl3path and sock can not be specified at the same timer   Fzno path and sock were specified.A UNIX Domain Stream Socket was expected, got )r   )r   r   fspathsocketAF_UNIXSOCK_STREAMsetblockingZsock_connectr,   familytypeZ_create_connection_transport)	r%   protocol_factorypathr|   r}   r~   r   	transportr^   r   r   r   create_unix_connection   sN   


z-_UnixSelectorEventLoop.create_unix_connectiond   T)r}   backlogr|   r   start_servingc             
      s  t |tr
td|d ur|std|d ur|d ur tdt|}ttjtj}|d dvrezt	
t	|jrBt| W n! tyL   Y n tyd } ztd|| W Y d }~nd }~ww z|| W nF ty }	 z|  |	jtjkrd|d}
ttj|
d  d }	~	w   |   |d u rtd	|jtjks|jtjkrtd
||d t| |g||||}|r|  tdI d H  |S )Nz*ssl argument must be an SSLContext or Noner   r   r   )r    z2Unable to check or remove stale UNIX socket %r: %rzAddress z is already in usez-path was not specified, and no sock specifiedr   F)rW   boolr>   r   r   r   r   r   r   statS_ISSOCKst_moderemoveFileNotFoundErrorrD   r   errorbindr,   rJ   Z
EADDRINUSEr   r   r   r   ZServerZ_start_servingr   sleep)r%   r   r   r}   r   r|   r   r   errrN   msgZserverr   r   r   create_unix_server  sp   



z)_UnixSelectorEventLoop.create_unix_serverc           
   
      s   zt j W n ty   tdw z| }W n ttjfy- } ztdd }~ww zt |j	}W n t
yB   tdw |rG|n|}|sMdS |  }	| |	d |||||d |	I d H S )Nzos.sendfile() is not availableznot a regular filer   )r   sendfileAttributeErrorr   SendfileNotAvailableErrorrC   ioUnsupportedOperationfstatst_sizerD   rg   _sock_sendfile_native_impl)
r%   r}   fileoffsetcountrC   r   fsize	blocksizefutr   r   r   _sock_sendfile_nativeU  s6   



z,_UnixSelectorEventLoop._sock_sendfile_nativec	                 C   s*  |  }	|d ur| | | r| ||| d S |r2|| }|dkr2| ||| || d S z
t|	|||}
W n ttfya   |d u rO| 	|| | 
|	| j||	||||||
 Y d S  ty } zK|d ur|jtjkrt|turtdtj}||_|}|dkrtd}| ||| || n| ||| || W Y d }~d S W Y d }~d S d }~w ttfy     ty } z| ||| || W Y d }~d S d }~ww |
dkr| ||| || d S ||
7 }||
7 }|d u r| 	|| | 
|	| j||	||||||
 d S )Nr   zsocket is not connectedzos.sendfile call failed)rC   remove_writer	cancelled_sock_sendfile_update_filepos
set_resultr   r   BlockingIOErrorInterruptedError_sock_add_cancellation_callbackZ
add_writerr   rD   rJ   ZENOTCONNr   ConnectionError	__cause__r   r   set_exceptionrk   rl   rm   )r%   r   Zregistered_fdr}   rC   r   r   r   
total_sentfdsentrN   new_excr   r   r   r   r   l  st   



z1_UnixSelectorEventLoop._sock_sendfile_native_implc                 C   s    |dkrt ||t j d S d S Nr   )r   lseekSEEK_SET)r%   rC   r   r   r   r   r   r     s   z4_UnixSelectorEventLoop._sock_sendfile_update_fileposc                    s    fdd}| | d S )Nc                    s.   |   r }|dkr | d S d S d S )Nr:   )r   rC   r   )r   r   r%   r}   r   r   cb  s   zB_UnixSelectorEventLoop._sock_add_cancellation_callback.<locals>.cb)add_done_callback)r%   r   r}   r   r   r   r   r     s   z6_UnixSelectorEventLoop._sock_add_cancellation_callbackr   NN)__name__
__module____qualname__r#   r,   r9   rP   r7   r0   r?   ra   rd   rw   rj   r   r   r   r   r   r   __classcell__r   r   r'   r   r!   9   sD    -
 


.CFr!   c                       s   e Zd ZdZd fdd	Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd ZejfddZdddZdd Zdd Z  ZS ) r[   i   Nc                    s   t  | || jd< || _|| _| | _|| _d| _d| _	t
| jj}t|sBt|sBt|sBd | _d | _d | _tdt
| jd | j| jj|  | j| jj| j| j |d urm| jtj|d  d S d S )Nr]   Fz)Pipe transport is for pipes/sockets only.)r"   r#   _extra_loop_piperC   _fileno	_protocol_closing_pausedr   r   r   r   S_ISFIFOr   S_ISCHRr   set_blockingry   connection_made_add_reader_read_readyr	   _set_result_unless_cancelled)r%   loopr]   r^   r_   r`   moder'   r   r   r#     s8   



z_UnixReadPipeTransport.__init__c                 C   s   | j jg}| jd u r|d n| jr|d |d| j  t| jdd }| jd urH|d urHt	|| jt
j}|rB|d n|d n| jd urS|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%   rI   r&   r   r   r   r   __repr__  s$   





z_UnixReadPipeTransport.__repr__c              
   C   s   z
t | j| j}W n$ ttfy   Y d S  ty. } z| |d W Y d }~d S d }~ww |r9| j	| d S | j
 rDtd|  d| _| j
| j | j
| jj | j
| jd  d S )Nz"Fatal read error on pipe transport%r was closed by peerT)r   readr   max_sizer   r   rD   _fatal_errorr   Zdata_receivedr   	get_debugr   rI   r   _remove_readerry   Zeof_received_call_connection_lost)r%   r8   rN   r   r   r   r     s    
z"_UnixReadPipeTransport._read_readyc                 C   sB   | j s| jrd S d| _| j| j | j rtd|  d S d S )NTz%r pauses reading)r   r   r   r   r   r   r   debugr%   r   r   r   pause_reading  s   
z$_UnixReadPipeTransport.pause_readingc                 C   sF   | j s| jsd S d| _| j| j| j | j r!td|  d S d S )NFz%r resumes reading)	r   r   r   r   r   r   r   r   r   r   r   r   r   resume_reading  s   
z%_UnixReadPipeTransport.resume_readingc                 C   
   || _ d S r   r   r%   r^   r   r   r   set_protocol     
z#_UnixReadPipeTransport.set_protocolc                 C      | j S r   r   r   r   r   r   get_protocol     z#_UnixReadPipeTransport.get_protocolc                 C   r   r   r   r   r   r   r   
is_closing  r   z!_UnixReadPipeTransport.is_closingc                 C   s   | j s
| d  d S d S r   )r   _closer   r   r   r   r,   !  s   z_UnixReadPipeTransport.closec                 C   0   | j d ur|d| t| d | j   d S d S Nzunclosed transport r*   r   r3   r,   r%   _warnr   r   r   __del__%     
z_UnixReadPipeTransport.__del__Fatal error on pipe transportc                 C   sZ   t |tr|jtjkr| j rtjd| |dd n| j||| | j	d | 
| d S Nz%r: %sTexc_info)message	exceptionr   r^   )rW   rD   rJ   ZEIOr   r   r   r   call_exception_handlerr   r   r%   rN   r  r   r   r   r   *  s   
z#_UnixReadPipeTransport._fatal_errorc                 C   s(   d| _ | j| j | j| j| d S NT)r   r   r   r   ry   r   r%   rN   r   r   r   r   8  s   z_UnixReadPipeTransport._closec                 C   N   z| j | W | j  d | _d | _ d | _d S | j  d | _d | _ d | _w r   r   Zconnection_lostr   r,   r   r  r   r   r   r   =     


z,_UnixReadPipeTransport._call_connection_lostr   r  )r   r   r   r   r#   r   r   r   r   r   r   r   r,   r1   r2   r  r   r   r   r   r   r   r'   r   r[     s    
r[   c                       s   e Zd Zd% fdd	Zdd Zdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd ZejfddZdd Zd&dd Zd'd!d"Zd#d$ Z  ZS )(rc   Nc           
         s  t  || || jd< || _| | _|| _t | _d| _	d| _
t| jj}t|}t|}t|}	|sJ|sJ|	sJd | _d | _d | _tdt| jd | j| jj|  |	sd|rptjdsp| j| jj| j| j |d ur| jtj|d  d S d S )Nr]   r   Fz?Pipe transport is only for pipes, sockets and character devicesZaix)r"   r#   r   r   rC   r   r   	bytearray_buffer
_conn_lostr   r   r   r   r   r   r   r   r   r   r   ry   r   r-   platform
startswithr   r   r	   r   )
r%   r   r]   r^   r_   r`   r   Zis_charis_fifo	is_socketr'   r   r   r#   J  s8   





z _UnixWritePipeTransport.__init__c                 C   s   | j jg}| jd u r|d n| jr|d |d| j  t| jdd }| jd urT|d urTt	|| jt
j}|rB|d n|d |  }|d|  n| jd ur_|d n|d d	d
|S )Nr   r   r   r   r   r   zbufsize=r   r   r   )r(   r   r   r   r   r   r   r   r
   r   r   EVENT_WRITEget_write_buffer_sizer   r   )r%   rI   r&   r   rs   r   r   r   r   o  s(   






z _UnixWritePipeTransport.__repr__c                 C   s
   t | jS r   )lenr  r   r   r   r   r    r   z-_UnixWritePipeTransport.get_write_buffer_sizec                 C   s8   | j  rtd|  | jr| t  d S |   d S )Nr   )r   r   r   rI   r  r   BrokenPipeErrorr   r   r   r   r     s
   
z#_UnixWritePipeTransport._read_readyc              
   C   s&  t |tr	t|}|sd S | js| jr'| jtjkrtd |  jd7  _d S | j	sz	t
| j|}W n5 ttfy@   d}Y n) ttfyI     tyh } z|  jd7  _| |d W Y d }~d S d }~ww |t|krqd S |dkr}t||d  }| j| j| j |  j	|7  _	|   d S )Nz=pipe closed by peer or os.write(pipe, data) raised exception.r   r   #Fatal write error on pipe transport)rW   r  
memoryviewr  r   r   Z!LOG_THRESHOLD_FOR_CONNLOST_WRITESr   warningr  r   writer   r   r   rk   rl   rm   r   r  r   Z_add_writer_write_readyZ_maybe_pause_protocol)r%   r8   nrN   r   r   r   r     s:   

z_UnixWritePipeTransport.writec              
   C   s  z
t | j| j}W n@ ttfy   Y d S  ttfy     tyJ } z | j	  |  j
d7  _
| j| j | |d W Y d }~d S d }~ww |t| jkrs| j	  | j| j |   | jrq| j| j | d  d S |dkr| jd |= d S d S )Nr   r  r   )r   r   r   r  r   r   rk   rl   rm   r4   r  r   _remove_writerr   r  Z_maybe_resume_protocolr   r   r   )r%   r"  rN   r   r   r   r!    s0   


z$_UnixWritePipeTransport._write_readyc                 C      dS r  r   r   r   r   r   can_write_eof     z%_UnixWritePipeTransport.can_write_eofc                 C   s<   | j rd S d| _ | js| j| j | j| jd  d S d S r  )r   r  r   r   r   ry   r   r   r   r   r   	write_eof  s   z!_UnixWritePipeTransport.write_eofc                 C   r   r   r   r   r   r   r   r     r   z$_UnixWritePipeTransport.set_protocolc                 C   r   r   r   r   r   r   r   r     r   z$_UnixWritePipeTransport.get_protocolc                 C   r   r   r   r   r   r   r   r     r   z"_UnixWritePipeTransport.is_closingc                 C   s$   | j d ur| js|   d S d S d S r   )r   r   r'  r   r   r   r   r,     s   z_UnixWritePipeTransport.closec                 C   r   r   r   r   r   r   r   r    r  z_UnixWritePipeTransport.__del__c                 C      |  d  d S r   )r   r   r   r   r   abort     z_UnixWritePipeTransport.abortr  c                 C   sN   t |tr| j rtjd| |dd n| j||| | jd | | d S r  )	rW   rD   r   r   r   r   r
  r   r   r  r   r   r   r     s   

z$_UnixWritePipeTransport._fatal_errorc                 C   sF   d| _ | jr| j| j | j  | j| j | j| j| d S r  )	r   r  r   r#  r   r4   r   ry   r   r  r   r   r   r     s   
z_UnixWritePipeTransport._closec                 C   r  r   r  r  r   r   r   r   
  r  z-_UnixWritePipeTransport._call_connection_lostr   r  r   )r   r   r   r#   r   r  r   r   r!  r%  r'  r   r   r   r,   r1   r2   r  r)  r   r   r   r   r   r   r'   r   rc   G  s$    %	#	

rc   c                   @   s   e Zd Zdd ZdS )rh   c           	   	   K   s   d }|t jkrt \}}z7t j|f||||d|d|| _|d ur4|  t| d|d| j_	d }W |d urC|  |  d S d S |d urR|  |  w w )NF)ro   rp   rq   rr   universal_newlinesrs   wb)	buffering)

subprocessPIPEr   
socketpairPopen_procr,   r   detachrp   )	r%   rM   ro   rp   rq   rr   rs   rt   Zstdin_wr   r   r   _start  s0   

z_UnixSubprocessTransport._startN)r   r   r   r4  r   r   r   r   rh     s    rh   c                   @   sF   e Zd Z	 dd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )r   c                 G      	 t  r   NotImplementedErrorr%   rz   rL   rM   r   r   r   ri   D  s   z&AbstractChildWatcher.add_child_handlerc                 C   r5  r   r6  r%   rz   r   r   r   remove_child_handlerO  s   z)AbstractChildWatcher.remove_child_handlerc                 C   r5  r   r6  r%   r   r   r   r   attach_loopW  s   z AbstractChildWatcher.attach_loopc                 C   r5  r   r6  r   r   r   r   r,   a  s   zAbstractChildWatcher.closec                 C   r5  r   r6  r   r   r   r   rf   h  s   zAbstractChildWatcher.is_activec                 C   r5  r   r6  r   r   r   r   	__enter__q  s   zAbstractChildWatcher.__enter__c                 C   r5  r   r6  r%   abcr   r   r   __exit__w  s   zAbstractChildWatcher.__exit__N)
r   r   r   ri   r:  r<  r,   rf   r=  rB  r   r   r   r   r   -  s    
	r   c                   @   sV   e Zd Z	 dd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd ZdS )r   c                 C      d | _ i | _d S r   r   
_callbacksr   r   r   r   r#        
zPidfdChildWatcher.__init__c                 C      | S r   r   r   r   r   r   r=    r&  zPidfdChildWatcher.__enter__c                 C      d S r   r   )r%   exc_type	exc_valueexc_tracebackr   r   r   rB    r&  zPidfdChildWatcher.__exit__c                 C      | j d uo	| j  S r   r   Z
is_runningr   r   r   r   rf        zPidfdChildWatcher.is_activec                 C   r(  r   r<  r   r   r   r   r,     r*  zPidfdChildWatcher.closec                 C   sd   | j d ur|d u r| jrtdt | j D ]\}}}| j | t| q| j	  || _ d S NzCA loop is being detached from a child watcher with pending handlers)
r   rE  r1   r2   RuntimeWarningvaluesr   r   r,   r4   )r%   r   pidfd_r   r   r   r<    s   

zPidfdChildWatcher.attach_loopc                 G   s\   | j |}|d ur|d ||f| j |< d S t|}| j|| j| |||f| j |< d S r   )rE  rQ   r   
pidfd_openr   r   _do_wait)r%   rz   rL   rM   ZexistingrS  r   r   r   ri     s   
z#PidfdChildWatcher.add_child_handlerc                 C   s~   | j |\}}}| j| z
t|d\}}W n ty*   d}td| Y nw t	|}t
| |||g|R   d S )Nr      zJchild process pid %d exit status already read:  will report returncode 255)rE  popr   r   r   waitpidChildProcessErrorr   r  r   r,   )r%   rz   rS  rL   rM   rT  r    r{   r   r   r   rV    s   	
zPidfdChildWatcher._do_waitc                 C   sF   z| j |\}}}W n
 ty   Y dS w | j| t| dS )NFT)rE  rX  rR   r   r   r   r,   )r%   rz   rS  rT  r   r   r   r:    s   
z&PidfdChildWatcher.remove_child_handlerN)r   r   r   r#   r=  rB  rf   r,   r<  ri   rV  r:  r   r   r   r   r   |  s    	r   c                   @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )BaseChildWatcherc                 C   rC  r   rD  r   r   r   r   r#     rF  zBaseChildWatcher.__init__c                 C   r(  r   rO  r   r   r   r   r,     r*  zBaseChildWatcher.closec                 C   rL  r   rM  r   r   r   r   rf     rN  zBaseChildWatcher.is_activec                 C      t  r   r6  )r%   expected_pidr   r   r   _do_waitpid  r   zBaseChildWatcher._do_waitpidc                 C   r\  r   r6  r   r   r   r   _do_waitpid_all  r   z BaseChildWatcher._do_waitpid_allc                 C   sj   | j d ur|d u r| jrtdt | j d ur| j tj || _ |d ur3|tj| j	 | 
  d S d S rP  )r   rE  r1   r2   rQ  r0   rA   SIGCHLDrP   	_sig_chldr_  r;  r   r   r   r<    s   
zBaseChildWatcher.attach_loopc              
   C   sZ   z|    W d S  ttfy     ty, } z| jd|d W Y d }~d S d }~ww )N$Unknown exception in SIGCHLD handler)r  r	  )r_  rk   rl   rm   r   r
  r  r   r   r   ra    s   zBaseChildWatcher._sig_chldN)
r   r   r   r#   r,   rf   r^  r_  r<  ra  r   r   r   r   r[    s    r[  c                       sN   e Zd Z	  fddZdd Zdd Zdd Zd	d
 Zdd Zdd Z	  Z
S )r   c                    s   | j   t   d S r   )rE  r4   r"   r,   r   r'   r   r   r,   
  s   
zSafeChildWatcher.closec                 C   rG  r   r   r   r   r   r   r=    r&  zSafeChildWatcher.__enter__c                 C   rH  r   r   r>  r   r   r   rB    r&  zSafeChildWatcher.__exit__c                 G   s   ||f| j |< | | d S r   )rE  r^  r8  r   r   r   ri     s   z"SafeChildWatcher.add_child_handlerc                 C   $   z| j |= W dS  ty   Y dS w NTFrE  rR   r9  r   r   r   r:       z%SafeChildWatcher.remove_child_handlerc                 C      t | jD ]}| | qd S r   r/   rE  r^  r9  r   r   r   r_  !  s   z SafeChildWatcher._do_waitpid_allc                 C   s   zt |t j\}}W n ty   |}d}td| Y nw |dkr%d S t|}| j r5t	d|| z
| j
|\}}W n tyY   | j rVtjd|dd Y d S Y d S w |||g|R   d S )NrW  8Unknown child process pid %d, will report returncode 255r   $process %s exited with returncode %s'Child watcher got an unexpected pid: %rTr  )r   rY  WNOHANGrZ  r   r  r   r   r   r   rE  rX  rR   )r%   r]  rz   r    r{   rL   rM   r   r   r   r^  &  s6   	

zSafeChildWatcher._do_waitpid)r   r   r   r,   r=  rB  ri   r:  r_  r^  r   r   r   r'   r   r     s    
r   c                       sR   e Zd Z	  fddZ fddZdd Zdd Zd	d
 Zdd Zdd Z	  Z
S )r   c                    s$   t    t | _i | _d| _d S r   )r"   r#   	threadingLock_lock_zombies_forksr   r'   r   r   r#   S  s   


zFastChildWatcher.__init__c                    s"   | j   | j  t   d S r   )rE  r4   rp  r"   r,   r   r'   r   r   r,   Y  s   

zFastChildWatcher.closec                 C   s<   | j  |  jd7  _| W  d    S 1 sw   Y  d S )Nr   )ro  rq  r   r   r   r   r=  ^  s   $zFastChildWatcher.__enter__c                 C   sv   | j ( |  jd8  _| js| js	 W d    d S t| j}| j  W d    n1 s.w   Y  td| d S )Nr   z5Caught subprocesses termination from unknown pids: %s)ro  rq  rp  rF   r4   r   r  )r%   r?  r@  rA  Zcollateral_victimsr   r   r   rB  d  s   
	zFastChildWatcher.__exit__c              	   G   s|   | j ( z| j|}W n ty#   ||f| j|< Y W d    d S w W d    n1 s.w   Y  |||g|R   d S r   )ro  rp  rX  rR   rE  )r%   rz   rL   rM   r{   r   r   r   ri   r  s   	z"FastChildWatcher.add_child_handlerc                 C   rc  rd  re  r9  r   r   r   r:    rf  z%FastChildWatcher.remove_child_handlerc              	   C   s  	 zt dt j\}}W n
 ty   Y d S w |dkrd S t|}| jF z
| j|\}}W n' tyV   | j	rR|| j
|< | j rJtd|| Y W d    q d }Y nw | j rctd|| W d    n1 smw   Y  |d u r~td|| n	|||g|R   q)NTr:   r   z,unknown process %s exited with returncode %srj  z8Caught subprocess termination from unknown pid: %d -> %d)r   rY  rl  rZ  r   ro  rE  rX  rR   rq  rp  r   r   r   r   r  )r%   rz   r    r{   rL   rM   r   r   r   r_    sH   


z FastChildWatcher._do_waitpid_all)r   r   r   r#   r,   r=  rB  ri   r:  r_  r   r   r   r'   r   r   I  s    	r   c                   @   sf   e Zd Z	 dd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd ZdS )r   c                 C   s   i | _ d | _d S r   )rE  _saved_sighandlerr   r   r   r   r#     rF  zMultiLoopChildWatcher.__init__c                 C   s
   | j d uS r   )rr  r   r   r   r   rf     r   zMultiLoopChildWatcher.is_activec                 C   sT   | j   | jd u rd S ttj}|| jkrtd nttj| j d | _d S )Nz+SIGCHLD handler was changed by outside code)	rE  r4   rr  rA   	getsignalr`  ra  r   r  )r%   rV   r   r   r   r,     s   



zMultiLoopChildWatcher.closec                 C   rG  r   r   r   r   r   r   r=    r&  zMultiLoopChildWatcher.__enter__c                 C   rH  r   r   r%   rI  exc_valexc_tbr   r   r   rB    r&  zMultiLoopChildWatcher.__exit__c                 G   s&   t  }|||f| j|< | | d S r   )r   get_running_looprE  r^  )r%   rz   rL   rM   r   r   r   r   ri     s   z'MultiLoopChildWatcher.add_child_handlerc                 C   rc  rd  re  r9  r   r   r   r:    rf  z*MultiLoopChildWatcher.remove_child_handlerc                 C   sN   | j d urd S ttj| j| _ | j d u rtd tj| _ ttjd d S )NzaPrevious SIGCHLD handler was set by non-Python code, restore to default handler on watcher close.F)rr  rA   r`  ra  r   r  rU   rH   r;  r   r   r   r<    s   


z!MultiLoopChildWatcher.attach_loopc                 C   rg  r   rh  r9  r   r   r   r_    s   z%MultiLoopChildWatcher._do_waitpid_allc           	      C   s   zt |t j\}}W n ty    |}d}td| d}Y nw |dkr'd S t|}d}z| j|\}}}W n t	yJ   tjd|dd Y d S w |
 rXtd|| d S |re| retd	|| |j|||g|R   d S )
NrW  ri  Fr   Trk  r  %Loop %r that handles pid %r is closedrj  )r   rY  rl  rZ  r   r  r   rE  rX  rR   	is_closedr   r   rx   )	r%   r]  rz   r    r{   Z	debug_logr   rL   rM   r   r   r   r^    s:   
z!MultiLoopChildWatcher._do_waitpidc              	   C   sD   z|    W d S  ttfy     ty!   tjddd Y d S w )Nrb  Tr  )r_  rk   rl   rm   r   r  )r%   r   r   r   r   r   ra  &  s   zMultiLoopChildWatcher._sig_chldN)r   r   r   r#   rf   r,   r=  rB  ri   r:  r<  r_  r^  ra  r   r   r   r   r     s    %r   c                   @   sl   e Zd Z	 dd Zdd Zdd Zdd Zd	d
 Zdd Ze	j
fddZdd Zdd Zdd Zdd ZdS )r   c                 C   s   t d| _i | _d S r   )	itertoolsr   _pid_counter_threadsr   r   r   r   r#   <  r)   zThreadedChildWatcher.__init__c                 C   r$  r  r   r   r   r   r   rf   @  r&  zThreadedChildWatcher.is_activec                 C   s   |    d S r   )_join_threadsr   r   r   r   r,   C  s   zThreadedChildWatcher.closec                 C   s0   	 dd t | j D }|D ]}|  qd S )Nc                 S   s   g | ]}|  r|js|qS r   )is_alivedaemon.0threadr   r   r   
<listcomp>H  s
    
z6ThreadedChildWatcher._join_threads.<locals>.<listcomp>)r/   r|  rR  r   )r%   threadsr  r   r   r   r}  F  s
   
z"ThreadedChildWatcher._join_threadsc                 C   rG  r   r   r   r   r   r   r=  M  r&  zThreadedChildWatcher.__enter__c                 C   rH  r   r   rt  r   r   r   rB  P  r&  zThreadedChildWatcher.__exit__c                 C   s:   dd t | j D }|r|| j dt| d d S d S )Nc                 S   s   g | ]}|  r|qS r   )r~  r  r   r   r   r  T  s    
z0ThreadedChildWatcher.__del__.<locals>.<listcomp>z0 has registered but not finished child processesr*   )r/   r|  rR  r(   r3   )r%   r  r  r   r   r   r  S  s   
zThreadedChildWatcher.__del__c                 G   sF   t  }tj| jdt| j ||||fdd}|| j|< |  d S )Nzwaitpid-T)targetnamerM   r  )	r   rw  rm  Threadr^  nextr{  r|  start)r%   rz   rL   rM   r   r  r   r   r   ri   [  s   

z&ThreadedChildWatcher.add_child_handlerc                 C   r$  r  r   r9  r   r   r   r:  d  s   z)ThreadedChildWatcher.remove_child_handlerc                 C   rH  r   r   r;  r   r   r   r<  j  r&  z ThreadedChildWatcher.attach_loopc                 C   s   z
t |d\}}W n ty   |}d}td| Y nw t|}| r-td|| | r9td|| n|j	|||g|R   | j
| d S )Nr   rW  ri  rj  rx  )r   rY  rZ  r   r  r   r   r   ry  rx   r|  rX  )r%   r   r]  rL   rM   rz   r    r{   r   r   r   r^  m  s&   	z ThreadedChildWatcher._do_waitpidN)r   r   r   r#   rf   r,   r}  r=  rB  r1   r2   r  ri   r:  r<  r^  r   r   r   r   r   /  s    	r   c                       sF   e Zd Z	 eZ fddZdd Z fddZdd Zd	d
 Z	  Z
S )_UnixDefaultEventLoopPolicyc                    s   t    d | _d S r   )r"   r#   _watcherr   r'   r   r   r#     s   

z$_UnixDefaultEventLoopPolicy.__init__c                 C   s~   t j2 | jd u r%t | _t t u r-| j| jj	 W d    d S W d    d S W d    d S 1 s8w   Y  d S r   )
r   ro  r  r   rm  current_threadmain_threadr<  _localr   r   r   r   r   _init_watcher  s   
"z)_UnixDefaultEventLoopPolicy._init_watcherc                    s@   	 t  | | jd urt t u r| j| d S d S d S r   )r"   set_event_loopr  rm  r  r  r<  r;  r'   r   r   r    s   
z*_UnixDefaultEventLoopPolicy.set_event_loopc                 C   s   	 | j d u r
|   | j S r   )r  r  r   r   r   r   re     s   
z-_UnixDefaultEventLoopPolicy.get_child_watcherc                 C   s    	 | j d ur| j   || _ d S r   )r  r,   )r%   ru   r   r   r   set_child_watcher  s   


z-_UnixDefaultEventLoopPolicy.set_child_watcher)r   r   r   r!   Z_loop_factoryr#   r  r  re   r  r   r   r   r'   r   r    s    
r  )2rJ   r   rz  r   r   rA   r   r   r.  r-   rm  r1    r   r   r   r   r   r   r	   r
   r   r   logr   __all__r  ImportErrorr   r   ZBaseSelectorEventLoopr!   ZReadTransportr[   Z_FlowControlMixinZWriteTransportrc   ZBaseSubprocessTransportrh   r   r   r[  r   r   r   r   ZBaseDefaultEventLoopPolicyr  r   r   r   r   r   r   <module>   sd    
	
   	 	 NON5Ji}Y2