a  ze~@sddlmZddlmZedZedZedZedZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZddlZdd lmZmZdd lmZdd lmZGd d d ejZejeeeeed\ZZGdddeeeZGdddeeddZGdddZGdddeZ GdddeZ!GdddZ"Gddde"e Z#eje#eeeed\Z$Z%Gdd d e"e!Z&eje&eeeed\Z'Z(Gd!d"d"Z)Gd#d$d$e)e Z*eje*eeeed\Z+Z,Gd%d&d&e)e!Z-eje-eeeed\Z.Z/e0d'kre1dS)()abc)util importlibz importlib.abczimportlib.machineryzimportlib.utilN)make_legacy_pycunload)without_source_date_epoch)SourceDateEpochTestMetac@seZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZddZddZejddZddZejd d!Zejd"d#Zejd$d%Zejd&d'Zd(S)) SimpleTestzShould have no issue importing a source module [basic]. And if there is a syntax error, it should raise a SyntaxError [syntax error]. cCs4d|_tjd|jd|_|j|j|j|_dS)NZspamZhamz.py)nameospathjoinfilepath machinerySourceFileLoaderloaderselfr_/opt/bitninja-python-dojo/embedded/lib/python3.9/test/test_importlib/source/test_file_loader.pysetUp!szSimpleTest.setUpcCsjGddd|jj}|dd}|tdt$tdt|}Wdn1s\0YdS)Nc@seZdZddZddZdS)z/SimpleTest.test_load_module_API..TestercSsdS)Nz attr = 42rr_rrr get_source(z:SimpleTest.test_load_module_API..Tester.get_sourcecSsdS)NFrrrrr is_package)rz:SimpleTest.test_load_module_API..Tester.is_packageN)__name__ __module__ __qualname__rrrrrrTester'sr Zblahzblah.pyignore) r FileLoaderZ addCleanuprwarningscatch_warnings simplefilterDeprecationWarning load_module)rr rmodulerrrtest_load_module_API&s     zSimpleTest.test_load_module_APIcCsGddd|jj}d}d}|||}||||||||||d|t||dWdn1s0YdS)Nc@s,eZdZddZddZddZddZd S) z0SimpleTest.test_get_filename_API..TestercSsdSNrrrrrget_code4rz9SimpleTest.test_get_filename_API..Tester.get_codecSsdSr*rrrrrr5rz;SimpleTest.test_get_filename_API..Tester.get_sourcecSsdSr*rrrrrr6rz;SimpleTest.test_get_filename_API..Tester.is_packagecSsdSr*rrrrr module_repr7rz.Tester.module_reprN)rrrr+rrr,rrrrr 3sr Z some_pathZ some_nameZXXX)rr" assertEqual get_filename assertRaises ImportError)rr r r rrrrtest_get_filename_API1s  z SimpleTest.test_get_filename_APIcCs$|j|j|j}||j|dSr*)rrr rr-rrotherrrr test_equalityBszSimpleTest.test_equalitycCs(|jd|j|j}||j|dS)Nr)rrr rZassertNotEqualrr2rrrtest_inequalityFszSimpleTest.test_inequalityc Cstd}|jd|d}t&tdt|d}Wdn1sR0Y| dt j d|ddd}| D]\}}| t|||qWdn1s0YdS)N_tempr!r__file__ __package__rcreate_modulesrrr#r$r%r&r'assertInsysmodulesitemsr-getattrrmappingrr(checkattrvaluerrr test_moduleKs   (zSimpleTest.test_modulec Cstd}|jd|d}t&tdt|d}Wdn1sR0Y| dt j d|dt j |dgdd}|D]\}}|t|||qWdn1s0YdS)N _pkg.__init___pkgr!)rr9__path__r:)rr<rrr#r$r%r&r'r=r>r?r r dirnamer@r-rArBrrr test_packageWs   (zSimpleTest.test_packagec Cstdd}|jd|d}t&tdt|d}Wdn1sT0Y| dt j d|ddd}| D]\}}| t|||qWdn1s0YdS)NrHz_pkg.modr!rIr8r;rBrrrtest_lacking_parentfs  (zSimpleTest.test_lacking_parentcs fddS)z-Fake mtime to always be higher than expected.cs |dS)Nr)r fxnrrurz'SimpleTest.fake_mtime..r)rrPrrOr fake_mtimesszSimpleTest.fake_mtimec Cs^td<}|jd|d}t&tdt|d}Wdn1sT0Yt |}t |j }t |dd}| dWdn1s0Yt&tdt|d}Wdn1s0Y| d|j dt|j ||tjd|t |||t |j |Wdn1sP0YdS)Nr6r!wztesting_var = 42 Z testing_varz'testing_var' not in {0})rr<rrr#r$r%r&r'id__dict__openwriter=formatlistkeysr-r>r?)rrCrr(Z module_idZmodule_dict_idfilerrrtest_module_reusews(  ( (  (  zSimpleTest.test_module_reusec Csd}d}d}t|T}t|}|D]}t|||q(t||d}|dWdn1sh0Y|jd|d}| t | |Wdn1s0Y|D]}| t |||q| t Pt&tdt||Wdn1s0YWdn1s60Y|D]}| t |||qDWdn1st0YdS)N)r9rJr:zr6rSz+++ bad syntax +++r!)rr<types ModuleTypesetattrrVrWrrr/ SyntaxError exec_moduler-rAr#r$r%r&r') rZ attributesrFr rCZ orig_modulerEr[rrrrtest_state_after_failures( ( (   Jz#SimpleTest.test_state_after_failurec Cstd}t|dd}|dWdn1s:0Y|jd|d}|tNt &t dt | dWdn1s0YWdn1s0Y| dtjWdn1s0YdS)Nr6rS=r!)rr<rVrWrrr/r`r#r$r%r&r'Z assertNotInr>r?)rrCr[rrrrtest_bad_syntaxs (   FzSimpleTest.test_bad_syntaxc CsJd}t|d}|dWdn1s.0Yztd|jd|}t&tdt | d}Wdn1s0Y| ||j | |j ||jWdn1s0YWt|tj|j |}tj|rFt|n8t|tj|j |}tj|rDt|0dS)Nz_temp.pyrSz# test file for importlibr6r!)rVrWrZuncacherrr#r$r%r&r'r-r9cache_from_source __cached__r unlinkr rKexistsshutilrmtree)r file_pathr[rmodpycacherrrtest_file_from_empty_string_dirs* (   ($  z*SimpleTest.test_file_from_empty_string_dirc Cstd}|d}|j|}t|d}|dWdn1sL0Yzt|dWnbty|dYnHt y}z0|j t t ddkr|d |WYd}~n d}~00|j d|d}td}|jd||_||||jd|tj|t|t&td t|d}Wdn1s`0Y||j|||jd|tj|Wdn1s0YdS) Nr6rSzx = 5)roz-cannot set modification time to large integerZ EOVERFLOWz2cannot set modification time to large integer ({})r!) rr<rerVrWr utime OverflowErrorZskipTestOSErrorerrnorArXrrr]r^spec_from_loader__spec__rar-xZ assertTruer rhrgr#r$r%r&r'rf) rrCsourceZcompiledferr(rlrrrtest_timestamp_overflows4  ( &     *z"SimpleTest.test_timestamp_overflowc Cs|jdi}td}|jd||_|t||Wdn1sR0Y|tNt &t dt | dWdn1s0YWdn1s0YdS)Nz good namezbad namer!)rrr]r^ ModuleSpecrvr/r0rar#r$r%r&r')rrr(rrrtest_unloadables  (   zSimpleTest.test_unloadablec Cstdz}|d}|j|}t|d}|dWdn1sL0Yt|dtj|tj j d|j d|}t d}|jd||_||||jdt|d}|dWdn1s0Yt|d||||jdt|d }|}Wdn1s60Y|t|d d d d ||jd|d dWdn1s0YdS)Nr6wb state = "old"2rinvalidation_modeold state = "new"newrblittle)rr<rerVrWr rq py_compilecompilePycInvalidationMode CHECKED_HASHrrr]r^rurvrar-statereadint from_bytes source_hashrrCrxpycfprrldatarrrtest_checked_hash_based_pycs6  (    (   (  z&SimpleTest.test_checked_hash_based_pycc Csbtd@}tjdd|d}|j|}t|d}|dWdn1s^0Yt |dt j |t j j d|jd|}td}|jd||_||||jdt|d}|d Wdn1s0Yt |d||||jdWdn1s40YWdn1sT0YdS) Nr6_imp.check_hash_based_pycsneverr~rrrrr)rr<unittestmockpatchrerVrWr rqrrrrrrr]r^rurvrar-r)rrCrxrrrrlrrr&test_overridden_checked_hash_based_pycs,   (    (  z1SimpleTest.test_overridden_checked_hash_based_pycc Cstdn}|d}|j|}t|d}|dWdn1sL0Yt|dtj|tj j d|j d|}t d}|jd||_||||jdt|d}|dWdn1s0Y||||jdt|d}|}Wdn1s*0Y|t|d d d d ||jd|d d Wdn1s0YdS)Nr6r~rrrrrrrrrrNr)rr<rerVrWr rqrrrUNCHECKED_HASHrrr]r^rurvrar-rrrrrrrrrtest_unchecked_hash_based_pyc)s4  (    (  (  z(SimpleTest.test_unchecked_hash_based_pycc Cstd}tjddn|d}|j|}t|d}|dWdn1s^0Yt |dt j |t j j d|jd|}td}|jd||_||||jdt|d}|d Wdn1s0Y||||jd t|d }|}Wdn1s<0Y|t|d d dd||jd |d dWdn1s0YWdn1s0YdS)Nr6ralwaysr~rrrrrrrrrrrNr)rr<rrrrerVrWr rqrrrrrrr]r^rurvrar-rrrrrrrrr(test_overridden_unchecked_hash_based_pycGs8   (    (  (  z3SimpleTest.test_overridden_unchecked_hash_based_pycN)rrr__doc__rr)r1r4r5rGrLrMrRr\rbrdrnrwrites_bytecode_filesr{r}rrrrrrrrr s2     "    r )rrrrc@s eZdZdS)r Nrrrrrrrr msr c@s eZdZdS)SourceDateEpoch_SimpleTestNrrrrrrrsrT) metaclassZsource_date_epochc@seZdZddZdejjdddZdddd Ze j ddd d Z ddd d Z ddddZ ddddZddddZddddZddddZddddZddddZddddZdS)BadBytecodeTestcCstdSr*)NotImplementedError)rr[ module_namerrrimport_zszBadBytecodeTest.import_F del_sourcerc Csz tjd=WntyYn0tj|||d|sH|j||}nt||t ||}|rt |d }| }||} Wdn1s0Yt |d"}| dur| | Wdn1s0Y|S)zwManipulate the bytecode of a module by passing it into a callable that returns what to use as the new bytecode.r6rrNr~) r>r?KeyErrorrrrrer rgrrVrrW) rr rCZ manipulatorrr bytecode_pathr[bcZnew_bcrrrmanipulate_bytecode}s"    & (z#BadBytecodeTest.manipulate_bytecodercCsPtd2}|jd|dd|d}|d||Wdn1sB0YdS)Nr6cSsdS)NrrrrrrrQrz2BadBytecodeTest._test_empty_file..rrr<rrtestrrCbc_pathrrr_test_empty_files  z BadBytecodeTest._test_empty_filecCsPtd2}|jd|dd|d}|d||Wdn1sB0YdS)Nr6cSs |ddS)NrrrrrrrQrz5BadBytecodeTest._test_partial_magic..rrrrrr_test_partial_magics  z#BadBytecodeTest._test_partial_magiccCsPtd2}|jd|dd|d}|d||Wdn1sB0YdS)Nr6cSs |ddS)NrrrrrrrQrz2BadBytecodeTest._test_magic_only..rrrrrr_test_magic_onlys  z BadBytecodeTest._test_magic_onlycCsPtd2}|jd|dd|d}|d||Wdn1sB0YdS)Nr6cSs |ddS)NrrrrrrQrz5BadBytecodeTest._test_partial_flags..rrrrrr_test_partial_flagss  z#BadBytecodeTest._test_partial_flagscCstd8}|jd|dd|tjjd}|d||Wdn1sH0Ytd8}|jd|dd|tjjd}|d||Wdn1s0YdS)Nr6cSs |ddSN rrrrrrQrz4BadBytecodeTest._test_partial_hash..rcSs |ddSrrrrrrrQr)rr<rrrrrrrrr_test_partial_hashs$ * z"BadBytecodeTest._test_partial_hashcCsPtd2}|jd|dd|d}|d||Wdn1sB0YdS)Nr6cSs |ddS)N rrrrrrQrz9BadBytecodeTest._test_partial_timestamp..rrrrrr_test_partial_timestamps  z'BadBytecodeTest._test_partial_timestampcCsPtd2}|jd|dd|d}|d||Wdn1sB0YdS)Nr6cSs |ddS)NrrrrrrQrz4BadBytecodeTest._test_partial_size..rrrrrr_test_partial_sizes  z"BadBytecodeTest._test_partial_sizec Cstdl}|jd|dd|d}|s.|dn|}|t||dWdn1s^0YWdn1s|0YdS)Nr6cSs |ddS)NrrrrrrrQrz2BadBytecodeTest._test_no_marshal..rrr<rr/EOFErrorr)rrrCrrkrrr_test_no_marshals  z BadBytecodeTest._test_no_marshalc Cstd}|jd|dd|d}|s.|dn|}|t}||dWdn1s^0Y||jjd||jj |Wdn1s0YdS)Nr6cSs|ddtdS)Nrsabcd)marshaldumpsrrrrrQrz8BadBytecodeTest._test_non_code_marshal..r) rr<rr/r0rr- exceptionr r )rrrCrrkcmrrr_test_non_code_marshals  *z&BadBytecodeTest._test_non_code_marshalc Cstdl}|jd|dd|d}|s.|dn|}|t||dWdn1s^0YWdn1s|0YdS)Nr6cSs|dddS)NrsrrrrrrQrz3BadBytecodeTest._test_bad_marshal..rr)rrrCrrkrrr_test_bad_marshals  z!BadBytecodeTest._test_bad_marshalcCsLtd.}|d|dd}|d||Wdn1s>0YdS)Nr6cSsd|ddS)NrrrrrrrQrz1BadBytecodeTest._test_bad_magic..rrrrr_test_bad_magics  zBadBytecodeTest._test_bad_magicN)rrrrrr TIMESTAMPrrrrrrrrrrrrrrrrrrrxs      rc@seZdZddZdS)BadBytecodeTestPEP451cCs4|||}t|}|j|||_||dSr*)rr]r^rrurvrarr[rrr(rrrrs  zBadBytecodeTestPEP451.import_Nrrrrrrrrrsrc@seZdZddZdS)BadBytecodeTestPEP302cCs\|||}t&tdt||}Wdn1s@0Y||tjdS)Nr!) rr#r$r%r&r'r=r>r?rrrrrs    (zBadBytecodeTestPEP302.import_Nrrrrrrsrc@seZdZeddZejddZddZejddZ ejd d Z ejd d Z ejd dZ ejddZ ejddZejddZejddZejddZejeddZejddZdS)SourceLoaderBadBytecodeTestcCs|jj|_dSr*)rrrclsrrr setUpClasssz&SourceLoaderBadBytecodeTest.setUpClasscsfdd}|dS)NcsR|||t|d$}t|dWdn1sD0YdSNrrrrVZ assertGreaterlenrr rCrr[rrrrs z9SourceLoaderBadBytecodeTest.test_empty_file..testrrrrrrtest_empty_files z+SourceLoaderBadBytecodeTest.test_empty_filecsfdd}|dS)NcsR|||t|d$}t|dWdn1sD0YdSrrrrrrr s z.testrrrrrtest_partial_magics z.SourceLoaderBadBytecodeTest.test_partial_magiccsfdd}|dS)NcsR|||t|d$}t|dWdn1sD0YdSrrrrrrr+s z9SourceLoaderBadBytecodeTest.test_magic_only..testrrrrrtest_magic_only's z+SourceLoaderBadBytecodeTest.test_magic_onlycsfdd}|dS)NcsT|||t|d&}|djjWdn1sF0YdS)Nrr)rrVr-rr MAGIC_NUMBER)r rCr bytecode_filerrrr6s   z8SourceLoaderBadBytecodeTest.test_bad_magic..testrrrrrtest_bad_magic2s z*SourceLoaderBadBytecodeTest.test_bad_magiccsfdd}|dS)NcsR|||t|d$}t|dWdn1sD0YdSrrr rCrr[rrrrBs z@SourceLoaderBadBytecodeTest.test_partial_timestamp..testrrrrrtest_partial_timestamp>s z2SourceLoaderBadBytecodeTest.test_partial_timestampcsfdd}|dS)NcsR|||t|d$}t|dWdn1sD0YdSrrrrrrrLs z.testrrrrrtest_partial_flagsIs z.SourceLoaderBadBytecodeTest.test_partial_flagscsfdd}|dS)NcsR|||t|d$}t|dWdn1sD0YdSrrrrrrrVs z;SourceLoaderBadBytecodeTest.test_partial_hash..testrrrrrtest_partial_hashSs z-SourceLoaderBadBytecodeTest.test_partial_hashcsfdd}|dS)NcsR|||t|d$}t|dWdn1sD0YdSrrrrrrras z;SourceLoaderBadBytecodeTest.test_partial_size..testrrrrrtest_partial_size]s z-SourceLoaderBadBytecodeTest.test_partial_sizecCs |dSr*rrrrrtest_no_marshalhsz+SourceLoaderBadBytecodeTest.test_no_marshalcCs |dSr*rrrrrtest_non_code_marshalmsz1SourceLoaderBadBytecodeTest.test_non_code_marshalcCs |dSr*)rrrrrtest_bad_marshalssz,SourceLoaderBadBytecodeTest.test_bad_marshalc Csd}td}t|d|j|d}t|d$}|d||Wdn1sb0Y||ddt j |d}|j |}t|d,}|d||d|Wdn1s0YWdn1s0YdS)Nrr6r+brrr)rr<rrrerVseekrWrr r getmtimer _pack_uint32r-r)rZzerosrCrr source_mtimeZsource_timestamprrrtest_old_timestampys   (   z.SourceLoaderBadBytecodeTest.test_old_timestampc Cstd}t|d|j|d}t|d$}|d|dWdn1s^0Yt |t j t j Bt j Bz"||ddWt |t jnt |t j0Wdn1s0YdS)Nr6rrr)rr<rrrerVrrWr chmodstatS_IRUSRS_IRGRPS_IROTHrS_IWUSR)rrCrrrrrtest_read_only_bytecodes   (z3SourceLoaderBadBytecodeTest.test_read_only_bytecodeN)rrr classmethodrrrrrrrrrrrrrrrrrrrrrrs8            rc@s eZdZdS)!SourceLoaderBadBytecodeTestPEP451Nrrrrrrsrc@s eZdZdS)!SourceLoaderBadBytecodeTestPEP302Nrrrrrrsrc@sheZdZeddZddZddZddZd d Zd d Z d dZ ddZ ddZ ddZ ddZdS)SourcelessLoaderBadBytecodeTestcCs|jj|_dSr*)rSourcelessFileLoaderrrrrrrsz*SourcelessLoaderBadBytecodeTest.setUpClasscsfdd}j|dddS)NcsZt}||Wdn1s,0Y|jj||jj|dSr*r/r0rr-rr r r rCrrrrrrs *z=SourcelessLoaderBadBytecodeTest.test_empty_file..testTrrrrrrrs z/SourcelessLoaderBadBytecodeTest.test_empty_filecsfdd}j|dddS)NcsZt}||Wdn1s,0Y|jj||jj|dSr*r r rrrrs *z@SourcelessLoaderBadBytecodeTest.test_partial_magic..testTrrrrrrrs z2SourcelessLoaderBadBytecodeTest.test_partial_magiccsfdd}j|dddS)Ncs:t||Wdn1s,0YdSr*r/rrr rCrrrrrs z=SourcelessLoaderBadBytecodeTest.test_magic_only..testTrrrrrrrs z/SourcelessLoaderBadBytecodeTest.test_magic_onlycsfdd}j|dddS)NcsZt}||Wdn1s,0Y|jj||jj|dSr*r r rrrrs *z.testTrrrrrrrs z.SourcelessLoaderBadBytecodeTest.test_bad_magiccsfdd}j|dddS)Ncs:t||Wdn1s,0YdSr*r r rrrrs zDSourcelessLoaderBadBytecodeTest.test_partial_timestamp..testTrrrrrrrs z6SourcelessLoaderBadBytecodeTest.test_partial_timestampcsfdd}j|dddS)Ncs:t||Wdn1s,0YdSr*r r rrrrs z@SourcelessLoaderBadBytecodeTest.test_partial_flags..testTrrrrrrrs z2SourcelessLoaderBadBytecodeTest.test_partial_flagscsfdd}j|dddS)Ncs:t||Wdn1s,0YdSr*r r rrrrs z?SourcelessLoaderBadBytecodeTest.test_partial_hash..testTrrrrrrrs z1SourcelessLoaderBadBytecodeTest.test_partial_hashcsfdd}j|dddS)Ncs:t||Wdn1s,0YdSr*r r rrrrs z?SourcelessLoaderBadBytecodeTest.test_partial_size..testTrrrrrrrs z1SourcelessLoaderBadBytecodeTest.test_partial_sizecCs|jdddSNTrrrrrrrsz/SourcelessLoaderBadBytecodeTest.test_no_marshalcCs|jdddSr rrrrrrsz5SourcelessLoaderBadBytecodeTest.test_non_code_marshalN)rrrrrrrrrrrrrrrrrrrrs   rc@s eZdZdS)%SourcelessLoaderBadBytecodeTestPEP451Nrrrrrrsrc@s eZdZdS)%SourcelessLoaderBadBytecodeTestPEP302Nrrrrrrsr__main__)2r7rrZimport_importlibrZ importlib_abcrZimportlib_utilrtrr rrirr>r]rr#Z test.supportrrZtest.test_py_compilerr Z LoaderTestsr Z test_bothZFrozen_SimpleTestZSource_SimpleTesttyperrrrrrZFrozen_SourceBadBytecodePEP451ZSource_SourceBadBytecodePEP451rZFrozen_SourceBadBytecodePEP302ZSource_SourceBadBytecodePEP302rrZ"Frozen_SourcelessBadBytecodePEP451Z"Source_SourcelessBadBytecodePEP451rZ"Frozen_SourcelessBadBytecodePEP302Z"Source_SourcelessBadBytecodePEP302rmainrrrrs        Q       J