B
    H/\4                 @   s0  d dl mZ dddddgZddd	d
ddgZee7 ZddddgZdddddddddddddddd d!d"d#gZd$d%d&d'd(d)d*d+d,g	Zee d d-lZd d-l	Z	d d.l
mZ d d/lmZmZmZ d d0lmZ e	jd1kZe	jd2kZer,d d3lmZ d d4lmZ d d5lmZ d d6lmZ d d7lmZ  d d8l!m"Z# eZ$nTd d9l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# yd d@lm&Z& W n e'k
r   dAZ&Y nX efZ(erd dBlm)Z) e(e)f7 Z(d d-l*Z*e*j+Z,d d-l+Z-d- Z.Z/ee-e0 edCdDZx>e-j1D ]4Z.e2e-e.Z/e3e/eefre/e0 e.< e4e. qW [.[/e5Z6d dEl7m8Z8 e89e6 e8j:Z;e8j<Z<e8j=Z=e8j>Z>G dFdG dGe?Z@e@fdHdZAdId ZBdJd	 ZCdYdKdZDereDjEZFdZdLdZDeFeD_E[FdMdN ZGdOd
 ZHdPd ZId[dRdZJe6fdSdTZKe-jLe-jMfdUdVZNe-jOfZPe-jQr$ePe-jRf7 ZPdWdX ZSd-S )\    )absolute_importZcreate_connectionsocketZ
SocketTypeZfromfdZ
socketpairgetaddrinfogethostbynamegethostbyname_exgethostbyaddrgetnameinfogetfqdncancel_wait	wait_read
wait_writewait_readwriteerrorZgaierrorZherrorZhtonlZhtonsZntohlZntohsZ	inet_atonZ	inet_ntoaZ	inet_ptonZ	inet_ntoptimeoutgethostnameZgetprotobynameZgetservbynameZgetservbyportZgetdefaulttimeoutZsetdefaulttimeoutZerrorTabAddressFamily
SocketKindZCMSG_LENZ
CMSG_SPACEdupZif_indextonameZif_nameindexZif_nametoindexZsethostnameN)get_hub_noargs)string_typesinteger_typesPY3)copy_globalsZwin32darwin)	WSAEINVAL)WSAEWOULDBLOCK)WSAEINPROGRESS)WSAEALREADY)
WSAEISCONN)formatError)EINVAL)EWOULDBLOCK)EINPROGRESS)EALREADY)EAGAIN)EISCONN)strerror)EBADF	   )
EPROTOTYPET)Z
only_namesZignore_missing_names)_hub_primitivesc                   s   e Zd Z fddZ  ZS )cancel_wait_exc                s   t t| td d S )Nz.File descriptor was closed in another greenlet)superr+   __init__r'   )self)	__class__ 3lib/python3.7/site-packages/gevent/_socketcommon.pyr-      s    
zcancel_wait_ex.__init__)__name__
__module____qualname__r-   __classcell__r0   r0   )r/   r1   r+      s   r+   c             C   s   t  | | dS )z&See :meth:`gevent.hub.Hub.cancel_wait`N)get_hubr
   )Zwatcherr   r0   r0   r1   r
      s    c             C   s   t  j| S )z
    gethostbyname(host) -> address

    Return the IP address (a string of the form '255.255.255.255') for a host.

    .. seealso:: :doc:`/dns`
    )r6   resolverr   )hostnamer0   r0   r1   r      s    c             C   s   t  j| S )a<  
    gethostbyname_ex(host) -> (name, aliaslist, addresslist)

    Return the true host name, a list of aliases, and a list of IP addresses,
    for a host.  The host argument is a string giving a host name or IP number.
    Resolve host and port into list of address info entries.

    .. seealso:: :doc:`/dns`
    )r6   r7   r   )r8   r0   r0   r1   r      s    
c             C   s   t  j| |||||S )a  
    Resolve host and port into list of address info entries.

    Translate the host/port argument into a sequence of 5-tuples that contain
    all the necessary arguments for creating a socket connected to that service.
    host is a domain name, a string representation of an IPv4/v6 address or
    None. port is a string service name such as 'http', a numeric port number or
    None. By passing None as the value of host and port, you can pass NULL to
    the underlying C API.

    The family, type and proto arguments can be optionally specified in order to
    narrow the list of addresses returned. Passing zero as a value for each of
    these arguments selects the full range of results.

    .. seealso:: :doc:`/dns`
    )r6   r7   r   )hostportfamilysocktypeprotoflagsr0   r0   r1   r      s    c             C   s*   t  j| |||||}dd |D }|S )Nc             S   s0   g | ](\}}}}}t |tt |t|||fqS r0   )_intenum_converterr   r   ).0Zafr<   r=   Z	canonnameZsar0   r0   r1   
<listcomp>   s   zgetaddrinfo.<locals>.<listcomp>)r6   r7   r   )r9   r:   r;   typer=   r>   Zaddrlistresultr0   r0   r1   r      s    c             C   s"   y|| S  t k
r   | S X d S )N)
ValueError)valueZ
enum_klassr0   r0   r1   r?      s    r?   c             C   s   t  j| S )a  
    gethostbyaddr(ip_address) -> (name, aliaslist, addresslist)

    Return the true host name, a list of aliases, and a list of IP addresses,
    for a host.  The host argument is a string giving a host name or IP number.

    .. seealso:: :doc:`/dns`
    )r6   r7   r   )Z
ip_addressr0   r0   r1   r      s    	c             C   s   t  j| |S )z{
    getnameinfo(sockaddr, flags) -> (host, port)

    Get host and port for a sockaddr.

    .. seealso:: :doc:`/dns`
    )r6   r7   r   )Zsockaddrr>   r0   r0   r1   r      s     c             C   s   |   } | r| dkrt } yt| \}}}W n tk
r@   Y n@X |d| x0|D ]$} t| trnd| krxP qTd| krTP qTW |} | S )a  Get fully qualified domain name from name.

    An empty argument is interpreted as meaning the local host.

    First the hostname returned by gethostbyaddr() is checked, then
    possibly existing aliases. In case no FQDN is available, hostname
    from gethostname() is returned.
    z0.0.0.0r      ..)stripr   r   r   insert
isinstancebytes)namer8   aliases_r0   r0   r1   r	      s     


c       
      C   s   d}t |}d}xl||k r|||d }	|dkr@|| |	|7 }q|rV|dkrV|dqd}|| j|	||d7 }|t  }qW |S )a  
    Send the complete contents of ``data_memory`` before returning.
    This is the core loop around :meth:`send`.

    :param timeleft: Either ``None`` if there is no timeout involved,
       or a float indicating the timeout to use.
    :param end: Either ``None`` if there is no timeout involved, or
       a float giving the absolute end time.
    :return: An updated value for ``timeleft`` (or None)
    :raises timeout: If ``timeleft`` was given and elapsed while
       sending this chunk.
    r   Nz	timed out   )r   )lensendtime)
r   data_memoryr>   timeleftendr   	data_sentlen_data_memoryZstarted_timerchunkr0   r0   r1   __send_chunk  s    

rZ   c             C   s   t |}|sdS t| ||d}d}d}d}	| jdk	rJ| j}	t |	 }xB||k rt|| |}
|||
 }t| |||	|}	|t |7 }qLW dS )zw
    Send the *data_memory* (which should be a memoryview)
    using the gevent *socket*, performing well on PyPy.
    r   i   N)rQ   maxZ
getsockoptr   rS   minrZ   )r   rT   r>   
SOL_SOCKET	SO_SNDBUFrX   Z
chunk_sizerW   rV   rU   Z	chunk_endrY   r0   r0   r1   _sendall?  s    #

r_   c             C   sx   | j tkst|ts|S |d d \}}t|d | j }|d d }t|dkr\|d |f}n|d ||d |d f}|S )N   r      )r;   _RESOLVABLE_FAMILIESrK   tupler   rQ   )ZsockZaddressr9   r:   rr0   r0   r1   _resolve_addr~  s    rf   )r   r   r   r   )r   r   r   r   )rF   )TZ
__future__r   Z_implementsZ__dns__Z__extensions__Z__imports__Z__py3_imports__extendrS   sysZgevent._hub_localr   r6   Zgevent._compatr   r   r   Zgevent._utilr   platformZ
is_windowsZis_macoserrnor   r    r   r!   r   r"   r   r#   r   r%   Zgevent.win32utilr   r&   r$   osr'   ImportErrorZ
GSENDAGAINr)   Z_socketr   Z_realsocketZ
__socket___nameZ_valueglobals__all__getattrrK   appendr   Z_timeout_errorZgeventr*   Zset_default_timeout_errorZwait_on_watcherwaitr   r   r   r   r+   r
   r   r   r   __doc__dr?   r   r   r	   rZ   r]   r^   r_   ZAF_INETrc   Zhas_ipv6ZAF_INET6rf   r0   r0   r0   r1   <module>   s   










$9