U
    O8cK)                     @   sX  d dl Z d dlZd dlmZmZmZ d dlmZ d dlm	  m
Z d dlmZmZmZ d dlmZ d dlmZ d dlmZ d4ddZdad	d
 Zdd ZdZdZdZdZddddgZ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(G d&d' d'Z)G d(d) d)Z*G d*d+ d+Z+G d,d- d-Z,G d.d/ d/Z-G d0d1 d1Z.d2d3 Z/dS )5    N)mkdtempmkstempNamedTemporaryFile)rmtree)assert_assert_equalassert_raises)urlparse)URLErrorc                 C   s$   | t  krtdd}|S tddS )z$Stub to replace urlopen for testing.Zurltmp_)prefixzName or service not knownN)valid_httpurlr   r
   )urldatatmpfile r   D/tmp/pip-unpacked-wheel-fd_gsd75/numpy/lib/tests/test__datasource.pyurlopen_stub   s    

r   c                   C   s   t jatt _d S N)urllib_requesturlopenold_urlopenr   r   r   r   r   setup_module   s    r   c                   C   s
   t t_d S r   )r   r   r   r   r   r   r   teardown_module!   s    r   zhttp://www.google.com/z
index.htmlzhttp://fake.abc.web/site/zfake.txtz/etc/shadowz../../shadowz..\system.datzc:\windows\system.dats   three is the magic numberc                 C   s"   t dd| dd\}}t| |S )N.txtdstmp_T)suffixr   dirtext)r   oscloseZfiledirfdpathr   r   r   valid_textfile2   s    
r#   c                 C   s*   t dd| d\}}t| t| |S )Nr   r   )r   r   r   )r   r   r   remover    r   r   r   invalid_textfile9   s    

r%   c                   C   s   t t S r   )	http_path	http_filer   r   r   r   r   A   s    r   c                   C   s   t t S r   )http_fakepathhttp_fakefiler   r   r   r   invalid_httpurlE   s    r*   c                   C   s   t S r   )r&   r   r   r   r   valid_baseurlI   s    r+   c                   C   s   t S r   )r(   r   r   r   r   invalid_baseurlM   s    r,   c                   C   s   t S r   )r'   r   r   r   r   valid_httpfileQ   s    r-   c                   C   s   t S r   )r)   r   r   r   r   invalid_httpfileU   s    r.   c                   @   sT   e Z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S )TestDataSourceOpenc                 C   s   t  | _t| j| _d S r   r   tmpdir
datasource
DataSourcedsselfr   r   r   setup_methodZ   s    zTestDataSourceOpen.setup_methodc                 C   s   t | j | `d S r   r   r1   r4   r5   r   r   r   teardown_method^   s    
z"TestDataSourceOpen.teardown_methodc                 C   s"   | j t }t| |  d S r   )r4   openr   r   r   )r6   fhr   r   r   test_ValidHTTPb   s    z!TestDataSourceOpen.test_ValidHTTPc              
   C   s\   t  }tt| jj| z| j| W n0 tk
rV } zt|jd k W 5 d }~X Y nX d S r   )r*   r   OSErrorr4   r:   r   errno)r6   r   er   r   r   test_InvalidHTTPg   s    z#TestDataSourceOpen.test_InvalidHTTPc                 C   s   t t| jjt  d S r   )r   r
   r4   _cacher*   r5   r   r   r   test_InvalidHTTPCacheURLErrorp   s    z0TestDataSourceOpen.test_InvalidHTTPCacheURLErrorc                 C   s*   t | j}| j|}t| |  d S r   )r#   r1   r4   r:   r   r   )r6   
local_filer;   r   r   r   test_ValidFiles   s    
z!TestDataSourceOpen.test_ValidFilec                 C   s   t | j}tt| jj| d S r   )r%   r1   r   r=   r4   r:   )r6   Zinvalid_filer   r   r   test_InvalidFiley   s    
z#TestDataSourceOpen.test_InvalidFilec                 C   s   zdd l }W n tk
r(   t  Y nX tj| jd}||d}|	t
 |  | j|}| }|  tt
| d S )Nr   zfoobar.txt.gzw)gzipImportErrorpytestskipr   r"   joinr1   r:   write
magic_liner   r4   readliner   )r6   rG   filepathfpresultr   r   r   test_ValidGzipFile}   s    
z%TestDataSourceOpen.test_ValidGzipFilec                 C   s   zdd l }W n tk
r(   t  Y nX tj| jd}||d}|	t
 |  | j|}| }|  tt
| d S )Nr   zfoobar.txt.bz2rF   )bz2rH   rI   rJ   r   r"   rK   r1   BZ2FilerL   rM   r   r4   r:   rN   r   )r6   rS   rO   rP   rQ   r   r   r   test_ValidBz2File   s    
z$TestDataSourceOpen.test_ValidBz2FileN)__name__
__module____qualname__r7   r9   r<   r@   rB   rD   rE   rR   rU   r   r   r   r   r/   Y   s   	r/   c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )TestDataSourceExistsc                 C   s   t  | _t| j| _d S r   r0   r5   r   r   r   r7      s    z!TestDataSourceExists.setup_methodc                 C   s   t | j | `d S r   r8   r5   r   r   r   r9      s    
z$TestDataSourceExists.teardown_methodc                 C   s   t | jt  d S r   )r   r4   existsr   r5   r   r   r   r<      s    z#TestDataSourceExists.test_ValidHTTPc                 C   s   t | jt d d S NF)r   r4   rZ   r*   r5   r   r   r   r@      s    z%TestDataSourceExists.test_InvalidHTTPc                 C   sD   t | j}t| j| t }t |}t| j| t| d S r   )r#   r1   r   r4   rZ   r   r   )r6   r   Zlocaldirr   r   r   rD      s    
z#TestDataSourceExists.test_ValidFilec                 C   s    t | j}t| j|d d S r[   )r%   r1   r   r4   rZ   r6   r   r   r   r   rE      s    
z%TestDataSourceExists.test_InvalidFileN)	rV   rW   rX   r7   r9   r<   r@   rD   rE   r   r   r   r   rY      s   
rY   c                   @   sL   e Z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S )TestDataSourceAbspathc                 C   s"   t jt | _t| j| _d S r   )r   r"   abspathr   r1   r2   r3   r4   r5   r   r   r   r7      s    z"TestDataSourceAbspath.setup_methodc                 C   s   t | j | `d S r   r8   r5   r   r   r   r9      s    
z%TestDataSourceAbspath.teardown_methodc                 C   sN   t t \}}}}}}tj| j||tjd}t|| j	
t  d S N/)r	   r   r   r"   rK   r1   stripsepr   r4   r^   )r6   schemenetlocupathpmsqryfrg
local_pathr   r   r   r<      s
    z$TestDataSourceAbspath.test_ValidHTTPc                 C   sB   t | j}tj|d }t|| j| t|| j| d S N)r#   r1   r   r"   splitr   r4   r^   )r6   r   tmpfilenamer   r   r   rD      s    
z$TestDataSourceAbspath.test_ValidFilec                 C   sP   t t \}}}}}}tj| j||tjd}t|| j	
t k d S r_   )r	   r*   r   r"   rK   r1   ra   rb   r   r4   r^   r   )r6   rc   rd   re   rf   rg   rh   Zinvalidhttpr   r   r   r@      s
    z&TestDataSourceAbspath.test_InvalidHTTPc                 C   sP   t | j}t | j}tj|d }t|| j|k t|| j|k d S rj   )r#   r1   r   r"   rl   r   r4   r^   )r6   Zinvalidfiler   rm   r   r   r   rE      s
    

z&TestDataSourceAbspath.test_InvalidFilec                    s   t  j}tj|d } fdd}t|t  j t|t  j t|| j t|| j t	D ]0}t|t
|  j t|| j q~d S )Nrk   c                    s   t j j| S r   )r   r"   r^   r4   xr5   r   r   <lambda>       z7TestDataSourceAbspath.test_sandboxing.<locals>.<lambda>)r#   r1   r   r"   rl   r   r   
startswithr*   malicious_filesr&   )r6   r   rm   tmp_pathfnr   r5   r   test_sandboxing   s    
z%TestDataSourceAbspath.test_sandboxingc                 C   sF   t j}z2dt _|   |   |   |   |   W 5 |t _X d S N\)r   rb   r<   rD   r@   rE   rv   r6   Zorig_os_sepr   r   r   test_windows_os_sep   s    z)TestDataSourceAbspath.test_windows_os_sepN)rV   rW   rX   r7   r9   r<   rD   r@   rE   rv   rz   r   r   r   r   r]      s   	r]   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TestRepositoryAbspathc                 C   s&   t jt | _tt | j| _d S r   )	r   r"   r^   r   r1   r2   
Repositoryr+   reposr5   r   r   r   r7      s    z"TestRepositoryAbspath.setup_methodc                 C   s   t | j | `d S r   r   r1   r}   r5   r   r   r   r9     s    
z%TestRepositoryAbspath.teardown_methodc           	      C   sT   t t \}}}}}}tj| jj||tjd}| j	t
 }t|| d S r_   )r	   r   r   r"   rK   r}   	_destpathra   rb   r^   r-   r   )	r6   rc   rd   re   rf   rg   rh   ri   rO   r   r   r   r<     s    z$TestRepositoryAbspath.test_ValidHTTPc                    s\    fdd}t |t  j tD ]0}t |t|  j t || j q&d S )Nc                    s   t j j| S r   )r   r"   r^   r}   rn   r5   r   r   rp     rq   z7TestRepositoryAbspath.test_sandboxing.<locals>.<lambda>)r   r-   rr   r1   rs   r&   )r6   rt   ru   r   r5   r   rv     s
    z%TestRepositoryAbspath.test_sandboxingc                 C   s.   t j}zdt _|   |   W 5 |t _X d S rw   )r   rb   r<   rv   ry   r   r   r   rz     s    z)TestRepositoryAbspath.test_windows_os_sepN)rV   rW   rX   r7   r9   r<   rv   rz   r   r   r   r   r{      s
   r{   c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )TestRepositoryExistsc                 C   s   t  | _tt | j| _d S r   )r   r1   r2   r|   r+   r}   r5   r   r   r   r7     s    z!TestRepositoryExists.setup_methodc                 C   s   t | j | `d S r   r~   r5   r   r   r   r9   #  s    
z$TestRepositoryExists.teardown_methodc                 C   s   t | j}t| j| d S r   )r#   r1   r   r}   rZ   r\   r   r   r   rD   '  s    
z#TestRepositoryExists.test_ValidFilec                 C   s    t | j}t| j|d d S r[   )r%   r1   r   r}   rZ   r\   r   r   r   rE   ,  s    
z%TestRepositoryExists.test_InvalidFilec                 C   s   t | jt  d S r   )r   r}   rZ   r   r5   r   r   r   test_RemoveHTTPFile0  s    z(TestRepositoryExists.test_RemoveHTTPFilec           
      C   sT   t  }t|\}}}}}}tj| jj|}t|d t|}	t	| j
|	 d S )Ni  )r   r	   r   r"   rK   r}   r   mkdirr#   r   rZ   )
r6   	localfilerc   rd   re   rf   rg   rh   ri   r   r   r   r   test_CachedHTTPFile3  s    z(TestRepositoryExists.test_CachedHTTPFileN)	rV   rW   rX   r7   r9   rD   rE   r   r   r   r   r   r   r     s   r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestOpenFuncc                 C   s   t  | _d S r   )r   r1   r5   r   r   r   r7   @  s    zTestOpenFunc.setup_methodc                 C   s   t | j d S r   )r   r1   r5   r   r   r   r9   C  s    zTestOpenFunc.teardown_methodc                 C   sH   t | j}tj|| jd}t| |  t|}t| |  d S )N)Zdestpath)r#   r1   r2   r:   r   r   )r6   rC   rP   r   r   r   test_DataSourceOpenF  s    

z TestOpenFunc.test_DataSourceOpenN)rV   rW   rX   r7   r9   r   r   r   r   r   r   ?  s   r   c                  C   s   t  } | `|   d S r   )r2   r3   Z
_istmpdest__del__)r4   r   r   r   test_del_attr_handlingQ  s    r   )N)0r   rI   tempfiler   r   r   shutilr   Znumpy.lib._datasourcelibZ_datasourcer2   Znumpy.testingr   r   r   urllib.requestrequestr   urllib.parser	   urllib.errorr
   r   r   r   r   r&   r'   r(   r)   rs   rM   r#   r%   r   r*   r+   r,   r-   r.   r/   rY   r]   r{   r   r   r   r   r   r   r   <module>   sH   
	 EA!!