o
    ʩZhK)                     @   sT  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 d1ddZdad	d
 Zdd ZdZdZdZdZg d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 d-d. d.Z.d/d0 Z/dS )2    N)mkdtempmkstempNamedTemporaryFile)rmtree)assert_assert_equalassert_raises)urlparse)URLErrorc                 C   s    | t  krtdd}|S td)z$Stub to replace urlopen for testing.Zurltmp_)prefixzName or service not known)valid_httpurlr   r
   )urldatatmpfile r   W/var/www/html/lang_env/lib/python3.10/site-packages/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.txt)z/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      t t S r   )	http_path	http_filer   r   r   r   r   A      r   c                   C   r&   r   )http_fakepathhttp_fakefiler   r   r   r   invalid_httpurlE   r)   r,   c                   C      t S r   )r'   r   r   r   r   valid_baseurlI      r.   c                   C   r-   r   )r*   r   r   r   r   invalid_baseurlM   r/   r0   c                   C   r-   r   )r(   r   r   r   r   valid_httpfileQ   r/   r1   c                   C   r-   r   )r+   r   r   r   r   invalid_httpfileU   r/   r2   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      t  | _t| j| _d S r   r   tmpdir
datasource
DataSourcedsselfr   r   r   setup_methodZ      zTestDataSourceOpen.setup_methodc                 C      t | j | `d S r   r   r6   r9   r:   r   r   r   teardown_method^      
z"TestDataSourceOpen.teardown_methodc                 C   s"   | j t }t| |  d S r   )r9   openr   r   r   )r;   fhr   r   r   test_ValidHTTPb   s   z!TestDataSourceOpen.test_ValidHTTPc              
   C   s^   t  }tt| jj| z	| j| W d S  ty. } zt|jd u  W Y d }~d S d }~ww r   )r,   r   OSErrorr9   rB   r   errno)r;   r   er   r   r   test_InvalidHTTPg   s   z#TestDataSourceOpen.test_InvalidHTTPc                 C   s   t t| jjt  d S r   )r   r
   r9   _cacher,   r:   r   r   r   test_InvalidHTTPCacheURLErrorp      z0TestDataSourceOpen.test_InvalidHTTPCacheURLErrorc                 C   s*   t | j}| j|}t| |  d S r   )r#   r6   r9   rB   r   r   )r;   
local_filerC   r   r   r   test_ValidFiles   s   
z!TestDataSourceOpen.test_ValidFilec                 C   s   t | j}tt| jj| d S r   )r%   r6   r   rE   r9   rB   )r;   Zinvalid_filer   r   r   test_InvalidFiley   s   
z#TestDataSourceOpen.test_InvalidFilec                 C   s   zdd l }W n ty   t  Y nw tj| jd}||d}|	t
 |  | j|}| }|  tt
| d S )Nr   zfoobar.txt.gzw)gzipImportErrorpytestskipr   r"   joinr6   rB   write
magic_liner   r9   readliner   )r;   rP   filepathfpresultr   r   r   test_ValidGzipFile}      
z%TestDataSourceOpen.test_ValidGzipFilec                 C   s   zdd l }W n ty   t  Y nw tj| jd}||d}|	t
 |  | j|}| }|  tt
| d S )Nr   zfoobar.txt.bz2rO   )bz2rQ   rR   rS   r   r"   rT   r6   BZ2FilerU   rV   r   r9   rB   rW   r   )r;   r]   rX   rY   rZ   r   r   r   test_ValidBz2File   r\   z$TestDataSourceOpen.test_ValidBz2FileN)__name__
__module____qualname__r<   r@   rD   rH   rJ   rM   rN   r[   r_   r   r   r   r   r3   Y   s    	r3   c                   @   <   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   r4   r   r5   r:   r   r   r   r<      r=   z!TestDataSourceExists.setup_methodc                 C   r>   r   r?   r:   r   r   r   r@      rA   z$TestDataSourceExists.teardown_methodc                 C      t | jt  d S r   )r   r9   existsr   r:   r   r   r   rD      rK   z#TestDataSourceExists.test_ValidHTTPc                 C   s   t | jt d d S NF)r   r9   rf   r,   r:   r   r   r   rH      s   z%TestDataSourceExists.test_InvalidHTTPc                 C   sD   t | j}t| j| t }t |}t| j| t| d S r   )r#   r6   r   r9   rf   r   r   )r;   r   Zlocaldirr   r   r   rM      s   
z#TestDataSourceExists.test_ValidFilec                 C       t | j}t| j|d d S rg   )r%   r6   r   r9   rf   r;   r   r   r   r   rN         
z%TestDataSourceExists.test_InvalidFileN)	r`   ra   rb   r<   r@   rD   rH   rM   rN   r   r   r   r   rd      s    
rd   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   r6   r7   r8   r9   r:   r   r   r   r<      s   z"TestDataSourceAbspath.setup_methodc                 C   r>   r   r?   r:   r   r   r   r@      rA   z%TestDataSourceAbspath.teardown_methodc                 C   sN   t t \}}}}}}tj| j||tjd}t|| j	
t  d S N/)r	   r   r   r"   rT   r6   stripsepr   r9   rl   )r;   schemenetlocupathpmsqryfrg
local_pathr   r   r   rD      s
   z$TestDataSourceAbspath.test_ValidHTTPc                 C   sB   t | j}tj|d }t|| j| t|| j| d S N)r#   r6   r   r"   splitr   r9   rl   )r;   r   tmpfilenamer   r   r   rM      s   
z$TestDataSourceAbspath.test_ValidFilec                 C   sP   t t \}}}}}}tj| j||tjd}t|| j	
t k d S rm   )r	   r,   r   r"   rT   r6   ro   rp   r   r9   rl   r   )r;   rq   rr   rs   rt   ru   rv   Zinvalidhttpr   r   r   rH      s
   z&TestDataSourceAbspath.test_InvalidHTTPc                 C   sP   t | j}t | j}tj|d }t|| j|k t|| j|k d S rx   )r#   r6   r   r"   rz   r   r9   rl   )r;   Zinvalidfiler   r{   r   r   r   rN      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 ]}t|t
|  j t|| j q?d S )Nry   c                       t j j| S r   )r   r"   rl   r9   xr:   r   r   <lambda>       z7TestDataSourceAbspath.test_sandboxing.<locals>.<lambda>)r#   r6   r   r"   rz   r   r   
startswithr,   malicious_filesr'   )r;   r   r{   tmp_pathfnr   r:   r   test_sandboxing   s   
z%TestDataSourceAbspath.test_sandboxingc                 C   sJ   t j}zdt _|   |   |   |   |   W |t _d S |t _w N\)r   rp   rD   rM   rH   rN   r   r;   Zorig_os_sepr   r   r   test_windows_os_sep   s   
z)TestDataSourceAbspath.test_windows_os_sepN)r`   ra   rb   r<   r@   rD   rM   rH   rN   r   r   r   r   r   r   rk      s    	rk   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"   rl   r   r6   r7   
Repositoryr.   reposr:   r   r   r   r<      s   z"TestRepositoryAbspath.setup_methodc                 C   r>   r   r   r6   r   r:   r   r   r   r@     rA   z%TestRepositoryAbspath.teardown_methodc           	      C   sT   t t \}}}}}}tj| jj||tjd}| j	t
 }t|| d S rm   )r	   r   r   r"   rT   r   	_destpathro   rp   rl   r1   r   )	r;   rq   rr   rs   rt   ru   rv   rw   rX   r   r   r   rD     s   z$TestRepositoryAbspath.test_ValidHTTPc                    s\    fdd}t |t  j tD ]}t |t|  j t || j qd S )Nc                    r|   r   )r   r"   rl   r   r}   r:   r   r   r     r   z7TestRepositoryAbspath.test_sandboxing.<locals>.<lambda>)r   r1   r   r6   r   r'   )r;   r   r   r   r:   r   r     s   z%TestRepositoryAbspath.test_sandboxingc                 C   s2   t j}zdt _|   |   W |t _d S |t _w r   )r   rp   rD   r   r   r   r   r   r     s   
z)TestRepositoryAbspath.test_windows_os_sepN)r`   ra   rb   r<   r@   rD   r   r   r   r   r   r   r      s    r   c                   @   rc   )TestRepositoryExistsc                 C   s   t  | _tt | j| _d S r   )r   r6   r7   r   r.   r   r:   r   r   r   r<     s   z!TestRepositoryExists.setup_methodc                 C   r>   r   r   r:   r   r   r   r@   #  rA   z$TestRepositoryExists.teardown_methodc                 C   s   t | j}t| j| d S r   )r#   r6   r   r   rf   ri   r   r   r   rM   '  s   
z#TestRepositoryExists.test_ValidFilec                 C   rh   rg   )r%   r6   r   r   rf   ri   r   r   r   rN   ,  rj   z%TestRepositoryExists.test_InvalidFilec                 C   re   r   )r   r   rf   r   r:   r   r   r   test_RemoveHTTPFile0  rK   z(TestRepositoryExists.test_RemoveHTTPFilec           
      C   sT   t  }t|\}}}}}}tj| jj|}t|d t|}	t	| j
|	 d S )Ni  )r   r	   r   r"   rT   r   r   mkdirr#   r   rf   )
r;   	localfilerq   rr   rs   rt   ru   rv   rw   r   r   r   r   test_CachedHTTPFile3  s   z(TestRepositoryExists.test_CachedHTTPFileN)	r`   ra   rb   r<   r@   rM   rN   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   r6   r:   r   r   r   r<   @  s   zTestOpenFunc.setup_methodc                 C   s   t | j d S r   )r   r6   r:   r   r   r   r@   C  s   zTestOpenFunc.teardown_methodc                 C   sH   t | j}tj|| jd}t| |  t|}t| |  d S )N)Zdestpath)r#   r6   r7   rB   r   r   )r;   rL   rY   r   r   r   test_DataSourceOpenF  s   

z TestOpenFunc.test_DataSourceOpenN)r`   ra   rb   r<   r@   r   r   r   r   r   r   ?  s    r   c                  C   s   t  } | `|   d S r   )r7   r8   Z
_istmpdest__del__)r9   r   r   r   test_del_attr_handlingQ  s   r   r   )0r   rR   tempfiler   r   r   shutilr   Znumpy.lib._datasourcelibZ_datasourcer7   Znumpy.testingr   r   r   urllib.requestrequestr   urllib.parser	   urllib.errorr
   r   r   r   r   r'   r(   r*   r+   r   rV   r#   r%   r   r,   r.   r0   r1   r2   r3   rd   rk   r   r   r   r   r   r   r   r   <module>   sD    
	EA!!