Your IP : 3.142.172.250


Current Path : /opt/alt/python310/lib64/python3.10/asyncio/__pycache__/
Upload File :
Current File : //opt/alt/python310/lib64/python3.10/asyncio/__pycache__/unix_events.cpython-310.opt-1.pyc

o

6��f���@s�dZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
mZddl
mZddl
mZddl
mZddl
mZdd	l
mZdd
l
mZddl
mZddl
mZdd
l
mZddlmZdZe
jdkred��dd�Zdd�ZGdd�dej�Z Gdd�dej!�Z"Gdd�dej#ej$�Z%Gdd�dej&�Z'Gdd�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+e(�Z.Gd,d-�d-ej/�Z0e Z1e0Z2dS).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�DefaultEventLoopPolicyZwin32z+Signals are not really supported on WindowscC�dS)zDummy signal handler.N�)�signum�framerr�:/opt/alt/python310/lib64/python3.10/asyncio/unix_events.py�_sighandler_noop*srcCs$zt�|�WSty|YSw�N)�os�waitstatus_to_exitcode�
ValueError)�statusrrrr/s
�rcs�eZdZdZd)�fdd�	Z�fdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
		d*dd�Z		d*dd�Z	d)dd�Z
dd�Z	d)ddddd�dd�Z	d)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|_dSr)�super�__init__�_signal_handlers)�self�selector��	__class__rrr#?�
z_UnixSelectorEventLoop.__init__cs`t���t��st|j�D]}|�|�qdS|jr.tjd|�d�t	|d�|j�
�dSdS)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,Cs
���z_UnixSelectorEventLoop.closecCs|D]
}|sq|�|�qdSr)�_handle_signal)r%�datarrrr�_process_self_dataQs
�z)_UnixSelectorEventLoop._process_self_datac
Gs.t�|�s
t�|�rtd��|�|�|��z
t�|j�	��Wnt
tfy5}ztt
|���d}~wwt�|||d�}||j|<zt�|t�t�|d�WdSty�}z8|j|=|js�zt�d�Wnt
tfy�}zt�d|�WYd}~nd}~ww|jtjkr�td|�d����d}~ww)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)rZiscoroutineZiscoroutinefunction�	TypeError�
_check_signalZ
_check_closed�signal�
set_wakeup_fdZ_csock�filenor�OSError�RuntimeError�strrZHandler$r�siginterruptr
�info�errno�EINVAL)r%r6�callback�args�exc�handleZnexcrrr�add_signal_handlerXs>
�
��
����z)_UnixSelectorEventLoop.add_signal_handlercCs:|j�|�}|durdS|jr|�|�dS|�|�dS)z2Internal helper that is the actual signal handler.N)r$�getZ
_cancelledr0Z_add_callback_signalsafe)r%r6rMrrrr7�sz%_UnixSelectorEventLoop._handle_signalc
Cs�|�|�z|j|=Wn
tyYdSw|tjkrtj}ntj}zt�||�WntyE}z|jtj	kr@t
d|�d����d}~ww|jsmzt�d�WdSttfyl}z
t
�d|�WYd}~dSd}~wwdS)zwRemove a handler for a signal.  UNIX only.

        Return True if a signal handler was removed, False if not.
        Fr<r=Nr:r;T)r?r$�KeyErrorr@�SIGINT�default_int_handler�SIG_DFLrCrHrIrDrArr
rG)r%r6�handlerrLrrrr0�s6
�
�����z,_UnixSelectorEventLoop.remove_signal_handlercCs6t|t�std|����|t��vrtd|����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�intr>r@�
valid_signalsrr5rrrr?�s

�z$_UnixSelectorEventLoop._check_signalcC�t|||||�Sr)�_UnixReadPipeTransport�r%�pipe�protocol�waiter�extrarrr�_make_read_pipe_transport��z0_UnixSelectorEventLoop._make_read_pipe_transportcCrXr)�_UnixWritePipeTransportrZrrr�_make_write_pipe_transport�r`z1_UnixSelectorEventLoop._make_write_pipe_transportc	

�s��t���U}
|
��std��|��}t||||||||f||d�|	��}|
�|��|j|�z|IdHWnt	t
fy?�tyQ|��|�
�IdH�wWd�|S1s]wY|S)NzRasyncio.get_child_watcher() is not activated, subprocess support is not installed.)r]r^)r�get_child_watcher�	is_activerD�
create_future�_UnixSubprocessTransport�add_child_handlerZget_pid�_child_watcher_callback�
SystemExit�KeyboardInterrupt�
BaseExceptionr,Z_wait)
r%r\rK�shell�stdin�stdout�stderr�bufsizer^�kwargs�watcherr]�transprrr�_make_subprocess_transport�s:�

���
���
��z1_UnixSelectorEventLoop._make_subprocess_transportcCs|�|j|j|�dSr)�call_soon_threadsafe�	call_soonZ_process_exited)r%�pid�
returncodersrrrrh�sz._UnixSelectorEventLoop._child_watcher_callback)�ssl�sock�server_hostname�ssl_handshake_timeoutc	�s�|r|durtd��n|durtd��|durtd��|durO|dur(td��t�|�}t�tjtjd�}z|�d�|�||�IdHWn(|���|durWtd��|j	tjksc|j
tjkrjtd|����|�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 )r|)rr�fspath�socket�AF_UNIX�SOCK_STREAM�setblockingZsock_connectr,�family�typeZ_create_connection_transport)	r%�protocol_factory�pathryrzr{r|�	transportr\rrr�create_unix_connection�sN�����

�
�z-_UnixSelectorEventLoop.create_unix_connection�dT)rz�backlogryr|�
start_servingc
�s��t|t�r
td��|dur|std��|dur�|dur td��t�|�}t�tjtj�}|ddvrezt	�
t�	|�j�rBt�|�Wn!t
yLYntyd}z
t�d||�WYd}~nd}~wwz|�|�WnFty�}	z|��|	jtjkr�d|�d�}
ttj|
�d��d}	~	w|���|dur�td	��|jtjks�|jtjkr�td
|����|�d�t�||g||||�}|r�|��t�d�IdH|S)Nz*ssl argument must be an SSLContext or Noner}r~r)r�z2Unable to check or remove stale UNIX socket %r: %rzAddress z is already in usez-path was not specified, and no sock specifiedrF)rU�boolr>rrr�r�r�r��stat�S_ISSOCK�st_mode�remove�FileNotFoundErrorrCr
�errorZbindr,rHZ
EADDRINUSEr�r�r�rZServerZ_start_servingr�sleep)r%r�r�rzr�ryr|r��errrL�msgZserverrrr�create_unix_serversp�
��

�������
�z)_UnixSelectorEventLoop.create_unix_serverc

�s��ztjWntyt�d��wz|��}Wnttjfy-}zt�d��d}~wwzt�|�j	}Wnt
yBt�d��w|rG|n|}|sMdS|��}	|�|	d|||||d�|	IdHS)Nzos.sendfile() is not availableznot a regular filer)
r�sendfile�AttributeErrorr�SendfileNotAvailableErrorrB�io�UnsupportedOperation�fstat�st_sizerCre�_sock_sendfile_native_impl)
r%rz�file�offset�countrBr�Zfsize�	blocksize�futrrr�_sock_sendfile_nativeUs6�
��
��
��
z,_UnixSelectorEventLoop._sock_sendfile_nativec	Cs*|��}	|dur
|�|�|��r|�|||�dS|r2||}|dkr2|�|||�|�|�dSz
t�|	|||�}
Wn�ttfya|durO|�	||�|�
|	|j||	||||||�
YdSty�}zK|dur�|j
t
jkr�t|�tur�tdt
j�}||_|}|dkr�t�d�}
|�|||�|�|
�n|�|||�|�|�WYd}~dSWYd}~dSd}~wttfy��ty�}z|�|||�|�|�WYd}~dSd}~ww|
dkr�|�|||�|�|�dS||
7}||
7}|du�r|�	||�|�
|	|j||	||||||�
dS)Nrzsocket is not connectedzos.sendfile call failed)rB�
remove_writer�	cancelled�_sock_sendfile_update_fileposZ
set_resultrr��BlockingIOError�InterruptedError�_sock_add_cancellation_callbackZ
add_writerr�rCrHZENOTCONNr��ConnectionError�	__cause__rr�Z
set_exceptionrirjrk)r%r�Z
registered_fdrzrBr�r�r��
total_sent�fdZsentrL�new_excr�rrrr�lst


�������
�z1_UnixSelectorEventLoop._sock_sendfile_native_implcCs |dkrt�||tj�dSdS�Nr)r�lseek�SEEK_SET)r%rBr�r�rrrr��s�z4_UnixSelectorEventLoop._sock_sendfile_update_fileposcs��fdd�}|�|�dS)Ncs.|��r���}|dkr��|�dSdSdS)Nr:)r�rBr�)r�r��r%rzrr�cb�s�zB_UnixSelectorEventLoop._sock_add_cancellation_callback.<locals>.cb)Zadd_done_callback)r%r�rzr�rr�rr��sz6_UnixSelectorEventLoop._sock_add_cancellation_callbackr�NN)�__name__�
__module__�__qualname__�__doc__r#r,r9rNr7r0r?r_rbrtrhr�r�r�r�r�r��
__classcell__rrr'rr!9sD-
 
�
�
���.��CFr!cs�eZdZdZd�fdd�	Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Ze
jfdd�Zddd�Zdd�Zdd�Z�ZS) rYiNcs�t��|�||jd<||_||_|��|_||_d|_d|_	t
�|j�j}t
�|�sBt
�|�sBt
�|�sBd|_d|_d|_td��t
�|jd�|j�|jj|�|j�|jj|j|j�|durm|j�tj|d�dSdS)Nr[Fz)Pipe transport is for pipes/sockets only.)r"r#�_extra�_loop�_piperB�_fileno�	_protocol�_closing�_pausedrr�r�r��S_ISFIFOr��S_ISCHRr�set_blockingrv�connection_made�_add_reader�_read_readyr	�_set_result_unless_cancelled)r%�loopr[r\r]r^�moder'rrr#�s8


���
��z_UnixReadPipeTransport.__init__cCs�|jjg}|jdur|�d�n|jr|�d�|�d|j���t|jdd�}|jdurH|durHt�	||jt
j�}|rB|�d�n|�d�n|jdurS|�d�n|�d�d�d	�
|��S)
N�closed�closing�fd=�	_selector�polling�idle�open�<{}>� )r(r�r��appendr�r��getattrr�r
�_test_selector_event�	selectorsZ
EVENT_READ�format�join)r%rGr&r�rrr�__repr__�s$



�

z_UnixReadPipeTransport.__repr__c
Cs�z
t�|j|j�}Wn$ttfyYdSty.}z
|�|d�WYd}~dSd}~ww|r9|j�	|�dS|j
��rDt�
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�rC�_fatal_errorr�Z
data_receivedr��	get_debugr
rGr��_remove_readerrvZeof_received�_call_connection_lost)r%r8rLrrrr��s ��
z"_UnixReadPipeTransport._read_readycCsB|js|jrdSd|_|j�|j�|j��rt�d|�dSdS)NTz%r pauses reading)r�r�r�r�r�r�r
�debug�r%rrr�
pause_readings
�z$_UnixReadPipeTransport.pause_readingcCsF|js|jsdSd|_|j�|j|j�|j��r!t�d|�dSdS)NFz%r resumes reading)	r�r�r�r�r�r�r�r
r�r�rrr�resume_readings
�z%_UnixReadPipeTransport.resume_readingcC�
||_dSr�r��r%r\rrr�set_protocol�
z#_UnixReadPipeTransport.set_protocolcC�|jSrr�r�rrr�get_protocol�z#_UnixReadPipeTransport.get_protocolcCr�r�r�r�rrr�
is_closingr�z!_UnixReadPipeTransport.is_closingcCs|js
|�d�dSdSr)r��_closer�rrrr,!s�z_UnixReadPipeTransport.closecC�0|jdur|d|��t|d�|j��dSdS�Nzunclosed transport r*�r�r3r,�r%�_warnrrr�__del__%�
�z_UnixReadPipeTransport.__del__�Fatal error on pipe transportcCsZt|t�r|jtjkr|j��rtjd||dd�n|j�||||j	d��|�
|�dS�Nz%r: %sT��exc_info)�message�	exceptionr�r\)rUrCrHZEIOr�r�r
r��call_exception_handlerr�r��r%rLr�rrrr�*s
��z#_UnixReadPipeTransport._fatal_errorcCs(d|_|j�|j�|j�|j|�dS�NT)r�r�r�r�rvr��r%rLrrrr�8sz_UnixReadPipeTransport._closecC�Nz|j�|�W|j��d|_d|_d|_dS|j��d|_d|_d|_wr�r�Zconnection_lostr�r,r�rrrrr�=�


�z,_UnixReadPipeTransport._call_connection_lostr��r�)r�r�r�r�r#r�r�r�r�r�r�r�r,r1r2r�r�r�r�r�rrr'rrY�s
rYcs�eZdZd%�fdd�	Zdd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zejfdd�Zdd�Zd&dd �Zd'd!d"�Zd#d$�Z�ZS)(raNc
st��||�||jd<||_|��|_||_t�|_d|_	d|_
t�|j�j
}t�|�}t�|�}t�|�}	|sJ|sJ|	sJd|_d|_d|_td��t�|jd�|j�|jj|�|	sd|rptj�d�sp|j�|jj|j|j�|dur|j�tj|d�dSdS)Nr[rFz?Pipe transport is only for pipes, sockets and character devices�aix)r"r#r�r�rBr�r��	bytearray�_buffer�
_conn_lostr�rr�r�r�r�r�r�rr�r�rvr�r-�platform�
startswithr�r�r	r�)
r%r�r[r\r]r^r�Zis_charZis_fifoZ	is_socketr'rrr#Js8




�
��z _UnixWritePipeTransport.__init__cCs�|jjg}|jdur|�d�n|jr|�d�|�d|j���t|jdd�}|jdurT|durTt�	||jt
j�}|rB|�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�ZEVENT_WRITE�get_write_buffer_sizer�r�)r%rGr&r�rprrrr�os(



�


z _UnixWritePipeTransport.__repr__cCs
t|j�Sr)�lenrr�rrrr�r�z-_UnixWritePipeTransport.get_write_buffer_sizecCs8|j��rt�d|�|jr|�t��dS|��dS)Nr�)r�r�r
rGrr��BrokenPipeErrorr�rrrr��s

z#_UnixWritePipeTransport._read_readyc
Cs&t|t�r	t|�}|s
dS|js|jr'|jtjkrt�d�|jd7_dS|j	s�z	t
�|j|�}Wn5t
tfy@d}Yn)ttfyI�tyh}z|jd7_|�|d�WYd}~dSd}~ww|t|�krqdS|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)rUr
�
memoryviewrr�rZ!LOG_THRESHOLD_FOR_CONNLOST_WRITESr
�warningrr�writer�r�r�rirjrkr�rr�Z_add_writer�_write_readyZ_maybe_pause_protocol)r%r8�nrLrrrr�s:

��z_UnixWritePipeTransport.writec
Csz
t�|j|j�}Wn@ttfyYdSttfy�tyJ}z |j�	�|j
d7_
|j�|j�|�
|d�WYd}~dSd}~ww|t|j�krs|j�	�|j�|j�|��|jrq|j�|j�|�d�dS|dkr|jd|�=dSdS)Nrrr)rrr�rr�r�rirjrkr4rr��_remove_writerr�rZ_maybe_resume_protocolr�r�r�)r%rrLrrrr�s0
��

�z$_UnixWritePipeTransport._write_readycCrrrr�rrr�
can_write_eof��z%_UnixWritePipeTransport.can_write_eofcCs<|jrdSd|_|js|j�|j�|j�|jd�dSdSr)r�rr�r�r�rvr�r�rrr�	write_eof�s�z!_UnixWritePipeTransport.write_eofcCr�rr�r�rrrr��r�z$_UnixWritePipeTransport.set_protocolcCr�rr�r�rrrr��r�z$_UnixWritePipeTransport.get_protocolcCr�rr�r�rrrr��r�z"_UnixWritePipeTransport.is_closingcCs$|jdur|js|��dSdSdSr)r�r�rr�rrrr,�s�z_UnixWritePipeTransport.closecCr�r�r�r�rrrr��r�z_UnixWritePipeTransport.__del__cC�|�d�dSr)r�r�rrr�abort��z_UnixWritePipeTransport.abortr�cCsNt|t�r|j��rtjd||dd�n|j�||||jd��|�|�dSr�)	rUrCr�r�r
r�rr�r�rrrrr��s

��z$_UnixWritePipeTransport._fatal_errorcCsFd|_|jr
|j�|j�|j��|j�|j�|j�|j|�dSr)	r�rr�rr�r4r�rvr�rrrrr�s
z_UnixWritePipeTransport._closecCrrrrrrrr�
rz-_UnixWritePipeTransport._call_connection_lostr�rr)r�r�r�r#r�rr�rrrrr�r�r�r,r1r2r�rr�r�r�r�rrr'rraGs$%	#	

rac@seZdZdd�ZdS)rfc		Ks�d}|tjkrtj�d�rt��\}}z7tj|f||||d|d�|��|_|dur:|�	�t
|��d|d�|j_d}W|durI|�	�|�	�dSdS|durX|�	�|�	�ww)Nr	F)rlrmrnroZuniversal_newlinesrp�wb)�	buffering)
�
subprocess�PIPEr-r
rr�Z
socketpair�Popen�_procr,r��detachrm)	r%rKrlrmrnrorprqZstdin_wrrr�_starts0�����
�z_UnixSubprocessTransport._startN)r�r�r�r&rrrrrfsrfc@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.
    cG�t��)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.
        ��NotImplementedError�r%rwrJrKrrrrgCs	z&AbstractChildWatcher.add_child_handlercCr')z�Removes the handler for process 'pid'.

        The function returns True if the handler was successfully removed,
        False if there was nothing to remove.r(�r%rwrrr�remove_child_handlerNsz)AbstractChildWatcher.remove_child_handlercCr')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.
        r(�r%r�rrr�attach_loopVsz AbstractChildWatcher.attach_loopcCr')zlClose the watcher.

        This must be called to make sure that any underlying resource is freed.
        r(r�rrrr,`szAbstractChildWatcher.closecCr')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.

        r(r�rrrrdgszAbstractChildWatcher.is_activecCr')zdEnter the watcher's context and allow starting new processes

        This function must return selfr(r�rrr�	__enter__pszAbstractChildWatcher.__enter__cCr')zExit the watcher's contextr(�r%�a�b�crrr�__exit__vszAbstractChildWatcher.__exit__N)r�r�r�r�rgr,r.r,rdr/r4rrrrr,s
	rc@sXeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�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.
    cC�d|_i|_dSr�r��
_callbacksr�rrrr#��
zPidfdChildWatcher.__init__cC�|Srrr�rrrr/�rzPidfdChildWatcher.__enter__cC�dSrr)r%�exc_type�	exc_value�
exc_tracebackrrrr4�rzPidfdChildWatcher.__exit__cC�|jduo	|j��Sr�r�Z
is_runningr�rrrrd��zPidfdChildWatcher.is_activecCrr�r.r�rrrr,�rzPidfdChildWatcher.closecCsd|jdur|dur|jrt�dt�|j��D]\}}}|j�|�t�|�q|j�	�||_dS�NzCA loop is being detached from a child watcher with pending handlers)
r�r7r1r2�RuntimeWarning�valuesr�rr,r4)r%r��pidfd�_rrrr.�s�

zPidfdChildWatcher.attach_loopcGs\|j�|�}|dur|d||f|j|<dSt�|�}|j�||j|�|||f|j|<dSr�)r7rOrZ
pidfd_openr�r��_do_wait)r%rwrJrKZexistingrErrrrg�s
z#PidfdChildWatcher.add_child_handlercCs~|j�|�\}}}|j�|�z
t�|d�\}}Wnty*d}t�d|�Ynwt	|�}t�
|�|||g|�R�dS)Nr�zJchild process pid %d exit status already read:  will report returncode 255)r7�popr�r�r�waitpid�ChildProcessErrorr
rrr,)r%rwrErJrKrFr rxrrrrG�s��	
zPidfdChildWatcher._do_waitcCsFz|j�|�\}}}Wn
tyYdSw|j�|�t�|�dS)NFT)r7rIrPr�r�rr,)r%rwrErFrrrr,�s�
z&PidfdChildWatcher.remove_child_handlerN)
r�r�r�r�r#r/r4rdr,r.rgrGr,rrrrr{s	rc@sDeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dS)�BaseChildWatchercCr5rr6r�rrrr#�r8zBaseChildWatcher.__init__cCrrrAr�rrrr,�rzBaseChildWatcher.closecCr>rr?r�rrrrd�r@zBaseChildWatcher.is_activecCr'rr()r%�expected_pidrrr�_do_waitpid�r�zBaseChildWatcher._do_waitpidcCr'rr(r�rrr�_do_waitpid_all�r�z BaseChildWatcher._do_waitpid_allcCsj|jdur|dur|jrt�dt�|jdur|j�tj�||_|dur3|�tj|j	�|�
�dSdSrB)r�r7r1r2rCr0r@�SIGCHLDrN�	_sig_chldrOr-rrrr.�s�
�zBaseChildWatcher.attach_loopc
CsZz|��WdSttfy�ty,}z|j�d|d��WYd}~dSd}~ww)N�$Unknown exception in SIGCHLD handler)r�r)rOrirjrkr�rrrrrrQ�s���zBaseChildWatcher._sig_chldN)
r�r�r�r#r,rdrNrOr.rQrrrrrL�srLcsPeZdZdZ�fdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
�ZS)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)r7r4r"r,r�r'rrr,	s
zSafeChildWatcher.closecCr9rrr�rrrr/
rzSafeChildWatcher.__enter__cCr:rrr0rrrr4rzSafeChildWatcher.__exit__cGs||f|j|<|�|�dSr)r7rNr*rrrrgsz"SafeChildWatcher.add_child_handlercC�$z|j|=WdStyYdSw�NTF�r7rPr+rrrr,��z%SafeChildWatcher.remove_child_handlercC�t|j�D]}|�|�qdSr�r/r7rNr+rrrrO s�z SafeChildWatcher._do_waitpid_allcCs�zt�|tj�\}}Wnty|}d}t�d|�Ynw|dkr%dSt|�}|j��r5t�	d||�z
|j
�|�\}}WntyY|j��rVtjd|dd�YdSYdSw|||g|�R�dS)NrH�8Unknown child process pid %d, will report returncode 255r�$process %s exited with returncode %s�'Child watcher got an unexpected pid: %rTr�)
rrJ�WNOHANGrKr
rrr�r�r�r7rIrP)r%rMrwr rxrJrKrrrrN%s6��	
�
���zSafeChildWatcher._do_waitpid)r�r�r�r�r,r/r4rgr,rOrNr�rrr'rr�s
rcsTeZdZdZ�fdd�Z�fdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
�ZS)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�)r"r#�	threadingZLock�_lock�_zombies�_forksr�r'rrr#Rs


zFastChildWatcher.__init__cs"|j��|j��t���dSr)r7r4r_r"r,r�r'rrr,Xs

zFastChildWatcher.closecCs<|j�|jd7_|Wd�S1swYdS)Nr)r^r`r�rrrr/]s$�zFastChildWatcher.__enter__cCsv|j�(|jd8_|js|js	Wd�dSt|j�}|j��Wd�n1s.wYt�d|�dS)Nrz5Caught subprocesses termination from unknown pids: %s)r^r`r_rEr4r
r)r%r1r2r3Zcollateral_victimsrrrr4cs�
�	�zFastChildWatcher.__exit__c	Gs||j�(z|j�|�}Wnty#||f|j|<YWd�dSwWd�n1s.wY|||g|�R�dSr)r^r_rIrPr7)r%rwrJrKrxrrrrgqs���	z"FastChildWatcher.add_child_handlercCrSrTrUr+rrrr,rVz%FastChildWatcher.remove_child_handlerc	Cs	zt�dtj�\}}Wn
tyYdSw|dkrdSt|�}|j�Fz
|j�|�\}}Wn'tyV|j	rR||j
|<|j��rJt
�d||�YWd�qd}Yn
w|j��rct
�d||�Wd�n1smwY|dur~t
�d||�n	|||g|�R�q)NTr:rz,unknown process %s exited with returncode %srZz8Caught subprocess termination from unknown pid: %d -> %d)rrJr\rKrr^r7rIrPr`r_r�r�r
r�r)r%rwr rxrJrKrrrrO�sH�

��
�
�����z FastChildWatcher._do_waitpid_all)r�r�r�r�r#r,r/r4rgr,rOr�rrr'rrHs	rc@sheZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�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)r7�_saved_sighandlerr�rrrr#�r8zMultiLoopChildWatcher.__init__cCs
|jduSr)rar�rrrrd�r�zMultiLoopChildWatcher.is_activecCsT|j��|jdurdSt�tj�}||jkrt�d�nt�tj|j�d|_dS)Nz+SIGCHLD handler was changed by outside code)	r7r4rar@�	getsignalrPrQr
r)r%rTrrrr,�s



zMultiLoopChildWatcher.closecCr9rrr�rrrr/�rzMultiLoopChildWatcher.__enter__cCr:rr�r%r;Zexc_valZexc_tbrrrr4�rzMultiLoopChildWatcher.__exit__cGs&t��}|||f|j|<|�|�dSr)r�get_running_loopr7rN)r%rwrJrKr�rrrrg�sz'MultiLoopChildWatcher.add_child_handlercCrSrTrUr+rrrr,�rVz*MultiLoopChildWatcher.remove_child_handlercCsN|jdurdSt�tj|j�|_|jdurt�d�tj|_t�tjd�dS)NzaPrevious SIGCHLD handler was set by non-Python code, restore to default handler on watcher close.F)rar@rPrQr
rrSrFr-rrrr.�s


z!MultiLoopChildWatcher.attach_loopcCrWrrXr+rrrrO�s�z%MultiLoopChildWatcher._do_waitpid_allc	Cs�zt�|tj�\}}Wnty |}d}t�d|�d}Yn
w|dkr'dSt|�}d}z|j�|�\}}}Wnt	yJtjd|dd�YdSw|�
�rXt�d||�dS|re|��ret�d	||�|j
|||g|�R�dS)
NrHrYFrTr[r��%Loop %r that handles pid %r is closedrZ)rrJr\rKr
rrr7rIrP�	is_closedr�r�ru)	r%rMrwr rxZ	debug_logr�rJrKrrrrNs:��
���z!MultiLoopChildWatcher._do_waitpidc	CsDz|��WdSttfy�ty!tjddd�YdSw)NrRTr�)rOrirjrkr
r)r%rrrrrrQ%s�zMultiLoopChildWatcher._sig_chldN)r�r�r�r�r#rdr,r/r4rgr,r.rOrNrQrrrrr�s%rc@sneZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	e
jfdd�Zdd�Z
dd�Zdd�Zdd�ZdS)raAThreaded child watcher implementation.

    The watcher uses a thread per process
    for waiting for the process finish.

    It doesn't require subscription on POSIX signal
    but a thread creation is not free.

    The watcher has O(1) complexity, its performance doesn't depend
    on amount of spawn processes.
    cCst�d�|_i|_dSr�)�	itertoolsr��_pid_counter�_threadsr�rrrr#;r)zThreadedChildWatcher.__init__cCrrrr�rrrrd?rzThreadedChildWatcher.is_activecCs|��dSr)�
_join_threadsr�rrrr,BszThreadedChildWatcher.closecCs.dd�t|j���D�}|D]}|��qdS)z%Internal: Join all non-daemon threadscSsg|]}|��r|js|�qSr)�is_alive�daemon��.0�threadrrr�
<listcomp>Gs
�
�z6ThreadedChildWatcher._join_threads.<locals>.<listcomp>N)r/rirDr�)r%�threadsrorrrrjEs
�z"ThreadedChildWatcher._join_threadscCr9rrr�rrrr/LrzThreadedChildWatcher.__enter__cCr:rrrcrrrr4OrzThreadedChildWatcher.__exit__cCs:dd�t|j���D�}|r||j�d�t|d�dSdS)NcSsg|]}|��r|�qSr)rkrmrrrrpSs
�z0ThreadedChildWatcher.__del__.<locals>.<listcomp>z0 has registered but not finished child processesr*)r/rirDr(r3)r%r�rqrrrr�Rs
��zThreadedChildWatcher.__del__cGsFt��}tj|jdt|j���||||fdd�}||j|<|��dS)Nzwaitpid-T)�target�namerKrl)	rrdr]ZThreadrN�nextrhri�start)r%rwrJrKr�rorrrrgZs
�
z&ThreadedChildWatcher.add_child_handlercCrrrr+rrrr,csz)ThreadedChildWatcher.remove_child_handlercCr:rrr-rrrr.irz ThreadedChildWatcher.attach_loopcCs�z
t�|d�\}}Wnty|}d}t�d|�Ynwt|�}|��r-t�d||�|��r9t�d||�n|j	|||g|�R�|j
�|�dS)NrrHrYrZre)rrJrKr
rrr�r�rfrurirI)r%r�rMrJrKrwr rxrrrrNls&��	�z ThreadedChildWatcher._do_waitpidN)r�r�r�r�r#rdr,rjr/r4r1r2r�rgr,r.rNrrrrr.s	rcsHeZdZdZeZ�fdd�Zdd�Z�fdd�Zdd	�Z	d
d�Z
�ZS)�_UnixDefaultEventLoopPolicyz:UNIX event loop policy with a watcher for child processes.cst���d|_dSr)r"r#�_watcherr�r'rrr#�s

z$_UnixDefaultEventLoopPolicy.__init__cCs~tj�2|jdur%t�|_t��t��ur-|j�|jj	�Wd�dSWd�dSWd�dS1s8wYdSr)
rr^rwrr]�current_thread�main_threadr.�_localr�r�rrr�
_init_watcher�s
��"�z)_UnixDefaultEventLoopPolicy._init_watchercs>t��|�|jdurt��t��ur|j�|�dSdSdS)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_looprwr]rxryr.r-r'rrr|�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)rwr{r�rrrrc�s
z-_UnixDefaultEventLoopPolicy.get_child_watchercCs|jdur
|j��||_dS)z$Set the watcher for child processes.N)rwr,)r%rrrrr�set_child_watcher�s


z-_UnixDefaultEventLoopPolicy.set_child_watcher)r�r�r�r�r!Z
_loop_factoryr#r{r|rcr}r�rrr'rrv�s
rv)3r�rHr�rgrr�r@r�r�r!r-r]r1�rrrrrrr	r
rr�logr
�__all__r
�ImportErrorrrZBaseSelectorEventLoopr!Z
ReadTransportrYZ_FlowControlMixinZWriteTransportraZBaseSubprocessTransportrfrrrLrrrrZBaseDefaultEventLoopPolicyrvrrrrrr�<module>sd
	
		�NON5Ji}Y2

?>