a :jgK)@sTddlZddlZddlmZmZmZddlmZddlm m Z ddl m Z mZmZddlmZddlmZddlmZd1ddZdad d Zd d Zd ZdZdZdZgdZdZ ddZ!ddZ"ddZ#ddZ$ddZ%ddZ&dd Z'd!d"Z(Gd#d$d$Z)Gd%d&d&Z*Gd'd(d(Z+Gd)d*d*Z,Gd+d,d,Z-Gd-d.d.Z.d/d0Z/dS)2N)mkdtempmkstempNamedTemporaryFile)rmtree)assert_ assert_equal assert_raises)urlparse)URLErrorcCs$|tkrtdd}|StddS)z$Stub to replace urlopen for testing.Zurltmp_)prefixzName or service not knownN) valid_httpurlrr )urldatatmpfilerJ/usr/local/lib/python3.9/site-packages/numpy/lib/tests/test__datasource.py urlopen_stubs  rcCstjatt_dSN)urllib_requesturlopen old_urlopenrrrrr setup_modulesrcCs tt_dSr)rrrrrrrteardown_module!srzhttp://www.google.com/z index.htmlzhttp://fake.abc.web/site/zfake.txt)z /etc/shadowz ../../shadowz ..\system.datzc:\windows\system.datsthree is the magic numbercCs"tdd|dd\}}t||S)N.txtdstmp_T)suffixr dirtext)roscloseZfiledirfdpathrrrvalid_textfile2s r#cCs*tdd|d\}}t|t||S)Nrr)rr r)rrrremover rrrinvalid_textfile9s  r%cCsttSr) http_path http_filerrrrr Asr cCsttSr) http_fakepath http_fakefilerrrrinvalid_httpurlEsr*cCstSr)r&rrrr valid_baseurlIsr+cCstSr)r(rrrrinvalid_baseurlMsr,cCstSr)r'rrrrvalid_httpfileQsr-cCstSr)r)rrrrinvalid_httpfileUsr.c@sTeZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ dS)TestDataSourceOpencCst|_t|j|_dSrrtmpdir datasource DataSourcedsselfrrr setup_methodZszTestDataSourceOpen.setup_methodcCst|j|`dSrrr1r4r5rrrteardown_method^s z"TestDataSourceOpen.teardown_methodcCs"|jt}t||dSr)r4openr rr)r6fhrrrtest_ValidHTTPbsz!TestDataSourceOpen.test_ValidHTTPc Cs^t}tt|jj|z|j|Wn2tyX}zt|jduWYd}~n d}~00dSr)r*rOSErrorr4r:rerrno)r6r errrtest_InvalidHTTPgs z#TestDataSourceOpen.test_InvalidHTTPcCstt|jjtdSr)rr r4_cacher*r5rrrtest_InvalidHTTPCacheURLErrorpsz0TestDataSourceOpen.test_InvalidHTTPCacheURLErrorcCs*t|j}|j|}t||dSr)r#r1r4r:rr)r6 local_filer;rrrtest_ValidFiless  z!TestDataSourceOpen.test_ValidFilecCst|j}tt|jj|dSr)r%r1rr=r4r:)r6Z invalid_filerrrtest_InvalidFileys z#TestDataSourceOpen.test_InvalidFilecCsz ddl}Wnty&tYn0tj|jd}||d}| t | |j |}| }| tt |dS)Nrz foobar.txt.gzw)gzip ImportErrorpytestskiprr"joinr1r:write magic_linerr4readliner)r6rGfilepathfpresultrrrtest_ValidGzipFile}s     z%TestDataSourceOpen.test_ValidGzipFilecCsz ddl}Wnty&tYn0tj|jd}||d}| t | |j |}|}| tt |dS)Nrzfoobar.txt.bz2rF)bz2rHrIrJrr"rKr1BZ2FilerLrMrr4r:rNr)r6rSrOrPrQrrrtest_ValidBz2Files     z$TestDataSourceOpen.test_ValidBz2FileN) __name__ __module__ __qualname__r7r9r<r@rBrDrErRrUrrrrr/Ys r/c@s<eZdZddZddZddZddZd d Zd d Zd S)TestDataSourceExistscCst|_t|j|_dSrr0r5rrrr7sz!TestDataSourceExists.setup_methodcCst|j|`dSrr8r5rrrr9s z$TestDataSourceExists.teardown_methodcCst|jtdSr)rr4existsr r5rrrr<sz#TestDataSourceExists.test_ValidHTTPcCst|jtddSNF)rr4rZr*r5rrrr@sz%TestDataSourceExists.test_InvalidHTTPcCsDt|j}t|j|t}t|}t|j|t|dSr)r#r1rr4rZrr)r6rZlocaldirrrrrDs  z#TestDataSourceExists.test_ValidFilecCs t|j}t|j|ddSr[)r%r1rr4rZr6rrrrrEs z%TestDataSourceExists.test_InvalidFileN) rVrWrXr7r9r<r@rDrErrrrrYs  rYc@sLeZdZddZddZddZddZd d Zd d Zd dZ ddZ dS)TestDataSourceAbspathcCs"tjt|_t|j|_dSr)rr"abspathrr1r2r3r4r5rrrr7sz"TestDataSourceAbspath.setup_methodcCst|j|`dSrr8r5rrrr9s z%TestDataSourceAbspath.teardown_methodcCsNtt\}}}}}}tj|j||tjd}t||j tdSN/) r r rr"rKr1stripseprr4r^)r6schemenetlocupathpmsqryfrg local_pathrrrr<s  z$TestDataSourceAbspath.test_ValidHTTPcCsBt|j}tj|d}t||j|t||j|dSN)r#r1rr"splitrr4r^)r6r tmpfilenamerrrrDs z$TestDataSourceAbspath.test_ValidFilecCsPtt\}}}}}}tj|j||tjd}t||j t kdSr_) r r*rr"rKr1rarbrr4r^r )r6rcrdrerfrgrhZ invalidhttprrrr@s  z&TestDataSourceAbspath.test_InvalidHTTPcCsPt|j}t|j}tj|d}t||j|kt||j|kdSrj)r#r1rr"rlrr4r^)r6Z invalidfilerrmrrrrEs   z&TestDataSourceAbspath.test_InvalidFilecstj}tj|d}fdd}t|tjt|tjt||jt||jt D]0}t|t |jt||jq~dS)Nrkcstjj|Sr)rr"r^r4xr5rrz7TestDataSourceAbspath.test_sandboxing..) r#r1rr"rlrr startswithr*malicious_filesr&)r6rrmtmp_pathfnrr5rtest_sandboxings  z%TestDataSourceAbspath.test_sandboxingcCsLtj}z8dt_|||||W|t_n|t_0dSN\)rrbr<rDr@rErvr6Z orig_os_seprrrtest_windows_os_seps z)TestDataSourceAbspath.test_windows_os_sepN) rVrWrXr7r9r<rDr@rErvrzrrrrr]s r]c@s4eZdZddZddZddZddZd d Zd S) TestRepositoryAbspathcCs&tjt|_tt|j|_dSr) rr"r^rr1r2 Repositoryr+reposr5rrrr7sz"TestRepositoryAbspath.setup_methodcCst|j|`dSrrr1r}r5rrrr9s z%TestRepositoryAbspath.teardown_methodc CsTtt\}}}}}}tj|jj||tjd}|j t }t ||dSr_) r r rr"rKr} _destpathrarbr^r-r) r6rcrdrerfrgrhrirOrrrr<s z$TestRepositoryAbspath.test_ValidHTTPcs\fdd}t|tjtD]0}t|t|jt||jq&dS)Ncstjj|Sr)rr"r^r}rnr5rrrprqz7TestRepositoryAbspath.test_sandboxing..)rr-rrr1rsr&)r6rtrurr5rrv s  z%TestRepositoryAbspath.test_sandboxingcCs4tj}z dt_||W|t_n|t_0dSrw)rrbr<rvryrrrrzs  z)TestRepositoryAbspath.test_windows_os_sepN)rVrWrXr7r9r<rvrzrrrrr{s r{c@s<eZdZddZddZddZddZd d Zd d Zd S)TestRepositoryExistscCst|_tt|j|_dSr)rr1r2r|r+r}r5rrrr7sz!TestRepositoryExists.setup_methodcCst|j|`dSrr~r5rrrr9#s z$TestRepositoryExists.teardown_methodcCst|j}t|j|dSr)r#r1rr}rZr\rrrrD's z#TestRepositoryExists.test_ValidFilecCs t|j}t|j|ddSr[)r%r1rr}rZr\rrrrE,s z%TestRepositoryExists.test_InvalidFilecCst|jtdSr)rr}rZr r5rrrtest_RemoveHTTPFile0sz(TestRepositoryExists.test_RemoveHTTPFilec CsTt}t|\}}}}}}tj|jj|}t|dt|} t |j | dS)Ni) r r rr"rKr}rmkdirr#rrZ) r6 localfilercrdrerfrgrhrirrrrtest_CachedHTTPFile3s  z(TestRepositoryExists.test_CachedHTTPFileN) rVrWrXr7r9rDrErrrrrrrs rc@s$eZdZddZddZddZdS) TestOpenFunccCs t|_dSr)rr1r5rrrr7@szTestOpenFunc.setup_methodcCst|jdSr)rr1r5rrrr9CszTestOpenFunc.teardown_methodcCsHt|j}tj||jd}t||t|}t||dS)N)Zdestpath)r#r1r2r:rr)r6rCrPrrrtest_DataSourceOpenFs  z TestOpenFunc.test_DataSourceOpenN)rVrWrXr7r9rrrrrr?srcCst}|`|dSr)r2r3Z _istmpdest__del__)r4rrrtest_del_attr_handlingQsr)N)0rrItempfilerrrshutilrZnumpy.lib._datasourcelibZ _datasourcer2Z numpy.testingrrrurllib.requestrequestr urllib.parser urllib.errorr rrrrr&r'r(r)rsrMr#r%r r*r+r,r-r.r/rYr]r{rrrrrrrsB     EA!!