o
    i/                     @   s   d dl Z d dlZd dlmZ ejdeddd 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 )    N)cbookz3.2modulezpytest-remotedata)nameobj_typealternativeFc                    s    fdd}|S )z
    Wrap ``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r%| d jtjtjfvr| i |S | d d  d}d}n t | d tr4t| d dks;| i |S | d d  d}d}t }t } ||fv rnd  | | d f}| d | |f | |d d   } t	 fdd|D r| i |S t
d	)
Nr      )	localhost	127.0.0.1z::1   )r   r	   r   c                 3   s    | ]}| v V  qd S )N ).0hhostr   ^/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/matplotlib/testing/disable_internet.py	<genexpr>C   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socketfamilyAF_INETAF_INET6tuplelengethostnamegetfqdnanyIOError)argskwargsaddr_argvalid_hostshostnamefqdnnew_addroriginal_functionr   r   new_function&   s(   "z(check_internet_off.<locals>.new_functionr   )r%   r&   r   r$   r   check_internet_off   s   "r'   c                 C   sx   ddl }trdS dad}| rtd |j a|ji }|j|}|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].
    r   NTzInternet access disabled)urllib.requestINTERNET_OFFprintrequestbuild_opener_orig_openerProxyHandlerinstall_openerr'   socket_create_connectionr   create_connectionsocket_bindbindsocket_connectconnect)verboseurllib__tracebackhide__no_proxy_handleropenerr   r   r   turn_off_internetK   s   

r;   c                 C   sF   ddl }tsdS da| rtd |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.
    r   NFzInternet access enabled)r(   r)   r*   r+   r/   r-   r0   r   r1   r2   r3   r4   r5   )r6   r7   r   r   r   turn_on_interneto   s   r<   c              	   c   sB    t }t| d zdV  W |st| d dS dS |s t| d w w )a*  
    Temporarily disables 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`.
    )r6   N)r)   r;   r<   )r6   already_disabledr   r   r   no_internet   s   

r>   )F)
contextlibr   
matplotlibr   warn_deprecated__name__socket_originalr1   r0   r3   r2   r5   r4   r)   r-   r'   r;   r<   contextmanagerr>   r   r   r   r   <module>   s"   

,
$