HEX
Server: Apache/2.4.65 (Debian)
System: Linux kubikelcreative 5.10.0-35-amd64 #1 SMP Debian 5.10.237-1 (2025-05-19) x86_64
User: www-data (33)
PHP: 8.4.13
Disabled: NONE
Upload Files
File: //usr/lib/python3.5/asyncio/__pycache__/unix_events.cpython-35.pyc


��j`���
@s�dZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZddl
mZddl
m
Z
ddl
mZddl
mZdd	l
mZdd
l
mZddl
mZddl
mZdd
l
mZddlmZddlmZdddddgZejdkrbed��dd�Zy
ejZWnek
r�dd�ZYnXGdd�dej�Z e!ed�r�dd�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/lib/python3.5/asyncio/unix_events.py�_sighandler_noop%srcCs|S)Nr)�pathrrr�<lambda>.src
seZdZdZd�fdd�Zdd�Z�fdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdddd�Zdddd�Z
eddd��Zdd�Zedddddddd��Zedddd d!ddd"d#��Z�S)$�_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__rrr7sz_UnixSelectorEventLoop.__init__cCs
tj�S)N)�socketZ
socketpair)rrrr�_socketpair;sz"_UnixSelectorEventLoop._socketpaircs8t�j�x$t|j�D]}|j|�qWdS)N)r�close�listr�remove_signal_handler)r�sig)r!rrr$>s
z_UnixSelectorEventLoop.closecCs+x$|D]}|sq|j|�qWdS)N)�_handle_signal)r�datarrrr�_process_self_dataCs
z)_UnixSelectorEventLoop._process_self_datac+Gs�tj|�stj|�r*td��|j|�|j�ytj|jj	��Wn=t
tfk
r�}ztt
|���WYdd}~XnXtj|||�}||j|<y$tj|t�tj|d�Wn�tk
r�}z�|j|=|jsXytjd�Wn;t
tfk
rW}ztjd|�WYdd}~XnX|jtjkr�t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���)rZiscoroutineZiscoroutinefunction�	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_handlerJs0

%

	#z)_UnixSelectorEventLoop.add_signal_handlercCsL|jj|�}|dkr"dS|jr;|j|�n
|j|�dS)z2Internal helper that is the actual signal handler.N)r�getZ
_cancelledr&Z_add_callback_signalsafe)rr'r=rrrr(ws	z%_UnixSelectorEventLoop._handle_signalc&Cs|j|�y|j|=Wntk
r3dSYnX|tjkrOtj}n	tj}ytj||�WnRtk
r�}z2|jtj	kr�t
dj|���n�WYdd}~XnX|jsytjd�Wn;t
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: %sTr+)r-r�KeyErrorr.�SIGINT�default_int_handler�SIG_DFLr2r7r8r3r9r/r1rr6)rr'Zhandlerr<rrrr&�s(

			#z,_UnixSelectorEventLoop.remove_signal_handlercCsbt|t�s$tdj|���d|ko>tjkns^tdj|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�intr,r9r.�NSIGr1)rr'rrrr-�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)rrHrIrJrKrrr�_make_write_pipe_transport�sz1_UnixSelectorEventLoop._make_write_pipe_transportc	ks�tj���}
|j�}t||||||||d|d||	�}|
j|j�|j|�y
|EdHWn+tk
r�}
z|
}WYdd}
~
XnXd}|dk	r�|j�|j	�EdH|�WdQRX|S)NrJrK)
r�get_child_watcherZ
create_future�_UnixSubprocessTransport�add_child_handlerZget_pid�_child_watcher_callback�	Exceptionr$Z_wait)rrIr;�shell�stdin�stdout�stderr�bufsizerK�kwargs�watcherrJ�transpr<�errrrr�_make_subprocess_transport�s$		



z1_UnixSelectorEventLoop._make_subprocess_transportcCs|j|j|�dS)N)Zcall_soon_threadsafeZ_process_exited)r�pid�
returncoder[rrrrR�sz._UnixSelectorEventLoop._child_watcher_callback�ssl�sock�server_hostnameccsc|dks!t|t�s!t�|rB|dkrZtd��n|dk	rZtd��|dk	r�|dk	r~td��tjtjtjd�}y&|jd�|j||�EdHWq6|j	��Yq6Xn\|dkr�td��|j
tjkstj|�r)tdj
|���|jd�|j||||�EdH\}}||fS)Nz/you have to pass server_hostname when using sslz+server_hostname is only meaningful with sslz3path and sock can not be specified at the same timerFzno path and sock were specifiedz2A UNIX Domain Stream Socket was expected, got {!r})rDr4�AssertionErrorr1r"�AF_UNIX�SOCK_STREAM�setblockingZsock_connectr$�familyr�_is_stream_socketr9Z_create_connection_transport)r�protocol_factoryrr`rarb�	transportrIrrr�create_unix_connection�s:!	


z-_UnixSelectorEventLoop.create_unix_connection�backlog�dc
!Cst|t�rtd��|dk	r�|dk	r?td��t|�}tjtjtj�}|dd
kr�y,tj	t
j|�j�r�t
j|�WnIt
k
r�Yn8tk
r�}ztjd||�WYdd}~XnXy|j|�Wq�tk
ri}zK|j�|jtjkrTdj|�}ttj|�d�n�WYdd}~Xq�|j��Yq�XnO|dkr�td��|jtjks�tj|�r�tdj|���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)rrn)rD�boolr,r1�_fspathr"rdre�stat�S_ISSOCK�os�st_mode�remove�FileNotFoundErrorr2r�errorZbindr$r7Z
EADDRINUSEr9rgrrhZServerZlistenrfZ_start_serving)
rrirrarlr`r\r<�msgZserverrrr�create_unix_server�sP	
&

	

z)_UnixSelectorEventLoop.create_unix_server)�__name__�
__module__�__qualname__�__doc__rr#r$r*r>r(r&r-rLrNr
r]rRrkryrr)r!rr1s,-
 
%r�set_blockingcCstj|d�dS)NF)rsr~)�fdrrr�_set_nonblocking9sr�cCs<tj|tj�}|tjB}tj|tj|�dS)N)�fcntlZF_GETFLrs�
O_NONBLOCKZF_SETFL)r�flagsrrrr�>s
cs�eZdZdZdd�fdd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dd�Zdd�Ze
jr�dd�Zddd�Zdd�Zdd�Z�S) rG�iNcs*t�j|�||jd<||_||_|j�|_||_d|_t	j
|j�j}tj
|�p�tj|�p�tj|�s�d|_d|_d|_td��t|j�|jj|jj|�|jj|jj|j|j�|dk	r&|jjtj|d�dS)NrHFz)Pipe transport is for pipes/sockets only.)rr�_extra�_loop�_piper0�_fileno�	_protocol�_closingrs�fstatrtrq�S_ISFIFOrr�S_ISCHRr1r��	call_soon�connection_made�_add_reader�_read_readyr	�_set_result_unless_cancelled)r�looprHrIrJrK�mode)r!rrrHs,
							
z_UnixReadPipeTransport.__init__cCs|jjg}|jdkr.|jd�n|jrD|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!rzr��appendr�r��getattrr�r
�_test_selector_eventrZ
EVENT_READ�join)rr6r r�rrr�__repr__es$	

z_UnixReadPipeTransport.__repr__cCs�ytj|j|j�}WnLttfk
r6Yn�tk
rj}z|j|d�WYdd}~Xn�X|r�|jj	|�ng|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)rs�readr��max_size�BlockingIOError�InterruptedErrorr2�_fatal_errorr�Z
data_receivedr��	get_debugrr6r��_remove_readerr�Zeof_received�_call_connection_lost)rr)r<rrrr�{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�)rrIrrr�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$�s	z_UnixReadPipeTransport.closecCs4|jdk	r0tjd|t�|jj�dS)Nzunclosed transport %r)r��warnings�warn�ResourceWarningr$)rrrr�__del__�sz_UnixReadPipeTransport.__del__zFatal error on pipe transportc	Cs�t|t�rL|jtjkrL|jj�rwtjd||dd�n+|jjd|d|d|d|j	i�|j
|�dS)Nz%r: %s�exc_infoT�message�	exceptionrjrI)rDr2r7ZEIOr�r�r�debug�call_exception_handlerr�r�)rr<r�rrrr��s!	z#_UnixReadPipeTransport._fatal_errorcCs6d|_|jj|j�|jj|j|�dS)NT)r�r�r�r�r�r�)rr<rrrr��s	z_UnixReadPipeTransport._closecCsDz|jj|�Wd|jj�d|_d|_d|_XdS)N)r��connection_lostr�r$r�)rr<rrrr��s
		z,_UnixReadPipeTransport._call_connection_losti)rzr{r|r�rr�r�r�r�r�r�r�r$r�PY34r�r�r�r�rr)r!rrGDs	rGcs�eZdZdd�fdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zejr�dd�Zdd�Zddd �Zdd!d"�Zd#d$�Z�S)%rMNc
sjt�j||�||jd<||_|j�|_||_t�|_d|_	d|_
tj|j�j
}tj|�}tj|�}tj|�}	|p�|p�|	s�d|_d|_d|_td��t|j�|jj|jj|�|	s|rAtjjd�rA|jj|jj|j|j�|dk	rf|jjtj|d�dS)NrHrFz?Pipe transport is only for pipes, sockets and character devicesZaix)rrr�r�r0r�r��	bytearray�_buffer�
_conn_lostr�rsr�rtrqr�r�rrr1r�r�r�r��sys�platform�
startswithr�r�r	r�)
rr�rHrIrJrKr�Zis_charZis_fifoZ	is_socket)r!rrr�s2
							
z _UnixWritePipeTransport.__init__cCs#|jjg}|jdkr.|jd�n|jrD|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!rzr�r�r�r�r�r�r
r�rZEVENT_WRITE�get_write_buffer_sizer�)rr6r r�rXrrrr��s(	


z _UnixWritePipeTransport.__repr__cCs
t|j�S)N)�lenr�)rrrrr�sz-_UnixWritePipeTransport.get_write_buffer_sizecCsI|jj�rtjd|�|jr;|jt��n
|j�dS)Nz%r was closed by peer)r�r�rr6r�r��BrokenPipeError)rrrrr�
s
	z#_UnixWritePipeTransport._read_readycCs�t|tttf�s*tt|���t|t�rEt|�}|sOdS|jsa|jr�|jtj	kr�t
jd�|jd7_dS|jsnyt
j|j|�}Wnettfk
r�d}YnHtk
r}z(|jd7_|j|d�dSWYdd}~XnX|t|�kr3dS|dkrUt|�|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)rD�bytesr��
memoryviewrc�reprr�r�rZ!LOG_THRESHOLD_FOR_CONNLOST_WRITESr�warningr�rs�writer�r�r�rSr�r�r�Z_add_writer�_write_readyZ_maybe_pause_protocol)rr)�nr<rrrr�s4*
	
z_UnixWritePipeTransport.writecCs;|jstd��ytj|j|j�}Wn{ttfk
rKYn�tk
r�}zD|jj�|j	d7_	|j
j|j�|j|d�WYdd}~Xn�X|t
|j�kr|jj�|j
j|j�|j�|jr|j
j|j�|jd�dS|dkr7|jd|�=dS)NzData should not be emptyrz#Fatal write error on pipe transportr)r�rcrsr�r�r�r�rS�clearr�r��_remove_writerr�r�Z_maybe_resume_protocolr�r�r�)rr�r<rrrr�4s(
#

	
z$_UnixWritePipeTransport._write_readycCsdS)NTr)rrrr�
can_write_eofNsz%_UnixWritePipeTransport.can_write_eofcCs[|jr
dS|jst�d|_|jsW|jj|j�|jj|jd�dS)NT)	r�r�rcr�r�r�r�r�r�)rrrr�	write_eofQs			z!_UnixWritePipeTransport.write_eofcCs
||_dS)N)r�)rrIrrrr�Zsz$_UnixWritePipeTransport.set_protocolcCs|jS)N)r�)rrrrr�]sz$_UnixWritePipeTransport.get_protocolcCs|jS)N)r�)rrrrr�`sz"_UnixWritePipeTransport.is_closingcCs'|jdk	r#|jr#|j�dS)N)r�r�r�)rrrrr$csz_UnixWritePipeTransport.closecCs4|jdk	r0tjd|t�|jj�dS)Nzunclosed transport %r)r�r�r�r�r$)rrrrr�lsz_UnixWritePipeTransport.__del__cCs|jd�dS)N)r�)rrrr�abortqsz_UnixWritePipeTransport.abortzFatal error on pipe transportc	Csyt|tj�r=|jj�rhtjd||dd�n+|jjd|d|d|d|ji�|j	|�dS)Nz%r: %sr�Tr�r�rjrI)
rDrZ_FATAL_ERROR_IGNOREr�r�rr�r�r�r�)rr<r�rrrr�ts	z$_UnixWritePipeTransport._fatal_errorcCs_d|_|jr%|jj|j�|jj�|jj|j�|jj|j|�dS)NT)	r�r�r�r�r�r�r�r�r�)rr<rrrr��s		
z_UnixWritePipeTransport._closecCsDz|jj|�Wd|jj�d|_d|_d|_XdS)N)r�r�r�r$r�)rr<rrrr��s
		z-_UnixWritePipeTransport._call_connection_lost)rzr{r|rr�r�r�r�r�r�r�r�r�r�r$rr�r�r�r�r�r�rr)r!rrM�s$%	!		rM�set_inheritablecCsittdd�}tj|tj�}|sJtj|tj||B�ntj|tj||@�dS)NZ
FD_CLOEXECr)r�r�ZF_GETFDZF_SETFD)rZinheritableZcloexec_flag�oldrrr�_set_inheritable�s
r�c@seZdZdd�ZdS)rPc	Ks�d}|tjkr=|jj�\}}t|j�d�tj|d|d|d|d|ddd||�|_|dk	r�|j�t	|j
�dd	|�|j_dS)
NFrTrUrVrWZuniversal_newlinesrX�wb�	buffering)�
subprocess�PIPEr�r#r�r0�Popen�_procr$r��detachrU)	rr;rTrUrVrWrXrYZstdin_wrrr�_start�s
z_UnixSubprocessTransport._startN)rzr{r|r�rrrrrP�srPc@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)rr^r:r;rrrrQ�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�)rr^rrr�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)
rzr{r|r}rQr�r�r$r�r�rrrrr�s
c@sdeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dS)�BaseChildWatchercCsd|_i|_dS)N)r��
_callbacks)rrrrrs	zBaseChildWatcher.__init__cCs|jd�dS)N)r�)rrrrr$	szBaseChildWatcher.closecCs
t��dS)N)r�)r�expected_pidrrr�_do_waitpidszBaseChildWatcher._do_waitpidcCs
t��dS)N)r�)rrrr�_do_waitpid_allsz BaseChildWatcher._do_waitpid_allcCs�|dks$t|tj�s$t�|jdk	rX|dkrX|jrXtjdt�|jdk	rz|jj	t
j�||_|dk	r�|jt
j|j
�|j�dS)NzCA loop is being detached from a child watcher with pending handlers)rDrZAbstractEventLooprcr�r�r�r��RuntimeWarningr&r.�SIGCHLDr>�	_sig_chldr�)rr�rrrr�s$$	zBaseChildWatcher.attach_loopcCsVy|j�WnAtk
rQ}z!|jjddd|i�WYdd}~XnXdS)Nr�z$Unknown exception in SIGCHLD handlerr�)r�rSr�r�)rr<rrrr�&s	zBaseChildWatcher._sig_chldcCsAtj|�rtj|�Stj|�r9tj|�S|SdS)N)rs�WIFSIGNALED�WTERMSIG�	WIFEXITED�WEXITSTATUS)r�statusrrr�_compute_returncode2s

z$BaseChildWatcher._compute_returncodeN)
rzr{r|rr$r�r�r�r�r�rrrrr�sr�cspeZdZdZ�fdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�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|jj�t�j�dS)N)r�r�rr$)r)r!rrr$Ks
zSafeChildWatcher.closecCs|S)Nr)rrrrr�OszSafeChildWatcher.__enter__cCsdS)Nr)rr�r�r�rrrr�RszSafeChildWatcher.__exit__cGs?|jdkrtd��||f|j|<|j|�dS)NzICannot add child handler, the child watcher does not have a loop attached)r�r3r�r�)rr^r:r;rrrrQUs
	z"SafeChildWatcher.add_child_handlercCs/y|j|=dSWntk
r*dSYnXdS)NTF)r�r@)rr^rrrr�`s


z%SafeChildWatcher.remove_child_handlercCs+x$t|j�D]}|j|�qWdS)N)r%r�r�)rr^rrrr�gsz SafeChildWatcher._do_waitpid_allcCs
|dkst�ytj|tj�\}}Wn.tk
ra|}d}tjd|�YnBX|dkrrdS|j|�}|jj	�r�tj
d||�y|jj|�\}}Wn7t
k
r�|jj	�r�tjd|dd�YnX||||�dS)Nr�z8Unknown child process pid %d, will report returncode 255z$process %s exited with returncode %sz'Child watcher got an unexpected pid: %rr�T)rcrs�waitpid�WNOHANG�ChildProcessErrorrr�r�r�r�r�r��popr@)rr�r^r�r_r:r;rrrr�ls,
	

	zSafeChildWatcher._do_waitpid)rzr{r|r}r$r�r�rQr�r�r�rr)r!rr@s	csveZdZdZ�fdd�Z�fdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�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).
    cs2t�j�tj�|_i|_d|_dS)Nr)rr�	threadingZLock�_lock�_zombies�_forks)r)r!rrr�s
	zFastChildWatcher.__init__cs+|jj�|jj�t�j�dS)N)r�r�r�rr$)r)r!rrr$�s

zFastChildWatcher.closec
Cs(|j�|jd7_|SWdQRXdS)Nr)r�r�)rrrrr��s
zFastChildWatcher.__enter__c
Csg|j�G|jd8_|js,|jr0dSt|j�}|jj�WdQRXtjd|�dS)Nrz5Caught subprocesses termination from unknown pids: %s)r�r�r�r4r�rr�)rr�r�r�Zcollateral_victimsrrrr��s
zFastChildWatcher.__exit__cGs�|jstd��|jdkr0td��|j�Gy|jj|�}Wn)tk
r{||f|j|<dSYnXWdQRX||||�dS)NzMust use the context managerzICannot add child handler, the child watcher does not have a loop attached)	r�rcr�r3r�r�r�r@r�)rr^r:r;r_rrrrQ�s	

z"FastChildWatcher.add_child_handlercCs/y|j|=dSWntk
r*dSYnXdS)NTF)r�r@)rr^rrrr��s


z%FastChildWatcher.remove_child_handlercCs9x2ytjdtj�\}}Wntk
r:dSYn X|dkrKdS|j|�}|j��y|jj|�\}}WnStk
r�|j	r�||j
|<|jj�r�t
jd||�wd}Yn#X|jj�r�t
jd||�WdQRX|dkr!t
jd||�q||||�qWdS)Nrrz,unknown process %s exited with returncode %sz$process %s exited with returncode %sz8Caught subprocess termination from unknown pid: %d -> %dr+)rsr�r�r�r�r�r�r�r@r�r�r�r�rr�r�)rr^r�r_r:r;rrrr��s6
	

	
	
	
z FastChildWatcher._do_waitpid_all)rzr{r|r}rr$r�r�rQr�r�rr)r!rr�s	csdeZdZdZeZ�fdd�Zdd�Z�fdd�Zdd	�Z	d
d�Z
�S)�_UnixDefaultEventLoopPolicyz:UNIX event loop policy with a watcher for child processes.cst�j�d|_dS)N)rr�_watcher)r)r!rrrs
z$_UnixDefaultEventLoopPolicy.__init__c
Cs^tj�N|jdkrSt�|_ttj�tj�rS|jj|j	j
�WdQRXdS)N)rr�r�rrDr��current_thread�_MainThreadr��_localr�)rrrr�
_init_watchers
z)_UnixDefaultEventLoopPolicy._init_watchercsKt�j|�|jdk	rGttj�tj�rG|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_loopr�rDr�r�r�r�)rr�)r!rrr�
sz*_UnixDefaultEventLoopPolicy.set_event_loopcCs |jdkr|j�|jS)zzGet the watcher for child processes.

        If not yet set, a SafeChildWatcher object is automatically created.
        N)r�r�)rrrrrOs
z-_UnixDefaultEventLoopPolicy.get_child_watchercCsJ|dks!t|t�s!t�|jdk	r=|jj�||_dS)z$Set the watcher for child processes.N)rDrrcr�r$)rrZrrr�set_child_watcher%s!
z-_UnixDefaultEventLoopPolicy.set_child_watcher)rzr{r|r}rZ
_loop_factoryrr�r�rOrrr)r!rr��s
r�)5r}r7rsr.r"rqr�r�r�r��rrrrrrr	r
rrr
�logr�__all__r��ImportErrorrZfspathrp�AttributeErrorZBaseSelectorEventLoopr�hasattrr�r�Z
ReadTransportrGZ_FlowControlMixinZWriteTransportrMr�r�ZBaseSubprocessTransportrPrr�rrZBaseDefaultEventLoopPolicyr�rrrrrr�<module>sh

���
F=On2