Ϫf^GhdZddlmZddlZddlZddlZddlZddlmZddl m Z ddl m Z m Z ddl mZddlZddlmZdd lmZdd lmZdd lmZdd lmZdd lmZddlmZGddeZGddeeZGddeZ GddeZ!Gdde!Z"Gdde!Z#GddeZ$gdZ%y)zV Tests for twisted.python.modules, abstract access to imported or importable objects. ) annotationsN)PathEntryFinder) ModuleType)Any Generator)Protocol)modules) networkString)FilePath)namedAny)TwistedModulesMixin)zipit)TestCaseceZdZ ddZy)_SupportsWalkModulescyN)selfimportPackagess ;/usr/lib/python3/dist-packages/twisted/test/test_modules.py walkModulesz _SupportsWalkModules.walkModuless N)rboolreturnz+Generator[modules.PythonModule, None, None])__name__ __module__ __qualname__rrrrrrs " 4 rrc,eZdZdZedf ddZy)TwistedModulesTestCasez/ Base class for L{modules} test cases. Fc|j|D]}|j|k(s|cS|jd|dy)z You don't ever actually want to do this, so it's not in the public API, but sometimes we want to compare the result of an iterative call with a lookup call and make sure they're the same for test purposes. rzUnable to find module z through iteration.N)rnamefail)rmodnamewherermodinfos rfindByIterationz&TwistedModulesTestCase.findByIteration*sK(((G G||w&  *7+5HIJrN)r%strr&rrrrzmodules.PythonModule)rrr__doc__r r(rrrr r %s?'.$ KK$K K  Krr cleZdZddZddZddZddZddZddZddZ ddZ dd Z dd Z dd Z dd Zy ) BasicTestsctdd}|j}|jd}|jdj||jd}|j |jdj||jdjd|j}|jd}|jdj||jd}|j |jdj||jdjd|j |j |j gtjd} |jd D cgc]} | j} } ttjjD]%}|jdstj|=' gd } |j| | y cc} w#ttjjD]%}|jdstj|='wxYw) zd Duplicate packages are not yielded when iterating over namespace packages. pkgutilsBimport pkgutil; __path__ = pkgutil.extend_path(__path__, __name__) test_package __init__.pynested_packagez module.pyrz module2.pyTr")r/ztest_package.nested_packagez"test_package.nested_package.modulez#test_package.nested_package.module2N) __import__pathEntryWithOnePackagechild setContentmakedirsreplaceSysPathpathr getModulerr#listsyskeys startswith assertEqual) rnamespaceBoilerplateentrytestPackagePath nestedEntry anotherEntryanotherPackagePathanotherNestedEntrymodulemod walkedNamesexpecteds rtest_namespacedPackagesz"BasicTests.test_namespacedPackages<s 9 V 2,,.++n5m,778LM%++,<= -(334HI+&11#6335 )//?  /::;OP/556FG##%  /::;OP  .99#> UZZ):):;<"">2  ,/5/A/AQU/A/VW388WKWs{{//12 ,$$^4 F+ ,  h/Xs{{//12 ,$$^4 F+ ,s$H$#H6H$H$$ assertFalseisLoaded)r shouldNotLoadr8s rtest_unimportablePackageGetItemz*BasicTests.test_unimportablePackageGetItemss$& !!113889&--&-   + n-6689rc`|j}|j|jg|jddit t j }|j|Dcgc]}|jc}dg|j|djycc}w)a  If a package has been explicitly forbidden from importing by setting a L{None} key in sys.modules under its name, L{modules.walkModules} should still be able to retrieve an unloaded L{modules.PythonModule} for that package. r/Nr) r3r7r8replaceSysModulesr:r rr>r#rSrT)r existentPathwalkedms r#test_unimportablePackageWalkModulesz.BasicTests.test_unimportablePackageWalkModuless335  \../0 56g))+, &1Q!&&1N3CD ++-.2s,B+c|j}t|j}|j|j |j |j gtjdg}ttj}tj j|j ttj}|j|||j||y)z~ Verify that L{modules.walkModules} ignores entries in sys.path which do not exist in the filesystem. r/N)r3r mktemprSexistsr7r8r r9r:rr;rRr>)rrYnonexistentPathrI beforeModules afterModuless rtest_nonexistentPathsz BasicTests.test_nonexistentPathss 335 "4;;=1 //12 \../0%%n56W0023  ,,-G//12  1 x0rc|j}t|j}|j|j |j d|j |jgttj}tjj|jttj}|j||y)z Verify that L{modules.walkModules} ignores entries in sys.path which refer to regular files in the filesystem. szip file or whatever N)r3r r^rSr_r5r7r8r:r rr;rRr>)rrYnonDirectoryPathrarbs rtest_nonDirectoryPathsz!BasicTests.test_nonDirectoryPathss 335 #DKKM2 )0023##$=> \../0W0023  (--.G//12   5rcl|jtjd|jdy)z Scrounge around in the top-level module namespace and make sure that Twisted shows up, and that the module thusly obtained is the same as the module that we find when we look for it explicitly by name. twistedNr>r r9r(rs rtest_twistedShowsUpzBasicTests.test_twistedShowsUps* **95t7K7KI7VWrc |jtjd|jdtjdy)zo Verify that the walkModules APIs will give us back subpackages, not just subpackages. twisted.pythonrh)r&Nrirjs rtest_dottedNameszBasicTests.test_dottedNamess@    . /  !19J9J99U V rctjD]-}|jd|jvd|jz/y)z Verify that the iterModules API will only return top-level modules and packages, not submodules or subpackages. .z9no nested modules should be returned from iterModules: %rN)r iterModulesrSr#filePath)rrFs rtest_onlyTopModuleszBasicTests.test_onlyTopModulessE ))+ F   v{{"K??$  rcdD]o}t|}|jtj|j ||j|j |j |qy)zl Verify that we can locate and load packages, modules, submodules, and subpackages. osrhrmztwisted.python.reflectN)r failUnlessIdenticalr r9loadr()rnr[s rtest_loadPackagesAndModulesz&BasicTests.test_loadPackagesAndModulessg O HA A  $ $W%6%6q%9%>%>%@! D  $ $T%9%9!%<%A%A%CQ G HrcdD]S}|jtj|jjj t j Uy)zu Verify that path entries discovered via module loading are, in fact, on sys.path somewhere. ruN) failUnlessInr r9 pathEntryrrr8r;)rrys rtest_pathEntriesOnPathz!BasicTests.test_pathEntriesOnPathsG O VA   g//2<<EEJJCHH U Vrct|j}|jtj|j g}|j d fd }|jdjdtj|j d|jtt|jd||_|j|dj|jdy ) z~ Verify that .py files will always be preferred to .pyc files, regardless of directory listing order. rLcL|}|jdfd }||_|S)Ncht}|j|j|Sr)r:sortreverse)xoriginalChildrens r evilChildrenzLBasicTests.test_alwaysPreferPy.._evilSmartPath..evilChildrens*)+, r)rr)children)pathNameorroriginalSmartPaths @r_evilSmartPathz6BasicTests.test_alwaysPreferPy.._evilSmartPath s*!(+A zz  &AJHrzabcd.py T)quietabcdN)rr)rr)r r^createDirectoryr rQr8 _smartPathr4r5 compileall compile_dirr>lenr:rrr)rmypathpprrs @rtest_alwaysPreferPyzBasicTests.test_alwaysPreferPys $++-(    6MM   Y**51v{{$7 T&//"345q9&  F,,fll9.EFrct|j}|jtj|j g}|j d}|j|j djdtj j|j td t|j}|jt|d|j|djdtjd=tj j!|j y#tjd=tj j!|j wxYw)z A package can delete its __path__ for some reasons, C{modules.PythonPath} should be able to deal with it. rrr0s del __path__ rN)r r^rr rQr8r4r5r;rRr2r:rr>rr#remove)rrrsubpathls rtest_packageMissingPathz"BasicTests.test_packageMissingPath!s  $++-(    6,,v&! m$//0AB  $6 )R^^%&A   SVQ '   QqTYY / F# HHOOFKK ( F# HHOOFKK (s AErc|jrJd|_tjj|jyNT)rr;r8rRrrjs r _setupSysPathz#PathModificationTests._setupSysPathMs,>>!! ../rc:|j}t|}|j|jdj d|j jdj t dt|d|jtj|j}|j|j|jdz|||d|j|djjd y) Nz foozle.pysx = 123 r0z__path__.append(z) z.foozler"foozle{)r^r rr4r5rr reprrr r9rr>r(rxr)rdoImportmoddir2fpmdr's r_underUnderPathTestz)PathModificationTests._underUnderPathTestRs++-   ;**<8 }-88 ,T']O3? @  ##D$4$45     9,gh !  H   *//133S9rc$|jy)z] Verify that iterModules will honor the __path__ of already-loaded packages. N)rrjs r"test_underUnderPathAlreadyImportedz8PathModificationTests.test_underUnderPathAlreadyImportedes   "rctj|j}|jDcgc] }|jj dd"}}|j |j|gdycc}w)Nrp)ab c__init__)r r9rrqr#splitrr>)rpkginfor'nfnis r _listModulesz"PathModificationTests._listModulesksh##D$4$45;B;N;N;PQ ""3'+QQ  67Rs%A>cD|j|jy)z Make sure the module list comes back as we expect from iterModules on a package, whether zipped or not. N)rrrjs rtest_listingModulesz)PathModificationTests.test_listingModulesqs  rcn|jt|j|jy)z Make sure the module list comes back as we expect from iterModules on a package, whether zipped or not, even if the package has already been imported. N)rr rrrjs r#_test_listingModulesAlreadyImportedz9PathModificationTests._test_listingModulesAlreadyImportedys* !!" rc|jr]d}tjj|jus J|dz|jtjvs J|dzyy)Nz>QD88<<>T%;%;; ITE\ I;))9 G4%< G9 rNr)T)rrrr)rrrr* itertoolscountrrrrrrrrrrrrrr7s@ !"J 0 :&# 8 Hrrc eZdZdZddZddZy)RebindingTestsz These tests verify that the default path interrogation API works properly even when sys.path has been rebound to a different object. c|jrJd|_tj|_tjddt_tjj |j yr)rr;r8 savedSysPathrRrrjs rrzRebindingTests._setupSysPathsG>>!!HH88A; ../rcH|jr|jt_yy)zF Clean up sys.path by re-binding our original object. N)rrr;r8rjs rrzRebindingTests.tearDowns >>((CH rNr)rrrr*rrrrrrrs 0)rrceZdZddZy)ZipPathModificationTestsc|jrJt|j|jdz|xjdz c_tj|jsJt j |y)Nz.zip)rrrzipfile is_zipfilerrrjs rrz&ZipPathModificationTests._setupSysPathsc>>!! d$$d&<&PythonPathTests.test_unhandledImporter..SecretImporterN)rrrrrrSecretImporterrs rrcSrr)r#rs rhookz4PythonPathTests.test_unhandledImporter..hooks !# #rz example/pathcyrr)r#s r sysloaderz9PythonPathTests.test_unhandledImporter..sysloadersrrcddS)NrrFr)entriessrz8PythonPathTests.test_unhandledImporter..sGAJx,@rN)r#objectrr)r#rrr)r rQr: iterEntriesr>r assertRaisesKeyError) rrsyspath sysmodulessyshookssyscacherspacerrs @@rtest_unhandledImporterz&PythonPathTests.test_unhandledImporters    $"",. 668 ""7J(IVu((*+ Wq) ($@Arctjgtjgi}|t}|j |j g}|j |ddt|j |ddttjjjdztzdz|j t|d|j |jty)z If the path a module loaded with L{PythonPath.__getitem__} is not present in the path importer cache, a warning is emitted, but the L{PythonModule} is returned as usual. rcategorymessagez (for module zR) not in path importer cache (PEP 302 violation - check your local configuration).rN)r rQr;r flushWarningstest_inconsistentImporterCacher> UserWarningr rh__file__parentdirnamerr#)rr thisModulewarningss rrz.PythonPathTests.test_inconsistentImporterCaches ""2s{{B;8_ %%t'J'J&KL !Z0+>  QK " W%% & - - / 7 7 9  D D  X* (3rcPtj}|jd|y)z L{PythonPath} implements the C{in} operator so that when it is the right-hand argument and the name of a module which exists on that L{PythonPath} is the left-hand argument, the result is C{True}. rvN)r rQassertInrthePaths rtest_containsModulez#PythonPathTests.test_containsModules $$& dG$rcPtj}|jd|y)z L{PythonPath} implements the C{in} operator so that when it is the right-hand argument and the name of a module which does not exist on that L{PythonPath} is the left-hand argument, the result is C{False}. bogusModuleN)r rQ assertNotInrs rtest_doesntContainModulez(PythonPathTests.test_doesntContainModules" $$& 0rNr)rrrr*rrrrrrrrrs B24*%1rr)r,rrrr)&r* __future__rrrr;r importlib.abcrtypesrtypingrrrrhtwisted.pythonr twisted.python.compatr twisted.python.filepathr twisted.python.reflectr #twisted.python.test.modules_helpersr twisted.python.test.test_zippathrtwisted.trial.unittestrrr r,rrrr__all__rrrr s# )!"/,+C2+ 8 K0(K,y)'y)xRH2RHj)*)*242D1hD1N r