B
    Å S]Tž  ã               @   sš  d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlmZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ dZe	jdkrêedƒ‚dd„ ZG dd„ dejƒZG dd„ dejƒZG dd„ dej ej!ƒZ"G dd„ dej#ƒZ$G dd„ dƒZ%G dd„ de%ƒZ&G dd „ d e&ƒZ'G d!d"„ d"e&ƒZ(G d#d$„ d$ej)ƒZ*eZ+e*Z,dS )%z2Selector event loop for Unix with signal handling.é    Né   )Úbase_events)Úbase_subprocess)Ú	constants)Ú
coroutines)Úevents)Úfutures)Úselector_events)Útasks)Ú
transports)Úlogger)ÚSelectorEventLoopÚAbstractChildWatcherÚSafeChildWatcherÚFastChildWatcherÚDefaultEventLoopPolicyZwin32z+Signals are not really supported on Windowsc             C   s   dS )zDummy signal handler.N© )ÚsignumÚframer   r   ú$lib/python3.7/asyncio/unix_events.pyÚ_sighandler_noop'   s    r   c                   sÊ   e Zd ZdZd)‡ fdd„	Z‡ fdd„Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
d*dd„Zd+dd„Zd,dd„Zdd„ Zd-dddddœdd„Zd.ddddddœdd „Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Z‡  ZS )/Ú_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   2   s    z_UnixSelectorEventLoop.__init__c                s^   t ƒ  ¡  t ¡ s2xFt| jƒD ]}|  |¡ qW n(| jrZtjd| ›dt	| d | j 
¡  d S )NzClosing the loop z@ on interpreter shutdown stage, skipping signal handlers removal)Úsource)r   ÚcloseÚsysÚis_finalizingÚlistr   Úremove_signal_handlerÚwarningsÚwarnÚResourceWarningÚclear)r   Úsig)r   r   r   r   6   s    
z_UnixSelectorEventLoop.closec             C   s"   x|D ]}|sq|   |¡ qW d S )N)Ú_handle_signal)r   Údatar   r   r   r   Ú_process_self_dataD   s    
z)_UnixSelectorEventLoop._process_self_datac             G   sL  t  |¡st  |¡rtdƒ‚|  |¡ |  ¡  yt | j 	¡ ¡ W n2 t
tfk
rt } ztt|ƒƒ‚W dd}~X Y nX t ||| d¡}|| j|< yt |t¡ t |d¡ W nš tk
rF } zz| j|= | jsyt d¡ W n4 t
tfk
r } zt d|¡ W dd}~X Y nX |jtjkr4td|› dƒ‚n‚ W dd}~X Y nX dS )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éÿÿÿÿzset_wakeup_fd(-1) failed: %szsig z cannot be caught)r   ZiscoroutineZiscoroutinefunctionÚ	TypeErrorÚ_check_signalZ_check_closedÚsignalÚset_wakeup_fdZ_csockÚfilenoÚ
ValueErrorÚOSErrorÚRuntimeErrorÚstrr   ZHandler   r   Úsiginterruptr   ÚinfoÚerrnoÚEINVAL)r   r(   ÚcallbackÚargsÚexcÚhandleZnexcr   r   r   Úadd_signal_handlerK   s0    



z)_UnixSelectorEventLoop.add_signal_handlerc             C   s8   | j  |¡}|dkrdS |jr*|  |¡ n
|  |¡ dS )z2Internal helper that is the actual signal handler.N)r   ÚgetZ
_cancelledr#   Z_add_callback_signalsafe)r   r(   r=   r   r   r   r)   x   s    z%_UnixSelectorEventLoop._handle_signalc          
   C   sä   |   |¡ y| j|= W n tk
r*   dS X |tjkr>tj}ntj}yt ||¡ W nB tk
r– } z$|jtj	kr„t
d|› dƒ‚n‚ W dd}~X Y nX | jsàyt d¡ W n2 ttfk
rÞ } zt d|¡ W dd}~X Y nX dS )zwRemove a handler for a signal.  UNIX only.

        Return True if a signal handler was removed, False if not.
        Fzsig z cannot be caughtNr,   zset_wakeup_fd(-1) failed: %sT)r.   r   ÚKeyErrorr/   ÚSIGINTÚdefault_int_handlerÚSIG_DFLr3   r8   r9   r4   r0   r2   r   r7   )r   r(   Zhandlerr<   r   r   r   r#   ‚   s(    

z,_UnixSelectorEventLoop.remove_signal_handlerc             C   sL   t |tƒstd|›ƒ‚d|  kr.tjk sHn td|› dtj› dƒ‚dS )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.
        zsig must be an int, not r   zsig z out of range(1, ú)N)Ú
isinstanceÚintr-   r/   ÚNSIGr2   )r   r(   r   r   r   r.   ¢   s    
z$_UnixSelectorEventLoop._check_signalc             C   s   t | ||||ƒS )N)Ú_UnixReadPipeTransport)r   ÚpipeÚprotocolÚwaiterÚextrar   r   r   Ú_make_read_pipe_transport®   s    z0_UnixSelectorEventLoop._make_read_pipe_transportc             C   s   t | ||||ƒS )N)Ú_UnixWritePipeTransport)r   rI   rJ   rK   rL   r   r   r   Ú_make_write_pipe_transport²   s    z1_UnixSelectorEventLoop._make_write_pipe_transportc	          
   Ë   s”   t  ¡ ‚}
|  ¡ }t| |||||||f||dœ|	—Ž}|
 | ¡ | j|¡ y|I d H  W n, tk
r„   | ¡  | 	¡ I d H  ‚ Y nX W d Q R X |S )N)rK   rL   )
r   Úget_child_watcherÚcreate_futureÚ_UnixSubprocessTransportÚadd_child_handlerZget_pidÚ_child_watcher_callbackÚ	Exceptionr   Z_wait)r   rJ   r;   ÚshellÚstdinÚstdoutÚstderrÚbufsizerL   ÚkwargsÚwatcherrK   Útranspr   r   r   Ú_make_subprocess_transport¶   s    




z1_UnixSelectorEventLoop._make_subprocess_transportc             C   s   |   |j|¡ d S )N)Zcall_soon_threadsafeZ_process_exited)r   ÚpidÚ
returncoder]   r   r   r   rT   Ë   s    z._UnixSelectorEventLoop._child_watcher_callback)ÚsslÚsockÚserver_hostnameÚssl_handshake_timeoutc      	      Ã   s   |d kst |tƒst‚|r,|d krLtdƒ‚n |d k	r<tdƒ‚|d k	rLtdƒ‚|d k	rº|d k	rdtdƒ‚t |¡}t tjtjd¡}y | 	d¡ |  
||¡I d H  W qú   | ¡  ‚ Y qúX n@|d krÊtdƒ‚|jtjksâ|jtjkrðtd|›ƒ‚| 	d¡ | j|||||d	I d H \}}||fS )
Nz/you have to pass server_hostname when using sslz+server_hostname is only meaningful with sslz1ssl_handshake_timeout is only meaningful with sslz3path and sock can not be specified at the same timer   Fzno path and sock were specifiedz.A UNIX Domain Stream Socket was expected, got )rd   )rE   r5   ÚAssertionErrorr2   ÚosÚfspathÚsocketÚAF_UNIXÚSOCK_STREAMÚsetblockingZsock_connectr   ÚfamilyÚtypeZ_create_connection_transport)	r   Úprotocol_factoryÚpathra   rb   rc   rd   Ú	transportrJ   r   r   r   Úcreate_unix_connectionÎ   sB    



z-_UnixSelectorEventLoop.create_unix_connectionéd   T)rb   Úbacklogra   rd   Ústart_servingc         
   Ã   sÊ  t |tƒrtdƒ‚|d k	r&|s&tdƒ‚|d k	rH|d k	r@tdƒ‚t |¡}t tjtj¡}|d dkrÊy t	 
t 	|¡j¡r„t |¡ W nB tk
rš   Y n0 tk
rÈ } zt d||¡ W d d }~X Y nX y| |¡ W nl tk
r0 }	 z8| ¡  |	jtjkrd|›d}
ttj|
ƒd ‚n‚ W d d }	~	X Y n   | ¡  ‚ Y nX n<|d krZtd	ƒ‚|jtjksv|jtjkr„td
|›ƒ‚| d¡ t | |g||||¡}|rÆ| ¡  tjd| dI d H  |S )Nz*ssl argument must be an SSLContext or Nonez1ssl_handshake_timeout is only meaningful with sslz3path and sock can not be specified at the same timer   )r   ú z2Unable to check or remove stale UNIX socket %r: %rzAddress z is already in usez-path was not specified, and no sock specifiedz.A UNIX Domain Stream Socket was expected, got F)Úloop)rE   Úboolr-   r2   rf   rg   rh   ri   rj   ÚstatÚS_ISSOCKÚst_modeÚremoveÚFileNotFoundErrorr3   r   ÚerrorZbindr   r8   Z
EADDRINUSErl   rm   rk   r   ZServerZ_start_servingr
   Zsleep)r   rn   ro   rb   rs   ra   rd   rt   Úerrr<   ÚmsgZserverr   r   r   Úcreate_unix_serverû   sZ    






z)_UnixSelectorEventLoop.create_unix_serverc          
   Ã   sô   y
t j W n, tk
r6 } zt d¡‚W d d }~X Y nX y| ¡ }W n2 ttjfk
rv } zt d¡‚W d d }~X Y nX yt  |¡j	}W n, t
k
r´ } zt d¡‚W d d }~X Y nX |r¾|n|}	|	sÊdS |  ¡ }
|  |
d |||||	d¡ |
I d H S )Nzos.sendfile() is not availableznot a regular filer   )rf   ÚsendfileÚAttributeErrorr   ÚSendfileNotAvailableErrorr1   ÚioÚUnsupportedOperationÚfstatÚst_sizer3   rQ   Ú_sock_sendfile_native_impl)r   rb   ÚfileÚoffsetÚcountr<   r1   r~   ZfsizeÚ	blocksizeÚfutr   r   r   Ú_sock_sendfile_native>  s(    
z,_UnixSelectorEventLoop._sock_sendfile_nativec	             C   s  |  ¡ }	|d k	r|  |¡ | ¡ r4|  |||¡ d S |rd|| }|dkrd|  |||¡ | |¡ d S yt |	|||¡}
W n* ttfk
rÆ   |d kr¢|  	||¡ |  
|	| j||	||||||¡
 Y nH tk
rj } z†|d k	r|jtjkrt|ƒtk	rtdtjƒ}||_|}|dkrBt d¡}|  |||¡ | |¡ n|  |||¡ | |¡ W d d }~X Y n¤ tk
r¤ } z|  |||¡ | |¡ W d d }~X Y njX |
dkrÊ|  |||¡ | |¡ nD||
7 }||
7 }|d krð|  	||¡ |  
|	| j||	||||||¡
 d S )Nr   zsocket is not connectedzos.sendfile call failed)r1   Úremove_writerÚ	cancelledÚ_sock_sendfile_update_fileposZ
set_resultrf   r   ÚBlockingIOErrorÚInterruptedErrorÚ_sock_add_cancellation_callbackZ
add_writerrˆ   r3   r8   ZENOTCONNrm   ÚConnectionErrorÚ	__cause__r   rƒ   Zset_exceptionrU   )r   r   Zregistered_fdrb   r1   rŠ   r‹   rŒ   Ú
total_sentÚfdZsentr<   Únew_excr~   r   r   r   rˆ   U  s`    






z1_UnixSelectorEventLoop._sock_sendfile_native_implc             C   s   |dkrt  ||t j¡ d S )Nr   )rf   ÚlseekÚSEEK_SET)r   r1   rŠ   r—   r   r   r   r‘   ™  s    z4_UnixSelectorEventLoop._sock_sendfile_update_fileposc                s   ‡ ‡fdd„}|  |¡ d S )Nc                s&   |   ¡ r"ˆ ¡ }|dkr"ˆ  |¡ d S )Nr,   )r   r1   r   )r   r˜   )r   rb   r   r   Úcbž  s    zB_UnixSelectorEventLoop._sock_add_cancellation_callback.<locals>.cb)Zadd_done_callback)r   r   rb   rœ   r   )r   rb   r   r”     s    z6_UnixSelectorEventLoop._sock_add_cancellation_callback)N)NN)NN)N)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r+   r>   r)   r#   r.   rM   rO   r^   rT   rq   r€   rŽ   rˆ   r‘   r”   Ú__classcell__r   r   )r   r   r   ,   s4   -
  
 

*?Dr   c                   s„   e Zd ZdZd‡ fdd„	Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zddd„Zdd„ Zdd„ Z‡  ZS ) rH   i   Nc                sÔ   t ƒ  |¡ || jd< || _|| _| ¡ | _|| _d| _t	 
| j¡j}t |¡s~t |¡s~t |¡s~d | _d | _d | _tdƒ‚t	 | jd¡ | j | jj| ¡ | j | jj| j| j¡ |d k	rÐ| j tj|d ¡ d S )NrI   Fz)Pipe transport is for pipes/sockets only.)r   r   Ú_extraÚ_loopÚ_piper1   Ú_filenoÚ	_protocolÚ_closingrf   r†   rz   rx   ÚS_ISFIFOry   ÚS_ISCHRr2   Úset_blockingÚ	call_soonÚconnection_madeÚ_add_readerÚ_read_readyr   Ú_set_result_unless_cancelled)r   rv   rI   rJ   rK   rL   Úmode)r   r   r   r   ª  s,    





z_UnixReadPipeTransport.__init__c             C   sÀ   | j jg}| jd kr | d¡ n| jr0| d¡ | d| j› ¡ t| jdd ƒ}| jd k	r|d k	rt 	|| jt
j¡}|r„| d¡ q°| d¡ n | jd k	r¦| d¡ n
| d¡ d d	 |¡¡S )
NÚclosedÚclosingzfd=Ú	_selectorÚpollingÚidleÚopenz<{}>ú )r   r   r¤   Úappendr§   r¥   Úgetattrr£   r	   Ú_test_selector_eventÚ	selectorsZ
EVENT_READÚformatÚjoin)r   r7   r   r´   r   r   r   Ú__repr__Ç  s"    




z_UnixReadPipeTransport.__repr__c          
   C   sº   yt  | j| j¡}W nD ttfk
r,   Y nŠ tk
rX } z|  |d¡ W d d }~X Y n^X |rl| j 	|¡ nJ| j
 ¡ r‚t d| ¡ d| _| j
 | j¡ | j
 | jj¡ | j
 | jd ¡ d S )Nz"Fatal read error on pipe transportz%r was closed by peerT)rf   Úreadr¥   Úmax_sizer’   r“   r3   Ú_fatal_errorr¦   Zdata_receivedr£   Ú	get_debugr   r7   r§   Ú_remove_readerr«   Zeof_receivedÚ_call_connection_lost)r   r*   r<   r   r   r   r®   Ü  s    
z"_UnixReadPipeTransport._read_readyc             C   s   | j  | j¡ d S )N)r£   rÃ   r¥   )r   r   r   r   Úpause_readingî  s    z$_UnixReadPipeTransport.pause_readingc             C   s   | j  | j| j¡ d S )N)r£   r­   r¥   r®   )r   r   r   r   Úresume_readingñ  s    z%_UnixReadPipeTransport.resume_readingc             C   s
   || _ d S )N)r¦   )r   rJ   r   r   r   Úset_protocolô  s    z#_UnixReadPipeTransport.set_protocolc             C   s   | j S )N)r¦   )r   r   r   r   Úget_protocol÷  s    z#_UnixReadPipeTransport.get_protocolc             C   s   | j S )N)r§   )r   r   r   r   Ú
is_closingú  s    z!_UnixReadPipeTransport.is_closingc             C   s   | j s|  d ¡ d S )N)r§   Ú_close)r   r   r   r   r   ý  s    z_UnixReadPipeTransport.closec             C   s.   | j d k	r*tjd| ›t| d | j  ¡  d S )Nzunclosed transport )r   )r¤   r$   r%   r&   r   )r   r   r   r   Ú__del__  s    
z_UnixReadPipeTransport.__del__úFatal error on pipe transportc             C   sZ   t |tƒr4|jtjkr4| j ¡ rLtjd| |dd n| j ||| | j	dœ¡ |  
|¡ d S )Nz%r: %sT)Úexc_info)ÚmessageÚ	exceptionrp   rJ   )rE   r3   r8   ZEIOr£   rÂ   r   ÚdebugÚcall_exception_handlerr¦   rÊ   )r   r<   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   r<   r   r   r   rÊ     s    z_UnixReadPipeTransport._closec             C   s4   z| j  |¡ W d | j ¡  d | _d | _ d | _X d S )N)r¦   Úconnection_lostr¤   r   r£   )r   r<   r   r   r   rÄ     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   rH   ¦  s   
rH   c                   s¢   e Zd Zd%‡ fdd„	Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd&dd „Zd'd!d"„Zd#d$„ Z‡  ZS )(rN   Nc       
         sþ   t ƒ  ||¡ || jd< || _| ¡ | _|| _tƒ | _d| _	d| _
t | j¡j}t |¡}t |¡}t |¡}	|s”|s”|	s”d | _d | _d | _tdƒ‚t | jd¡ | j | jj| ¡ |	sÈ|ràtj d¡sà| j | jj| j| j¡ |d k	rú| j tj|d ¡ d S )NrI   r   Fz?Pipe transport is only for pipes, sockets and character devicesZaix)r   r   r¢   r¤   r1   r¥   r¦   Ú	bytearrayÚ_bufferÚ
_conn_lostr§   rf   r†   rz   rx   r©   r¨   ry   r2   rª   r£   r«   r¬   r    ÚplatformÚ
startswithr­   r®   r   r¯   )
r   rv   rI   rJ   rK   rL   r°   Zis_charZis_fifoZ	is_socket)r   r   r   r   '  s2    





z _UnixWritePipeTransport.__init__c             C   sØ   | j jg}| jd kr | d¡ n| jr0| d¡ | d| j› ¡ t| jdd ƒ}| jd k	r¨|d k	r¨t 	|| jt
j¡}|r„| d¡ n
| d¡ |  ¡ }| d|› ¡ n | jd k	r¾| d¡ n
| d¡ d	 d
 |¡¡S )Nr±   r²   zfd=r³   r´   rµ   zbufsize=r¶   z<{}>r·   )r   r   r¤   r¸   r§   r¥   r¹   r£   r	   rº   r»   ZEVENT_WRITEÚget_write_buffer_sizer¼   r½   )r   r7   r   r´   rZ   r   r   r   r¾   L  s&    





z _UnixWritePipeTransport.__repr__c             C   s
   t | jƒS )N)ÚlenrÔ   )r   r   r   r   rØ   d  s    z-_UnixWritePipeTransport.get_write_buffer_sizec             C   s6   | j  ¡ rt d| ¡ | jr*|  tƒ ¡ n|  ¡  d S )Nz%r was closed by peer)r£   rÂ   r   r7   rÔ   rÊ   ÚBrokenPipeError)r   r   r   r   r®   g  s
    
z#_UnixWritePipeTransport._read_readyc          
   C   s2  t |tttfƒstt|ƒƒ‚t |tƒr.t|ƒ}|s6d S | jsB| jrj| jtj	krXt
 d¡ |  jd7  _d S | jsyt | j|¡}W nV ttfk
r    d}Y n< tk
rÚ } z|  jd7  _|  |d¡ d S d }~X Y nX |t|ƒkrìd S |dkrt|ƒ|d … }| j | j| j¡ |  j|7  _|  ¡  d S )Nz=pipe closed by peer or os.write(pipe, data) raised exception.r   r   z#Fatal write error on pipe transport)rE   ÚbytesrÓ   Ú
memoryviewre   ÚreprrÕ   r§   r   Z!LOG_THRESHOLD_FOR_CONNLOST_WRITESr   ÚwarningrÔ   rf   Úwriter¥   r’   r“   rU   rÁ   rÙ   r£   Z_add_writerÚ_write_readyZ_maybe_pause_protocol)r   r*   Únr<   r   r   r   rß   p  s4    


z_UnixWritePipeTransport.writec          
   C   sö   | j stdƒ‚yt | j| j ¡}W nj ttfk
r:   Y n¸ tk
rŒ } z6| j  ¡  |  j	d7  _	| j
 | j¡ |  |d¡ W d d }~X Y nfX |t| j ƒkrÞ| j  ¡  | j
 | j¡ |  ¡  | jrÚ| j
 | j¡ |  d ¡ d S |dkrò| j d |…= d S )NzData should not be emptyr   z#Fatal write error on pipe transportr   )rÔ   re   rf   rß   r¥   r’   r“   rU   r'   rÕ   r£   Ú_remove_writerrÁ   rÙ   Z_maybe_resume_protocolr§   rÃ   rÄ   )r   rá   r<   r   r   r   rà   ‘  s(    


z$_UnixWritePipeTransport._write_readyc             C   s   dS )NTr   )r   r   r   r   Úcan_write_eof«  s    z%_UnixWritePipeTransport.can_write_eofc             C   sB   | j r
d S | jst‚d| _ | js>| j | j¡ | j | jd ¡ d S )NT)	r§   r¤   re   rÔ   r£   rÃ   r¥   r«   rÄ   )r   r   r   r   Ú	write_eof®  s    
z!_UnixWritePipeTransport.write_eofc             C   s
   || _ d S )N)r¦   )r   rJ   r   r   r   rÇ   ·  s    z$_UnixWritePipeTransport.set_protocolc             C   s   | j S )N)r¦   )r   r   r   r   rÈ   º  s    z$_UnixWritePipeTransport.get_protocolc             C   s   | j S )N)r§   )r   r   r   r   rÉ   ½  s    z"_UnixWritePipeTransport.is_closingc             C   s   | j d k	r| js|  ¡  d S )N)r¤   r§   rä   )r   r   r   r   r   À  s    z_UnixWritePipeTransport.closec             C   s.   | j d k	r*tjd| ›t| d | j  ¡  d S )Nzunclosed transport )r   )r¤   r$   r%   r&   r   )r   r   r   r   rË   Å  s    
z_UnixWritePipeTransport.__del__c             C   s   |   d ¡ d S )N)rÊ   )r   r   r   r   ÚabortË  s    z_UnixWritePipeTransport.abortúFatal error on pipe transportc             C   sN   t |tƒr(| j ¡ r@tjd| |dd n| j ||| | jdœ¡ |  |¡ d S )Nz%r: %sT)rÍ   )rÎ   rÏ   rp   rJ   )	rE   r3   r£   rÂ   r   rÐ   rÑ   r¦   rÊ   )r   r<   rÎ   r   r   r   rÁ   Î  s    

z$_UnixWritePipeTransport._fatal_errorc             C   sF   d| _ | jr| j | j¡ | j ¡  | j | j¡ | j | j|¡ d S )NT)	r§   rÔ   r£   râ   r¥   r'   rÃ   r«   rÄ   )r   r<   r   r   r   rÊ   Ü  s    
z_UnixWritePipeTransport._closec             C   s4   z| j  |¡ W d | j ¡  d | _d | _ d | _X d S )N)r¦   rÒ   r¤   r   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   rN   $  s"   %	!	

rN   c               @   s   e Zd Zdd„ ZdS )rR   c       	   	   K   sŠ   d }|t jkrt ¡ \}}zPt j|f||||d|dœ|—Ž| _|d k	rh| ¡  t| ¡ d|d| j_	d }W d |d k	r„| ¡  | ¡  X d S )NF)rV   rW   rX   rY   Zuniversal_newlinesrZ   Úwb)Ú	buffering)
Ú
subprocessÚPIPErh   Z
socketpairÚPopenÚ_procr   r¶   ÚdetachrW   )	r   r;   rV   rW   rX   rY   rZ   r[   Zstdin_wr   r   r   Ú_startð  s    
z_UnixSubprocessTransport._startN)r   rž   rŸ   rî   r   r   r   r   rR   î  s   rR   c               @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )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 ƒ ‚dS )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   r_   r:   r;   r   r   r   rS     s    	z&AbstractChildWatcher.add_child_handlerc             C   s
   t ƒ ‚dS )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ï   )r   r_   r   r   r   Úremove_child_handler)  s    z)AbstractChildWatcher.remove_child_handlerc             C   s
   t ƒ ‚dS )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ï   )r   rv   r   r   r   Úattach_loop1  s    z AbstractChildWatcher.attach_loopc             C   s
   t ƒ ‚dS )zlClose the watcher.

        This must be called to make sure that any underlying resource is freed.
        N)rï   )r   r   r   r   r   ;  s    zAbstractChildWatcher.closec             C   s
   t ƒ ‚dS )zdEnter the watcher's context and allow starting new processes

        This function must return selfN)rï   )r   r   r   r   Ú	__enter__B  s    zAbstractChildWatcher.__enter__c             C   s
   t ƒ ‚dS )zExit the watcher's contextN)rï   )r   ÚaÚbÚcr   r   r   Ú__exit__H  s    zAbstractChildWatcher.__exit__N)
r   rž   rŸ   r    rS   rð   rñ   r   rò   rö   r   r   r   r   r     s   
r   c               @   sD   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )ÚBaseChildWatcherc             C   s   d | _ i | _d S )N)r£   Ú
_callbacks)r   r   r   r   r   O  s    zBaseChildWatcher.__init__c             C   s   |   d ¡ d S )N)rñ   )r   r   r   r   r   S  s    zBaseChildWatcher.closec             C   s
   t ƒ ‚d S )N)rï   )r   Úexpected_pidr   r   r   Ú_do_waitpidV  s    zBaseChildWatcher._do_waitpidc             C   s
   t ƒ ‚d S )N)rï   )r   r   r   r   Ú_do_waitpid_allY  s    z BaseChildWatcher._do_waitpid_allc             C   s~   |d kst |tjƒst‚| jd k	r<|d kr<| jr<t dt¡ | jd 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)rE   r   ZAbstractEventLoopre   r£   rø   r$   r%   ÚRuntimeWarningr#   r/   ÚSIGCHLDr>   Ú	_sig_chldrû   )r   rv   r   r   r   rñ   \  s    
zBaseChildWatcher.attach_loopc          
   C   sF   y|   ¡  W n4 tk
r@ } z| j d|dœ¡ W d d }~X Y nX d S )Nz$Unknown exception in SIGCHLD handler)rÎ   rÏ   )rû   rU   r£   rÑ   )r   r<   r   r   r   rþ   p  s    zBaseChildWatcher._sig_chldc             C   s2   t  |¡rt  |¡ S t  |¡r*t  |¡S |S d S )N)rf   ÚWIFSIGNALEDÚWTERMSIGÚ	WIFEXITEDÚWEXITSTATUS)r   Ústatusr   r   r   Ú_compute_returncode|  s
    


z$BaseChildWatcher._compute_returncodeN)
r   rž   rŸ   r   r   rú   rû   rñ   rþ   r  r   r   r   r   r÷   M  s   r÷   c                   sP   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
‡  ZS )r   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 )N)rø   r'   r   r   )r   )r   r   r   r   •  s    
zSafeChildWatcher.closec             C   s   | S )Nr   )r   r   r   r   rò   ™  s    zSafeChildWatcher.__enter__c             C   s   d S )Nr   )r   ró   rô   rõ   r   r   r   rö   œ  s    zSafeChildWatcher.__exit__c             G   s.   | j d krtdƒ‚||f| j|< |  |¡ d S )NzICannot add child handler, the child watcher does not have a loop attached)r£   r4   rø   rú   )r   r_   r:   r;   r   r   r   rS   Ÿ  s
    
z"SafeChildWatcher.add_child_handlerc             C   s&   y| j |= dS  tk
r    dS X d S )NTF)rø   r@   )r   r_   r   r   r   rð   ª  s
    z%SafeChildWatcher.remove_child_handlerc             C   s"   xt | jƒD ]}|  |¡ qW d S )N)r"   rø   rú   )r   r_   r   r   r   rû   ±  s    z SafeChildWatcher._do_waitpid_allc             C   sÒ   |dkst ‚yt |tj¡\}}W n( tk
rJ   |}d}t d|¡ Y n0X |dkrXd S |  |¡}| j 	¡ rzt 
d||¡ y| j |¡\}}W n. tk
r¼   | j 	¡ r¸tjd|dd Y nX |||f|žŽ  d S )Nr   éÿ   z8Unknown child process pid %d, will report returncode 255z$process %s exited with returncode %sz'Child watcher got an unexpected pid: %rT)rÍ   )re   rf   ÚwaitpidÚWNOHANGÚChildProcessErrorr   rÞ   r  r£   rÂ   rÐ   rø   Úpopr@   )r   rù   r_   r  r`   r:   r;   r   r   r   rú   ¶  s,    


zSafeChildWatcher._do_waitpid)r   rž   rŸ   r    r   rò   rö   rS   rð   rû   rú   r¡   r   r   )r   r   r   Š  s   	r   c                   sT   e Zd ZdZ‡ fdd„Z‡ fdd„Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
‡  ZS )r   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 )Nr   )r   r   Ú	threadingZLockÚ_lockÚ_zombiesÚ_forks)r   )r   r   r   r   ã  s    

zFastChildWatcher.__init__c                s"   | j  ¡  | j ¡  tƒ  ¡  d S )N)rø   r'   r  r   r   )r   )r   r   r   r   é  s    

zFastChildWatcher.closec          	   C   s$   | j  |  jd7  _| S Q R X d S )Nr   )r  r  )r   r   r   r   rò   î  s    zFastChildWatcher.__enter__c          	   C   sT   | j 8 |  jd8  _| js"| js&d S t| jƒ}| j ¡  W d Q R X t d|¡ d S )Nr   z5Caught subprocesses termination from unknown pids: %s)r  r  r  r5   r'   r   rÞ   )r   ró   rô   rõ   Zcollateral_victimsr   r   r   rö   ô  s    
zFastChildWatcher.__exit__c          	   G   sz   | j stdƒ‚| jd kr tdƒ‚| j: y| j |¡}W n" tk
rZ   ||f| j|< d S X W d Q R X |||f|žŽ  d S )NzMust use the context managerzICannot add child handler, the child watcher does not have a loop attached)	r  re   r£   r4   r  r  r	  r@   rø   )r   r_   r:   r;   r`   r   r   r   rS     s    
z"FastChildWatcher.add_child_handlerc             C   s&   y| j |= dS  tk
r    dS X d S )NTF)rø   r@   )r   r_   r   r   r   rð     s
    z%FastChildWatcher.remove_child_handlerc          	   C   sö   xðyt  dt j¡\}}W n tk
r,   d S X |dkr:d S |  |¡}| jv y| j |¡\}}W nB tk
r¢   | j	rš|| j
|< | j ¡ r˜t d||¡ wd }Y nX | j ¡ r¼t d||¡ W d Q R X |d krÞt d||¡ q|||f|žŽ  qW d S )Nr,   r   z,unknown process %s exited with returncode %sz$process %s exited with returncode %sz8Caught subprocess termination from unknown pid: %d -> %d)rf   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û     s6    





z FastChildWatcher._do_waitpid_all)r   rž   rŸ   r    r   r   rò   rö   rS   rð   rû   r¡   r   r   )r   r   r   Ù  s   	r   c                   sH   e Zd ZdZeZ‡ fdd„Zdd„ Z‡ fdd„Zdd	„ Z	d
d„ Z
‡  ZS )Ú_UnixDefaultEventLoopPolicyz:UNIX event loop policy with a watcher for child processes.c                s   t ƒ  ¡  d | _d S )N)r   r   Ú_watcher)r   )r   r   r   r   K  s    
z$_UnixDefaultEventLoopPolicy.__init__c          	   C   sH   t j8 | jd kr:tƒ | _tt ¡ tjƒr:| j | j	j
¡ W d Q R X d S )N)r   r  r  r   rE   r
  Úcurrent_threadÚ_MainThreadrñ   Ú_localr£   )r   r   r   r   Ú_init_watcherO  s    
z)_UnixDefaultEventLoopPolicy._init_watcherc                s6   t ƒ  |¡ | jdk	r2tt ¡ tjƒr2| j |¡ dS )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  rE   r
  r  r  rñ   )r   rv   )r   r   r   r  W  s    
z*_UnixDefaultEventLoopPolicy.set_event_loopc             C   s   | j dkr|  ¡  | j S )zzGet the watcher for child processes.

        If not yet set, a SafeChildWatcher object is automatically created.
        N)r  r  )r   r   r   r   rP   e  s    
z-_UnixDefaultEventLoopPolicy.get_child_watcherc             C   s4   |dkst |tƒst‚| jdk	r*| j ¡  || _dS )z$Set the watcher for child processes.N)rE   r   re   r  r   )r   r\   r   r   r   Úset_child_watchero  s    

z-_UnixDefaultEventLoopPolicy.set_child_watcher)r   rž   rŸ   r    r   Z_loop_factoryr   r  r  rP   r  r¡   r   r   )r   r   r  G  s   
r  )-r    r8   r„   rf   r»   r/   rh   rx   ré   r    r
  r$   Ú r   r   r   r   r   r   r	   r
   r   Úlogr   Ú__all__rÖ   ÚImportErrorr   ZBaseSelectorEventLoopr   ZReadTransportrH   Z_FlowControlMixinZWriteTransportrN   ZBaseSubprocessTransportrR   r   r÷   r   r   ZBaseDefaultEventLoopPolicyr  r   r   r   r   r   r   Ú<module>   sP   
  |~ JF=On3