o
     JAfL                     @   s$  d Z 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zddl	Z	W n e
y1   dZ	Y nw 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 ZG dd dejZG dd dejejZG dd deZG dd deZdS )zEvent loop using a selector and related classes.

A selector is a "notify-when-ready" multiplexer.  For a subclass which
also includes support for signal handling, see the unix_events sub-module.
)BaseSelectorEventLoop    N   )base_events)	constants)events)futures)	protocols)sslproto)
transports)trsock)loggerc                 C   s2   z|  |}W n
 ty   Y dS w t|j|@ S NF)get_keyKeyErrorboolr   )selectorfdeventkey r   ./usr/lib/python3.10/asyncio/selector_events.py_test_selector_event    s   r   c                       s  e Zd ZdZdS fdd	ZdSdddddZ	dSddddejd	d
dZ	dTddZ	 fddZ
dd Zdd Zdd Zdd Zdd ZdddejfddZdddejfddZddej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/d0 Zd1d2 Zd3d4 ZdSd5d6Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAdB Z#dCdD Z$dSdEdFZ%dGdH Z&dIdJ Z'dKdL Z(dMdN Z)dOdP Z*dQdR Z+  Z,S )Ur   zJSelector event loop.

    See events.EventLoop for API specification.
    Nc                    sF   t    |d u rt }td|jj || _| 	  t
 | _d S )NzUsing selector: %s)super__init__	selectorsDefaultSelectorr   debug	__class____name__	_selector_make_self_pipeweakrefWeakValueDictionary_transports)selfr   r   r   r   r   1   s   
zBaseSelectorEventLoop.__init__extraserverc                C      t | |||||S N)_SelectorSocketTransport)r$   sockprotocolwaiterr'   r(   r   r   r   _make_socket_transport;   s   
z,BaseSelectorEventLoop._make_socket_transportF)server_sideserver_hostnamer'   r(   ssl_handshake_timeoutc             	   C   s0   t j| ||||||	d}
t| ||
||d |
jS )N)r2   r&   )r	   ZSSLProtocolr+   Z_app_transport)r$   Zrawsockr-   
sslcontextr.   r0   r1   r'   r(   r2   Zssl_protocolr   r   r   _make_ssl_transport@   s   z)BaseSelectorEventLoop._make_ssl_transportc                 C   r)   r*   )_SelectorDatagramTransport)r$   r,   r-   addressr.   r'   r   r   r   _make_datagram_transportM   s   z.BaseSelectorEventLoop._make_datagram_transportc                    sP   |   rtd|  rd S |   t   | jd ur&| j  d | _d S d S )Nz!Cannot close a running event loop)Z
is_runningRuntimeError	is_closed_close_self_piper   closer   r$   r%   r   r   r;   R   s   



zBaseSelectorEventLoop.closec                 C   sB   |  | j  | j  d | _| j  d | _|  jd8  _d S )Nr   )_remove_reader_ssockfilenor;   _csock_internal_fdsr<   r   r   r   r:   ]   s   

z&BaseSelectorEventLoop._close_self_pipec                 C   sN   t  \| _| _| jd | jd |  jd7  _| | j | j d S )NFr   )	socket
socketpairr>   r@   setblockingrA   _add_readerr?   _read_from_selfr<   r   r   r   r    e   s
   z%BaseSelectorEventLoop._make_self_pipec                 C   s   d S r*   r   r$   datar   r   r   _process_self_datam      z(BaseSelectorEventLoop._process_self_datac                 C   sN   	 z| j d}|sW d S | | W n ty   Y q  ty%   Y d S w q)NTi   )r>   recvrI   InterruptedErrorBlockingIOErrorrG   r   r   r   rF   p   s   z%BaseSelectorEventLoop._read_from_selfc                 C   sR   | j }|d u r	d S z|d W d S  ty(   | jr%tjddd Y d S Y d S w )N    z3Fail to write a null byte into the self-pipe socketTexc_info)r@   sendOSError_debugr   r   )r$   Zcsockr   r   r   _write_to_self|   s   z$BaseSelectorEventLoop._write_to_selfd   c              
   C   s"   |  | | j|||||| d S r*   )rE   r?   _accept_connection)r$   protocol_factoryr,   r3   r(   backlogr2   r   r   r   _start_serving   s   
z$BaseSelectorEventLoop._start_servingc                 C   s  t |D ]}}z| \}}	| jrtd||	| |d W nO tttfy,   Y  d S  t	ym }
 z6|
j
t
jt
jt
jt
jfv rb| d|
t|d | |  | tj| j|||||| n W Y d }
~
qd }
~
ww d|	i}| ||||||}| | qd S )Nz#%r got a new connection from %r: %rFz&socket.accept() out of system resource)message	exceptionrB   peername)rangeacceptrS   r   r   rD   rM   rL   ConnectionAbortedErrorrR   errnoZEMFILEZENFILEZENOBUFSZENOMEMcall_exception_handlerr   TransportSocketr=   r?   Z
call_laterr   ZACCEPT_RETRY_DELAYrY   _accept_connection2Zcreate_task)r$   rW   r,   r3   r(   rX   r2   _connaddrexcr'   r^   r   r   r   rV      sH   
z(BaseSelectorEventLoop._accept_connectionc              
      s   d }d }z7| }|   }	|r| j||||	d|||d}n
| j|||	||d}z	|	I d H  W W d S  ty<   |   w  ttfyF     ty| }
 z+| jrqd|
d}|d ur]||d< |d ure||d< | | W Y d }
~
d S W Y d }
~
d S d }
~
ww )NT)r.   r0   r'   r(   r2   )r.   r'   r(   z3Error on transport creation for incoming connection)rZ   r[   r-   	transport)	create_futurer4   r/   BaseExceptionr;   
SystemExitKeyboardInterruptrS   ra   )r$   rW   re   r'   r3   r(   r2   r-   rh   r.   rg   contextr   r   r   rc      sJ   z)BaseSelectorEventLoop._accept_connection2c              
   C   s   |}t |ts"zt| }W n tttfy!   td|d w z| j| }W n
 ty3   Y d S w | sBt	d|d|d S )NzInvalid file object: zFile descriptor z is used by transport )

isinstanceintr?   AttributeError	TypeError
ValueErrorr#   r   
is_closingr8   )r$   r   r?   rh   r   r   r   _ensure_fd_no_transport   s&   
z-BaseSelectorEventLoop._ensure_fd_no_transportc           	   	   G   s   |    t||| d }z| j|}W n ty)   | j|tj|d f Y |S w |j|j	}\}}| j
||tjB ||f |d urH|  |S r*   )_check_closedr   Handler   r   r   registerr   
EVENT_READrH   modifycancel	r$   r   callbackargshandler   maskreaderwriterr   r   r   rE      "   z!BaseSelectorEventLoop._add_readerc                 C   s   |   rdS z| j|}W n
 ty   Y dS w |j|j}\}}|tj M }|s1| j| n
| j	||d |f |d urE|
  dS dS )NFT)r9   r   r   r   r   rH   r   rx   
unregisterry   rz   r$   r   r   r   r   r   r   r   r   r=     s    z$BaseSelectorEventLoop._remove_readerc           	   	   G   s   |    t||| d }z| j|}W n ty)   | j|tjd |f Y |S w |j|j	}\}}| j
||tjB ||f |d urH|  |S r*   )ru   r   rv   r   r   r   rw   r   EVENT_WRITErH   ry   rz   r{   r   r   r   _add_writer!  r   z!BaseSelectorEventLoop._add_writerc                 C   s   |   rdS z| j|}W n
 ty   Y dS w |j|j}\}}|tj M }|s1| j| n
| j	|||df |durE|
  dS dS )Remove a writer callback.FNT)r9   r   r   r   r   rH   r   r   r   ry   rz   r   r   r   r   _remove_writer1  s    z$BaseSelectorEventLoop._remove_writerc                 G   "   |  | | j||g|R   dS )zAdd a reader callback.N)rt   rE   r$   r   r|   r}   r   r   r   
add_readerH     
z BaseSelectorEventLoop.add_readerc                 C      |  | | |S )zRemove a reader callback.)rt   r=   r$   r   r   r   r   remove_readerM     

z#BaseSelectorEventLoop.remove_readerc                 G   r   )zAdd a writer callback..N)rt   r   r   r   r   r   
add_writerR  r   z BaseSelectorEventLoop.add_writerc                 C   r   )r   )rt   r   r   r   r   r   remove_writerW  r   z#BaseSelectorEventLoop.remove_writerc              	         t | | jr| dkrtdz||W S  ttfy$   Y nw |  }|	 }| 
| | || j|||}|tj| j||d |I dH S )zReceive data from the socket.

        The return value is a bytes object representing the data received.
        The maximum amount of data to be received at once is specified by
        nbytes.
        r   the socket must be non-blockingr~   N)r   _check_ssl_socketrS   
gettimeoutrr   rK   rM   rL   ri   r?   rt   rE   
_sock_recvadd_done_callback	functoolspartial_sock_read_done)r$   r,   nfutr   r~   r   r   r   	sock_recv\  s"   


zBaseSelectorEventLoop.sock_recvc                 C   "   |d u s|  s| | d S d S r*   )	cancelledr   r$   r   r   r~   r   r   r   r   r     z%BaseSelectorEventLoop._sock_read_donec              
   C      |  rd S z||}W n, ttfy   Y d S  ttfy"     ty9 } z|| W Y d }~d S d }~ww || d S r*   )	donerK   rM   rL   rk   rl   rj   set_exception
set_result)r$   r   r,   r   rH   rg   r   r   r   r   v  s   z BaseSelectorEventLoop._sock_recvc              	      r   )zReceive data from the socket.

        The received data is written into *buf* (a writable buffer).
        The return value is the number of bytes written.
        r   r   r   N)r   r   rS   r   rr   	recv_intorM   rL   ri   r?   rt   rE   _sock_recv_intor   r   r   r   )r$   r,   bufr   r   r~   r   r   r   sock_recv_into  s"   


z$BaseSelectorEventLoop.sock_recv_intoc              
   C   r   r*   )	r   r   rM   rL   rk   rl   rj   r   r   )r$   r   r,   r   nbytesrg   r   r   r   r     s   z%BaseSelectorEventLoop._sock_recv_intoc              	      s   t | | jr| dkrtdz||}W n ttfy'   d}Y nw |t|kr0dS | 	 }|
 }| | | || j||t||g}|tj| j||d |I dH S )a  Send data to the socket.

        The socket must be connected to a remote socket. This method continues
        to send data from data until either all data has been sent or an
        error occurs. None is returned on success. On error, an exception is
        raised, and there is no way to determine how much data, if any, was
        successfully processed by the receiving end of the connection.
        r   r   Nr   )r   r   rS   r   rr   rQ   rM   rL   lenri   r?   rt   r   _sock_sendall
memoryviewr   r   r   _sock_write_done)r$   r,   rH   r   r   r   r~   r   r   r   sock_sendall  s*   
	


z"BaseSelectorEventLoop.sock_sendallc              
   C   s   |  rd S |d }z|||d  }W n, ttfy!   Y d S  ttfy*     tyA } z|| W Y d }~d S d }~ww ||7 }|t|krS|	d  d S ||d< d S )Nr   )
r   rQ   rM   rL   rk   rl   rj   r   r   r   )r$   r   r,   viewposstartr   rg   r   r   r   r     s$   
z#BaseSelectorEventLoop._sock_sendallc                    s   t | | jr| dkrtd|jtjks"t jr;|jtj	kr;| j
||j|j|j| dI dH }|d \}}}}}|  }| ||| |I dH S )zTConnect to a remote socket at address.

        This method is a coroutine.
        r   r   )familytypeprotoloopN)r   r   rS   r   rr   r   rB   AF_INETZ	_HAS_IPv6AF_INET6Z_ensure_resolvedr   r   ri   _sock_connect)r$   r,   r6   Zresolvedrd   r   r   r   r   sock_connect  s    

z"BaseSelectorEventLoop.sock_connectc              
   C   s   |  }z|| W nG ttfy2   | | | || j|||}|tj	| j
||d Y d S  ttfy;     tyR } z|| W Y d }~d S d }~ww |d  d S )Nr   )r?   connectrM   rL   rt   r   _sock_connect_cbr   r   r   r   rk   rl   rj   r   r   )r$   r   r,   r6   r   r~   rg   r   r   r   r     s$   

z#BaseSelectorEventLoop._sock_connectc                 C   r   r*   )r   r   r   r   r   r   r     r   z&BaseSelectorEventLoop._sock_write_donec              
   C   s   |  rd S z|tjtj}|dkrt|d| W n, ttfy(   Y d S  tt	fy1     t
yH } z|| W Y d }~d S d }~ww |d  d S )Nr   zConnect call failed )r   
getsockoptrB   
SOL_SOCKETSO_ERRORrR   rM   rL   rk   rl   rj   r   r   )r$   r   r,   r6   errrg   r   r   r   r     s    z&BaseSelectorEventLoop._sock_connect_cbc                    sD   t | | jr| dkrtd|  }| || |I dH S )aW  Accept a connection.

        The socket must be bound to an address and listening for connections.
        The return value is a pair (conn, address) where conn is a new socket
        object usable to send and receive data on the connection, and address
        is the address bound to the socket on the other end of the connection.
        r   r   N)r   r   rS   r   rr   ri   _sock_accept)r$   r,   r   r   r   r   sock_accept"  s   

z!BaseSelectorEventLoop.sock_acceptc              
   C   s   |  }z| \}}|d W nF ttfy7   | | | || j||}|t	j
| j||d Y d S  ttfy@     tyW } z|| W Y d }~d S d }~ww |||f d S )NFr   )r?   r^   rD   rM   rL   rt   rE   r   r   r   r   r   rk   rl   rj   r   r   )r$   r   r,   r   re   r6   r~   rg   r   r   r   r   1  s"   

z"BaseSelectorEventLoop._sock_acceptc              	      s   | j |j= | }|  | I d H  z| j|j|||ddI d H W |  |r.|  || j |j< S |  |r?|  || j |j< w )NF)fallback)	r#   _sock_fd
is_readingpause_reading_make_empty_waiterZsock_sendfile_sock_reset_empty_waiterresume_reading)r$   Ztranspfileoffsetcountr   r   r   r   _sendfile_nativeB  s"   
z&BaseSelectorEventLoop._sendfile_nativec                 C   s   |D ];\}}|j |j}\}}|tj@ r&|d ur&|jr!| | n| | |tj@ r=|d ur=|jr8| | q| | qd S r*   )	fileobjrH   r   rx   Z
_cancelledr=   Z_add_callbackr   r   )r$   Z
event_listr   r   r   r   r   r   r   r   _process_eventsP  s   

z%BaseSelectorEventLoop._process_eventsc                 C   s   |  |  |  d S r*   )r=   r?   r;   )r$   r,   r   r   r   _stop_serving^  s   z#BaseSelectorEventLoop._stop_servingr*   NNN)-r   
__module____qualname____doc__r   r/   r   ZSSL_HANDSHAKE_TIMEOUTr4   r7   r;   r:   r    rI   rF   rT   rY   rV   rc   rt   rE   r=   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r   r   r%   r   r   +   sl    


	
.
)

r   c                       s   e Zd ZdZ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ejfddZdddZdd Zdd Zdd Zdd Z  ZS ) _SelectorTransporti   Nc                    s   t  || t|| jd< z	| | jd< W n ty&   d | jd< Y nw d| jvrEz	| | jd< W n tj	yD   d | jd< Y nw || _
| | _d| _| | || _|  | _d| _d| _| jd urm| j  | |j| j< d S )NrB   Zsocknamer\   Fr   )r   r   r   rb   _extragetsocknamerR   getpeernamerB   errorr   r?   r   _protocol_connectedset_protocol_server_buffer_factory_buffer
_conn_lost_closingZ_attachr#   )r$   r   r,   r-   r'   r(   r%   r   r   r   o  s0   





z_SelectorTransport.__init__c                 C   s   | j jg}| jd u r|d n| jr|d |d| j  | jd urc| j sct| jj	| jt
j}|r=|d n|d t| jj	| jt
j}|rQd}nd}|  }|d| d	| d
 dd|S )Nclosedclosingzfd=zread=pollingz	read=idlepollingZidlezwrite=<z
, bufsize=>z<{}> )r   r   r   appendr   r   _loopr9   r   r   r   rx   r   get_write_buffer_sizeformatjoin)r$   infor   statebufsizer   r   r   __repr__  s.   



z_SelectorTransport.__repr__c                 C   s   |  d  d S r*   )_force_closer<   r   r   r   abort  s   z_SelectorTransport.abortc                 C   s   || _ d| _d S NT)	_protocolr   r$   r-   r   r   r   r     s   
z_SelectorTransport.set_protocolc                 C      | j S r*   )r   r<   r   r   r   get_protocol     z_SelectorTransport.get_protocolc                 C   r   r*   )r   r<   r   r   r   rs     r   z_SelectorTransport.is_closingc                 C   sX   | j rd S d| _ | j| j | js*|  jd7  _| j| j | j| jd  d S d S NTr   )	r   r   r=   r   r   r   r   	call_soon_call_connection_lostr<   r   r   r   r;     s   z_SelectorTransport.closec                 C   s0   | j d ur|d| t| d | j   d S d S )Nzunclosed transport )source)r   ResourceWarningr;   )r$   _warnr   r   r   __del__  s   
z_SelectorTransport.__del__Fatal error on transportc                 C   sN   t |tr| j rtjd| |dd n| j||| | jd | | d S )Nz%r: %sTrO   )rZ   r[   rh   r-   )	rn   rR   r   	get_debugr   r   ra   r   r   )r$   rg   rZ   r   r   r   _fatal_error  s   

z_SelectorTransport._fatal_errorc                 C   sd   | j rd S | jr| j  | j| j | js!d| _| j| j |  j d7  _ | j| j	| d S r   )
r   r   clearr   r   r   r   r=   r   r   r$   rg   r   r   r   r     s   
z_SelectorTransport._force_closec                 C   s   z*| j r
| j| W | j  d | _d | _d | _| j}|d ur)|  d | _d S d S | j  d | _d | _d | _| j}|d urG|  d | _w r*   )r   r   Zconnection_lostr   r;   r   r   Z_detach)r$   rg   r(   r   r   r   r     s*   


z(_SelectorTransport._call_connection_lostc                 C   s
   t | jS r*   )r   r   r<   r   r   r   r        
z(_SelectorTransport.get_write_buffer_sizec                 G   s$   | j rd S | jj||g|R   d S r*   )r   r   rE   r   r   r   r   rE     s   z_SelectorTransport._add_reader)NN)r  )r   r   r   max_size	bytearrayr   r   r   r   r   r   r   rs   r;   warningswarnr  r  r   r   r   rE   r   r   r   r%   r   r   c  s"    

r   c                       s   e Zd ZdZejj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 Zdd Zdd Zdd Zdd Zdd Z fddZdd  Zd!d" Z  ZS )$r+   TNc                    s   d | _ t ||||| d| _d| _d | _t| j | j	
| jj|  | j	
| j| j| j |d ur?| j	
tj|d  d S d S r   )_read_ready_cbr   r   _eof_paused_empty_waiterr   Z_set_nodelayr   r   r   r   connection_maderE   r   _read_readyr   _set_result_unless_cancelled)r$   r   r,   r-   r.   r'   r(   r%   r   r   r     s   

z!_SelectorSocketTransport.__init__c                    s.   t |tjr| j| _n| j| _t | d S r*   )rn   r   ZBufferedProtocol_read_ready__get_bufferr  _read_ready__data_receivedr   r   r   r%   r   r   r     s   
z%_SelectorSocketTransport.set_protocolc                 C   s   | j  o| j S r*   )r  r   r<   r   r   r   r     s   z#_SelectorSocketTransport.is_readingc                 C   sB   | j s| jrd S d| _| j| j | j rtd|  d S d S )NTz%r pauses reading)r   r  r   r=   r   r  r   r   r<   r   r   r   r     s   
z&_SelectorSocketTransport.pause_readingc                 C   sD   | j s| jsd S d| _| | j| j | j r td|  d S d S )NFz%r resumes reading)	r   r  rE   r   r  r   r  r   r   r<   r   r   r   r   "  s   
z'_SelectorSocketTransport.resume_readingc                 C   s   |    d S r*   )r  r<   r   r   r   r  *  s   z$_SelectorSocketTransport._read_readyc              
   C   sD  | j rd S z| jd}t|stdW n" ttfy     ty7 } z| |d W Y d }~d S d }~ww z| j	
|}W n- ttfyL   Y d S  ttfyU     tym } z| |d W Y d }~d S d }~ww |sv|   d S z	| j| W d S  ttfy     ty } z| |d W Y d }~d S d }~ww )Nz%get_buffer() returned an empty bufferz/Fatal error: protocol.get_buffer() call failed.$Fatal read error on socket transportz3Fatal error: protocol.buffer_updated() call failed.)r   r   Z
get_bufferr   r8   rk   rl   rj   r  r   r   rM   rL   _read_ready__on_eofZbuffer_updated)r$   r   rg   r   r   r   r   r  -  sP   z0_SelectorSocketTransport._read_ready__get_bufferc              
   C   s   | j rd S z	| j| j}W n- ttfy   Y d S  ttfy#     ty; } z| 	|d W Y d }~d S d }~ww |sD| 
  d S z	| j| W d S  ttfyW     tyo } z| 	|d W Y d }~d S d }~ww )Nr  z2Fatal error: protocol.data_received() call failed.)r   r   rK   r  rM   rL   rk   rl   rj   r  r  r   Zdata_received)r$   rH   rg   r   r   r   r  R  s4   z3_SelectorSocketTransport._read_ready__data_receivedc              
   C   s   | j  rtd|  z| j }W n" ttfy     ty4 } z| 	|d W Y d }~d S d }~ww |r@| j 
| j d S |   d S )Nz%r received EOFz1Fatal error: protocol.eof_received() call failed.)r   r  r   r   r   Zeof_receivedrk   rl   rj   r  r=   r   r;   )r$   Z	keep_openrg   r   r   r   r  k  s    
z,_SelectorSocketTransport._read_ready__on_eofc              
   C   s(  t |tttfstdt|j| jrtd| j	d ur"td|s&d S | j
r=| j
tjkr4td |  j
d7  _
d S | jsz| j|}W n, ttfyS   Y n, ttfy\     tyt } z| |d W Y d }~d S d }~ww ||d  }|sd S | j| j| j | j| |   d S )N/data argument must be a bytes-like object, not z%Cannot call write() after write_eof()z(unable to write; sendfile is in progresssocket.send() raised exception.r   %Fatal write error on socket transport)rn   bytesr	  r   rq   r   r   r  r8   r  r   r   !LOG_THRESHOLD_FOR_CONNLOST_WRITESr   warningr   r   rQ   rM   rL   rk   rl   rj   r  r   r   r   _write_readyextend_maybe_pause_protocol)r$   rH   r   rg   r   r   r   write  sB   

z_SelectorSocketTransport.writec              
   C   s*  | j rd S z	| j| j}W nK ttfy   Y d S  ttfy#     tyY } z+| j	
| j | j  | |d | jd urN| j| W Y d }~d S W Y d }~d S d }~ww |rb| jd |= |   | js| j	
| j | jd ur{| jd  | jr| d  d S | jr| jtj d S d S d S )Nr  )r   r   rQ   r   rM   rL   rk   rl   rj   r   r   r   r  r  r  r   _maybe_resume_protocolr   r   r   r  shutdownrB   SHUT_WR)r$   r   rg   r   r   r   r    s<   


z%_SelectorSocketTransport._write_readyc                 C   s2   | j s| jrd S d| _| js| jtj d S d S r   )r   r  r   r   r#  rB   r$  r<   r   r   r   	write_eof  s   z"_SelectorSocketTransport.write_eofc                 C   s   dS r   r   r<   r   r   r   can_write_eof  rJ   z&_SelectorSocketTransport.can_write_eofc                    s.   t  | | jd ur| jtd d S d S )NzConnection is closed by peer)r   r   r  r   ConnectionErrorr  r%   r   r   r     s   
z._SelectorSocketTransport._call_connection_lostc                 C   s6   | j d ur	td| j | _ | js| j d  | j S )NzEmpty waiter is already set)r  r8   r   ri   r   r   r<   r   r   r   r     s   
z+_SelectorSocketTransport._make_empty_waiterc                 C   s
   d | _ d S r*   )r  r<   r   r   r   r     r  z,_SelectorSocketTransport._reset_empty_waiterr   )r   r   r   Z_start_tls_compatibler   Z_SendfileModeZ
TRY_NATIVEZ_sendfile_compatibler   r   r   r   r   r  r  r  r  r!  r  r%  r&  r   r   r   r   r   r   r%   r   r+     s*    %'r+   c                       sJ   e Zd ZejZ		d fdd	Zdd Zdd Zddd	Z	d
d Z
  ZS )r5   Nc                    sb   t  |||| || _| j| jj|  | j| j| j| j	 |d ur/| jt
j|d  d S d S r*   )r   r   _addressr   r   r   r  rE   r   r  r   r  )r$   r   r,   r-   r6   r.   r'   r%   r   r   r     s   

z#_SelectorDatagramTransport.__init__c                 C   s   t dd | jD S )Nc                 s   s    | ]	\}}t |V  qd S r*   )r   ).0rH   rd   r   r   r   	<genexpr>  s    zC_SelectorDatagramTransport.get_write_buffer_size.<locals>.<genexpr>)sumr   r<   r   r   r   r     s   z0_SelectorDatagramTransport.get_write_buffer_sizec              
   C   s   | j rd S z| j| j\}}W nE ttfy   Y d S  ty4 } z| j| W Y d }~d S d }~w t	t
fy=     tyU } z| |d W Y d }~d S d }~ww | j|| d S )Nz&Fatal read error on datagram transport)r   r   recvfromr  rM   rL   rR   r   error_receivedrk   rl   rj   r  Zdatagram_receivedr$   rH   rf   rg   r   r   r   r    s    z&_SelectorDatagramTransport._read_readyc              
   C   s|  t |tttfstdt|j|sd S | jr+|d | jfvr(td| j | j}| j	rE| jrE| j	t
jkr<td |  j	d7  _	d S | jsz| jd rW| j| W d S | j|| W d S  ttfyt   | j| j| j Y n: ty } z| j| W Y d }~d S d }~w ttfy     ty } z| |d W Y d }~d S d }~ww | j t||f | !  d S )Nr  z!Invalid address: must be None or r  r   r\   'Fatal write error on datagram transport)"rn   r  r	  r   rq   r   r   r(  rr   r   r   r  r   r  r   r   r   rQ   sendtorM   rL   r   r   r   _sendto_readyrR   r   r-  rk   rl   rj   r  r   r   r.  r   r   r   r0    sR   


z!_SelectorDatagramTransport.sendtoc              
   C   s  | j ro| j  \}}z| jd r| j| n| j|| W nL ttfy2   | j ||f Y n= t	yJ } z| j
| W Y d }~d S d }~w ttfyS     tyk } z| |d W Y d }~d S d }~ww | j s|   | j s| j| j | jr| d  d S d S d S )Nr\   r/  )r   popleftr   r   rQ   r0  rM   rL   
appendleftrR   r   r-  rk   rl   rj   r  r"  r   r   r   r   r   r.  r   r   r   r1  0  s>   
z(_SelectorDatagramTransport._sendto_readyr   r*   )r   r   r   collectionsdequer   r   r   r  r0  r1  r   r   r   r%   r   r5     s    
+r5   )r   __all__r4  r`   r   r   rB   r
  r!   ZsslImportError r   r   r   r   r   r	   r
   r   logr   r   ZBaseEventLoopr   Z_FlowControlMixinZ	Transportr   r+   r5   r   r   r   r   <module>   sH        <  o