a zeZ@sddlmZddlmZmZmZddlZddlZddlZddl m Z ddl Z ddl Z ddl Z ddlZddlZddlZGdddejZGdddejZGd d d ejZGd d d ejZGd ddejZddZedkredS))Path)unloadcheck_warnings CleanImportN)spec_from_file_locationc@s\eZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ dS) PkgutilTestscCs.t|_|tj|jtjd|jdSNr) tempfilemkdtempdirname addCleanupshutilrmtreesyspathinsertselfrE/opt/bitninja-python-dojo/embedded/lib/python3.9/test/test_pkgutil.pysetUps zPkgutilTests.setUpcCs tjd=dSr)rrrrrrtearDownszPkgutilTests.tearDowncCsd}d}tj|j|}t|ttj|dd}|ttj|dd}|||ttj|dttj|ddd}|||t |d}| ||t |d}| ||t j |=dS)Ntest_getdata_filesys%Hello, world! Second line Third line __init__.pywbres.txtsub sub/res.txt) osrjoinr mkdiropenclosewritepkgutilget_data assertEqualrmodules)rpkg RESOURCE_DATAZ package_dirfres1res2rrrrs$       z!PkgutilTests.test_getdata_filesysc Csd}d}d}tj|j|}t|d}||dd||d|||d||tj d |t |d }| ||t |d }| ||g}t |gD]} || t j|| jq| |dgtjd =tj|=dS) Ntest_getdata_zipfile.ziptest_getdata_zipfilerw /__init__.py/res.txtz /sub/res.txtrrr)rrr r zipfileZipFilewritestrr#rrr%r&r' iter_modulesassertIsInstance ModuleInfoappendnamer() rzipr)r*zip_filezr,r-names moduleinforrrr/;s*     z!PkgutilTests.test_getdata_zipfilec Cs0d}d}d}tj|j|}t|d}||dd||d||tj d|ztt |d }| ||g}t t|gD]}||t j||jq| ||gWtjd=tj|dntjd=tj|d0d } |t| tt d Wdn1s"0YdS) Nr.r/rr0r1r2r3rrz9path must be None or list of paths to look for modules inZ invalid_path)rrr r r4r5r6r#rrr%r&r'r7rr8r9r:r;r(popZassertRaisesRegex ValueErrorlist) rr<r)r*r=r>resr?r@Z expected_msgrrrtest_issue44061_iter_modules^s0   z)PkgutilTests.test_issue44061_iter_modulescCsRd}tj|j|}t|d|tj|tj|jgdD]}| dq>dS)NZunreadable_packager)rzunexpected package found) rrr r r!r rmdirr% walk_packagesfail)rZ package_namedtrrrtest_unreadable_dir_on_syspaths  z+PkgutilTests.test_unreadable_dir_on_syspathc Cshd}tj|j|}t|ttj|dd}|ttj|dttj|ddd}|ttj|ddd}|d}tj|j|}t|ttj|dd}|ttj|dttj|ddd}|ttj|ddd}|gd}ddt|jgD}| |||D]}| d rXqDt j |=qDdS) Ntest_walkpackages_filesysrrrzmod.py)rzsub.test_walkpackages_filesysz!sub.test_walkpackages_filesys.modrLztest_walkpackages_filesys.subz!test_walkpackages_filesys.sub.modcSsg|] }|dqSr.0errr z:PkgutilTests.test_walkpackages_filesys..mod) rrr r r!r"r#r%rGr'endswithrr() rpkg1Zpkg1_dirr+pkg2Zpkg2_direxpectedactualr)rrrrLs6    z&PkgutilTests.test_walkpackages_filesysc Csd}d}d}tj|j|}t|d}||dd||d|dd||d|dd||dd||d|dd||d|dd|tj d |gd }d d t |gD}| ||tjd =|D]}| d rqtj|=qdS)zBTests the same as test_walkpackages_filesys, only with a zip file.ztest_walkpackages_zipfile.ziptest_walkpackages_zipfilerr0r1r2/z/mod.pyr)rzsub.test_walkpackages_zipfilez!sub.test_walkpackages_zipfile.modrZztest_walkpackages_zipfile.subz!test_walkpackages_zipfile.sub.modcSsg|] }|dqSrMrrOrrrrRrSz:PkgutilTests.test_walkpackages_zipfile..rTN)rrr r r4r5r6r#rrr%rGr'rUr() rr<rVrWr=r>rXrYr)rrrrZs*   z&PkgutilTests.test_walkpackages_zipfilecCsd}|ttftt|Wdn1s60Yd}|ttftt|Wdn1sv0YdS)NZtest_dirstest_dir) assertRaises TypeErrorrBrCr%rG)r str_inputZ bytes_inputrrr2test_walk_packages_raises_on_string_or_bytes_inputs ,z?PkgutilTests.test_walk_packages_raises_on_string_or_bytes_inputc Csddl}ddl}dtfdtjfdtjjfd|fd|fd|jfd|jfd |jjfd |jjfd |jjjfd |jjjfd tfdtfdtj fdtj fdt fdt fdtjff}dt fdt fdt fdt fdt fdt fdt fdt fdtfdt fdt fdtfdtfd tfd!t fd"t fd#t ff}d$}|D]}tj|j|}ztj|dd%WntyVYqYn0tj|d&}t|d'"}|d|Wdn1s0Ytt|}|||ff7}t|dkr||dd(t ff7}q|d)t ff7}|D]N\} } |j| d*&t| } || | Wdn1s@0Yq|D]n\} } |j| d*F|| t| Wdn1s0YWdn1s0YqRdS)+Nrrzos.pathzos.path:pathseploggingzlogging:zlogging.handlerszlogging.handlers:zlogging.handlers:SysLogHandlerzlogging.handlers.SysLogHandlerz(logging.handlers:SysLogHandler.LOG_ALERTz(logging.handlers.SysLogHandler.LOG_ALERTz builtins.intz builtins:intzbuiltins.int.from_byteszbuiltins:int.from_byteszbuiltins.ZeroDivisionErrorzbuiltins:ZeroDivisionErrorzos:pathrNg@Tr2z?abczabc/foofoozos.foozos.foo:zos.pth:pathsepzlogging.handlers:NoSuchHandlerz,logging.handlers:SysLogHandler.NO_SUCH_VALUEz,logging.handlers.SysLogHandler.NO_SUCH_VALUEZeroDivisionErrorz os.path.9abcZ9abc) u वमसéÈu안녕하세요u さよならuありがとうu Хорошоuспасибоu现代汉语常用字表exist_okrr0u ०वमस)s)r`Zlogging.handlersrrpathsephandlersZ SysLogHandlerZ LOG_ALERTint from_bytesrbr]rB ImportErrorAttributeErrorr r makedirsUnicodeEncodeErrorr"r$flush importlibinvalidate_caches import_modulelenZsubTestr% resolve_namer'r\) rr`Z success_casesZ failure_casesZ unicode_wordsZuwrIr+rTrhrXoexcrrrtest_name_resolutions       (   0  z!PkgutilTests.test_name_resolutionN) __name__ __module__ __qualname__rrrr/rErKrLrZr_ryrrrrrs#' (# rc@sLeZdZGdddeZGdddeZddZddZd d Zd d Z d S)PkgutilPEP302Testsc@s$eZdZddZddZddZdS)zPkgutilPEP302Tests.MyTestLoadercCsdSNr)rspecrrr create_moduleDsz-PkgutilPEP302Tests.MyTestLoader.create_modulecCs|jddd|jd<dS)NloadsrrN)__dict__get)rrTrrr exec_moduleGsz+PkgutilPEP302Tests.MyTestLoader.exec_modulecCsdS)N Hello, world!r)rrrrrr&Ksz(PkgutilPEP302Tests.MyTestLoader.get_dataN)rzr{r|rrr&rrrr MyTestLoaderCsrc@seZdZdddZdS)z!PkgutilPEP302Tests.MyTestImporterNcCs t}t|d|jj|gdS)Nz<%s>)loadersubmodule_search_locations)r}rr __class__rz)rfullnamertargetrrrr find_specOs  z+PkgutilPEP302Tests.MyTestImporter.find_spec)NN)rzr{r|rrrrrMyTestImporterNsrcCstjd|dSr)r meta_pathrrrrrrrVszPkgutilPEP302Tests.setUpcCs tjd=dSr)rrrrrrrYszPkgutilPEP302Tests.tearDowncCs |tdddtjd=dS)Nradummyr)r'r%r&rr(rrrrtest_getdata_pep302\sz&PkgutilPEP302Tests.test_getdata_pep302cCsDddl}||jd|tddd||jdtjd=dS)NrrNrarr)rar'rr%r&rr()rrarrrtest_alreadyloadedas z%PkgutilPEP302Tests.test_alreadyloadedN) rzr{r|objectrrrrrrrrrrr}As  r}c@s4eZdZddZddZddZddZd d Zd S) ExtendPathTestscCspt}tjd|tj||}t|ttj|dd}| dWdn1sb0Y|S)Nrrr0zKfrom pkgutil import extend_path __path__ = extend_path(__path__, __name__) ) r r rrrrr r!r"r$)rpkgnamer Zpkgdirflrrr create_initos (zExtendPathTests.create_initcCsTtj|||d}t|d"}td||dWdn1sF0YdS)N.pyr0zvalue={})file)rrr r"printformat)rr rZsubmodule_namevalue module_namerrrrcreate_submodulezs z ExtendPathTests.create_submodulec Csd}||}||}|||dd|||ddddl}ddl}||jjd||jjd|t|j tt j ||t j ||gt |t |tj d=tj d=tjd=tjd=tjd=dS)NrabarrbazrNfoo.barfoo.baz)rrfoo.barfoo.bazr'rrrsorted__path__rrr r rrr(rrZ dirname_0Z dirname_1rarrr test_simples*      zExtendPathTests.test_simplec Cstj}tj}d}d}||}tj||}d||}tj |dtj |dzV| |||dt |t||}||} |D]r} t| |} | j} z | j} WntyYn0|| t jj|| | || t jj|t| |q|tt|dWdn1s,0Y|tt|dWdn1sd0YWt|tjd=ztj d=tj d=WntyYn0n>t|tjd=ztj d=tj d=WntyYn00dS)NspamZeggsz{}.{}rzinvalid.modulez.spamz spam.eggs)r%iter_importers get_importerrrrr rrr(rArrrrtrC _get_specrrnr8 machinery FileFinderr'SourceFileLoader assertIsNoner\rmr rKeyError) rrrrmodnamer ZpathitemrZ importersZexpected_importerfinderrrrrrtest_iter_importerssZ         , .    z#ExtendPathTests.test_iter_importersc Csd}||}||}|||ddttj||d|||ddddl}ddl}||j j d||j j d|t |j t tj||tj||gt|t|tjd=tjd=tjd=tjd=tjd=dS) NrarrrrrNrr)rrrunlinkrr rrr'rrrrrr rrr(rrrrtest_mixed_namespaces,      z$ExtendPathTests.test_mixed_namespaceN)rzr{r|rrrrrrrrrrns  0rc@s,eZdZddZddZddZddZd S) NestedNamespacePackageTestcCst|_tjdd|_dSr~)r r basedirrrold_pathrrrrrs z NestedNamespacePackageTest.setUpcCs |jtjdd<t|jdSr~)rrrr rrrrrrrsz#NestedNamespacePackageTest.tearDowncCs|dd\}}tj|j|dtjj}tj|ddttj||dd}| |Wdn1sr0YdS)N.rNTrerr0) rsplitrrr rreplacesepror"r$)rr;contentsbasefinal base_pathr+rrrrs z(NestedNamespacePackageTest.create_modulecCsd}|d||d||d||d||dd|dd tjd tj|jd tjd tj|jd d dl}|t d | t |j dd dl }|t d| t |jj dd dlm}d dlm}| |d| |ddS)NzBimport pkgutil; __path__ = pkgutil.extend_path(__path__, __name__)za.pkg.__init__zb.pkg.__init__za.pkg.subpkg.__init__zb.pkg.subpkg.__init__za.pkg.subpkg.czc = 1zb.pkg.subpkg.dzd = 2rabr)z pkg.subpkg)c)rIrN)rrrrrr rr)r rr'rurZ pkg.subpkgZsubpkgZ pkg.subpkg.crZ pkg.subpkg.drI)rZpkgutil_boilerplater)rrIrrr test_nesteds(           z&NestedNamespacePackageTest.test_nestedN)rzr{r|rrrrrrrrrsrc@seZdZddZddZddZddZeed kd d d Z d dZ ddZ ddZ ddZ ddZddZddZddZdS)ImportlibMigrationTestscCs tdtfS)Nz5This emulation is deprecated, use 'importlib' instead)rDeprecationWarningrrrrcheck_deprecateds z(ImportlibMigrationTests.check_deprecatedcCs6|tdWdn1s(0YdSNr2)rr%Z ImpImporterrrrrtest_importer_deprecateds z0ImportlibMigrationTests.test_importer_deprecatedcCs<| tddddWdn1s.0YdSr)rr%Z ImpLoaderrrrrtest_loader_deprecated"s z.ImportlibMigrationTests.test_loader_deprecatedcCsltR}|td|td|td|t|jdWdn1s^0YdSNrrz test.supportr)rassertIsNotNoner% get_loaderr'ruwarningsrr0rrr test_get_loader_avoids_emulation&s z8ImportlibMigrationTests.test_get_loader_avoids_emulation__main__znot compatible with __main__cCsbt}bzPt2}|tt|t|jdWdn1sF0YW|an|a0dSr) __loader__rrr%rrzr'rur)rZ this_loaderr0rrr0test_get_loader_handles_missing_loader_attribute-s2zHImportlibMigrationTests.test_get_loader_handles_missing_loader_attributecCs^d}tt|}|`t|$|tj|<t|}Wdn1sF0Y||dSNrtyper__spec__rr(r%rrrr;rTrrrr.test_get_loader_handles_missing_spec_attribute9s   (zFImportlibMigrationTests.test_get_loader_handles_missing_spec_attributecCs`d}tt|}d|_t|$|tj|<t|}Wdn1sH0Y||dSrrrrrr+test_get_loader_handles_spec_attribute_noneBs   (zCImportlibMigrationTests.test_get_loader_handles_spec_attribute_nonecCs>d}dtj|<zt|}Wtj|=n tj|=0||dSNz totally bogus)rr(r%rrrr;rrrr#test_get_loader_None_in_sys_modulesKs   z;ImportlibMigrationTests.test_get_loader_None_in_sys_modulescCsd}t|}||dSr)r% find_loaderrrrrrtest_find_loader_missing_moduleTs z7ImportlibMigrationTests.test_find_loader_missing_modulecCsltR}|td|td|td|t|jdWdn1s^0YdSr)rrr%rr'rurrrrr!test_find_loader_avoids_emulationYs z9ImportlibMigrationTests.test_find_loader_avoids_emulationcCsLt2}|td|t|jdWdn1s>0YdS)Nz*??r)rrr%rr'rurrrrr"test_get_importer_avoids_emulation`sz:ImportlibMigrationTests.test_get_importer_avoids_emulationcCs4zttdWnty.|dYn0dS)Nz/homez3Unexpected AttributeError when calling get_importer)r%rrrnrHrrrrtest_issue44061fs z'ImportlibMigrationTests.test_issue44061cCsJt0}tD]}q|t|jdWdn1s<0YdSr)rr%rr'rur)rr0Zimporterrrr$test_iter_importers_avoids_emulationlszs*  /-})^