
    hhG                    h   S r SSKJr  SSKrSSKrSSKrSSKrSSKJr  SSK	J
r
  SSKJrJr  SSKJr  SSKrSSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr   " S S\5      r " S S\\5      r  " S S\ 5      r! " S S\ 5      r" " S S\"5      r# " S S\"5      r$ " S S\5      r%/ SQr&g)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)TestCasec                  &    \ rS rSr    SS jrSrg)_SupportsWalkModules   c                    g N )selfimportPackagess     `/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/test/test_modules.pywalkModules _SupportsWalkModules.walkModules   s     	    r   N)r   boolreturnz+Generator[modules.PythonModule, None, None])__name__
__module____qualname____firstlineno__r   __static_attributes__r   r   r   r   r      s    "	4r   r   c                  8    \ rS rSrSr\S4       SS jjrSrg)TwistedModulesTestCase%   z'
Base class for L{modules} test cases.
Fc                    UR                  US9 H  nUR                  U:X  d  M  Us  $    U R                  SU< S35        g)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.
r   zUnable to find module z through iteration.N)r   namefail)r   modnamewherer   modinfos        r   findByIteration&TwistedModulesTestCase.findByIteration*   sG     (((GG||w& H 			*7+5HIJr   r   N)r*   strr+   r   r   r   r   zmodules.PythonModule)r   r   r    r!   __doc__r	   r-   r"   r   r   r   r$   r$   %   sE     '.$	KK $K 	K
 
K Kr   r$   c                      \ rS rSrSS jrSS jrSS jrSS jrSS jrSS jr	SS jr
SS	 jrSS
 jrSS jrSS jrSS jrSrg)
BasicTests;   c                   [        S5        SnU R                  5       nUR                  S5      nUR                  S5      R                  U5        UR                  S5      nUR	                  5         UR                  S5      R                  U5        UR                  S5      R                  S5        U R                  5       nUR                  S5      nUR                  S5      R                  U5        UR                  S5      nUR	                  5         UR                  S5      R                  U5        UR                  S5      R                  S5        U R                  UR                  UR                  /5        [        R                  " S5      n UR                  S	S
9 V	s/ s H  oR                  PM     n
n	[        [        R                  R                  5       5       H,  nUR                  S5      (       d  M  [        R                  U	 M.     / SQnU R                  X5        gs  sn	f ! [        [        R                  R                  5       5       H,  nUR                  S5      (       d  M  [        R                  U	 M.     f = f)zL
Duplicate packages are not yielded when iterating over namespace
packages.
pkgutilsB   import pkgutil; __path__ = pkgutil.extend_path(__path__, __name__)test_package__init__.pynested_packagez	module.pyr   z
module2.pyTr'   )r6   ztest_package.nested_packagez"test_package.nested_package.modulez#test_package.nested_package.module2N)
__import__pathEntryWithOnePackagechild
setContentmakedirsreplaceSysPathpathr	   	getModuler   r(   listsyskeys
startswithassertEqual)r   namespaceBoilerplateentrytestPackagePathnestedEntryanotherEntryanotherPackagePathanotherNestedEntrymodulemodwalkedNamesexpecteds               r   test_namespacedPackages"BasicTests.test_namespacedPackages<   s    	9 V 	2 ,,.++n5m,778LM%++,<=-(334HI+&11#6335)//?  /::;OP/556FG##%  /::;OP  .99#>UZZ):):;<"">2
	,/5/A/AQU/A/VW/V88/VKWs{{//12$$^44F+ 3
 	/ Xs{{//12$$^44F+ 3s$   H* !H%6H* %H* *?J-Jc                    / n[         R                  " U R                  5       R                  /UR                  0 0 SS0S9nU R                  U/ 5        U R                  US   R                  5       5        g)z
If a package has been explicitly forbidden from importing by setting a
L{None} key in sys.modules under its name,
L{modules.PythonPath.__getitem__} should still be able to retrieve an
unloaded L{modules.PythonModule} for that package.
r6   N)sysPathmoduleLoaderimporterCachesysPathHooks
moduleDict)r	   
PythonPathr:   r?   appendrE   assertFalseisLoaded)r   shouldNotLoadr?   s      r   test_unimportablePackageGetItem*BasicTests.test_unimportablePackageGetItem   ss     $&!!113889&--&-
 	+n-6689r   c                f   U R                  5       nU R                  UR                  /5        U R                  SS05        [	        [
        R                  " 5       5      nU R                  U Vs/ s H  o3R                  PM     snS/5        U R                  US   R                  5       5        gs  snf )z
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.
r6   Nr   )r:   r>   r?   replaceSysModulesrA   r	   r   rE   r(   r[   r\   )r   existentPathwalkedms       r   #test_unimportablePackageWalkModules.BasicTests.test_unimportablePackageWalkModules   s     335\../056g))+,&1&Q&&&1N3CD++-. 2s   -B.c                   U R                  5       n[        U R                  5       5      nU R                  UR	                  5       5        U R                  UR                  /5        [        R                  " S5      /n[        [        R                  " 5       5      n[        R                  R                  UR                  5        [        [        R                  " 5       5      nU R                  XC5        U R                  XS5        g)zf
Verify that L{modules.walkModules} ignores entries in sys.path which
do not exist in the filesystem.
r6   N)r:   r   mktempr[   existsr>   r?   r	   r@   rA   r   rB   rZ   rE   )r   rb   nonexistentPathrP   beforeModulesafterModuless         r   test_nonexistentPaths BasicTests.test_nonexistentPaths   s    
 335"4;;=1//12\../0%%n56W0023,,-G//1210r   c                   U R                  5       n[        U R                  5       5      nU R                  UR	                  5       5        UR                  S5        U R                  UR                  /5        [        [        R                  " 5       5      n[        R                  R                  UR                  5        [        [        R                  " 5       5      nU R                  X45        g)zp
Verify that L{modules.walkModules} ignores entries in sys.path which
refer to regular files in the filesystem.
s   zip file or whatever
N)r:   r   rh   r[   ri   r<   r>   r?   rA   r	   r   rB   rZ   rE   )r   rb   nonDirectoryPathrk   rl   s        r   test_nonDirectoryPaths!BasicTests.test_nonDirectoryPaths   s    
 335#DKKM2)0023##$=>\../0W0023(--.G//125r   c                n    U R                  [        R                  " S5      U R                  S5      5        g)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.
twistedNrE   r	   r@   r-   r   s    r   test_twistedShowsUpBasicTests.test_twistedShowsUp   s*     	**95t7K7KI7VWr   c           	         U R                  [        R                  " S5      U R                  S[        R                  " S5      S95        g)zW
Verify that the walkModules APIs will give us back subpackages, not just
subpackages.
twisted.pythonrt   )r+   Nru   rv   s    r   test_dottedNamesBasicTests.test_dottedNames   s@    
 	./  !19J9J99U V	
r   c                    [         R                  " 5        H/  nU R                  SUR                  ;   SUR                  -  5        M1     g)zq
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	   iterModulesr[   r(   filePath)r   rM   s     r   test_onlyTopModulesBasicTests.test_onlyTopModules   s@    
 ))+Fv{{"K??$ ,r   c                    S Hq  n[        U5      nU R                  [        R                  " U5      R	                  5       U5        U R                  U R                  U5      R	                  5       U5        Ms     g)zT
Verify that we can locate and load packages, modules, submodules, and
subpackages.
osrt   rz   ztwisted.python.reflectN)r   failUnlessIdenticalr	   r@   loadr-   )r   nrd   s      r   test_loadPackagesAndModules&BasicTests.test_loadPackagesAndModules   sa    
 OAA$$W%6%6q%9%>%>%@!D$$T%9%9!%<%A%A%CQG Or   c                    S HU  nU R                  [        R                  " U5      R                  R                  R
                  [        R
                  5        MW     g)z]
Verify that path entries discovered via module loading are, in fact, on
sys.path somewhere.
r   N)failUnlessInr	   r@   	pathEntryr   r?   rB   )r   r   s     r   test_pathEntriesOnPath!BasicTests.test_pathEntriesOnPath   sA    
 OAg//2<<EEJJCHHU Or   c                  ^ [        U R                  5       5      nUR                  5         [        R                  " UR
                  /S9nUR                  mS
U4S jjnUR                  S5      R                  S5        [        R                  " UR
                  SS9  U R                  [        [        UR                  5       5      5      S5        X2l        U R                  US   R                  UR                  S5      5        g	)zf
Verify that .py files will always be preferred to .pyc files, regardless of
directory listing order.
rT   c                N   >^ T" U 5      nUR                   mSU4S jjnX!l         U$ )Nc                 h   > [        T" 5       5      n U R                  5         U R                  5         U $ r   )rA   sortreverse)xoriginalChildrens    r   evilChildrenLBasicTests.test_alwaysPreferPy.<locals>._evilSmartPath.<locals>.evilChildren  s*     )+,		r   )r   r   )children)pathNameor   r   originalSmartPaths      @r   _evilSmartPath6BasicTests.test_alwaysPreferPy.<locals>._evilSmartPath  s(    !(+A zz &JHr   zabcd.py   
T)quiet   abcdN)r   r/   r   r   )r   rh   createDirectoryr	   rY   r?   
_smartPathr;   r<   
compileallcompile_dirrE   lenrA   r   r   )r   mypathppr   r   s       @r   test_alwaysPreferPyBasicTests.test_alwaysPreferPy  s    
 $++-( 6MM	 	Y**51v{{$7T&//"345q9&F,,fll9.EFr   c                   [        U R                  5       5      nUR                  5         [        R                  " UR
                  /S9nUR                  S5      nUR                  5         UR                  S5      R                  S5        [        R
                  R                  UR
                  5        [        S5         [        UR                  5       5      nU R                  [        U5      S5        U R                  US   R                  S5        [        R                  S	 [        R
                  R!                  UR
                  5        g! [        R                  S	 [        R
                  R!                  UR
                  5        f = f)zk
A package can delete its __path__ for some reasons,
C{modules.PythonPath} should be able to deal with it.
r   r   r7   s   del __path__
   r   N)r   rh   r   r	   rY   r?   r;   r<   rB   rZ   r9   rA   r   rE   r   r(   remove)r   r   r   subpathls        r   test_packageMissingPath"BasicTests.test_packageMissingPath!  s
   
 $++-( 6,,v&!m$//0AB$6	)R^^%&ASVQ'QqTYY/F#HHOOFKK( F#HHOOFKK(s   ?AE <F	r   Nr   None)r   r   r    r!   rQ   r^   re   rm   rq   rw   r{   r   r   r   r   r   r"   r   r   r   r2   r2   ;   sE    J0X:$/1*6&X

HVG@)r   r2   c                      \ rS rSrSr\R                  " 5       rSS jrSS jr	SSS jjr
SS jrSS jrSS jrSS	 jrSS
 jrSrg)PathModificationTestsi7  zd
These tests share setup/cleanup behavior of creating a dummy package and
stuffing some code in it.
c                   U R                  5       U l        [        U R                  5      U l        U R                  R	                  5         S[        U R                  5      4-  U l        U R                  R                  U R                  5      U l	        U R                  R	                  5         U R                  R                  S5      R                  S5        U R                  R                  S5      R                  S5        U R                  R                  S5      R                  S5        U R                  R                  S5      R                  S5        SU l        g )Nzpyspacetests%dr7   r   za.pyzb.pyzc__init__.pyF)rh   pathExtensionNamer   pathExtensionr   next
_serialnumpackageNamer;   packagePathr<   	pathSetUprv   s    r   setUpPathModificationTests.setUp@  s   !%%d&<&<=**,+tDOO/D.FF--33D4D4DE((*}-88=v&11#6v&11#6~.99#>r   c                    U R                   (       a   eSU l         [        R                  R                  U R                  5        g NT)r   rB   r?   rZ   r   rv   s    r   _setupSysPath#PathModificationTests._setupSysPathM  s,    >>!!../r   c                4   U R                  5       n[        U5      nUR                  5         UR                  S5      R	                  S5        U R
                  R                  S5      R	                  [        S[        U5       S35      5        U R                  5         [        R                  " U R                  5      nU R                  U R                  U R                  S-   XAS9US   5        U R                  US   R                  5       R                  S	5        g )
Nz	foozle.pys   x = 123
r7   z__path__.append(z)
z.foozler'   foozle{   )rh   r   r   r;   r<   r   r
   reprr   r	   r@   r   rE   r-   r   r   )r   doImportmoddir2fpmdr,   s        r   _underUnderPathTest)PathModificationTests._underUnderPathTestR  s    ++- 

;**<8}-88,T']O3?@	
 	##D$4$45    9,g !  H		
 	*//133S9r   c                $    U R                  5         g)zM
Verify that iterModules will honor the __path__ of already-loaded packages.
N)r   rv   s    r   "test_underUnderPathAlreadyImported8PathModificationTests.test_underUnderPathAlreadyImportede  s     	  "r   c                   [         R                  " U R                  5      nUR                  5        Vs/ s H   o"R                  R                  S5      S   PM"     nnUR                  5         U R                  U/ SQ5        g s  snf )Nr~   )ab	c__init__)r	   r@   r   r   r(   splitr   rE   )r   pkginfor,   nfnis       r   _listModules"PathModificationTests._listModulesk  si    ##D$4$45;B;N;N;PQ;P""3'+;PQ		67 Rs   'Bc                D    U R                  5         U R                  5         g)zi
Make sure the module list comes back as we expect from iterModules on a
package, whether zipped or not.
N)r   r   rv   s    r   test_listingModules)PathModificationTests.test_listingModulesq  s    
 	r   c                n    U R                  5         [        U R                  5        U R                  5         g)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)r   r   r   r   rv   s    r   "test_listingModulesAlreadyImported8PathModificationTests.test_listingModulesAlreadyImportedy  s*     	!!"r   c                    U R                   (       a`  Sn[        R                  R                  5       U R                  L d
   US-   5       eU R                  [        R                  ;  d
   US-   5       eg g )Nz<path cleanup failed: don't be surprised if other tests breakz, 1z, 2)r   rB   r?   popr   )r   HORKs     r   tearDownPathModificationTests.tearDown  s]     >>QD88<<>T%;%;;ITE\I;))9G4%<G9 r   )r   r   r   r   r   Nr   )T)r   r   r   r   )r   r   r    r!   r0   	itertoolscountr   r   r   r   r   r   r   r   r   r"   r   r   r   r   r   7  s>    
 "J0
:&#8Hr   r   c                  ,    \ rS rSrSrSS jrSS jrSrg)RebindingTestsi  z
These tests verify that the default path interrogation API works properly
even when sys.path has been rebound to a different object.
c                    U R                   (       a   eSU l         [        R                  U l        [        R                  S S  [        l        [        R                  R	                  U R
                  5        g r   )r   rB   r?   savedSysPathrZ   r   rv   s    r   r   RebindingTests._setupSysPath  sG    >>!!HH88A;../r   c                R    U R                   (       a  U R                  [        l        gg)z6
Clean up sys.path by re-binding our original object.
N)r   r   rB   r?   rv   s    r   r   RebindingTests.tearDown  s     >>((CH r   )r   r   Nr   )r   r   r    r!   r0   r   r   r"   r   r   r   r   r     s    
0)r   r   c                      \ rS rSrSS jrSrg)ZipPathModificationTestsi  c                   U R                   (       a   e[        U R                  U R                  S-   5        U =R                  S-  sl        [        R                  " U R                  5      (       d   e[
        R                  U 5        g )Nz.zip)r   r   r   zipfile
is_zipfiler   r   rv   s    r   r   &ZipPathModificationTests._setupSysPath  sf    >>!!d$$d&<&<v&EF&(!!$"8"89999++D1r   r   Nr   )r   r   r    r!   r   r"   r   r   r   r   r     s    2r   r   c                  @    \ rS rSrSrS	S jrS	S jrS	S jrS	S jrSr	g)
PythonPathTestsi  z~
Tests for the class which provides the implementation for all of the
public API of L{twisted.python.modules}, L{PythonPath}.
c                  ^^	  " S S5      mS	U4S jjnS/n0 nU/n0 nS
S jn[         R                  " X#XEU5      n[        UR                  5       5      m	U R	                  [        T	5      S5        U R                  [        U	4S j5        g)ze
Make sure that the behavior when encountering an unknown importer
type is not catastrophic failure.
c                      \ rS rSrSrg)>PythonPathTests.test_unhandledImporter.<locals>.SecretImporteri  r   N)r   r   r    r!   r"   r   r   r   SecretImporterr     s    r   r   c                   > T" 5       $ r   r   )r(   r   s    r   hook4PythonPathTests.test_unhandledImporter.<locals>.hook  s    !##r   zexample/pathc                    g r   r   )r(   s    r   	sysloader9PythonPathTests.test_unhandledImporter.<locals>.sysloader  s    r   r   c                    > T S   S   $ )Nr   rM   r   )entriess   r   <lambda>8PythonPathTests.test_unhandledImporter.<locals>.<lambda>  s    GAJx,@r   N)r(   objectr   r   )r(   r  r   r   )r	   rY   rA   iterEntriesrE   r   assertRaisesKeyError)
r   r   syspath
sysmodulessyshookssyscacher   spacer   r  s
           @@r   test_unhandledImporter&PythonPathTests.test_unhandledImporter  s    	 		$ "",.
668	 ""7IVu((*+Wq)($@Ar   c                   [         R                  " / [        R                   / 0 5      nU[           nU R	                  U R
                  /5      nU R                  US   S   [        5        U R                  US   S   [        [        R                  5      R                  5       R                  5       S-   [        -   S-   5        U R                  [        U5      S5        U R                  UR                  [        5        g)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.
r   categorymessagez (for module zR) not in path importer cache (PEP 302 violation - check your local configuration).r   N)r	   rY   rB   r   flushWarningstest_inconsistentImporterCacherE   UserWarningr   rt   __file__parentdirnamer   r(   )r   r  
thisModulewarningss       r   r  .PythonPathTests.test_inconsistentImporterCache  s     ""2s{{B;8_
%%t'J'J&KL!Z0+>QK	"W%%&--/779DD	
 	X*(3r   c                R    [         R                  " 5       nU R                  SU5        g)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}.
r   N)r	   rY   assertInr   thePaths     r   test_containsModule#PythonPathTests.test_containsModule  s      $$&dG$r   c                R    [         R                  " 5       nU R                  SU5        g)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	   rY   assertNotInr  s     r   test_doesntContainModule(PythonPathTests.test_doesntContainModule  s"     $$&0r   r   Nr   )
r   r   r    r!   r0   r  r  r!  r&  r"   r   r   r   r   r     s    
B24*%1r   r   )r2   r   r   r   r   )'r0   
__future__r   r   r   rB   r   importlib.abcr   typesr   typingr   r   typing_extensionsr   rt   twisted.pythonr	   twisted.python.compatr
   twisted.python.filepathr   twisted.python.reflectr   #twisted.python.test.modules_helpersr    twisted.python.test.test_zippathr   twisted.trial.unittestr   r   r$   r2   r   r   r   r   __all__r   r   r   <module>r5     s    #   
  )  ! &  " / , + C 2 +8 K0( K,y)' y)xRH2 RHj)* )*24 2D1h D1Nr   