Server : LiteSpeed
System : Linux server51.dnsbootclub.com 4.18.0-553.62.1.lve.el8.x86_64 #1 SMP Mon Jul 21 17:50:35 UTC 2025 x86_64
User : nandedex ( 1060)
PHP Version : 8.1.33
Disable Function : NONE
Directory :  /opt/cppython/lib/python3.8/asyncio/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]


Current File : //opt/cppython/lib/python3.8/asyncio/__pycache__/unix_events.cpython-38.opt-1.pyc
U

?��gۿ�@s�dZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
mZddl
mZddl
mZddl
mZddl
mZdd	l
mZdd
l
mZddl
mZddl
mZdd
l
mZddlmZdZe
jdkr�ed��dd�ZGdd�dej�ZGdd�dej �Z!Gdd�dej"ej#�Z$Gdd�dej%�Z&Gdd�d�Z'dd�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.�Z/eZ0e/Z1dS),z2Selector event loop for Unix with signal handling.�N�)�base_events)�base_subprocess)�	constants)�
coroutines)�events)�
exceptions)�futures)�selector_events)�tasks)�
transports)�logger)�SelectorEventLoop�AbstractChildWatcher�SafeChildWatcher�FastChildWatcher�MultiLoopChildWatcher�ThreadedChildWatcher�DefaultEventLoopPolicy�win32z+Signals are not really supported on WindowscCsdS)zDummy signal handler.N�)�signum�framerr�2/opt/cppython/lib/python3.8/asyncio/unix_events.py�_sighandler_noop*srcs�eZdZdZd)�fdd�	Z�fdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
d*dd�Zd+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|�d�t	|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
GsLt�|�st�|�rtd��|�|�|��zt�|j�	��Wn2t
tfk
rt}ztt
|���W5d}~XYnXt�|||d�}||j|<zt�|t�t�|d�Wn�tk
�rF}zz|j|=|j�szt�d�Wn4t
tfk
�r}zt�d|�W5d}~XYnX|jtjk�r4td|�d���n�W5d}~XYnXdS)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_signal�
_check_closed�signal�
set_wakeup_fdZ_csock�fileno�
ValueError�OSError�RuntimeError�strr�Handlerr�siginterruptr
�info�errno�EINVAL)r r0�callback�args�exc�handleZnexcrrr�add_signal_handlerNs2
�

z)_UnixSelectorEventLoop.add_signal_handlercCs8|j�|�}|dkrdS|jr*|�|�n
|�|�dS)z2Internal helper that is the actual signal handler.N)r�getZ
_cancelledr*Z_add_callback_signalsafe)r r0rJrrrr1{sz%_UnixSelectorEventLoop._handle_signalc
Cs�|�|�z|j|=Wntk
r,YdSX|tjkr@tj}ntj}zt�||�WnBtk
r�}z$|jtj	kr�t
d|�d���n�W5d}~XYnX|js�zt�d�Wn2ttfk
r�}zt
�d|�W5d}~XYnXdS)zwRemove a handler for a signal.  UNIX only.

        Return True if a signal handler was removed, False if not.
        Fr6r7Nr4r5T)r9r�KeyErrorr;�SIGINT�default_int_handler�SIG_DFLr?rErFr@r<r>r
rD)r r0�handlerrIrrrr*�s(

z,_UnixSelectorEventLoop.remove_signal_handlercCs6t|t�std|����|t��kr2td|����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�intr8r;�
valid_signalsr>r/rrrr9�s
z$_UnixSelectorEventLoop._check_signalcCst|||||�Sr)�_UnixReadPipeTransport�r �pipe�protocol�waiter�extrarrr�_make_read_pipe_transport�sz0_UnixSelectorEventLoop._make_read_pipe_transportcCst|||||�Sr)�_UnixWritePipeTransportrVrrr�_make_write_pipe_transport�sz1_UnixSelectorEventLoop._make_write_pipe_transportc	

�s�t����}
|
��std��|��}t||||||||f||d�|	��}|
�|��|j|�z|IdHWnDt	t
fk
r��Yn,tk
r�|��|�
�IdH�YnXW5QRX|S)NzRasyncio.get_child_watcher() is not activated, subprocess support is not installed.)rYrZ)r�get_child_watcher�	is_activer@�
create_future�_UnixSubprocessTransport�add_child_handlerZget_pid�_child_watcher_callback�
SystemExit�KeyboardInterrupt�
BaseExceptionr&�_wait)
r rXrH�shell�stdin�stdout�stderr�bufsizerZ�kwargs�watcherrY�transprrr�_make_subprocess_transport�s8

���
�z1_UnixSelectorEventLoop._make_subprocess_transportcCs|�|j|�dSr)�call_soon_threadsafeZ_process_exited)r �pid�
returncoderorrrrc�sz._UnixSelectorEventLoop._child_watcher_callback)�ssl�sock�server_hostname�ssl_handshake_timeoutc	�s
|r|dkr6td��n |dk	r&td��|dk	r6td��|dk	r�|dk	rNtd��t�|�}t�tjtjd�}z |�d�|�||�IdHWq�|���Yq�Xn@|dkr�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 )rw)r>�os�fspath�socket�AF_UNIX�SOCK_STREAM�setblockingZsock_connectr&�family�typeZ_create_connection_transport)	r �protocol_factory�pathrtrurvrw�	transportrXrrr�create_unix_connection�sR���



��
�z-_UnixSelectorEventLoop.create_unix_connection�dT)ru�backlogrtrw�
start_servingc
�s�t|t�rtd��|dk	r&|s&td��|dk	�rH|dk	r@td��t�|�}t�tjtj�}|ddkr�z t	�
t�	|�j�r�t�|�WnBt
k
r�Yn0tk
r�}zt�d||�W5d}~XYnXz|�|�Wnltk
�r0}	z8|��|	jtjk�rd|�d�}
ttj|
�d�n�W5d}	~	XYn|���YnXn<|dk�rZtd	��|jtjk�sv|jtjk�r�td
|����|�d�t�||g||||�}|�r�|��tjd|d�IdH|S)
Nz*ssl argument must be an SSLContext or Nonerxryr)r�z2Unable to check or remove stale UNIX socket %r: %rzAddress z is already in usez-path was not specified, and no sock specifiedrzF)�loop)rR�boolr8r>r{r|r}r~r�stat�S_ISSOCK�st_mode�remove�FileNotFoundErrorr?r
�error�bindr&rEZ
EADDRINUSEr�r�r�rZServerZ_start_servingr�sleep)r r�r�rur�rtrwr��errrI�msgZserverrrr�create_unix_serversn
�
�
�

�
��
�z)_UnixSelectorEventLoop.create_unix_serverc
�s�z
tjWn,tk
r6}zt�d��W5d}~XYnXz|��}Wn2ttjfk
rv}zt�d��W5d}~XYnXzt�|�j	}Wn,t
k
r�}zt�d��W5d}~XYnX|r�|n|}	|	s�dS|��}
|�|
d|||||	d�|
IdHS)Nzos.sendfile() is not availableznot a regular filer)
r{�sendfile�AttributeErrorr�SendfileNotAvailableErrorr=�io�UnsupportedOperation�fstat�st_sizer?r`�_sock_sendfile_native_impl)r ru�file�offset�countrIr=r��fsize�	blocksize�futrrr�_sock_sendfile_nativeJs2
��z,_UnixSelectorEventLoop._sock_sendfile_nativec	Cs,|��}	|dk	r|�|�|��r4|�|||�dS|rd||}|dkrd|�|||�|�|�dSzt�|	|||�}
W�nDttfk
r�|dkr�|�	||�|�
|	|j||	||||||�
Y�nbtk
�rj}z�|dk	�r|j
t
jk�rt|�tk	�rtdt
j�}||_|}|dk�rBt�d�}
|�|||�|�|
�n|�|||�|�|�W5d}~XYn�ttfk
�r��Yn�tk
�r�}z|�|||�|�|�W5d}~XYnjX|
dk�r�|�|||�|�|�nD||
7}||
7}|dk�r
|�	||�|�
|	|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?rEZENOTCONNr��ConnectionError�	__cause__rr��
set_exceptionrdrerf)r r�Z
registered_fdrur=r�r�r��
total_sent�fd�sentrI�new_excr�rrrr�as�

�


�
��
�

�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)Nr4)r�r=r�)r�r��r rurr�cb�szB_UnixSelectorEventLoop._sock_add_cancellation_callback.<locals>.cb)�add_done_callback)r r�rur�rr�rr��sz6_UnixSelectorEventLoop._sock_add_cancellation_callback)N)NN)NN)N)N)N)�__name__�
__module__�__qualname__�__doc__rr&r3rKr1r*r9r[r]rprcr�r�r�r�r�r��
__classcell__rrr"rr/sH-
 �
�
�
��.��CFrcs�eZdZdZd�fdd�	Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Ze
jfdd�Zddd�Zdd�Zdd�Z�ZS) rUiNcs�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�|dk	r�|j�tj|d�dS)NrWFz)Pipe transport is for pipes/sockets only.)rr�_extra�_loop�_piper=�_fileno�	_protocol�_closing�_pausedr{r�r�r��S_ISFIFOr��S_ISCHRr>�set_blocking�	call_soon�connection_made�_add_reader�_read_readyr	�_set_result_unless_cancelled)r r�rWrXrYrZ�moder"rrr�s:


���
�z_UnixReadPipeTransport.__init__cCs�|jjg}|jdkr |�d�n|jr0|�d�|�d|j���t|jdd�}|jdk	r�|dk	r�t�	||jt
j�}|r�|�d�q�|�d�n |jdk	r�|�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 rDr!r�rrr�__repr__�s(


�

z_UnixReadPipeTransport.__repr__c
Cs�zt�|j|j�}WnDttfk
r,Yn�tk
rX}z|�|d�W5d}~XYn^X|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
rDr��_remove_readerr�Zeof_received�_call_connection_lost)r r2rIrrrr��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 rXrrr�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,|jdk	r(|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||dd�n|j�||||j	d��|�
|�dS�Nz%r: %sT��exc_info)�message�	exceptionr�rX)rRr?rEZEIOr�r�r
r��call_exception_handlerr�r��r rIr�rrrr�s
�z#_UnixReadPipeTransport._fatal_errorcCs(d|_|j�|j�|j�|j|�dS�NT)r�r�r�r�r�r��r rIrrrr�-sz_UnixReadPipeTransport._closecCs4z|j�|�W5|j��d|_d|_d|_XdSr�r�r&r�r�Zconnection_lostrrrrr�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"rrU�s
rUcs�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)(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�|dk	r�|j�tj|d�dS)NrWrFz?Pipe transport is only for pipes, sockets and character devicesZaix)rrr�r�r=r�r��	bytearray�_buffer�
_conn_lostr�r{r�r�r�r�r�r�r>r�r�r�r�r'�platform�
startswithr�r�r	r�)
r r�rWrXrYrZr�Zis_charZis_fifoZ	is_socketr"rrr?s:




�
�z _UnixWritePipeTransport.__init__cCs�|jjg}|jdkr |�d�n|jr0|�d�|�d|j���t|jdd�}|jdk	r�|dk	r�t�	||jt
j�}|r�|�d�n
|�d�|��}|�d|���n |jdk	r�|�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 rDr!r�rlrrrr�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
rDrr��BrokenPipeErrorr�rrrr�s

z#_UnixWritePipeTransport._read_readyc
Cs4t|t�rt|�}|sdS|js&|jrN|jtjkr<t�d�|jd7_dS|j	�szt
�|j|�}Wntt
tfk
r�d}YnZttfk
r��YnBtk
r�}z$|jd7_|�|d�WY�dSd}~XYnX|t|�kr�dS|dk�rt|�|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)rRr�
memoryviewrr�rZ!LOG_THRESHOLD_FOR_CONNLOST_WRITESr
�warningrr{�writer�r�r�rdrerfr�r
r�Z_add_writer�_write_readyZ_maybe_pause_protocol)r r2�nrIrrrr�s6


z_UnixWritePipeTransport.writec
Cszt�|j|j�}Wn�ttfk
r,Yn�ttfk
rD�Yn�tk
r�}z6|j�	�|j
d7_
|j�|j�|�
|d�W5d}~XYnfX|t|j�kr�|j�	�|j�|j�|��|jr�|j�|j�|�d�dS|dkr�|jd|�=dS)Nrrr)r{rr�rr�r�rdrerfr.rr��_remove_writerr�r
Z_maybe_resume_protocolr�r�r�)r rrIrrrr�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|jdk	r|js|��dSr)r�r�rr�rrrr&�sz_UnixWritePipeTransport.closecCs,|jdk	r(|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||dd�n|j�||||jd��|�|�dSr�)	rRr?r�r�r
r�r�r�r�r�rrrr��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._closecCs4z|j�|�W5|j��d|_d|_d|_XdSrrrrrrr��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)rac		Ks�d}|tjkrt��\}}zPtj|f||||d|d�|��|_|dk	rh|��t|��d|d�|j_	d}W5|dk	r�|��|��XdS)NF)rhrirjrk�universal_newlinesrl�wb)�	buffering)
�
subprocess�PIPEr}�
socketpairr&�Popen�_procr��detachri)	r rHrhrirjrkrlrmZstdin_wrrr�_starts.
���z_UnixSubprocessTransport._startN)r�r�r�rrrrrra	srac@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 rrrGrHrrrrb9s	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 rrrrr�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�rbr$r&r&r_r'r,rrrrr"s
	rcCs2t�|�rt�|�St�|�r*t�|�S|SdSr)r{�WIFSIGNALED�WTERMSIG�	WIFEXITED�WEXITSTATUS)�statusrrr�_compute_returncodeqs



r2c@sDeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dS)�BaseChildWatchercCsd|_i|_dSr)r��
_callbacksr�rrrr�szBaseChildWatcher.__init__cCs|�d�dSr)r&r�rrrr&�szBaseChildWatcher.closecCs|jdk	o|j��Sr)r�Z
is_runningr�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|jdk	r$|dkr$|jr$t�dt�|jdk	r<|j�tj�||_|dk	rb|�tj|j	�|�
�dS)NzCA loop is being detached from a child watcher with pending handlers)r�r4r+r,�RuntimeWarningr*r;�SIGCHLDrK�	_sig_chldr7r%rrrr&�s�
zBaseChildWatcher.attach_loopc
Cs^z|��WnLttfk
r&�Yn4tk
rX}z|j�d|d��W5d}~XYnXdS)N�$Unknown exception in SIGCHLD handler)r�r�)r7rdrerfr�r�rrrrr:�s�zBaseChildWatcher._sig_chldN)
r�r�r�rr&r_r6r7r&r:rrrrr3sr3csPeZdZdZ�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)r4r.rr&r�r"rrr&�s
zSafeChildWatcher.closecCs|Srrr�rrrr'�szSafeChildWatcher.__enter__cCsdSrrr(rrrr,�szSafeChildWatcher.__exit__cGs||f|j|<|�|�dSr)r4r6r"rrrrb�sz"SafeChildWatcher.add_child_handlercCs*z|j|=WdStk
r$YdSXdS�NTF�r4rMr#rrrr$�s
z%SafeChildWatcher.remove_child_handlercCst|j�D]}|�|�q
dSr�r)r4r6r#rrrr7�sz SafeChildWatcher._do_waitpid_allcCs�zt�|tj�\}}Wn(tk
r>|}d}t�d|�Yn.X|dkrLdSt|�}|j��rlt�	d||�z|j
�|�\}}Wn.tk
r�|j��r�tjd|dd�YnX|||f|��dS)N��8Unknown child process pid %d, will report returncode 255r�$process %s exited with returncode %s�'Child watcher got an unexpected pid: %rTr�)
r{�waitpid�WNOHANG�ChildProcessErrorr
rr2r�r�r�r4�poprM)r r5rrr1rsrGrHrrrr6�s4�

�
�zSafeChildWatcher._do_waitpid)r�r�r�r�r&r'r,rbr$r7r6r�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�)rr�	threading�Lock�_lock�_zombies�_forksr�r"rrrs

zFastChildWatcher.__init__cs"|j��|j��t���dSr)r4r.rJrr&r�r"rrr&s

zFastChildWatcher.closec
Cs0|j� |jd7_|W5QR�SQRXdS)Nr)rIrKr�rrrr'szFastChildWatcher.__enter__c	Cs^|j�B|jd8_|js"|js0W5QR�dSt|j�}|j��W5QRXt�d|�dS)Nrz5Caught subprocesses termination from unknown pids: %s)rIrKrJrAr.r
r)r r)r*r+Zcollateral_victimsrrrr,s
�zFastChildWatcher.__exit__c	Gsf|j�Fz|j�|�}Wn.tk
rF||f|j|<YW5QR�dSXW5QRX|||f|��dSr)rIrJrFrMr4)r rrrGrHrsrrrrb'sz"FastChildWatcher.add_child_handlercCs*z|j|=WdStk
r$YdSXdSr<r=r#rrrr$5s
z%FastChildWatcher.remove_child_handlerc	Cs�zt�dtj�\}}Wntk
r,YdSX|dkr:dSt|�}|j��z|j�|�\}}WnNtk
r�|j	r�||j
|<|j��r�t
�d||�YW5QR�qd}YnX|j��r�t
�d||�W5QRX|dkr�t
�d||�q|||f|��qdS)Nr4rz,unknown process %s exited with returncode %srAz8Caught subprocess termination from unknown pid: %d -> %d)r{rCrDrEr2rIr4rFrMrKrJr�r�r
r�r)r rrr1rsrGrHrrrr7<s@

�

��z FastChildWatcher._do_waitpid_all)r�r�r�r�rr&r'r,rbr$r7r�rrr"rr�s	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)r4�_saved_sighandlerr�rrrrzszMultiLoopChildWatcher.__init__cCs
|jdk	Sr)rLr�rrrr_~szMultiLoopChildWatcher.is_activecCsT|j��|jdkrdSt�tj�}||jkr:t�d�nt�tj|j�d|_dS)Nz+SIGCHLD handler was changed by outside code)	r4r.rLr;�	getsignalr9r:r
r)r rQrrrr&�s


zMultiLoopChildWatcher.closecCs|Srrr�rrrr'�szMultiLoopChildWatcher.__enter__cCsdSrr�r �exc_type�exc_val�exc_tbrrrr,�szMultiLoopChildWatcher.__exit__cGs&t��}|||f|j|<|�|�dSr)r�get_running_loopr4r6)r rrrGrHr�rrrrb�sz'MultiLoopChildWatcher.add_child_handlercCs*z|j|=WdStk
r$YdSXdSr<r=r#rrrr$�s
z*MultiLoopChildWatcher.remove_child_handlercCsN|jdk	rdSt�tj|j�|_|jdkr<t�d�tj|_t�tjd�dS)NzaPrevious SIGCHLD handler was set by non-Python code, restore to default handler on watcher close.F)rLr;r9r:r
rrPrCr%rrrr&�s


z!MultiLoopChildWatcher.attach_loopcCst|j�D]}|�|�q
dSrr>r#rrrr7�sz%MultiLoopChildWatcher._do_waitpid_allc	Cs�zt�|tj�\}}Wn,tk
rB|}d}t�d|�d}YnX|dkrPdSt|�}d}z|j�|�\}}}Wn$t	k
r�tjd|dd�YnHX|�
�r�t�d||�n.|r�|��r�t�d	||�|j
|||f|��dS)
Nr?r@FrTrBr��%Loop %r that handles pid %r is closedrA)r{rCrDrEr
rr2r4rFrM�	is_closedr�r�rq)	r r5rrr1rsZ	debug_logr�rGrHrrrr6�s:�
��z!MultiLoopChildWatcher._do_waitpidc	CsLz|��Wn:ttfk
r&�Yn"tk
rFtjddd�YnXdS)Nr;Tr�)r7rdrerfr
r)r rrrrrr:�szMultiLoopChildWatcher._sig_chldN)r�r�r�r�rr_r&r'r,rbr$r&r7r6r:rrrrrgs%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�szThreadedChildWatcher.__init__cCsdSrrr�rrrr_�szThreadedChildWatcher.is_activecCs|��dSr)�
_join_threadsr�rrrr&�szThreadedChildWatcher.closecCs.dd�t|j���D�}|D]}|��qdS)z%Internal: Join all non-daemon threadscSsg|]}|��r|js|�qSr)�is_alive�daemon��.0�threadrrr�
<listcomp>�s�z6ThreadedChildWatcher._join_threads.<locals>.<listcomp>N)r)rW�valuesr�)r �threadsr]rrrrX�sz"ThreadedChildWatcher._join_threadscCs|Srrr�rrrr'szThreadedChildWatcher.__enter__cCsdSrrrNrrrr,szThreadedChildWatcher.__exit__cCs6dd�t|j���D�}|r2||j�d�t|d�dS)NcSsg|]}|��r|�qSr)rYr[rrrr^	s�z0ThreadedChildWatcher.__del__.<locals>.<listcomp>z0 has registered but not finished child processesr$)r)rWr_r#r-)r r�r`rrrr�s�zThreadedChildWatcher.__del__cGsFt��}tj|jdt|j���||||fdd�}||j|<|��dS)Nzwaitpid-T)�target�namerHrZ)	rrRrG�Threadr6�nextrVrW�start)r rrrGrHr�r]rrrrbs
�
z&ThreadedChildWatcher.add_child_handlercCsdSrrr#rrrr$sz)ThreadedChildWatcher.remove_child_handlercCsdSrrr%rrrr&sz ThreadedChildWatcher.attach_loopcCs�zt�|d�\}}Wn(tk
r<|}d}t�d|�Yn Xt|�}|��r\t�d||�|��rtt�d||�n|j	|||f|��|j
�|�dS)Nrr?r@rArS)r{rCrEr
rr2r�r�rTrqrWrF)r r�r5rGrHrrr1rsrrrr6"s&�
�z ThreadedChildWatcher._do_waitpidN)r�r�r�r�rr_r&rXr'r,r+r,r�rbr$r&r6rrrrr�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)rr�_watcherr�r"rrrAs
z$_UnixDefaultEventLoopPolicy.__init__c	CsHtj�8|jdkr:t�|_tt��tj�r:|j�|j	j
�W5QRXdSr)rrIrgrrRrG�current_thread�_MainThreadr&�_localr�r�rrr�
_init_watcherEs
�z)_UnixDefaultEventLoopPolicy._init_watchercs6t��|�|jdk	r2tt��tj�r2|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_looprgrRrGrhrir&r%r"rrrlMs

�z*_UnixDefaultEventLoopPolicy.set_event_loopcCs|jdkr|��|jS)z~Get the watcher for child processes.

        If not yet set, a ThreadedChildWatcher object is automatically created.
        N)rgrkr�rrrr^[s
z-_UnixDefaultEventLoopPolicy.get_child_watchercCs|jdk	r|j��||_dS)z$Set the watcher for child processes.N)rgr&)r rnrrr�set_child_watcheres

z-_UnixDefaultEventLoopPolicy.set_child_watcher)r�r�r�r�rZ
_loop_factoryrrkrlr^rmr�rrr"rrf=s
rf)2r�rEr�rUr{r�r;r}r�rr'rGr+�rrrrrrr	r
rr�logr
�__all__r�ImportErrorrZBaseSelectorEventLooprZ
ReadTransportrUZ_FlowControlMixinZWriteTransportr\ZBaseSubprocessTransportrarr2r3rrrrZBaseDefaultEventLoopPolicyrfrrrrrr�<module>s`	
	�NO5Ji}Y3

F1le Man4ger