ÿØÿà JFIF      ÿÛ „ 	 ( %!1!%)+//.383,7(-.+



-%%-////---/-.+/--+------/------/--0+--/-/-----.-----ÿÀ  ¥2" ÿÄ               ÿÄ J  	     ! 1AQ"aq2‘#BR‚¡ÁÑ3br’¢±Âð$CSƒ²á4c“%DsÓñÿÄ              ÿÄ *        !1AQa‘"2q3±ð#b¡ÿÚ   ? ¼QxJQaÍuò¸Zö Úü8,ÐÚú
"SSn<rçù–´âE—^ªBÖ9À\†¸ÔÁT­ÃÛ5
ëd´³Í#Ý;Þ38œî ¶H£M:wÎ3…³…âpÔF&‚FK¸9„â4àGEõªfÿ ‘ñ(ßw­pŽF|È¥ù®häðÍÑ¶¹‘[ÒinÙW¶ùñY˜Q{›K"išÒ[Ú8žë\F¹@-?v"ÔU”,ìöžkÿ {I‡£šÍ?e
ríV
..............................................................................................................................................................................
.............................................................................                                                  
                                                                                                                                                                                     ÿØÿà JFIF      ÿÛ „ 	 ( %!1!%)+//.383,7(-.+



-%%-////---/-.+/--+------/------/--0+--/-/-----.-----ÿÀ  ¥2" ÿÄ               ÿÄ J  	     ! 1AQ"aq2‘#BR‚¡ÁÑ3br’¢±Âð$CSƒ²á4c“%DsÓñÿÄ              ÿÄ *        !1AQa‘"2q3±ð#b¡ÿÚ   ? ¼QxJQaÍuò¸Zö Úü8,ÐÚú
"SSn<rçù–´âE—^ªBÖ9À\†¸ÔÁT­ÃÛ5
ëd´³Í#Ý;Þ38œî ¶H£M:wÎ3…³…âpÔF&‚FK¸9„â4àGEõªfÿ ‘ñ(ßw­pŽF|È¥ù®häðÍÑ¶¹‘[ÒinÙW¶ùñY˜Q{›K"išÒ[Ú8žë\F¹@-?v"ÔU”,ìöžkÿ {I‡£šÍ?e
ríV
..............................................................................................................................................................................
.............................................................................                                                  
                                                                                                                                                                                     3

  \                 @   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
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dlmZ ddlmZ dddddgZejdkreddd Zy
ejZW n ek
r,   dd ZY nX G dd dejZ e!edrVdd Z"nddl#Z#d d Z"G d!d" d"ej$Z%G d#d$ d$ej&ej'Z(e!ed%rej)Z*nddl#Z#d&d' 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.Z0G d/d0 d0ej1Z2e Z3e2Z4dS )1z2Selector event loop for Unix with signal handling.    N   )base_events)base_subprocess)compat)	constants)
coroutines)events)futures)selector_events)	selectors)
transports)	coroutine)loggerSelectorEventLoopAbstractChildWatcherSafeChildWatcherFastChildWatcherDefaultEventLoopPolicyZwin32z+Signals are not really supported on Windowsc             C   s   dS )zDummy signal handler.N )signumframer   r   +/usr/lib64/python3.6/asyncio/unix_events.py_sighandler_noop%   s    r   c             C   s   | S )Nr   )pathr   r   r   <lambda>.   s    r   c                   s   e Zd ZdZd" fdd	Z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ed%ddZdd ZeddddddZed&ddddd d!Z  ZS )'_UnixSelectorEventLoopzdUnix event loop.

    Adds signal handling and UNIX Domain Socket support to SelectorEventLoop.
    Nc                s   t  j| i | _d S )N)super__init___signal_handlers)selfselector)	__class__r   r   r   7   s    z_UnixSelectorEventLoop.__init__c             C   s   t j S )N)socketZ
socketpair)r   r   r   r   _socketpair;   s    z"_UnixSelectorEventLoop._socketpairc                s^   t  j  tj s2xFt| jD ]}| j| qW n(| jrZtjd| dt	| d | j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%   >   s    
z_UnixSelectorEventLoop.closec             C   s"   x|D ]}|sq| j | qW d S )N)_handle_signal)r   datar   r   r   r   _process_self_dataL   s    
z)_UnixSelectorEventLoop._process_self_datac          +   G   sH  t j|st j|rtd| j| | j  ytj| jj	  W n2 t
tfk
rt } ztt|W Y dd}~X nX tj||| }|| j|< ytj|t tj|d W n tk
rB } zz| j|= | jsytjd W n4 t
tfk
r } ztjd| W Y dd}~X nX |jtjkr0tdj|n W Y dd}~X nX dS )zAdd 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()NFr   zset_wakeup_fd(-1) failed: %szsig {} 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format)r   r.   callbackargsexchandleZnexcr   r   r   add_signal_handlerS   s0    



z)_UnixSelectorEventLoop.add_signal_handlerc             C   s8   | j j|}|dkrdS |jr*| j| n
| j| dS )z2Internal helper that is the actual signal handler.N)r   getZ
_cancelledr)   Z_add_callback_signalsafe)r   r.   rD   r   r   r   r/      s    z%_UnixSelectorEventLoop._handle_signalc          &   C   s   | j | y| j|= W n tk
r*   dS X |tjkr>tj}ntj}ytj|| W n@ tk
r } z$|jtj	krt
dj|n W Y dd}~X nX | jsytjd W n2 ttfk
r } ztjd| W Y dd}~X 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 {} cannot be caughtNr   zset_wakeup_fd(-1) failed: %sTr2   )r4   r   KeyErrorr5   SIGINTdefault_int_handlerSIG_DFLr9   r>   r?   r:   r@   r6   r8   r   r=   )r   r.   ZhandlerrC   r   r   r   r)      s(    

z,_UnixSelectorEventLoop.remove_signal_handlerc             C   sH   t |tstdj|d|  ko,tjk n  sDtdj|tjdS )zInternal 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}r   zsig {} out of range(1, {})N)
isinstanceintr3   r@   r5   NSIGr8   )r   r.   r   r   r   r4      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   rO   rP   rQ   rR   r   r   r   _make_write_pipe_transport   s    z1_UnixSelectorEventLoop._make_write_pipe_transportc	             k   s   t j }
| j }t| |||||||f||d|	}|
j|j | j| y|E d H  W n& tk
r~ } z
|}W Y d d }~X nX d }|d k	r|j  |j	 E d H  |W d Q R X |S )N)rQ   rR   )
r   get_child_watcherZcreate_future_UnixSubprocessTransportadd_child_handlerZget_pid_child_watcher_callback	Exceptionr%   Z_wait)r   rP   rB   shellstdinstdoutstderrbufsizerR   kwargswatcherrQ   transprC   errr   r   r   _make_subprocess_transport   s$    




z1_UnixSelectorEventLoop._make_subprocess_transportc             C   s   | j |j| d S )N)Zcall_soon_threadsafeZ_process_exited)r   pid
returncoderb   r   r   r   rY      s    z._UnixSelectorEventLoop._child_watcher_callback)sslsockserver_hostnamec            c   s  |d kst |tst|r,|d kr<tdn|d k	r<td|d k	r|d k	rTtdtjtjtjd}y |jd | j||E d H  W q   |j	   Y qX nB|d krtd|j
tjkstj|j rtdj||jd | j||||E d H \}}||fS )Nz/you have to pass server_hostname when using sslz+server_hostname is only meaningful with sslz3path and sock can not be specified at the same timer   Fzno path and sock were specifiedz2A UNIX Domain Stream Socket was expected, got {!r})rK   r;   AssertionErrorr8   r"   AF_UNIXSOCK_STREAMsetblockingZsock_connectr%   familyr   _is_stream_sockettyper@   Z_create_connection_transport)r   protocol_factoryr   rg   rh   ri   	transportrP   r   r   r   create_unix_connection   s:    


z-_UnixSelectorEventLoop.create_unix_connectiond   )rh   backlogrg   c      
   !   C   s  t |trtd|d k	r0|d k	r,tdt|}tjtjtj}|d d
kry tj	t
j|jrnt
j| W nB tk
r   Y n0 tk
r } ztjd|| W Y d d }~X nX y|j| W nj tk
r } z8|j  |jtjkrdj|}ttj|d n W Y d d }~X n   |j   Y nX n>|d krBtd|jtjks`tj|j rntdj|tj| |g}	|j| |jd	 | j||||	 |	S )Nz*ssl argument must be an SSLContext or Nonez3path and sock can not be specified at the same timer    z2Unable to check or remove stale UNIX socket %r: %rzAddress {!r} is already in usez-path was not specified, and no sock specifiedz2A UNIX Domain Stream Socket was expected, got {!r}F)r   rv   )rK   boolr3   r8   _fspathr"   rk   rl   statS_ISSOCKosst_moderemoveFileNotFoundErrorr9   r   errorZbindr%   r>   Z
EADDRINUSEr@   rn   r   ro   rp   ZServerZlistenrm   Z_start_serving)
r   rq   r   rh   ru   rg   rc   rC   msgZserverr   r   r   create_unix_server  sP    

 




z)_UnixSelectorEventLoop.create_unix_server)N)NN)NN)N)N)__name__
__module____qualname____doc__r   r#   r%   r1   rE   r/   r)   r4   rS   rU   r   rd   rY   rs   r   __classcell__r   r   )r!   r   r   1   s,   -
  
 
%r   set_blockingc             C   s   t j| d d S )NF)r{   r   )fdr   r   r   _set_nonblockingB  s    r   c             C   s,   t j | t j}|tjB }t j | t j| d S )N)fcntlZF_GETFLr{   
O_NONBLOCKZF_SETFL)r   flagsr   r   r   r   G  s    
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ejrhdd Zd!ddZdd Zdd Z  ZS )"rN      i   Nc                s   t  j| || jd< || _|| _|j | _|| _d| _t	j
| jj}tj|pbtj|pbtj|s~d | _d | _d | _tdt| j | jj| jj|  | jj| jj| j| j |d k	r| jjtj|d  d S )NrO   Fz)Pipe transport is for pipes/sockets only.)r   r   _extra_loop_piper7   _fileno	_protocol_closingr{   fstatr|   ry   S_ISFIFOrz   S_ISCHRr8   r   	call_soonconnection_made_add_reader_read_readyr	   _set_result_unless_cancelled)r   looprO   rP   rQ   rR   mode)r!   r   r   r   Q  s,    






z_UnixReadPipeTransport.__init__c             C   s   | j jg}| jd kr |jd n| jr0|jd |jd| j  t| jdd }| jd k	r|d k	rtj	|| jt
j}|r|jd q|jd n | jd k	r|jd n
|jd dd	j| S )
Nclosedclosingzfd=%s	_selectorpollingidleopenz<%s> )r!   r   r   appendr   r   getattrr   r
   _test_selector_eventr   Z
EVENT_READjoin)r   r=   r    r   r   r   r   __repr__n  s$    




z_UnixReadPipeTransport.__repr__c             C   s   yt j| j| j}W nD ttfk
r,   Y n tk
rX } z| j|d W Y d d }~X n^X |rl| jj	| nJ| j
j rtjd|  d| _| j
j| j | j
j| jj | j
j| jd  d S )Nz"Fatal read error on pipe transportz%r was closed by peerT)r{   readr   max_sizeBlockingIOErrorInterruptedErrorr9   _fatal_errorr   Zdata_receivedr   	get_debugr   r=   r   _remove_readerr   Zeof_received_call_connection_lost)r   r0   rC   r   r   r   r     s    
z"_UnixReadPipeTransport._read_readyc             C   s   | j j| j d S )N)r   r   r   )r   r   r   r   pause_reading  s    z$_UnixReadPipeTransport.pause_readingc             C   s   | j 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   rP   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| j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 j  d S )Nzunclosed transport %r)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j rLtjd| |dd n| jj||| | j	d | j
| d S )Nz%r: %sT)exc_info)message	exceptionrr   rP   )rK   r9   r>   ZEIOr   r   r   debugcall_exception_handlerr   r   )r   rC   r   r   r   r   r     s    
z#_UnixReadPipeTransport._fatal_errorc             C   s(   d| _ | jj| j | jj| j| d S )NT)r   r   r   r   r   r   )r   rC   r   r   r   r     s    z_UnixReadPipeTransport._closec             C   s4   z| j j| W d | jj  d | _d | _ d | _X d S )N)r   connection_lostr   r%   r   )r   rC   r   r   r   r     s    
z,_UnixReadPipeTransport._call_connection_losti   )NN)r   )r   r   r   r   r   r   r   r   r   r   r   r   r%   r   PY34r   r   r   r   r   r   r   )r!   r   rN   M  s   
rN   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ejr|dd Zdd Zd&dd Zd'd!d"Zd#d$ Z  ZS )(rT   Nc       
         s   t  j|| || jd< || _|j | _|| _t | _d| _	d| _
tj| jj}tj|}tj|}tj|}	|px|px|	sd | _d | _d | _tdt| j | jj| jj|  |	s|rtjjd r| jj| jj| j| j |d k	r| jjtj|d  d S )NrO   r   Fz?Pipe transport is only for pipes, sockets and character devicesaix)r   r   r   r   r7   r   r   	bytearray_buffer
_conn_lostr   r{   r   r|   ry   r   r   rz   r8   r   r   r   r   r&   platform
startswithr   r   r	   r   )
r   r   rO   rP   rQ   rR   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 |jd n| jr0|jd |jd| j  t| jdd }| jd k	r|d k	rtj	|| jt
j}|r|jd n
|jd | j }|jd|  n | jd k	r|jd n
|jd d	d
j| S )Nr   r   zfd=%sr   r   r   z
bufsize=%sr   z<%s>r   )r!   r   r   r   r   r   r   r   r
   r   r   ZEVENT_WRITEget_write_buffer_sizer   )r   r=   r    r   r_   r   r   r   r     s(    





z _UnixWritePipeTransport.__repr__c             C   s
   t | jS )N)lenr   )r   r   r   r   r     s    z-_UnixWritePipeTransport.get_write_buffer_sizec             C   s6   | j j rtjd|  | jr*| jt  n| j  d S )Nz%r was closed by peer)r   r   r   r=   r   r   BrokenPipeError)r   r   r   r   r     s
    
z#_UnixWritePipeTransport._read_readyc             C   s0  t |tttfstt|t |tr.t|}|s6d S | jsB| jrj| jtj	krXt
jd |  jd7  _d S | jsytj| j|}W nT ttfk
r   d}Y n: tk
r } z|  jd7  _| j|d d S d }~X nX |t|krd S |dkrt||d  }| jj| j| j |  j|7  _| j  d S )Nz=pipe closed by peer or os.write(pipe, data) raised exception.r   r   z#Fatal write error on pipe transport)rK   bytesr   
memoryviewrj   reprr   r   r   Z!LOG_THRESHOLD_FOR_CONNLOST_WRITESr   warningr   r{   writer   r   r   rZ   r   r   r   Z_add_writer_write_readyZ_maybe_pause_protocol)r   r0   nrC   r   r   r   r     s4    


z_UnixWritePipeTransport.writec             C   s   | j stdytj| j| j }W nj ttfk
r:   Y n tk
r } z8| j j  |  j	d7  _	| j
j| j | j|d W Y d d }~X nfX |t| j kr| j j  | j
j| j | j  | jr| j
j| 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   rj   r{   r   r   r   r   rZ   r-   r   r   _remove_writerr   r   Z_maybe_resume_protocolr   r   r   )r   r   rC   r   r   r   r   >  s(    


z$_UnixWritePipeTransport._write_readyc             C   s   dS )NTr   )r   r   r   r   can_write_eofX  s    z%_UnixWritePipeTransport.can_write_eofc             C   sB   | j r
d S | jstd| _ | js>| jj| j | jj| jd  d S )NT)	r   r   rj   r   r   r   r   r   r   )r   r   r   r   	write_eof[  s    
z!_UnixWritePipeTransport.write_eofc             C   s
   || _ d S )N)r   )r   rP   r   r   r   r   d  s    z$_UnixWritePipeTransport.set_protocolc             C   s   | j S )N)r   )r   r   r   r   r   g  s    z$_UnixWritePipeTransport.get_protocolc             C   s   | j S )N)r   )r   r   r   r   r   j  s    z"_UnixWritePipeTransport.is_closingc             C   s   | j d k	r| j r| j  d S )N)r   r   r   )r   r   r   r   r%   m  s    z_UnixWritePipeTransport.closec             C   s,   | j d k	r(tjd|  t| d | j j  d S )Nzunclosed transport %r)r$   )r   r*   r+   r,   r%   )r   r   r   r   r   v  s    
z_UnixWritePipeTransport.__del__c             C   s   | j d  d S )N)r   )r   r   r   r   abort|  s    z_UnixWritePipeTransport.abortFatal error on pipe transportc             C   sP   t |tjr*| jj rBtjd| |dd n| jj||| | jd | j	| d S )Nz%r: %sT)r   )r   r   rr   rP   )
rK   r   Z_FATAL_ERROR_IGNOREr   r   r   r   r   r   r   )r   rC   r   r   r   r   r     s    
z$_UnixWritePipeTransport._fatal_errorc             C   sF   d| _ | jr| jj| j | jj  | jj| j | jj| j| d S )NT)	r   r   r   r   r   r-   r   r   r   )r   rC   r   r   r   r     s    
z_UnixWritePipeTransport._closec             C   s4   z| j j| W d | jj  d | _d | _ d | _X d S )N)r   r   r   r%   r   )r   rC   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   rT     s$   %	!	

rT   set_inheritablec             C   sN   t tdd}tj| tj}|s4tj| tj||B  ntj| tj|| @  d S )NZ
FD_CLOEXECr   )r   r   ZF_GETFDZF_SETFD)r   ZinheritableZcloexec_flagoldr   r   r   _set_inheritable  s
    r   c               @   s   e Zd Zdd ZdS )rW   c       	   	   K   sv   d }|t jkr*| jj \}}t|j d t j|f||||d|d|| _|d k	rr|j  t	|j
 d|d| j_d S )NF)r[   r\   r]   r^   Zuniversal_newlinesr_   wb)	buffering)
subprocessPIPEr   r#   r   r7   Popen_procr%   r   detachr\   )	r   rB   r[   r\   r]   r^   r_   r`   Zstdin_wr   r   r   _start  s    
z_UnixSubprocessTransport._startN)r   r   r   r   r   r   r   r   rW     s   rW   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   re   rA   rB   r   r   r   rX     s    	z&AbstractChildWatcher.add_child_handlerc             C   s
   t  dS )zRemoves 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   re   r   r   r   remove_child_handler  s    z)AbstractChildWatcher.remove_child_handlerc             C   s
   t  dS )zAttach 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   r   r   r   r   attach_loop  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__  s    zAbstractChildWatcher.__enter__c             C   s
   t  dS )zExit the watcher's contextN)r   )r   abcr   r   r   __exit__	  s    zAbstractChildWatcher.__exit__N)
r   r   r   r   rX   r   r   r%   r   r   r   r   r   r   r     s   
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     s    zBaseChildWatcher.__init__c             C   s   | j d  d S )N)r   )r   r   r   r   r%     s    zBaseChildWatcher.closec             C   s
   t  d S )N)r   )r   expected_pidr   r   r   _do_waitpid  s    zBaseChildWatcher._do_waitpidc             C   s
   t  d S )N)r   )r   r   r   r   _do_waitpid_all  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jdt | jd k	rT| jj	t
j || _|d k	rz|jt
j| j | j  d S )NzCA loop is being detached from a child watcher with pending handlers)rK   r   ZAbstractEventLooprj   r   r   r*   r+   RuntimeWarningr)   r5   SIGCHLDrE   	_sig_chldr   )r   r   r   r   r   r     s    
zBaseChildWatcher.attach_loopc             C   sF   y| j   W n4 tk
r@ } z| jjd|d W Y d d }~X nX d S )Nz$Unknown exception in SIGCHLD handler)r   r   )r   rZ   r   r   )r   rC   r   r   r   r   1  s    zBaseChildWatcher._sig_chldc             C   s2   t j|rt j| S t j|r*t j|S |S d S )N)r{   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     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 j  t j  d S )N)r   r-   r   r%   )r   )r!   r   r   r%   V  s    
zSafeChildWatcher.closec             C   s   | S )Nr   )r   r   r   r   r   Z  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|< | j| d S )NzICannot add child handler, the child watcher does not have a loop attached)r   r:   r   r   )r   re   rA   rB   r   r   r   rX   `  s
    
z"SafeChildWatcher.add_child_handlerc             C   s&   y| j |= dS  tk
r    dS X d S )NTF)r   rG   )r   re   r   r   r   r   k  s
    z%SafeChildWatcher.remove_child_handlerc             C   s"   xt | jD ]}| j| qW d S )N)r(   r   r   )r   re   r   r   r   r   r  s    z SafeChildWatcher._do_waitpid_allc             C   s   |dkst ytj|tj\}}W n( tk
rJ   |}d}tjd| Y n0X |dkrXd S | j|}| jj	 rztj
d|| y| jj|\}}W n. tk
r   | jj	 rt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   )rj   r{   waitpidWNOHANGChildProcessErrorr   r   r   r   r   r   r   poprG   )r   r   re   r   rf   rA   rB   r   r   r   r   w  s,    


zSafeChildWatcher._do_waitpid)r   r   r   r   r%   r   r   rX   r   r   r   r   r   r   )r!   r   r   K  s   	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  j  tj | _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  | jj  t j  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   sV   | j : |  jd8  _| js$| j r(d S t| j}| jj  W d Q R X tjd| d S )Nr   z5Caught subprocesses termination from unknown pids: %s)r   r   r   r;   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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   rj   r   r:   r   r   r   rG   r   )r   re   rA   rB   rf   r   r   r   rX     s    
z"FastChildWatcher.add_child_handlerc             C   s&   y| j |= dS  tk
r    dS X d S )NTF)r   rG   )r   re   r   r   r   r     s
    z%FastChildWatcher.remove_child_handlerc             C   s   xyt jdt j\}}W n tk
r,   d S X |dkr:d S | j|}| jv y| jj|\}}W nB tk
r   | j	r|| j
|< | jj rtjd|| wd }Y nX | jj rtjd|| W d Q R X |d krtj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 -> %dr2   )r{   r   r   r   r   r   r   r   rG   r   r   r   r   r   r   r   )r   re   r   rf   rA   rB   r   r   r   r     s6    





z FastChildWatcher._do_waitpid_all)r   r   r   r   r   r%   r   r   rX   r   r   r   r   r   )r!   r   r     s   	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  j  d | _d S )N)r   r   _watcher)r   )r!   r   r   r     s    
z$_UnixDefaultEventLoopPolicy.__init__c          
   C   sH   t j8 | jd kr:t | _ttj tjr:| jj| j	j
 W d Q R X d S )N)r   r   r  r   rK   r   current_thread_MainThreadr   _localr   )r   r   r   r   _init_watcher  s    
z)_UnixDefaultEventLoopPolicy._init_watcherc                s6   t  j| | jdk	r2ttj tjr2| jj| dS )zSet 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  rK   r   r  r  r   )r   r   )r!   r   r   r    s    
z*_UnixDefaultEventLoopPolicy.set_event_loopc             C   s   | j dkr| j  | 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   rV   &  s    
z-_UnixDefaultEventLoopPolicy.get_child_watcherc             C   s4   |dkst |tst| jdk	r*| jj  || _dS )z$Set the watcher for child processes.N)rK   r   rj   r  r%   )r   ra   r   r   r   set_child_watcher0  s    

z-_UnixDefaultEventLoopPolicy.set_child_watcher)r   r   r   r   r   Z_loop_factoryr   r  r  rV   r  r   r   r   )r!   r   r     s   
r   )5r   r>   r{   r5   r"   ry   r   r&   r   r*    r   r   r   r   r   r   r	   r
   r   r   r   logr   __all__r   ImportErrorr   fspathrx   AttributeErrorZBaseSelectorEventLoopr   hasattrr   r   ZReadTransportrN   Z_FlowControlMixinZWriteTransportrT   r   r   ZBaseSubprocessTransportrW   r   r   r   r   ZBaseDefaultEventLoopPolicyr   r   r   r   r   r   r   <module>   sn   

  
  O
F=On2