B
    ]t\                 @   sl   d dl Z d dlZd dlZejZejZejjZejj	Z
dadadd ZdddZdddZe jdd	d
ZdS )    NFc                s    fdd}|S )z
    Wraps ``original_function``, which in most cases is assumed
    to be a `socket.socket` method, to raise an `IOError` for any operations
    on non-local AF_INET sockets.
    c                 s   t | d tjrF| d jtjtjfkr0| |S | d d  d}d}n<t | d trdt| d dksn| |S | d d  d}d}t }t } ||fkrd  | | d f}| d | |f | |d d   } t	 fdd|D r| |S t
d	d S )
Nr      )	localhostz	127.0.0.1z::1   )r   z	127.0.0.1r   c             3   s   | ]}| kV  qd S )N ).0h)hostr   Blib/python3.7/site-packages/matplotlib/testing/disable_internet.py	<genexpr>@   s    z;check_internet_off.<locals>.new_function.<locals>.<genexpr>z[An attempt was made to connect to the internet by a test that was not marked `remote_data`.)
isinstancesocketZfamilyZAF_INETZAF_INET6tuplelenZgethostnameZgetfqdnanyIOError)argskwargsZaddr_argZvalid_hostsZhostnameZfqdnZnew_addr)original_function)r   r	   new_function#   s(    

"
z(check_internet_off.<locals>.new_functionr   )r   r   r   )r   r	   check_internet_off   s    "r   c             C   sp   t rdS da d}| rtd tj atji }tj|}tj| tt	t
_ttt
j
_ttt
j
_t
S )a1  
    Disable internet access via python by preventing connections from being
    created using the socket module.  Presumably this could be worked around by
    using some other means of accessing the internet, but all default python
    modules (urllib, requests, etc.) use socket [citation needed].
    NTzInternet access disabled)INTERNET_OFFprinturllibrequestZbuild_opener_orig_openerZProxyHandlerinstall_openerr   socket_create_connectionr   create_connectionsocket_bindbindsocket_connectconnect)verboseZ__tracebackhide__Zno_proxy_handleropenerr   r   r	   turn_off_internetH   s    

r$   c             C   s>   t sdS da | rtd tjt tt_t	tj_
ttj_tS )zL
    Restore internet access.  Not used, but kept in case it is needed.
    NFzInternet access enabled)r   r   r   r   r   r   r   r   r   r   r   r    r!   )r"   r   r   r	   turn_on_internetk   s    r%   c          	   c   s.   t }t| d z
dV  W d|s(t| d X dS )a7  Context manager to temporarily disable internet access (if not already
    disabled).  If it was already disabled before entering the context manager
    (i.e. `turn_off_internet` was called previously) then this is a no-op and
    leaves internet access disabled until a manual call to `turn_on_internet`.
    )r"   N)r   r$   r%   )r"   Zalready_disabledr   r   r	   no_internet   s    

r&   )F)F)F)
contextlibr   Zurllib.requestr   Zsocket_originalr   r   r   r   r!   r    r   r   r   r$   r%   contextmanagerr&   r   r   r   r	   <module>   s   ,
#
