o
    hK)                     @   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.urltmp_)prefixzName or service not known)valid_httpurlr   r
   )urldatatmpfile r   t/var/www/html/construction_image-detection-poc/venv/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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      r0   c                   C   r/   r   )r,   r   r   r   r   invalid_baseurlM   r1   r2   c                   C   r/   r   )r*   r   r   r   r   valid_httpfileQ   r1   r3   c                   C   r/   r   )r-   r   r   r   r   invalid_httpfileU   r1   r4   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   r8   r;   r<   r   r   r   teardown_method^      
z"TestDataSourceOpen.teardown_methodc                 C   s"   | j t }t| |  d S r   )r;   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   OSErrorr;   rD   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
   r;   _cacher.   r<   r   r   r   test_InvalidHTTPCacheURLErrorp      z0TestDataSourceOpen.test_InvalidHTTPCacheURLErrorc                 C   s*   t | j}| j|}t| |  d S r   )r%   r8   r;   rD   r   r    )r=   
local_filerE   r   r   r   test_ValidFiles   s   
z!TestDataSourceOpen.test_ValidFilec                 C   s   t | j}tt| jj| d S r   )r'   r8   r   rG   r;   rD   )r=   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$   joinr8   rD   write
magic_liner    r;   readliner   )r=   rS   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.bz2rR   )bz2rT   rU   rV   r   r$   rW   r8   BZ2FilerX   rY   r    r;   rD   rZ   r   )r=   r`   r[   r\   r]   r   r   r   test_ValidBz2File   r_   z$TestDataSourceOpen.test_ValidBz2FileN)__name__
__module____qualname__r>   rB   rF   rJ   rL   rO   rQ   r^   rb   r   r   r   r   r5   Y   s    	r5   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   r6   r   r7   r<   r   r   r   r>      r?   z!TestDataSourceExists.setup_methodc                 C   r@   r   rA   r<   r   r   r   rB      rC   z$TestDataSourceExists.teardown_methodc                 C      t | jt  d S r   )r   r;   existsr   r<   r   r   r   rF      rM   z#TestDataSourceExists.test_ValidHTTPc                 C   s   t | jt d d S NF)r   r;   ri   r.   r<   r   r   r   rJ      s   z%TestDataSourceExists.test_InvalidHTTPc                 C   sD   t | j}t| j| t }t |}t| j| t| d S r   )r%   r8   r   r;   ri   r   r   )r=   r   localdirr   r   r   rO      s   
z#TestDataSourceExists.test_ValidFilec                 C       t | j}t| j|d d S rj   )r'   r8   r   r;   ri   r=   r   r   r   r   rQ         
z%TestDataSourceExists.test_InvalidFileN)	rc   rd   re   r>   rB   rF   rJ   rO   rQ   r   r   r   r   rg      s    
rg   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   r8   r9   r:   r;   r<   r   r   r   r>      s   z"TestDataSourceAbspath.setup_methodc                 C   r@   r   rA   r<   r   r   r   rB      rC   z%TestDataSourceAbspath.teardown_methodc                 C   sN   t t \}}}}}}tj| j||tjd}t|| j	
t  d S N/)r	   r   r   r$   rW   r8   stripsepr   r;   rp   )r=   schemenetlocupathpmsqryfrg
local_pathr   r   r   rF      s
   z$TestDataSourceAbspath.test_ValidHTTPc                 C   sB   t | j}tj|d }t|| j| t|| j| d S N)r%   r8   r   r$   splitr   r;   rp   )r=   r   tmpfilenamer   r   r   rO      s   
z$TestDataSourceAbspath.test_ValidFilec                 C   sP   t t \}}}}}}tj| j||tjd}t|| j	
t k d S rq   )r	   r.   r   r$   rW   r8   rs   rt   r   r;   rp   r   )r=   ru   rv   rw   rx   ry   rz   invalidhttpr   r   r   rJ      s
   z&TestDataSourceAbspath.test_InvalidHTTPc                 C   sP   t | j}t | j}tj|d }t|| j|k t|| j|k d S r|   )r%   r8   r   r$   r~   r   r;   rp   )r=   invalidfiler   r   r   r   r   rQ      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 )Nr}   c                       t j j| S r   )r   r$   rp   r;   xr<   r   r   <lambda>       z7TestDataSourceAbspath.test_sandboxing.<locals>.<lambda>)r%   r8   r   r$   r~   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   rt   rF   rO   rJ   rQ   r   r=   orig_os_sepr   r   r   test_windows_os_sep   s   
z)TestDataSourceAbspath.test_windows_os_sepN)rc   rd   re   r>   rB   rF   rO   rJ   rQ   r   r   r   r   r   r   ro      s    	ro   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$   rp   r   r8   r9   
Repositoryr0   reposr<   r   r   r   r>      s   z"TestRepositoryAbspath.setup_methodc                 C   r@   r   r   r8   r   r<   r   r   r   rB     rC   z%TestRepositoryAbspath.teardown_methodc           	      C   sT   t t \}}}}}}tj| jj||tjd}| j	t
 }t|| d S rq   )r	   r   r   r$   rW   r   	_destpathrs   rt   rp   r3   r   )	r=   ru   rv   rw   rx   ry   rz   r{   r[   r   r   r   rF     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$   rp   r   r   r<   r   r   r     r   z7TestRepositoryAbspath.test_sandboxing.<locals>.<lambda>)r   r3   r   r8   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   rt   rF   r   r   r   r   r   r     s   
z)TestRepositoryAbspath.test_windows_os_sepN)rc   rd   re   r>   rB   rF   r   r   r   r   r   r   r      s    r   c                   @   rf   )TestRepositoryExistsc                 C   s   t  | _tt | j| _d S r   )r   r8   r9   r   r0   r   r<   r   r   r   r>     s   z!TestRepositoryExists.setup_methodc                 C   r@   r   r   r<   r   r   r   rB   #  rC   z$TestRepositoryExists.teardown_methodc                 C   s   t | j}t| j| d S r   )r%   r8   r   r   ri   rm   r   r   r   rO   '  s   
z#TestRepositoryExists.test_ValidFilec                 C   rl   rj   )r'   r8   r   r   ri   rm   r   r   r   rQ   ,  rn   z%TestRepositoryExists.test_InvalidFilec                 C   rh   r   )r   r   ri   r   r<   r   r   r   test_RemoveHTTPFile0  rM   z(TestRepositoryExists.test_RemoveHTTPFilec           
      C   sT   t  }t|\}}}}}}tj| jj|}t|d t|}	t	| j
|	 d S )Ni  )r   r	   r   r$   rW   r   r   mkdirr%   r   ri   )
r=   	localfileru   rv   rw   rx   ry   rz   r{   r   r   r   r   test_CachedHTTPFile3  s   z(TestRepositoryExists.test_CachedHTTPFileN)	rc   rd   re   r>   rB   rO   rQ   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   r8   r<   r   r   r   r>   @  s   zTestOpenFunc.setup_methodc                 C   s   t | j d S r   )r   r8   r<   r   r   r   rB   C  s   zTestOpenFunc.teardown_methodc                 C   sH   t | j}tj|| jd}t| |  t|}t| |  d S )N)destpath)r%   r8   r9   rD   r   r    )r=   rN   r\   r   r   r   test_DataSourceOpenF  s   

z TestOpenFunc.test_DataSourceOpenN)rc   rd   re   r>   rB   r   r   r   r   r   r   ?  s    r   c                  C   s   t  } | `|   d S r   )r9   r:   
_istmpdest__del__)r;   r   r   r   test_del_attr_handlingQ  s   r   r   )0r   rU   tempfiler   r   r   shutilr   numpy.lib._datasourcelib_datasourcer9   numpy.testingr   r   r   urllib.requestrequestr   urllib.parser	   urllib.errorr
   r   r   r   r   r)   r*   r,   r-   r   rY   r%   r'   r   r.   r0   r2   r3   r4   r5   rg   ro   r   r   r   r   r   r   r   r   <module>   sD    
	EA!!