a ze$u@sddlZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z m Z mZmZddlZddlZddlZddlZddlZddlmZmZmZz ddlZWneydZYn0dZeeddZdZd d Zd d Z eZ!eee!e"eZ#d Z$dZ%dZ&ej'(dZ)ej'(dZ*ej+,e$dZ-dZ.Gdddej/Z0Gddde0Z1e 2Gddde1Z3Gdddej/Z4ddZ5e6dkre7dS)N)support)ZipFileZipInfo ZIP_STORED ZIP_DEFLATED) extract_tb extract_stackprint_tbzHdef get_name(): return __name__ def get_file(): return __file__ execz def do_raise(): raise TypeError cCs6t|}tjjtddt|d@|d@|}|S)NzZ'd?d@Z(dAdBZ)dCdDZ*dEdFZ+dGdHZ,dIdJZ-e.e/j0dKudLdMdNZ1dOdPZ2dQdRZ3dSdTZ4dUdVZ5dKS)WUncompressedZipImportTestCasecCs ttjt|dSr&) linecache clearcache zipimport_zip_directory_cacher+r%r/r-rrrr/Ps z#UncompressedZipImportTestCase.setUpc Cs|tj||D]\}\}}tj||}|dtjkrVtj|st |qtj |}tj|sxt |t |d}| |Wdq1s0YqdS)Nwb) addCleanuprrmtreeitemsrr!joinr isdirmakedirsdirnameopenwrite) r.filesZdirNamenamerrr!ZdnamefprrrmakeTreeWs      z&UncompressedZipImportTestCase.makeTreec Ks|tj|t|dd}|D]0\}\}}t|t|}|j|_ | ||q"| dd} | durn| |_ Wdn1s0Y| dd} | durt |d} | }Wdn1s0Yt |d$} | | | |Wdn1s 0YdS)Nwcommentstuffrbr:)r;runlinkrr=rtime localtime compression compress_typewritestrgetrIrBreadrC) r.rDZzipNamekwzrErrzinforIrJfrrrmakeZiphs   $   &  z%UncompressedZipImportTestCase.makeZipcOsz|j|fi|tjdttd|}|d}|durJ|||rv| }| |t jjtg|R|dS)Nrrcall) rXr'r!insertTEMP_ZIPr import_moduler>rRZget_file assertEqualr)r.Z expected_extrDmodulesrTmodrYfilerrrdoTests z$UncompressedZipImportTestCase.doTestcCsdtjvr|ddtjvr&tjd=dttfi}z|d|dWn&tyj|jt krf| dYn0|jt kr| ddS)Nzlibzzlib is a builtin modulezzlib.pyr#z&expected test to not raise ImportErrorz"expected test to raise ImportError) r'builtin_module_namesZskipTestr^NOWtest_srcra ImportErrorrOrfailrr.rDrrr testAFakeZlibs       z+UncompressedZipImportTestCase.testAFakeZlibcCs"tdttfi}|d|tdSNr#TESTMODrdrerarhrrrtestPysz$UncompressedZipImportTestCase.testPycCs"ttttfi}|t|tdSr&)rlpyc_extrdtest_pycrarhrrrtestPycsz%UncompressedZipImportTestCase.testPyccCs.tdttfttttfi}|t|tdSrj)rlrdrernrorarhrrrtestBoths  z&UncompressedZipImportTestCase.testBothcsjd}tj|}tjt|dd|d}tdtdftdtd|fi}fd d }jd|t|d dS) N state = 'old'???r Fr# state = 'new'r$cs|jddS)Noldr]stater_r-rrcheckszFUncompressedZipImportTestCase.testUncheckedHashBasedPyc..checkrY rr source_hash_bootstrap_external_code_to_hash_pyccompilerlrdrar.sourcer}bytecoderDrzrr-rtestUncheckedHashBasedPycs    z7UncompressedZipImportTestCase.testUncheckedHashBasedPycz_imp.check_hash_based_pycsalwayscsjd}tj|}tjt|dd|d}tdtdftdtd|fi}fd d }jd|t|d dS) Nrrrsr Fr#rtr$rucs|jddS)Nnewrwryr-rrrzszOUncompressedZipImportTestCase.test_checked_hash_based_change_pyc..checkr{r|rrr-r"test_checked_hash_based_change_pycs    z@UncompressedZipImportTestCase.test_checked_hash_based_change_pyccCs"tdtdfi}|d|tdS)Nr#)rlrdrarhrrr testEmptyPysz)UncompressedZipImportTestCase.testEmptyPycCsFtt}|ddN<tdttfttt|fi}|d|tdS)Nrr# bytearrayrorlrdrernrar.Z badmagic_pycrDrrr testBadMagics   z*UncompressedZipImportTestCase.testBadMagiccCs\tt}|ddN<ttt|fi}z|d|tWntyLYn 0|ddS)Nrrr#z)expected ImportError; import from bad pyc)rrorlrnrdrarfrgrrrr testBadMagic2s z+UncompressedZipImportTestCase.testBadMagic2cCsFtt}|ddN<tdttfttt|fi}|d|tdS)N r#r)r.Z badtime_pycrDrrr testBadMTimes   z*UncompressedZipImportTestCase.testBadMTimecCs>ttdtt}tdttfttt|fi}|d|tdS)Nr r#)rtest_colenrerlrdrnra)r.Ztwenty_thirty_eight_pycrDrrr test2038MTimes   z+UncompressedZipImportTestCase.test2038MTimecCsBttj}|dtttf|ttttfi}|t|ttdSN__init__)TESTPACKrr rnrdrorlra)r.packdirrDrrr testPackages  z)UncompressedZipImportTestCase.testPackagecCsbttj}|ttj}|dtttf|dtttf|ttttfi}|t|tttdSr) rrr TESTPACK2rnrdrorlrar.rpackdir2rDrrrtestSubPackage s z,UncompressedZipImportTestCase.testSubPackagecCsRttj}|ttj}|tdf|tdf|ttttfi}|t|tttdSNr) rrr rrdrlrnrorarrrrtestSubNamespacePackages z5UncompressedZipImportTestCase.testSubNamespacePackagec CsRttj}|ttj}|tdtj}|tdf|ttttf|tdf|tdf|ttttf|tdtttf|ttttfi}|tdf|tdtttf|tdf|tdtttf|ttttfi}tj d}| ||t }| ||t jd|t jd|tt}|dt|j|j\} } |tjt | tjd|d| tjdt|tjddd }|d t|jt|jddtjd } ||dd | tdttf}|d|jtjd tdttdf}|tjt |jtjd dttf} t| }|dt|j|j\} } |tjt | tjd |d| tjd td| tf}|tjt |jtjd td| tdf}|tjt |jtjd td| tdf}|d|jtjd dS) N3r2 path1.ziprrrr9)rrr rrdrlrnror!abspathrXTEMP_DIRrGr'rZrr\r]r__path__basenamesplitrlistr>__file__) r.rrpackdir3files1files2zip1zip2r_p1p2mpathsubpkgrrrtestMixedNamespacePackagesl          z7UncompressedZipImportTestCase.testMixedNamespacePackagec Cs2ttj}|ttj}|tdtj}|tdf|ttttf|tdf|tdf|ttttf|tdtttf|ttttfi}tj d}| |||tdf|tdtttf|tdf|tdtttf|ttttfi}tj d}| ||t j d|t j d|t t}|dt|j|j\} } |d| tjd|d| tjdt |tjd dd }|d t|jt|jddtjd } ||dd | t d ttf}|d|jtjd t d ttdf}|d|jtjd d ttf} t | }|dt|j|j\} } |d| tjd |d| tjd t d | tf}|d|jtjd t d | tdf}|d|jtjd t d | tdf}|d|jtjd dS)Nrrrrz path2.ziprrrrr9rrr)rrr rrdrlrnror!rrXr'rZrr\r]rrrrrr>r) r.rrrrrrrr_rrrrrrrtestNamespacePackageks`         z2UncompressedZipImportTestCase.testNamespacePackagec Csttj}|ttj}|dtttf|dtttf|ttttfdtttfi}|t j t t t dP}| D]6\}\}}t|t|}|j|_d|_|||qzWdn1s0Ytt } || jt || td| d} || || tj|| d| d} || d| j | t} || t| j t!"tj#d} tj$%t t}|| ||| |dd|| |td|| |td|t}t&|}t!"|} |'|t(j)v|| *td|| *|d|| || j | j+}||*|d|||| j tt tjt}||jt ||j,ttjdS)NrZspamrHsspamTrF)-rrr rrnrdrorlr;rrLr[rr=rrMrNrOrPrIrQr7 zipimporterr]archive is_package find_moduleassertIsNotNoneassertIsInstance assertFalse load_module get_filenamerrr\rr!r>r" assertTruer'r^ get_source __loader__prefix)r.rrrDrUrErrrVziZfind_modload_modr_Zexisting_pack_pathZexpected_path_pathmod_pathmod_nameloaderzi2rrrtestZipImporterMethodssX   ,       z4UncompressedZipImportTestCase.testZipImporterMethodscCsPttj}|ttj}|dtttf|ttttfi}|t j t t t dP}| D]6\}\}}t|t|}|j|_d|_|||q^Wdn1s0Ytt tj|} || jt || j||| td| t} || t| j|| ttjdd|| ttjtdt tj|t} t| } | t} || | | tj|!| t| t}|| t|jttjt}t"|}t#$|} |%|t&j'v|| (td|| (|d|| || j| j)}||(|d|||| jdS)NrrHseggsTF)*rrr rrnrdrorlr;rrLr[rr=rrMrNrOrPrIrQr7rr]rrrrrrrrrrr"rr\rr'r^rr)r.rrrDrUrErrrVrr_Zpkg_pathrZfind_mod_dottedrrrrrrr$testZipImporterMethodsInSubDirectorysX  ,       zBUncompressedZipImportTestCase.testZipImporterMethodsInSubDirectorycCs|tjtttd>}|j|_d}tddtdD}|||Wdn1s\0Yt t}| || || dt|dS)NrHz testdata.datcss|] }|VqdSr&r).0xrrr "z.zzipimporter object)r;rrLr[rrObytesrangerQr7rr]get_dataZassertInrepr)r.rUrErrrrr testGetDatas * z)UncompressedZipImportTestCase.testGetDatacCsFd}tt|ddtt|}ttt|fdtdfi}|t|tdS)Nzif 1: # indent hack def get_file(): return __file__ if __loader__.get_data("some.data") != b"some data": raise AssertionError("bad data") r r z some.dataz some data)rrrdrrlrnra)r.srcrrDrrrtestImporterAttr)s  z.UncompressedZipImportTestCase.testImporterAttrcCs\d}tdt|fi}||tjdttt}| | dd| t |j ddS)Nz[if 1: # indent hack def test(val): assert(val) return val r#rrF) rlrdrXr'r!rZr[rr\r]test assertRaisesAssertionError)r.rrDr_rrrtestDefaultOptimizationLevel4s  z:UncompressedZipImportTestCase.testDefaultOptimizationLevelcCs&tdttfi}|jd|tdddS)Nr#s6Some StuffSome StuffSome StuffSome StuffSome StuffSome StuffSome StuffSome StuffSome StuffSome StuffSome StuffSome StuffSome StuffSome StuffSome StuffSome StuffSome StuffSome StuffSome StuffSome StuffSome StuffSome StuffSome StuffSome StuffSome StuffSome StuffSome StuffSome StuffSome StuffSome StuffSome Stuff)rJrkrhrrrtestImport_WithStuffAs z2UncompressedZipImportTestCase.testImport_WithStuffcCs|t|tdSr&)r]inspectZ getsourcere)r.modulerrrassertModuleSourceHsz0UncompressedZipImportTestCase.assertModuleSourcecCs(tdttfi}|jd|t|jddSNr#r{)rlrdrerarrhrrr testGetSourceKsz+UncompressedZipImportTestCase.testGetSourcecCsLtttddttt}tdttfttt|fi}|jt|t|jddS)Nr r r#r{) rrrerdrrlrnrar)r.rrDrrrtestGetCompiledSourceOs   z3UncompressedZipImportTestCase.testGetCompiledSourcecCs.tdttfdtdfi}|jd|t|ddS)Nr#xyz.txtz>>> log.append(True) r{rk)r.callbackrDrrr runDoctestUs z(UncompressedZipImportTestCase.runDoctestcCsLg}tjd}t_ztjd|dtdW|t_n|t_0||dgdSNrT)packageZmodule_relativeZglobs)doctestZmasterZtestfilelocalsr])r.rlogZ old_masterrrr doDoctestFileZsz+UncompressedZipImportTestCase.doDoctestFilecCs||jdSr&)rrr-rrrtestDoctestFilefsz-UncompressedZipImportTestCase.testDoctestFilecCs.g}tjd|dtd||dgdSr)rZ DocFileTestrrunr])r.rrrrrdoDoctestSuiteis  z,UncompressedZipImportTestCase.doDoctestSuitecCs||jdSr&)rrr-rrrtestDoctestSuiteqsz.UncompressedZipImportTestCase.testDoctestSuitecCsz |Wntdj}t|dd\}}}}||tt|j dd\}}}}||tt }t |d|| |tYn 0tddS)NrrrzThis ought to be impossible)Zdo_raiser'exc_infotb_nextrr] raise_srcstriprtb_frameioStringIOr rgetvalueendswithr)r.rtbrWlnonlinesrrr doTracebackts  z)UncompressedZipImportTestCase.doTracebackcCs(tdttfi}|jd|t|jddSr)rlrdrrarrhrrr testTracebacksz+UncompressedZipImportTestCase.testTracebackNzneed an unencodable filenamecCs~tjd}|tj|t|d8}ttdtt }|j |_ | |t Wdn1s`0Yt|tdSNz.ziprHr#)rTESTFN_UNENCODABLEr;rLrrrlrMrNrdrOrPrQrer7rrr.filenamerUrVrrrtestUnencodables  *z-UncompressedZipImportTestCase.testUnencodablecCstjd}|tj|t|d8}ttdtt }|j |_ | |t Wdn1s`0Yt|tt||t$ttt|Wdn1s0Y|t$ttt|Wdn1s0YdSr)rZTESTFNr;rLrrrlrMrNrdrOrPrQrer7rrfsencoder TypeErrorr memoryviewrrrr testBytesPaths  *  2 z+UncompressedZipImportTestCase.testBytesPathcCs&tdttfi}|jd|tdddS)Nr#scommentrIrkrhrrr testCommentsz)UncompressedZipImportTestCase.testCommentcCs(tdttfi}|jd|tddddS)Nr#s@cruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftcruftshi)rJrIrkrhrrrtestBeginningCruftAndCommentsz:UncompressedZipImportTestCase.testBeginningCruftAndCommentcCs*tdttfi}|jd|tddddS)Nr#cir rkrhrrrtestLargestPossibleCommentsz8UncompressedZipImportTestCase.testLargestPossibleComment)6r1r2r3rrOr/rrGr[rXrarirmrprqrunittestZmockZpatchrrrrrrrrrrrrrrrrrrrrrrrrrrrZskipIfrrrr r rrrrrrr4Ls\         MI81      r4c@seZdZeZdS)CompressedZipImportTestCaseN)r1r2r3rrOrrrrrsrc@sTeZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ dS)BadFileZipImportTestCasecCs|tjtj|dSr&)rr7ZipImportErrorr)r.rrrrassertZipFailuresz)BadFileZipImportTestCase.assertZipFailurecCs|ddS)NZAdfjdkFJKDFJjdklfjsrr-rrr testNoFilesz#BadFileZipImportTestCase.testNoFilecCs|ddSrrr-rrrtestEmptyFilenamesz*BadFileZipImportTestCase.testEmptyFilenamecCsB|ttjd|jttjtdd|ttjtttdS)N)Zkwd)rr r7rrlrrrr-rrr testBadArgss   z$BadFileZipImportTestCase.testBadArgscCs|dddS)NAirr-rrrtestFilenameTooLongsz,BadFileZipImportTestCase.testFilenameTooLongcCs"tttt|tdSr&)rrLrlZcreate_empty_filerr-rrr testEmptyFiles  z&BadFileZipImportTestCase.testEmptyFilec Cstttttjd}zZt||tj }t tWdn1sR0YWt tdttnt tdtt0dS)Nri) rrLrlrrBO_CREATcloserr7rrchmod)r.fdcmrrrtestFileUnreadables  *  z+BadFileZipImportTestCase.testFileUnreadablecCs4ttttd}|d||tdS)Nw+Zaaaaaaaaaaaaaaaaaaaaaa)rrLrlrBrCrr)r.rFrrrtestNotZipFiles    z'BadFileZipImportTestCase.testNotZipFilecCs*ttttd}|tdd|d|t t}z| t |j d| t |j d| t |jd| t |jd| t |jd| t |jdtj}|| dd| ||j d| ||jd| t|jd| ||jd| ||jdWtjn tj0dS)Nr#z=IiPKZaaaaaaaaaaaaaaaaaaabc)rrLrlrBrCrrrr7rrr rrrget_coderrrr]OSErrorr8r+)r.rFrUerrorrrr_testBogusZipFiles*    z*BadFileZipImportTestCase._testBogusZipFileN) r1r2r3rrrrrrr"r$r)rrrrrsrcCsttdSr&)rrLrlrrrrtearDownModulesr*__main__)8r'rr rimportlib.utilrrMrZ unittest.mockrrzipfilerrrrr7r5rrr tracebackrrr rbrfrerrrrr"rdrrorlrrr!rrr[rcache_from_sourceZpyc_filernZTestCaser%r4Z requires_zlibrrr*r1mainrrrrs\       iK