o 6a(@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 |tkr tdd}|Std)z$Stub to replace urlopen for testing.urltmp_)prefixzName or service not known) valid_httpurlrr )urldatatmpfilerB/usr/lib/python3/dist-packages/numpy/lib/tests/test__datasource.py urlopen_stubs  rcCstjatt_dSN)urllib_requesturlopen old_urlopenrrrrr setup_modules rcCs tt_dSr)rrrrrrrteardown_module!s rzhttp://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)rosclosefiledirfdpathrrrvalid_textfile2s r%cCs*tdd|d\}}t|t||S)Nrr)rr r)rrr remover!rrrinvalid_textfile9s  r'cCttSr) http_path http_filerrrrr Ar cCr(r) http_fakepath http_fakefilerrrrinvalid_httpurlEr+r.cCtSr)r)rrrr valid_baseurlIr0cCr/r)r,rrrrinvalid_baseurlMr1r2cCr/r)r*rrrrvalid_httpfileQr1r3cCr/r)r-rrrrinvalid_httpfileUr1r4c@sTeZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ dS)TestDataSourceOpencCt|_t|j|_dSrrtmpdir datasource DataSourcedsselfrrrsetupZzTestDataSourceOpen.setupcCt|j|`dSrrr8r;r<rrrteardown^ zTestDataSourceOpen.teardowncCs"|jt}t||dSr)r;openr rr )r=fhrrrtest_ValidHTTPbs z!TestDataSourceOpen.test_ValidHTTPc Cs^t}tt|jj|z |j|WdSty.}zt|jduWYd}~dSd}~wwr)r.rIOErrorr;rDrerrno)r=rerrrtest_InvalidHTTPgsz#TestDataSourceOpen.test_InvalidHTTPcCstt|jjtdSr)rr r;_cacher.r<rrrtest_InvalidHTTPCacheURLErrorpz0TestDataSourceOpen.test_InvalidHTTPCacheURLErrorcCs*t|j}|j|}t||dSr)r%r8r;rDrr )r= local_filerErrrtest_ValidFiless   z!TestDataSourceOpen.test_ValidFilecCst|j}tt|jj|dSr)r'r8rrGr;rD)r= invalid_filerrrtest_InvalidFileys z#TestDataSourceOpen.test_InvalidFilecCszddl}Wn tytYnwtj|jd}||d}| t | |j |}| }| tt |dS)Nrz foobar.txt.gzw)gzip ImportErrorpytestskiprr$joinr8rDwrite magic_liner r;readliner)r=rSfilepathfpresultrrrtest_ValidGzipFile}      z%TestDataSourceOpen.test_ValidGzipFilecCszddl}Wn tytYnwtj|jd}||d}| t | |j |}|}| tt |dS)Nrzfoobar.txt.bz2rR)bz2rTrUrVrr$rWr8BZ2FilerXrYr r;rDrZr)r=r`r[r\r]rrrtest_ValidBz2Filer_z$TestDataSourceOpen.test_ValidBz2FileN) __name__ __module__ __qualname__r>rBrFrJrLrOrQr^rbrrrrr5Ys  r5c@<eZdZddZddZddZddZd d Zd d Zd S)TestDataSourceExistscCr6rr7r<rrrr>r?zTestDataSourceExists.setupcCr@rrAr<rrrrBrCzTestDataSourceExists.teardowncCt|jtdSr)rr;existsr r<rrrrFrMz#TestDataSourceExists.test_ValidHTTPcCst|jtddSNF)rr;rir.r<rrrrJsz%TestDataSourceExists.test_InvalidHTTPcCsDt|j}t|j|t}t|}t|j|t|dSr)r%r8rr;rirr)r=rlocaldirrrrrOs  z#TestDataSourceExists.test_ValidFilecC t|j}t|j|ddSrj)r'r8rr;rir=rrrrrQ z%TestDataSourceExists.test_InvalidFileN) rcrdrer>rBrFrJrOrQrrrrrgs rgc@sLeZdZddZddZddZddZd d Zd d Zd dZ ddZ dS)TestDataSourceAbspathcCs"tjt|_t|j|_dSr)rr$abspathrr8r9r:r;r<rrrr>szTestDataSourceAbspath.setupcCr@rrAr<rrrrBrCzTestDataSourceAbspath.teardowncCsNtt\}}}}}}tj|j||tjd}t||j tdSN/) r r rr$rWr8stripseprr;rp)r=schemenetlocupathpmsqryfrg local_pathrrrrFs  z$TestDataSourceAbspath.test_ValidHTTPcCsBt|j}tj|d}t||j|t||j|dSN)r%r8rr$splitrr;rp)r=r tmpfilenamerrrrOs z$TestDataSourceAbspath.test_ValidFilecCsPtt\}}}}}}tj|j||tjd}t||j t kdSrq) r r.rr$rWr8rsrtrr;rpr )r=rurvrwrxryrz invalidhttprrrrJs  z&TestDataSourceAbspath.test_InvalidHTTPcCsPt|j}t|j}tj|d}t||j|kt||j|kdSr|)r%r8rr$r~rr;rp)r= invalidfilerrrrrrQs  z&TestDataSourceAbspath.test_InvalidFilecstj}tj|d}fdd}t|tjt|tjt||jt||jt D]}t|t |jt||jq?dS)Nr}ctjj|Sr)rr$rpr;xr<rrz7TestDataSourceAbspath.test_sandboxing..) r%r8rr$r~rr startswithr.malicious_filesr))r=rrtmp_pathfnrr<rtest_sandboxings  z%TestDataSourceAbspath.test_sandboxingcCsJtj}zdt_|||||W|t_dS|t_wN\)rrtrFrOrJrQrr= orig_os_seprrrtest_windows_os_seps z)TestDataSourceAbspath.test_windows_os_sepN) rcrdrer>rBrFrOrJrQrrrrrrros roc@s4eZdZddZddZddZddZd d Zd S) TestRepositoryAbspathcCs&tjt|_tt|j|_dSr) rr$rprr8r9 Repositoryr0reposr<rrrr>szTestRepositoryAbspath.setupcCr@rrr8rr<rrrrBrCzTestRepositoryAbspath.teardownc CsTtt\}}}}}}tj|jj||tjd}|j t }t ||dSrq) r r rr$rWr _destpathrsrtrpr3r) r=rurvrwrxryrzr{r[rrrrFs z$TestRepositoryAbspath.test_ValidHTTPcs\fdd}t|tjtD]}t|t|jt||jqdS)Ncrr)rr$rprrr<rrrrz7TestRepositoryAbspath.test_sandboxing..)rr3rr8rr))r=rrrr<rr s z%TestRepositoryAbspath.test_sandboxingcCs2tj}zdt_||W|t_dS|t_wr)rrtrFrrrrrrs  z)TestRepositoryAbspath.test_windows_os_sepN)rcrdrer>rBrFrrrrrrrs  rc@rf)TestRepositoryExistscCst|_tt|j|_dSr)rr8r9rr0rr<rrrr>szTestRepositoryExists.setupcCr@rrr<rrrrB#rCzTestRepositoryExists.teardowncCst|j}t|j|dSr)r%r8rrrirmrrrrO's z#TestRepositoryExists.test_ValidFilecCrlrj)r'r8rrrirmrrrrQ,rnz%TestRepositoryExists.test_InvalidFilecCrhr)rrrir r<rrrtest_RemoveHTTPFile0rMz(TestRepositoryExists.test_RemoveHTTPFilec CsTt}t|\}}}}}}tj|jj|}t|dt|} t |j | dS)Ni) r r rr$rWrrmkdirr%rri) r= localfilerurvrwrxryrzr{rrrrtest_CachedHTTPFile3s  z(TestRepositoryExists.test_CachedHTTPFileN) rcrdrer>rBrOrQrrrrrrrs rc@s$eZdZddZddZddZdS) TestOpenFunccCs t|_dSr)rr8r<rrrr>@s zTestOpenFunc.setupcCst|jdSr)rr8r<rrrrBCszTestOpenFunc.teardowncCsHt|j}tj||jd}t||t|}t||dS)N)destpath)r%r8r9rDrr )r=rNr\rrrtest_DataSourceOpenFs   z TestOpenFunc.test_DataSourceOpenN)rcrdrer>rBrrrrrr?s rcCst}|`|dSr)r9r: _istmpdest__del__)r;rrrtest_del_attr_handlingQs rr)0rrUtempfilerrrshutilrnumpy.lib._datasourcelib _datasourcer9 numpy.testingrrrurllib.requestrequestr urllib.parser urllib.errorr rrrrr)r*r,r-rrYr%r'r r.r0r2r3r4r5rgrorrrrrrrrsD      EA!!