Edit File: unix_events.cpython-38.pyc
U ����@��dۿ����������������������@���s���d�Z�ddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddlZddlZddl mZ�ddl mZ�ddl mZ�ddl mZ�ddl mZ�dd l mZ�dd l mZ�ddl mZ�ddl mZ�dd l mZ�ddlmZ�dZe jdkr�ed��dd��ZG�dd��dej�ZG�dd��dej �Z!G�dd��dej"ej#�Z$G�dd��dej%�Z&G�dd��d�Z'dd��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+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�DefaultEventLoopPolicyZwin32z+Signals are not really supported on Windowsc�����������������C���s���dS�)zDummy signal handler.N��)�signum�framer���r����8/opt/alt/python38/lib64/python3.8/asyncio/unix_events.py�_sighandler_noop*���s����r���c�����������������������s����e�Zd�ZdZd)��fdd� Z��fdd�Zdd��Zd d ��Zdd��Zd d��Z dd��Z d*dd�Zd+dd�Zd,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. Nc��������������������s���t����|��i�|�_d�S��N)�super�__init__�_signal_handlers)�self�selector�� __class__r���r���r���5���s����z_UnixSelectorEventLoop.__init__c��������������������sZ���t�������t���s.t|�j�D�]}|��|��qn(|�jrVtjd|��d�t |�d��|�j� ���d�S�)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!���r���r���r%���9���s���� �z_UnixSelectorEventLoop.closec�����������������C���s���|D�]}|sq|���|��qd�S�r���)�_handle_signal)r����datar���r���r���r����_process_self_dataG���s����z)_UnixSelectorEventLoop._process_self_datac�������������� ���G���sL��t��|�st��|�rtd��|��|��|�����zt�|�j� ����W�n2�t tfk rt�}�ztt |���W�5�d}~X�Y�nX�t�|||�d�}||�j|<�zt�|t��t�|d��W�n��tk �rF�}�zz|�j|=�|�j�szt�d��W�n4�t tfk �r�}�zt�d|��W�5�d}~X�Y�nX�|jtjk�r4td|��d���n��W�5�d}~X�Y�nX�dS�)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)r���ZiscoroutineZiscoroutinefunction� TypeError� _check_signalZ _check_closed�signal� set_wakeup_fdZ_csock�fileno� ValueError�OSError�RuntimeError�strr���ZHandler���r����siginterruptr ����info�errno�EINVAL)r���r/����callback�args�exc�handleZnexcr���r���r����add_signal_handlerN���s2���� � z)_UnixSelectorEventLoop.add_signal_handlerc�����������������C���s8���|�j��|�}|dkrdS�|jr*|��|��n |��|��dS�)z2Internal helper that is the actual signal handler.N)r����getZ _cancelledr)���Z_add_callback_signalsafe)r���r/���rG���r���r���r���r0���{���s����z%_UnixSelectorEventLoop._handle_signalc�������������� ���C���s����|���|��z|�j|=�W�n�tk r,���Y�dS�X�|tjkr@tj}ntj}zt�||��W�nB�tk r��}�z$|jtj kr�t d|��d���n��W�5�d}~X�Y�nX�|�js�zt�d��W�n2�ttfk r��}�zt �d|��W�5�d}~X�Y�nX�dS�)zwRemove a handler for a signal. UNIX only. Return True if a signal handler was removed, False if not. Fr5���r6���Nr3���r4���T)r8���r����KeyErrorr9����SIGINT�default_int_handler�SIG_DFLr=���rB���rC���r>���r:���r<���r ���rA���)r���r/����handlerrF���r���r���r���r)�������s(���� z,_UnixSelectorEventLoop.remove_signal_handlerc�����������������C���s6���t�|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�intr7���r9���� valid_signalsr<���r.���r���r���r���r8�������s���� z$_UnixSelectorEventLoop._check_signalc�����������������C���s���t�|�||||�S�r���)�_UnixReadPipeTransport�r����pipe�protocol�waiter�extrar���r���r����_make_read_pipe_transport����s����z0_UnixSelectorEventLoop._make_read_pipe_transportc�����������������C���s���t�|�||||�S�r���)�_UnixWritePipeTransportrS���r���r���r����_make_write_pipe_transport����s����z1_UnixSelectorEventLoop._make_write_pipe_transportc ����������� ��� �������s����t������} | ���std��|����}t|�|||||||f||d�| ��}| �|���|�j|��z|I�d�H��W�nD�t t fk r������Y�n,�tk r����|����|� ��I�d�H����Y�nX�W�5�Q�R�X�|S�)NzRasyncio.get_child_watcher() is not activated, subprocess support is not installed.)rV���rW���)r����get_child_watcher� is_activer>���� create_future�_UnixSubprocessTransport�add_child_handlerZget_pid�_child_watcher_callback� SystemExit�KeyboardInterrupt� BaseExceptionr%���Z_wait) r���rU���rE����shell�stdin�stdout�stderr�bufsizerW����kwargs�watcherrV����transpr���r���r����_make_subprocess_transport����s8���� ������� ��z1_UnixSelectorEventLoop._make_subprocess_transportc�����������������C���s���|���|j|��d�S�r���)�call_soon_threadsafeZ_process_exited)r����pid� returncoderk���r���r���r���r`�������s����z._UnixSelectorEventLoop._child_watcher_callback)�ssl�sock�server_hostname�ssl_handshake_timeoutc���������� ����������s ��|d�kst�|t�st�|r,|d�krLtd��n |d�k r<td��|d�k rLtd��|d�k r�|d�k rdtd��t�|�}t�tjtjd�}z |� d��|�� ||�I�d�H��W�q����|������Y�q�X�n@|d�kr�td��|jtjks�|j tjkr�td|����|� d��|�j|||||d �I�d�H�\}}||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 timer���Fzno path and sock were specified�.A UNIX Domain Stream Socket was expected, got )rs���)rO���r?����AssertionErrorr<����os�fspath�socket�AF_UNIX�SOCK_STREAM�setblockingZsock_connectr%����family�typeZ_create_connection_transport) r����protocol_factory�pathrp���rq���rr���rs���� transportrU���r���r���r����create_unix_connection����sT������� �� ����z-_UnixSelectorEventLoop.create_unix_connection�d���T)rq����backlogrp���rs���� start_servingc������������� �������s���t�|t�rtd��|d�k r&|s&td��|d�k �rH|d�k r@td��t�|�}t�tjtj�}|d�dkr�z t � t� |�j�r�t�|��W�nB�t k r����Y�n0�tk r��}�zt�d||��W�5�d�}~X�Y�nX�z|�|��W�nl�tk �r0�} �z8|����| jtjk�rd|�d�} ttj| �d��n��W�5�d�} ~ X�Y�n���|������Y�nX�n<|d�k�rZtd ��|jtjk�sv|jtjk�r�td |����|�d��t�|�|g||||�}|�r�|����tjd|�d�I�d�H��|S�) Nz*ssl argument must be an SSLContext or Nonert���ru���r���)r�����z2Unable to check or remove stale UNIX socket %r: %rzAddress z is already in usez-path was not specified, and no sock specifiedrv���F)�loop)rO����boolr7���r<���rx���ry���rz���r{���r|����stat�S_ISSOCK�st_mode�remove�FileNotFoundErrorr=���r ����errorZbindr%���rB���Z EADDRINUSEr~���r���r}���r���ZServerZ_start_servingr����sleep)r���r����r����rq���r����rp���rs���r�����errrF����msgZserverr���r���r����create_unix_server��sn���� � � �� � �� ���z)_UnixSelectorEventLoop.create_unix_serverc�������������� �������s����z t�j�W�n,�tk r6�}�zt�d��W�5�d�}~X�Y�nX�z|���}W�n2�ttjfk rv�}�zt�d��W�5�d�}~X�Y�nX�zt��|�j }W�n,�t k r��}�zt�d��W�5�d�}~X�Y�nX�|r�|n|} | s�dS�|����} |��| d�||||| d��| I�d�H�S�)Nzos.sendfile() is not availableznot a regular filer���) rx����sendfile�AttributeErrorr����SendfileNotAvailableErrorr;����io�UnsupportedOperation�fstat�st_sizer=���r]����_sock_sendfile_native_impl)r���rq����file�offset�countrF���r;���r����Zfsize� blocksize�futr���r���r����_sock_sendfile_nativeJ��s2���� �����z,_UnixSelectorEventLoop._sock_sendfile_nativec �����������������C���s,��|����} |d�k r|��|��|���r4|��|||��d�S�|rd||�}|dkrd|��|||��|�|��d�S�zt�| |||�} W��nD�ttfk r����|d�kr�|�� ||��|�� | |�j|| ||||||� �Y��nb�tk �rj�}�z�|d�k �r|j t jk�rt|�tk �rtdt j�}||_|}|dk�rBt�d�} |��|||��|�| ��n|��|||��|�|��W�5�d�}~X�Y�n��ttfk �r������Y�n��tk �r��}�z|��|||��|�|��W�5�d�}~X�Y�njX�| dk�r�|��|||��|�|��nD|| 7�}|| 7�}|d�k�r |�� ||��|�� | |�j|| ||||||� �d�S�)Nr���zsocket is not connectedzos.sendfile call failed)r;���� remove_writer� cancelled�_sock_sendfile_update_fileposZ set_resultrx���r�����BlockingIOError�InterruptedError�_sock_add_cancellation_callbackZ add_writerr����r=���rB���ZENOTCONNr����ConnectionError� __cause__r���r����Z set_exceptionra���rb���rc���)r���r����Z registered_fdrq���r;���r����r����r����� total_sent�fdZsentrF����new_excr����r���r���r���r����a��s����� ������ � ��� � ������z1_UnixSelectorEventLoop._sock_sendfile_native_implc�����������������C���s���|dkrt��||t�j��d�S��Nr���)rx����lseek�SEEK_SET)r���r;���r����r����r���r���r���r�������s����z4_UnixSelectorEventLoop._sock_sendfile_update_fileposc��������������������s������fdd�}|��|��d�S�)Nc��������������������s&���|�����r"����}|dkr"���|��d�S�)Nr3���)r����r;���r����)r����r�����r���rq���r���r����cb���s����zB_UnixSelectorEventLoop._sock_add_cancellation_callback.<locals>.cb)Zadd_done_callback)r���r����rq���r����r���r����r���r�������s����z6_UnixSelectorEventLoop._sock_add_cancellation_callback)N)NN)NN)N)N)N)�__name__� __module__�__qualname__�__doc__r���r%���r2���rH���r0���r)���r8���rX���rZ���rl���r`���r����r����r����r����r����r����� __classcell__r���r���r!���r���r���/���sH���- ��� ��� �� ����.�����CFr���c�����������������������s����e�Zd�ZdZd��fdd� Zdd��Zdd��Zd d ��Zdd��Zd d��Z dd��Z dd��Zdd��Ze jfdd�Zddd�Zdd��Zdd��Z���ZS�) rR���i���Nc��������������������s����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��|d�k r�|�j�tj|d���d�S�)NrT���Fz)Pipe transport is for pipes/sockets only.)r���r����_extra�_loop�_piper;����_fileno� _protocol�_closing�_pausedrx���r����r����r�����S_ISFIFOr�����S_ISCHRr<����set_blocking� call_soon�connection_made�_add_reader�_read_readyr ����_set_result_unless_cancelled)r���r����rT���rU���rV���rW����moder!���r���r���r������s:���� ���� ��z_UnixReadPipeTransport.__init__c�����������������C���s����|�j�jg}|�jd�kr |�d��n|�jr0|�d��|�d|�j�����t|�jdd��}|�jd�k r�|d�k r�t� ||�jt j�}|r�|�d��q�|�d��n |�jd�k 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� selectorsZ EVENT_READ�format�join)r���rA���r ���r����r���r���r����__repr__���s(���� ��� z_UnixReadPipeTransport.__repr__c�������������� ���C���s����zt��|�j|�j�}W�nD�ttfk r,���Y�n��tk rX�}�z|��|d��W�5�d�}~X�Y�n^X�|rl|�j� |��nJ|�j ���r�t� d|���d|�_|�j �|�j��|�j �|�jj��|�j �|�jd���d�S�)Nz"Fatal read error on pipe transport�%r was closed by peerT)rx����readr�����max_sizer����r����r=����_fatal_errorr����Z data_receivedr����� get_debugr ���rA���r�����_remove_readerr����Zeof_received�_call_connection_lost)r���r1���rF���r���r���r���r�������s���� z"_UnixReadPipeTransport._read_readyc�����������������C���s>���|�j�s|�jrd�S�d|�_|�j�|�j��|�j���r:t�d|���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���sB���|�j�s|�jsd�S�d|�_|�j�|�j|�j��|�j���r>t�d|���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���s ���||�_�d�S�r����r�����r���rU���r���r���r����set_protocol ��s����z#_UnixReadPipeTransport.set_protocolc�����������������C���s���|�j�S�r���r����r����r���r���r����get_protocol��s����z#_UnixReadPipeTransport.get_protocolc�����������������C���s���|�j�S�r����r����r����r���r���r���� is_closing��s����z!_UnixReadPipeTransport.is_closingc�����������������C���s���|�j�s|��d���d�S�r���)r�����_closer����r���r���r���r%�����s����z_UnixReadPipeTransport.closec�����������������C���s,���|�j�d�k r(|d|���t|�d��|�j�����d�S��Nzunclosed transport r#����r����r,���r%����r����_warnr���r���r����__del__��s���� z_UnixReadPipeTransport.__del__�Fatal error on pipe transportc�����������������C���sZ���t�|t�r4|jtjkr4|�j���rLtjd|�|dd��n|�j�|||�|�j d���|�� |��d�S��Nz%r: %sT��exc_info)�message� exceptionr����rU���)rO���r=���rB���ZEIOr����r����r ���r�����call_exception_handlerr����r�����r���rF���r����r���r���r���r������s���� �z#_UnixReadPipeTransport._fatal_errorc�����������������C���s(���d|�_�|�j�|�j��|�j�|�j|��d�S��NT)r����r����r����r����r����r�����r���rF���r���r���r���r����-��s����z_UnixReadPipeTransport._closec�����������������C���s4���z|�j�|��W�5�|�j�����d�|�_�d�|�_d�|�_X�d�S�r����r����r%���r����r����Zconnection_lostr����r���r���r���r����2��s���� z,_UnixReadPipeTransport._call_connection_lost)NN)r����)r����r����r����r����r���r����r����r����r����r����r����r����r%���r*���r+���r����r����r����r����r����r���r���r!���r���rR������s��� rR���c�����������������������s����e�Zd�Zd%��fdd� Zdd��Zdd��Zdd ��Zd d��Zdd ��Zdd��Z dd��Z dd��Zdd��Zdd��Z dd��Zejfdd�Zdd��Zd&dd �Zd'd!d"�Zd#d$��Z���ZS�)(rY���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��|d�k r�|�j�tj|d���d�S�)NrT���r���Fz?Pipe transport is only for pipes, sockets and character devicesZaix)r���r���r����r����r;���r����r����� bytearray�_buffer� _conn_lostr����rx���r����r����r����r����r����r����r<���r����r����r����r����r&����platform� startswithr����r����r ���r����) r���r����rT���rU���rV���rW���r����Zis_charZis_fifoZ is_socketr!���r���r���r���?��s:���� �� ��z _UnixWritePipeTransport.__init__c�����������������C���s����|�j�jg}|�jd�kr |�d��n|�jr0|�d��|�d|�j�����t|�jdd��}|�jd�k r�|d�k r�t� ||�jt j�}|r�|�d��n |�d��|����}|�d|�����n |�jd�k 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����ZEVENT_WRITE�get_write_buffer_sizer����r����)r���rA���r ���r����rh���r���r���r���r����d��s,���� ��� z _UnixWritePipeTransport.__repr__c�����������������C���s ���t�|�j�S�r���)�lenr����r����r���r���r���r����|��s����z-_UnixWritePipeTransport.get_write_buffer_sizec�����������������C���s6���|�j����rt�d|���|�jr*|��t����n|�����d�S�)Nr����)r����r����r ���rA���r����r�����BrokenPipeErrorr����r���r���r���r������s ���� z#_UnixWritePipeTransport._read_readyc�������������� ���C���sR��t�|tttf�stt|���t�|t�r.t|�}|s6d�S�|�jsB|�jrj|�jtj krXt �d��|��jd7��_d�S�|�j�s8zt �|�j|�}W�nt�ttfk r����d}Y�nZ�ttfk r������Y�nB�tk r��}�z$|��jd7��_|��|d��W�Y��d�S�d�}~X�Y�nX�|t|�k�rd�S�|dk�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)rO����bytesr����� memoryviewrw����reprr����r����r���Z!LOG_THRESHOLD_FOR_CONNLOST_WRITESr ����warningr����rx����writer����r����r����ra���rb���rc���r����r����r����Z_add_writer�_write_readyZ_maybe_pause_protocol)r���r1����nrF���r���r���r���r�����s8���� z_UnixWritePipeTransport.writec�������������� ���C���s��|�j�std��zt�|�j|�j��}W�n��ttfk r:���Y�n��ttfk rR�����Y�n��t k r��}�z6|�j�� ���|��jd7��_|�j� |�j��|��|d��W�5�d�}~X�Y�nhX�|t|�j��kr�|�j�� ���|�j� |�j��|�����|�jr�|�j�|�j��|��d���d�S�|dk�r|�j�d�|�=�d�S�)NzData should not be emptyr���r��r���)r����rw���rx���r��r����r����r����ra���rb���rc���r-���r����r�����_remove_writerr����r����Z_maybe_resume_protocolr����r����r����)r���r��rF���r���r���r���r�����s,���� z$_UnixWritePipeTransport._write_readyc�����������������C���s���dS�r����r���r����r���r���r���� can_write_eof���s����z%_UnixWritePipeTransport.can_write_eofc�����������������C���sB���|�j�r d�S�|�jst�d|�_�|�js>|�j�|�j��|�j�|�jd���d�S�r����) r����r����rw���r����r����r����r����r����r����r����r���r���r���� write_eof���s���� z!_UnixWritePipeTransport.write_eofc�����������������C���s ���||�_�d�S�r���r����r����r���r���r���r�������s����z$_UnixWritePipeTransport.set_protocolc�����������������C���s���|�j�S�r���r����r����r���r���r���r�������s����z$_UnixWritePipeTransport.get_protocolc�����������������C���s���|�j�S�r���r����r����r���r���r���r�������s����z"_UnixWritePipeTransport.is_closingc�����������������C���s���|�j�d�k r|�js|�����d�S�r���)r����r����r��r����r���r���r���r%������s����z_UnixWritePipeTransport.closec�����������������C���s,���|�j�d�k r(|d|���t|�d��|�j�����d�S�r����r����r����r���r���r���r�������s���� z_UnixWritePipeTransport.__del__c�����������������C���s���|���d���d�S�r���)r����r����r���r���r����abort���s����z_UnixWritePipeTransport.abortr����c�����������������C���sN���t�|t�r(|�j���r@tjd|�|dd��n|�j�|||�|�jd���|��|��d�S�r����) rO���r=���r����r����r ���r����r����r����r����r����r���r���r���r�������s���� �z$_UnixWritePipeTransport._fatal_errorc�����������������C���sF���d|�_�|�jr|�j�|�j��|�j����|�j�|�j��|�j�|�j|��d�S�r����) r����r����r����r ��r����r-���r����r����r����r����r���r���r���r�������s���� z_UnixWritePipeTransport._closec�����������������C���s4���z|�j�|��W�5�|�j�����d�|�_�d�|�_d�|�_X�d�S�r���r����r����r���r���r���r�������s���� z-_UnixWritePipeTransport._call_connection_lost)NN)r����)N)r����r����r����r���r����r����r����r��r��r ��r��r����r����r����r%���r*���r+���r����r��r����r����r����r����r���r���r!���r���rY���<��s"���% # rY���c�������������������@���s���e�Zd�Zdd��ZdS�)r^���c����������� ��� ���K���s����d�}|t�jkrt���\}}zPt�j|f||||d|d�|��|�_|d�k rh|����t|���d|d�|�j_ d�}W�5�|d�k r�|����|����X�d�S�)NF)rd���re���rf���rg���Zuniversal_newlinesrh����wb)� buffering) � subprocess�PIPErz���Z socketpairr%����Popen�_procr�����detachre���) r���rE���rd���re���rf���rg���rh���ri���Zstdin_wr���r���r����_start��s.���� �������z_UnixSubprocessTransport._startN)r����r����r����r��r���r���r���r���r^��� ��s���r^���c�������������������@���sH���e�Zd�ZdZdd��Zdd��Zdd��Zdd ��Zd d��Zdd ��Z dd��Z dS�)r���aH��Abstract 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. c�����������������G���s ���t����dS�)a��Register 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���rn���rD���rE���r���r���r���r_���9��s���� z&AbstractChildWatcher.add_child_handlerc�����������������C���s ���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���rn���r���r���r����remove_child_handlerD��s����z)AbstractChildWatcher.remove_child_handlerc�����������������C���s ���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����r���r���r����attach_loopL��s����z AbstractChildWatcher.attach_loopc�����������������C���s ���t����dS�)zlClose the watcher. This must be called to make sure that any underlying resource is freed. Nr��r����r���r���r���r%���V��s����zAbstractChildWatcher.closec�����������������C���s ���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����r���r���r���r\���]��s����zAbstractChildWatcher.is_activec�����������������C���s ���t����dS�)zdEnter the watcher's context and allow starting new processes This function must return selfNr��r����r���r���r���� __enter__f��s����zAbstractChildWatcher.__enter__c�����������������C���s ���t����dS�)zExit the watcher's contextNr���r����a�b�cr���r���r����__exit__l��s����zAbstractChildWatcher.__exit__N)r����r����r����r����r_���r��r��r%���r\���r��r!��r���r���r���r���r���"��s��� r���c�����������������C���s2���t��|��rt��|���S�t��|��r*t��|��S�|�S�d�S�r���)rx����WIFSIGNALED�WTERMSIG� WIFEXITED�WEXITSTATUS)�statusr���r���r����_compute_returncodeq��s ���� r'��c�������������������@���sD���e�Zd�Zdd��Zdd��Zdd��Zdd��Zd d ��Zdd��Zd d��Z dS�)�BaseChildWatcherc�����������������C���s���d�|�_�i�|�_d�S�r���)r����� _callbacksr����r���r���r���r������s����zBaseChildWatcher.__init__c�����������������C���s���|���d���d�S�r���)r��r����r���r���r���r%������s����zBaseChildWatcher.closec�����������������C���s���|�j�d�k o|�j����S�r���)r����Z is_runningr����r���r���r���r\������s����zBaseChildWatcher.is_activec�����������������C���s ���t����d�S�r���r��)r����expected_pidr���r���r����_do_waitpid���s����zBaseChildWatcher._do_waitpidc�����������������C���s ���t����d�S�r���r��r����r���r���r����_do_waitpid_all���s����z BaseChildWatcher._do_waitpid_allc�����������������C���s~���|d�kst�|tj�st�|�jd�k r<|d�kr<|�jr<t�dt��|�jd�k rT|�j� t j��||�_|d�k rz|�t j|�j ��|�����d�S�)NzCA loop is being detached from a child watcher with pending handlers)rO���r���ZAbstractEventLooprw���r����r)��r*���r+����RuntimeWarningr)���r9����SIGCHLDrH���� _sig_chldr,��r��r���r���r���r�����s����� zBaseChildWatcher.attach_loopc�������������� ���C���s^���z|������W�nL�ttfk r&�����Y�n4�tk rX�}�z|�j�d|d���W�5�d�}~X�Y�nX�d�S�)N�$Unknown exception in SIGCHLD handler)r����r����)r,��ra���rb���rc���r����r����r����r���r���r���r/�����s�����zBaseChildWatcher._sig_chldN) r����r����r����r���r%���r\���r+��r,��r��r/��r���r���r���r���r(����s���r(��c�����������������������sP���e�Zd�ZdZ��fdd�Zdd��Zdd��Zdd ��Zd d��Zdd ��Z dd��Z ���ZS�)r���ad��'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) c��������������������s���|�j�����t������d�S�r���)r)��r-���r���r%���r����r!���r���r���r%������s���� zSafeChildWatcher.closec�����������������C���s���|�S�r���r���r����r���r���r���r�����s����zSafeChildWatcher.__enter__c�����������������C���s���d�S�r���r���r��r���r���r���r!�����s����zSafeChildWatcher.__exit__c�����������������G���s���||f|�j�|<�|��|��d�S�r���)r)��r+��r��r���r���r���r_������s����z"SafeChildWatcher.add_child_handlerc�����������������C���s*���z|�j�|=�W�dS��tk r$���Y�dS�X�d�S��NTF�r)��rJ���r��r���r���r���r�����s ����z%SafeChildWatcher.remove_child_handlerc�����������������C���s���t�|�j�D�]}|��|��q d�S�r����r(���r)��r+��r��r���r���r���r,�����s����z SafeChildWatcher._do_waitpid_allc�����������������C���s����|dkst��zt�|tj�\}}W�n(�tk rJ���|}d}t�d|��Y�n.X�|dkrXd�S�t|�}|�j� ��rxt� d||��z|�j�|�\}}W�n.�t k r����|�j� ��r�tjd|dd��Y�nX�|||f|����d�S�)Nr��������8Unknown child process pid %d, will report returncode 255�$process %s exited with returncode %s�'Child watcher got an unexpected pid: %rTr����)rw���rx����waitpid�WNOHANG�ChildProcessErrorr ���r��r'��r����r����r����r)���poprJ���)r���r*��rn���r&��ro���rD���rE���r���r���r���r+�����s6����� �� ��zSafeChildWatcher._do_waitpid)r����r����r����r����r%���r��r!��r_���r��r,��r+��r����r���r���r!���r���r������s��� r���c�����������������������sT���e�Zd�ZdZ��fdd�Z��fdd�Zdd��Zdd ��Zd d��Zdd ��Z dd��Z ���ZS�)r���aW��'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). c��������������������s$���t�������t���|�_i�|�_d|�_d�S�r����)r���r���� threadingZLock�_lock�_zombies�_forksr����r!���r���r���r�����s���� zFastChildWatcher.__init__c��������������������s"���|�j�����|�j����t������d�S�r���)r)��r-���r>��r���r%���r����r!���r���r���r%�����s���� zFastChildWatcher.closec�������������� ���C���s0���|�j�� �|��jd7��_|�W��5�Q�R���S�Q�R�X�d�S�)Nr���)r=��r?��r����r���r���r���r����s����zFastChildWatcher.__enter__c�������������� ���C���s^���|�j��B�|��jd8��_|�js"|�js0W�5�Q�R���d�S�t|�j�}|�j����W�5�Q�R�X�t�d|��d�S�)Nr���z5Caught subprocesses termination from unknown pids: %s)r=��r?��r>��r?���r-���r ���r��)r���r��r��r ��Zcollateral_victimsr���r���r���r!����s���� �zFastChildWatcher.__exit__c�������������� ���G���st���|�j�std��|�j�F�z|�j�|�}W�n.�tk rT���||f|�j|<�Y�W�5�Q�R���d�S�X�W�5�Q�R�X�|||f|����d�S�)NzMust use the context manager)r?��rw���r=��r>��r;��rJ���r)��)r���rn���rD���rE���ro���r���r���r���r_���'��s����z"FastChildWatcher.add_child_handlerc�����������������C���s*���z|�j�|=�W�dS��tk r$���Y�dS�X�d�S�r1��r2��r��r���r���r���r��5��s ����z%FastChildWatcher.remove_child_handlerc�������������� ���C���s����zt��dt�j�\}}W�n�tk r,���Y�d�S�X�|dkr:d�S�t|�}|�j���z|�j�|�\}}W�nN�tk r����|�j r�||�j |<�|�j���r�t �d||��Y�W�5�Q�R���q�d�}Y�nX�|�j���r�t �d||��W�5�Q�R�X�|d�kr�t �d||��q�|||f|����q�d�S�)Nr3���r���z,unknown process %s exited with returncode %sr6��z8Caught subprocess termination from unknown pid: %d -> %d)rx���r8��r9��r:��r'��r=��r)��r;��rJ���r?��r>��r����r����r ���r����r��)r���rn���r&��ro���rD���rE���r���r���r���r,��<��s@���� �� ����z FastChildWatcher._do_waitpid_all)r����r����r����r����r���r%���r��r!��r_���r��r,��r����r���r���r!���r���r������s��� r���c�������������������@���sh���e�Zd�ZdZdd��Zdd��Zdd��Zdd ��Zd d��Zdd ��Z dd��Z dd��Zdd��Zdd��Z dd��ZdS�)r���a~��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). c�����������������C���s���i�|�_�d�|�_d�S�r���)r)���_saved_sighandlerr����r���r���r���r���z��s����zMultiLoopChildWatcher.__init__c�����������������C���s ���|�j�d�k S�r���)r@��r����r���r���r���r\���~��s����zMultiLoopChildWatcher.is_activec�����������������C���sT���|�j�����|�jd�krd�S�t�tj�}||�jkr:t�d��nt�tj|�j��d�|�_d�S�)Nz+SIGCHLD handler was changed by outside code) r)��r-���r@��r9���� getsignalr.��r/��r ���r��)r���rN���r���r���r���r%������s���� zMultiLoopChildWatcher.closec�����������������C���s���|�S�r���r���r����r���r���r���r�����s����zMultiLoopChildWatcher.__enter__c�����������������C���s���d�S�r���r����r����exc_typeZexc_valZexc_tbr���r���r���r!�����s����zMultiLoopChildWatcher.__exit__c�����������������G���s&���t����}|||f|�j|<�|��|��d�S�r���)r����get_running_loopr)��r+��)r���rn���rD���rE���r����r���r���r���r_������s����z'MultiLoopChildWatcher.add_child_handlerc�����������������C���s*���z|�j�|=�W�dS��tk r$���Y�dS�X�d�S�r1��r2��r��r���r���r���r�����s ����z*MultiLoopChildWatcher.remove_child_handlerc�����������������C���sN���|�j�d�k rd�S�t�tj|�j�|�_�|�j�d�kr<t�d��tj|�_�t�tjd��d�S�)NzaPrevious SIGCHLD handler was set by non-Python code, restore to default handler on watcher close.F)r@��r9���r.��r/��r ���r��rM���r@���r��r���r���r���r�����s���� z!MultiLoopChildWatcher.attach_loopc�����������������C���s���t�|�j�D�]}|��|��q d�S�r���r3��r��r���r���r���r,�����s����z%MultiLoopChildWatcher._do_waitpid_allc����������� ������C���s����|dkst��zt�|tj�\}}W�n,�tk rN���|}d}t�d|��d}Y�nX�|dkr\d�S�t|�}d}z|�j� |�\}}}W�n$�t k r����tjd|dd��Y�nHX�|���r�t�d||��n.|r�|���r�t� d ||��|j|||f|����d�S�) Nr���r4��r5��FTr7��r�����%Loop %r that handles pid %r is closedr6��)rw���rx���r8��r9��r:��r ���r��r'��r)��r;��rJ���� is_closedr����r����rm���) r���r*��rn���r&��ro���Z debug_logr����rD���rE���r���r���r���r+�����s<����� ����z!MultiLoopChildWatcher._do_waitpidc�������������� ���C���sL���z|������W�n:�ttfk r&�����Y�n"�tk rF���tjddd��Y�nX�d�S�)Nr0��Tr����)r,��ra���rb���rc���r ���r��)r���r���r���r���r���r���r/�����s����zMultiLoopChildWatcher._sig_chldN)r����r����r����r����r���r\���r%���r��r!��r_���r��r��r,��r+��r/��r���r���r���r���r���g��s���%r���c�������������������@���sn���e�Zd�ZdZdd��Zdd��Zdd��Zdd ��Zd d��Zdd ��Z e jfdd�Zdd��Z dd��Zdd��Zdd��ZdS�)r���aA��Threaded 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. c�����������������C���s���t��d�|�_i�|�_d�S�r����)� itertoolsr�����_pid_counter�_threadsr����r���r���r���r������s����zThreadedChildWatcher.__init__c�����������������C���s���dS�r����r���r����r���r���r���r\������s����zThreadedChildWatcher.is_activec�����������������C���s���|������d�S�r���)� _join_threadsr����r���r���r���r%������s����zThreadedChildWatcher.closec�����������������C���s.���dd��t�|�j����D��}|D�]}|����qdS�)z%Internal: Join all non-daemon threadsc�����������������S���s���g�|�]}|����r|js|�qS�r���)�is_alive�daemon��.0�threadr���r���r���� <listcomp>���s������z6ThreadedChildWatcher._join_threads.<locals>.<listcomp>N)r(���rI���valuesr����)r����threadsrO��r���r���r���rJ�����s����z"ThreadedChildWatcher._join_threadsc�����������������C���s���|�S�r���r���r����r���r���r���r����s����zThreadedChildWatcher.__enter__c�����������������C���s���d�S�r���r���rB��r���r���r���r!����s����zThreadedChildWatcher.__exit__c�����������������C���s6���dd��t�|�j����D��}|r2||�j��d�t|�d��d�S�)Nc�����������������S���s���g�|�]}|����r|�qS�r���)rK��rM��r���r���r���rP�� ��s�����z0ThreadedChildWatcher.__del__.<locals>.<listcomp>z0 has registered but not finished child processesr#���)r(���rI��rQ��r"���r,���)r���r����rR��r���r���r���r������s�����zThreadedChildWatcher.__del__c�����������������G���sF���t����}tj|�jdt|�j����||||fdd�}||�j|<�|����d�S�)Nzwaitpid-T)�target�namerE���rL��) r���rD��r<��ZThreadr+���nextrH��rI���start)r���rn���rD���rE���r����rO��r���r���r���r_�����s���� � z&ThreadedChildWatcher.add_child_handlerc�����������������C���s���dS�r����r���r��r���r���r���r����s����z)ThreadedChildWatcher.remove_child_handlerc�����������������C���s���d�S�r���r���r��r���r���r���r����s����z ThreadedChildWatcher.attach_loopc�����������������C���s����|dkst��zt�|d�\}}W�n(�tk rH���|}d}t�d|��Y�n X�t|�}|���rht�d||��|� ��r�t�d||��n|j |||f|����|�j�|��d�S�)Nr���r4��r5��r6��rE��) rw���rx���r8��r:��r ���r��r'��r����r����rF��rm���rI��r;��)r���r����r*��rD���rE���rn���r&��ro���r���r���r���r+��"��s(����� ��z ThreadedChildWatcher._do_waitpidN)r����r����r����r����r���r\���r%���rJ��r��r!��r*���r+���r����r_���r��r��r+��r���r���r���r���r������s��� r���c�����������������������sH���e�Zd�ZdZeZ��fdd�Zdd��Z��fdd�Zdd ��Z d d��Z ���ZS�)�_UnixDefaultEventLoopPolicyz:UNIX event loop policy with a watcher for child processes.c��������������������s���t�������d�|�_d�S�r���)r���r����_watcherr����r!���r���r���r���A��s���� z$_UnixDefaultEventLoopPolicy.__init__c�������������� ���C���sH���t�j�8�|�jd�kr:t��|�_tt���tj�r:|�j�|�j j ��W�5�Q�R�X�d�S�r���)r���r=��rX��r���rO���r<���current_thread�_MainThreadr���_localr����r����r���r���r���� _init_watcherE��s���� �z)_UnixDefaultEventLoopPolicy._init_watcherc��������������������s6���t����|��|�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_looprX��rO���r<��rY��rZ��r��r��r!���r���r���r]��M��s ���� �z*_UnixDefaultEventLoopPolicy.set_event_loopc�����������������C���s���|�j�dkr|�����|�j�S�)z~Get the watcher for child processes. If not yet set, a ThreadedChildWatcher object is automatically created. N)rX��r\��r����r���r���r���r[���[��s���� z-_UnixDefaultEventLoopPolicy.get_child_watcherc�����������������C���s4���|dkst�|t�st�|�jdk r*|�j����||�_dS�)z$Set the watcher for child processes.N)rO���r���rw���rX��r%���)r���rj���r���r���r����set_child_watchere��s���� z-_UnixDefaultEventLoopPolicy.set_child_watcher)r����r����r����r����r���Z _loop_factoryr���r\��r]��r[���r^��r����r���r���r!���r���rW��=��s��� rW��)2r����rB���r����rG��rx���r����r9���rz���r����r��r&���r<��r*�����r���r���r���r���r���r���r ���r ���r���r����logr ����__all__r�����ImportErrorr���ZBaseSelectorEventLoopr���Z ReadTransportrR���Z_FlowControlMixinZWriteTransportrY���ZBaseSubprocessTransportr^���r���r'��r(��r���r���r���r���ZBaseDefaultEventLoopPolicyrW��r���r���r���r���r���r����<module>���s`��� ���� ��NO5Ji}Y3