3
­?d]Ç,  ã               @   s¬  d dl mZmZmZ d dlZ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jjZd dlmZmZmZmZ ejd  dkr¢d dljZd dlmZ d dlmZ n d dlZd dlmZ d dlmZ d6d	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'„ Z0G d(d)„ d)e1ƒZ2G d*d+„ d+e1ƒZ3G d,d-„ d-e1ƒZ4G d.d/„ d/e1ƒZ5G d0d1„ d1e1ƒZ6G d2d3„ d3e1ƒZ7d4d5„ Z8dS )7é    )ÚdivisionÚabsolute_importÚprint_functionN)ÚmkdtempÚmkstempÚNamedTemporaryFile)Úrmtree)Úassert_Úassert_equalÚassert_raisesÚassert_warnsé   )Ú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   úA/tmp/pip-build-op5p2y5_/numpy/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 )N)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j|ƒ |S )Nz.txtÚdstmp_T)Úsuffixr   ÚdirÚtext)r   ÚosÚclose)ÚfiledirÚfdÚpathr   r   r   Úvalid_textfile<   s    
r&   c             C   s*   t dd| d�\}}tj|ƒ tj|ƒ |S )Nz.txtr   )r   r   r   )r   r!   r"   Úremove)r#   r$   r%   r   r   r   Úinvalid_textfileC   s    

r(   c               C   s   t t S )N)Ú	http_pathÚ	http_filer   r   r   r   r   K   s    r   c               C   s   t t S )N)Úhttp_fakepathÚhttp_fakefiler   r   r   r   Úinvalid_httpurlO   s    r-   c               C   s   t S )N)r)   r   r   r   r   Úvalid_baseurlS   s    r.   c               C   s   t S )N)r+   r   r   r   r   Úinvalid_baseurlW   s    r/   c               C   s   t S )N)r*   r   r   r   r   Úvalid_httpfile[   s    r0   c               C   s   t S )N)r,   r   r   r   r   Úinvalid_httpfile_   s    r1   c               @   sv   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ejjejd dkdd�dd„ ƒZdS )ÚTestDataSourceOpenc             C   s   t ƒ | _tj| jƒ| _d S )N)r   ÚtmpdirÚ
datasourceÚ
DataSourceÚds)Úselfr   r   r   Úsetupd   s    zTestDataSourceOpen.setupc             C   s   t | jƒ | `d S )N)r   r3   r6   )r7   r   r   r   Úteardownh   s    
zTestDataSourceOpen.teardownc             C   s"   | j jtƒ ƒ}t|ƒ |jƒ  d S )N)r6   Úopenr   r	   r"   )r7   Úfhr   r   r   Útest_ValidHTTPl   s    z!TestDataSourceOpen.test_ValidHTTPc             C   s\   t ƒ }tt| jj|ƒ y| jj|ƒ W n0 tk
rV } zt|jd kƒ W Y d d }~X nX d S )N)r-   r   ÚIOErrorr6   r:   r	   Úerrno)r7   r   Úer   r   r   Útest_InvalidHTTPq   s    z#TestDataSourceOpen.test_InvalidHTTPc             C   s   t t| jjtƒ ƒ d S )N)r   r   r6   Ú_cacher-   )r7   r   r   r   Útest_InvalidHTTPCacheURLErrorz   s    z0TestDataSourceOpen.test_InvalidHTTPCacheURLErrorc             C   s*   t | jƒ}| jj|ƒ}t|ƒ |jƒ  d S )N)r&   r3   r6   r:   r	   r"   )r7   Ú
local_filer;   r   r   r   Útest_ValidFile}   s    
z!TestDataSourceOpen.test_ValidFilec             C   s   t | jƒ}tt| jj|ƒ d S )N)r(   r3   r   r=   r6   r:   )r7   Zinvalid_filer   r   r   Útest_InvalidFileƒ   s    
z#TestDataSourceOpen.test_InvalidFilec             C   s‚   ydd l }W n tk
r(   tjƒ  Y nX tjj| jdƒ}|j|dƒ}|j	t
ƒ |jƒ  | jj|ƒ}|jƒ }|jƒ  tt
|ƒ d S )Nr   zfoobar.txt.gzÚw)ÚgzipÚImportErrorÚpytestÚskipr!   r%   Újoinr3   r:   ÚwriteÚ
magic_liner"   r6   Úreadliner
   )r7   rG   ÚfilepathÚfpÚresultr   r   r   Útest_ValidGzipFile‡   s    
z%TestDataSourceOpen.test_ValidGzipFilec             C   s‚   ydd l }W n tk
r(   tjƒ  Y nX tjj| jdƒ}|j|dƒ}|j	t
ƒ |jƒ  | jj|ƒ}|jƒ }|jƒ  tt
|ƒ d S )Nr   zfoobar.txt.bz2rF   )Úbz2rH   rI   rJ   r!   r%   rK   r3   ÚBZ2FilerL   rM   r"   r6   r:   rN   r
   )r7   rS   rO   rP   rQ   r   r   r   Útest_ValidBz2File—   s    
z$TestDataSourceOpen.test_ValidBz2Filer   r   zPython 2 only)Úreasonc             C   s˜   ydd l }W n tk
r(   tjƒ  Y nX tjj| jdƒ}|j|dƒ}|j	t
ƒ |jƒ  ttƒ�$ | jj|dƒ}|jƒ }|jƒ  W d Q R X tt
|ƒ d S )Nr   zfoobar.txt.bz2rF   Úrt)rS   rH   rI   rJ   r!   r%   rK   r3   rT   rL   rM   r"   r   ÚRuntimeWarningr6   r:   rN   r
   )r7   rS   rO   rP   rQ   r   r   r   Útest_Bz2File_text_mode_warning§   s    

z1TestDataSourceOpen.test_Bz2File_text_mode_warningN)Ú__name__Ú
__module__Ú__qualname__r8   r9   r<   r@   rB   rD   rE   rR   rU   rI   ÚmarkZskipifÚsysÚversion_inforY   r   r   r   r   r2   c   s   	r2   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| jƒ| _d S )N)r   r3   r4   r5   r6   )r7   r   r   r   r8   »   s    zTestDataSourceExists.setupc             C   s   t | jƒ | `d S )N)r   r3   r6   )r7   r   r   r   r9   ¿   s    
zTestDataSourceExists.teardownc             C   s   t | jjtƒ ƒƒ d S )N)r	   r6   Úexistsr   )r7   r   r   r   r<   Ã   s    z#TestDataSourceExists.test_ValidHTTPc             C   s   t | jjtƒ ƒdƒ d S )NF)r
   r6   ra   r-   )r7   r   r   r   r@   Æ   s    z%TestDataSourceExists.test_InvalidHTTPc             C   sD   t | jƒ}t| jj|ƒƒ tƒ }t |ƒ}t| jj|ƒƒ t|ƒ d S )N)r&   r3   r	   r6   ra   r   r   )r7   r   Zlocaldirr   r   r   rD   É   s    
z#TestDataSourceExists.test_ValidFilec             C   s    t | jƒ}t| jj|ƒdƒ d S )NF)r(   r3   r
   r6   ra   )r7   r   r   r   r   rE   Ó   s    
z%TestDataSourceExists.test_InvalidFileN)	rZ   r[   r\   r8   r9   r<   r@   rD   rE   r   r   r   r   r`   º   s   
r`   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jtƒ ƒ| _tj| jƒ| _d S )N)r!   r%   Úabspathr   r3   r4   r5   r6   )r7   r   r   r   r8   Ù   s    zTestDataSourceAbspath.setupc             C   s   t | jƒ | `d S )N)r   r3   r6   )r7   r   r   r   r9   Ý   s    
zTestDataSourceAbspath.teardownc             C   sN   t tƒ ƒ\}}}}}}tjj| j||jtjƒjdƒƒ}t|| j	j
tƒ ƒƒ d S )Nú/)r   r   r!   r%   rK   r3   ÚstripÚsepr
   r6   rc   )r7   ÚschemeÚnetlocÚupathÚpmsÚqryÚfrgÚ
local_pathr   r   r   r<   á   s    z$TestDataSourceAbspath.test_ValidHTTPc             C   sB   t | jƒ}tjj|ƒd }t|| jj|ƒƒ t|| jj|ƒƒ d S )Né   éÿÿÿÿ)r&   r3   r!   r%   Úsplitr
   r6   rc   )r7   r   Útmpfilenamer   r   r   rD   ç   s    
z$TestDataSourceAbspath.test_ValidFilec             C   sP   t tƒ ƒ\}}}}}}tjj| j||jtjƒjdƒƒ}t|| j	j
tƒ ƒkƒ d S )Nrd   )r   r-   r!   r%   rK   r3   re   rf   r	   r6   rc   r   )r7   rg   rh   ri   rj   rk   rl   Zinvalidhttpr   r   r   r@   ï   s    z&TestDataSourceAbspath.test_InvalidHTTPc             C   sP   t | jƒ}t | jƒ}tjj|ƒd }t|| jj|ƒkƒ t|| jj|ƒkƒ d S )Nrn   ro   )r&   r3   r!   r%   rp   r	   r6   rc   )r7   Zinvalidfiler   rq   r   r   r   rE   õ   s
    

z&TestDataSourceAbspath.test_InvalidFilec                s¸   t ˆ jƒ}tjj|ƒd }‡ fdd„}t|tƒ ƒjˆ jƒƒ t|tƒ ƒjˆ jƒƒ t||ƒjˆ jƒƒ t||ƒjˆ jƒƒ x8t	D ]0}t|t
| ƒjˆ jƒƒ t||ƒjˆ jƒƒ q€W d S )Nrn   c                s   t jjˆ jj| ƒƒS )N)r!   r%   rc   r6   )Úx)r7   r   r   Ú<lambda>  s    z7TestDataSourceAbspath.test_sandboxing.<locals>.<lambda>ro   )r&   r3   r!   r%   rp   r	   r   Ú
startswithr-   Úmalicious_filesr)   )r7   r   rq   Útmp_pathÚfnr   )r7   r   Útest_sandboxingþ   s    

z%TestDataSourceAbspath.test_sandboxingc             C   sF   t j}z2dt _| jƒ  | jƒ  | jƒ  | jƒ  | jƒ  W d |t _X d S )Nú\)r!   rf   r<   rD   r@   rE   rx   )r7   Úorig_os_sepr   r   r   Útest_windows_os_sep  s    z)TestDataSourceAbspath.test_windows_os_sepN)rZ   r[   r\   r8   r9   r<   rD   r@   rE   rx   r{   r   r   r   r   rb   Ø   s   	rb   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jtƒ ƒ| _tjtƒ | jƒ| _d S )N)	r!   r%   rc   r   r3   r4   Ú
Repositoryr.   Úrepos)r7   r   r   r   r8     s    zTestRepositoryAbspath.setupc             C   s   t | jƒ | `d S )N)r   r3   r~   )r7   r   r   r   r9     s    
zTestRepositoryAbspath.teardownc       	      C   sT   t tƒ ƒ\}}}}}}tjj| jj||jtjƒjdƒƒ}| jj	t
ƒ ƒ}t||ƒ d S )Nrd   )r   r   r!   r%   rK   r~   Ú	_destpathre   rf   rc   r0   r
   )	r7   rg   rh   ri   rj   rk   rl   rm   rO   r   r   r   r<   "  s
    z$TestRepositoryAbspath.test_ValidHTTPc                s`   ‡ fdd„}t |tƒ ƒjˆ jƒƒ x8tD ]0}t |t| ƒjˆ jƒƒ t ||ƒjˆ jƒƒ q(W d S )Nc                s   t jjˆ jj| ƒƒS )N)r!   r%   rc   r~   )rr   )r7   r   r   rs   *  s    z7TestRepositoryAbspath.test_sandboxing.<locals>.<lambda>)r	   r0   rt   r3   ru   r)   )r7   rv   rw   r   )r7   r   rx   )  s
    
z%TestRepositoryAbspath.test_sandboxingc             C   s.   t j}zdt _| jƒ  | jƒ  W d |t _X d S )Nry   )r!   rf   r<   rx   )r7   rz   r   r   r   r{   0  s    z)TestRepositoryAbspath.test_windows_os_sepN)rZ   r[   r\   r8   r9   r<   rx   r{   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jtƒ | jƒ| _d S )N)r   r3   r4   r}   r.   r~   )r7   r   r   r   r8   ;  s    zTestRepositoryExists.setupc             C   s   t | jƒ | `d S )N)r   r3   r~   )r7   r   r   r   r9   ?  s    
zTestRepositoryExists.teardownc             C   s   t | jƒ}t| jj|ƒƒ d S )N)r&   r3   r	   r~   ra   )r7   r   r   r   r   rD   C  s    
z#TestRepositoryExists.test_ValidFilec             C   s    t | jƒ}t| jj|ƒdƒ d S )NF)r(   r3   r
   r~   ra   )r7   r   r   r   r   rE   H  s    
z%TestRepositoryExists.test_InvalidFilec             C   s   t | jjtƒ ƒƒ d S )N)r	   r~   ra   r   )r7   r   r   r   Útest_RemoveHTTPFileL  s    z(TestRepositoryExists.test_RemoveHTTPFilec       
      C   sT   t ƒ }t|ƒ\}}}}}}tjj| jj|ƒ}tj|dƒ t|ƒ}	t	| jj
|	ƒƒ d S )NiÀ  )r   r   r!   r%   rK   r~   r   Úmkdirr&   r	   ra   )
r7   Ú	localfilerg   rh   ri   rj   rk   rl   rm   r   r   r   r   Útest_CachedHTTPFileO  s    z(TestRepositoryExists.test_CachedHTTPFileN)	rZ   r[   r\   r8   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 )N)r   r3   )r7   r   r   r   r8   \  s    zTestOpenFunc.setupc             C   s   t | jƒ d S )N)r   r3   )r7   r   r   r   r9   _  s    zTestOpenFunc.teardownc             C   sH   t | jƒ}tj|| jd�}t|ƒ |jƒ  tj|ƒ}t|ƒ |jƒ  d S )N)Zdestpath)r&   r3   r4   r:   r	   r"   )r7   rC   rP   r   r   r   Útest_DataSourceOpenb  s    

z TestOpenFunc.test_DataSourceOpenN)rZ   r[   r\   r8   r9   r†   r   r   r   r   r…   [  s   r…   c              C   s   t jƒ } | `| jƒ  d S )N)r4   r5   Z
_istmpdestÚ__del__)r6   r   r   r   Útest_del_attr_handlingm  s    rˆ   )N)9Ú
__future__r   r   r   r!   r^   rI   Útempfiler   r   r   Úshutilr   Znumpy.lib._datasourceÚlibZ_datasourcer4   Znumpy.testingr	   r
   r   r   r_   Úurllib.requestÚrequestr   Úurllib.parser   Úurllib.errorr   Úurllib2r   r   r   r   r)   r*   r+   r,   ru   rM   r&   r(   r   r-   r.   r/   r0   r1   Úobjectr2   r`   rb   r|   r€   r…   rˆ   r   r   r   r   Ú<module>   sP   

	WA!!