σ
 m[c           @` sΊ   d  d l  m Z m Z m Z m Z d  d l Z d  d l Z d  d l m Z e j Z	 e j
 Z e j j Z e j j Z e a d a d   Z e d  Z e d  Z e j e d   Z d S(   i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsN(   t   urllibc         ` s     f d   } | S(   u΄   
    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    	      ` sU  t  |  d t j  r_ |  d j t j t j f k rB   |  |   S|  d d } d } d } nP t  |  d t  o t |  d  d k s   |  |   S|  d d } d } d	 } t j   } t j   } | | | f k rd } | |  | d f } |  |  | f |  | d }  n  t	 g  | D] } | | k ^ q rE  |  |   St
 d   d  S(
   Ni    i   u	   localhostu	   127.0.0.1u   ::1i   u[   An attempt was made to connect to the internet by a test that was not marked `remote_data`.(   u	   localhostu	   127.0.0.1u   ::1(   u	   localhostu	   127.0.0.1(   t
   isinstancet   sockett   familyt   AF_INETt   AF_INET6t   tuplet   lent   gethostnamet   getfqdnt   anyt   IOError(	   t   argst   kwargst   hostt   addr_argt   valid_hostst   hostnamet   fqdnt   new_addrt   h(   t   original_function(    sB   lib/python2.7/site-packages/matplotlib/testing/disable_internet.pyt   new_function'   s(    	) %(    (   R   R   (    (   R   sB   lib/python2.7/site-packages/matplotlib/testing/disable_internet.pyt   check_internet_off    s    "c         C` s£   t  r
 d St a  t } |  r) t d  n  t j j   a t j j i   } t j j |  } t j j |  t	 t
  t _ t	 t  t j _ t	 t  t j _ t S(   u1  
    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].
    Nu   Internet access disabled(   t   INTERNET_OFFt   Truet   printR   t   requestt   build_openert   _orig_openert   ProxyHandlert   install_openerR   t   socket_create_connectionR   t   create_connectiont   socket_bindt   bindt   socket_connectt   connect(   t   verboset   __tracebackhide__t   no_proxy_handlert   opener(    (    sB   lib/python2.7/site-packages/matplotlib/testing/disable_internet.pyt   turn_off_internetL   s    c         C` sX   t  s
 d St a  |  r# t d  n  t j j t  t t _	 t
 t j _ t t j _ t S(   uL   
    Restore internet access.  Not used, but kept in case it is needed.
    Nu   Internet access enabled(   R   t   FalseR   R   R   R#   R!   R$   R   R%   R&   R'   R(   R)   (   R*   (    (    sB   lib/python2.7/site-packages/matplotlib/testing/disable_internet.pyt   turn_on_interneto   s    	c         c` s:   t  } t d |   z	 d VWd | s5 t d |   n  Xd S(   u7  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.   R0   (   R*   t   already_disabled(    (    sB   lib/python2.7/site-packages/matplotlib/testing/disable_internet.pyt   no_internet   s    	(   t
   __future__R    R   R   R   t
   contextlibR   t	   six.movesR   t   socket_originalR%   R$   R'   R&   R)   R(   R/   R   t   NoneR!   R   R.   R0   t   contextmanagerR2   (    (    (    sB   lib/python2.7/site-packages/matplotlib/testing/disable_internet.pyt   <module>   s   "			,#