HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //lib/python3/dist-packages/twisted/test/__pycache__/test_modules.cpython-38.pyc
U


W[�E�@sdZddlmZmZddlZddlZddlZddlZddlZddl	m
Z
ddlmZddl
mZddlmZddlmZdd	lmZdd
lmZGdd�dee�ZGd
d�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZdddddgZdS)zV
Tests for twisted.python.modules, abstract access to imported or importable
objects.
�)�division�absolute_importN)�modules)�
networkString)�FilePath)�namedAny)�TestCase)�TwistedModulesMixin)�zipitc@seZdZdZedfdd�ZdS)�TwistedModulesTestCasez/
    Base class for L{modules} test cases.
    FcCs8|j|d�D]}|j|kr|Sq|�d|f�dS)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.
        ��importPackagesz+Unable to find module %r through iteration.N)�walkModules�nameZfail)�self�modname�wherer
�modinfo�r�;/usr/lib/python3/dist-packages/twisted/test/test_modules.py�findByIteration!s

z&TwistedModulesTestCase.findByIterationN)�__name__�
__module__�__qualname__�__doc__rrrrrrrsrc@sleZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�ZdS)�
BasicTestsc
Cs0td�d}|��}|�d�}|�d��|�|�d�}|��|�d��|�|�d��d�|��}|�d�}|�d��|�|�d�}|��|�d��|�|�d��d�|�|j|jg�t�d�}zd	d
�|j
dd�D�}	W5t	t
j���D]}|�d�r�t
j|=q�Xdd
ddg}
|�|	|
�dS)zd
        Duplicate packages are not yielded when iterating over namespace
        packages.
        �pkgutilsBimport pkgutil; __path__ = pkgutil.extend_path(__path__, __name__)�test_package�__init__.pyZnested_packagez	module.py�z
module2.pycSsg|]
}|j�qSr�r)�.0�modrrr�
<listcomp>msz6BasicTests.test_namespacedPackages.<locals>.<listcomp>Trztest_package.nested_packagez"test_package.nested_package.modulez#test_package.nested_package.module2N)�
__import__�pathEntryWithOnePackage�child�
setContent�makedirs�replaceSysPath�pathr�	getModule�list�sys�keys�
startswithr�assertEqual)rZnamespaceBoilerplate�entryZtestPackagePathZnestedEntryZanotherEntryZanotherPackagePathZanotherNestedEntry�moduleZwalkedNames�expectedrrr�test_namespacedPackages0s@�





�

�z"BasicTests.test_namespacedPackagescCsHg}tj|��jg|jiiddid�}|�|g�|�|d���dS)a
        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.
        rN)�sysPathZmoduleLoaderZ
importerCacheZsysPathHooksZ
moduleDict)r�
PythonPathr%r*�appendr0�assertFalse�isLoaded)rZ
shouldNotLoadr*rrr�test_unimportablePackageGetItem~s�z*BasicTests.test_unimportablePackageGetItemcCs^|��}|�|jg�|�ddi�tt���}|�dd�|D�dg�|�|d�	��dS)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.
        rNcSsg|]
}|j�qSrr )r!�mrrrr#�szBBasicTests.test_unimportablePackageWalkModules.<locals>.<listcomp>r)
r%r)r*ZreplaceSysModulesr,rrr0r8r9)r�existentPathZwalkedrrr�#test_unimportablePackageWalkModules�s�z.BasicTests.test_unimportablePackageWalkModulescCs~|��}t|���}|�|���|�|jg�t�d�g}t	t�
��}tj�|j�t	t�
��}|�
||�|�
||�dS)z~
        Verify that L{modules.walkModules} ignores entries in sys.path which
        do not exist in the filesystem.
        rN)r%r�mktempr8�existsr)r*rr+r,rr-r7r0)rr<ZnonexistentPathr3�
beforeModules�afterModulesrrr�test_nonexistentPaths�sz BasicTests.test_nonexistentPathscCsp|��}t|���}|�|���|�d�|�|jg�tt	�
��}tj�|j�tt	�
��}|�
||�dS)z�
        Verify that L{modules.walkModules} ignores entries in sys.path which
        refer to regular files in the filesystem.
        szip file or whatever
N)r%rr>r8r?r'r)r*r,rrr-r7r0)rr<ZnonDirectoryPathr@rArrr�test_nonDirectoryPaths�s
z!BasicTests.test_nonDirectoryPathscCs|�t�d�|�d��dS)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�r0rr+r�rrrr�test_twistedShowsUp�s�zBasicTests.test_twistedShowsUpcCs&|�t�d�|jdt�d�d��dS)zo
        Verify that the walkModules APIs will give us back subpackages, not just
        subpackages.
        �twisted.pythonrD)rNrErFrrr�test_dottedNames�s��zBasicTests.test_dottedNamescCs*t��D]}|�d|jkd|j�qdS)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�iterModulesr8r�filePath)rr2rrr�test_onlyTopModules�s��zBasicTests.test_onlyTopModulescCsBdD]8}t|�}|�t�|���|�|�|�|���|�qdS)zl
        Verify that we can locate and load packages, modules, submodules, and
        subpackages.
        ��osrDrHztwisted.python.reflectN)rZfailUnlessIdenticalrr+�loadr)r�nr;rrr�test_loadPackagesAndModules�s��z&BasicTests.test_loadPackagesAndModulescCs(dD]}|�t�|�jjjtj�qdS)zu
        Verify that path entries discovered via module loading are, in fact, on
        sys.path somewhere.
        rNN)ZfailUnlessInrr+Z	pathEntryrLr*r-)rrQrrr�test_pathEntriesOnPath�s
�z!BasicTests.test_pathEntriesOnPathcs�t|���}|��tj|jgd�}|j��fdd�}|�d��d�t	j
|jdd�|�tt
|����d�||_|�|d	j|�d��d
S)z~
        Verify that .py files will always be preferred to .pyc files, regardless of
        directory listing order.
        �r5cs$�|�}|j��fdd�}||_|S)Ncst���}|��|��|S�N)r,�sort�reverse)�x�ZoriginalChildrenrr�evilChildrens
zLBasicTests.test_alwaysPreferPy.<locals>._evilSmartPath.<locals>.evilChildren)�children)ZpathName�orZ�ZoriginalSmartPathrYr�_evilSmartPaths
z6BasicTests.test_alwaysPreferPy.<locals>._evilSmartPathzabcd.py�
T)�quiet��abcdN)rr>�createDirectoryrr6r*Z
_smartPathr&r'�
compileallZcompile_dirr0�lenr,r[rL)r�mypath�ppr^rr]r�test_alwaysPreferPys�zBasicTests.test_alwaysPreferPyc	Cs�t|���}|��tj|jgd�}|�d�}|��|�d��d�tj�	|j�t
d�z2t|�
��}|�t|�d�|�|djd�W5tjd=tj�|j�XdS)z�
        A package can delete its __path__ for some reasons,
        C{modules.PythonPath} should be able to deal with it.
        rTrbrs
del __path__
�rN)rr>rcrr6r*r&r'r-r7r$�remover,rr0rer)rrfrgZsubpath�lrrr�test_packageMissingPath)s
z"BasicTests.test_packageMissingPathN)rrrr4r:r=rBrCrGrIrMrRrSrhrlrrrrr.sN
rc@sZeZdZdZe��Zdd�Zdd�Zddd�Z	d	d
�Z
dd�Zd
d�Zdd�Z
dd�ZdS)�PathModificationTestszp
    These tests share setup/cleanup behavior of creating a dummy package and
    stuffing some code in it.
    cCs�|��|_t|j�|_|j��dt|j�f|_|j�|j�|_	|j	��|j	�d��
d�|j	�d��
d�|j	�d��
d�|j	�d��
d�d|_dS)Nzpyspacetests%drrza.pyzb.pyzc__init__.pyF)r>�pathExtensionNamerZ
pathExtensionrc�next�
_serialnum�packageNamer&�packagePathr'�	pathSetUprFrrr�setUpIs


zPathModificationTests.setUpcCs"|jr
t�d|_tj�|j�dS�NT)rs�AssertionErrorr-r*r7rnrFrrr�
_setupSysPathWs
z#PathModificationTests._setupSysPathTcCs�|��}t|�}|��|�d��d�|j�d��td�t|����|�	�t
�|j�}|�
|j|jd||d�|d�|�
|d��jd�dS)	Nz	foozle.pysx = 123
rz__path__.append({0})
z.foozlerZfoozle�{)r>rrcr&r'rrr�format�reprrwrr+rqr0rrPrX)rZdoImportZmoddir2Zfpmdrrrr�_underUnderPathTest]s ���z)PathModificationTests._underUnderPathTestcCs|��dS)z]
        Verify that iterModules will honor the __path__ of already-loaded packages.
        N)r{rFrrr�"test_underUnderPathAlreadyImportednsz8PathModificationTests.test_underUnderPathAlreadyImportedcCs<t�|j�}dd�|��D�}|��|�|dddg�dS)NcSsg|]}|j�d�d�qS)rJ���)r�split)r!rrrrr#wsz6PathModificationTests._listModules.<locals>.<listcomp>�a�bZ	c__init__)rr+rqrKrVr0)rZpkginfoZnfnirrr�_listModulesus�z"PathModificationTests._listModulescCs|��|��dS)z�
        Make sure the module list comes back as we expect from iterModules on a
        package, whether zipped or not.
        N)rwr�rFrrr�test_listingModules}sz)PathModificationTests.test_listingModulescCs|��t|j�|��dS)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)rwrrqr�rFrrr�"test_listingModulesAlreadyImported�s
z8PathModificationTests.test_listingModulesAlreadyImportedcCsB|jr>d}tj��|jks&t|d��|jtjks>t|d��dS)Nz<path cleanup failed: don't be surprised if other tests breakz, 1z, 2)rsr-r*�poprnrv)rZHORKrrr�tearDown�szPathModificationTests.tearDownN)T)rrrr�	itertools�countrprtrwr{r|r�r�r�r�rrrrrm@s
	rmc@s eZdZdZdd�Zdd�ZdS)�RebindingTestsz�
    These tests verify that the default path interrogation API works properly
    even when sys.path has been rebound to a different object.
    cCs:|jr
t�d|_tj|_tjdd�t_tj�|j�dSru)rsrvr-r*�savedSysPathr7rnrFrrrrw�s

zRebindingTests._setupSysPathcCs|jr|jt_dS)zF
        Clean up sys.path by re-binding our original object.
        N)rsr�r-r*rFrrrr��szRebindingTests.tearDownN)rrrrrwr�rrrrr��sr�c@seZdZdd�ZdS)�ZipPathModificationTestscCsH|jr
t�t|j|jd�|jd7_t�|j�s:t�t�|�dS)Nz.zip)rsrvr
rn�zipfileZ
is_zipfilermrwrFrrrrw�s

z&ZipPathModificationTests._setupSysPathN)rrrrwrrrrr��sr�c@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�PythonPathTestsz�
    Tests for the class which provides the implementation for all of the
    public API of L{twisted.python.modules}, L{PythonPath}.
    cs~Gdd�dt���fdd�}dg}i}|g}i}dd�}t�|||||�}t|����|�t��d�|�t�fd	d
��dS)z}
        Make sure that the behavior when encountering an unknown importer
        type is not catastrophic failure.
        c@seZdZdS)z>PythonPathTests.test_unhandledImporter.<locals>.SecretImporterN)rrrrrrr�SecretImporter�sr�cs��SrUrr )r�rr�hook�sz4PythonPathTests.test_unhandledImporter.<locals>.hookzexample/pathcSsdSrUrr rrr�	sysloader�sz9PythonPathTests.test_unhandledImporter.<locals>.sysloaderrics�ddS)Nrr2rr)�entriesrr�<lambda>�rz8PythonPathTests.test_unhandledImporter.<locals>.<lambda>N)	�objectrr6r,ZiterEntriesr0reZassertRaises�KeyError)rr�ZsyspathZ
sysmodulesZsyshooksZsyscacher��spacer)r�r�r�test_unhandledImporter�s"�z&PythonPathTests.test_unhandledImportercCs�t�gtjgi�}|t}|�|jg�}|�|ddt�|�|ddtt	j
�����dtd�|�t
|�d�|�|jt�dS)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).riN)rr6r-rZ
flushWarnings�test_inconsistentImporterCacher0�UserWarningrrD�__file__�parent�dirnamerer)rr�Z
thisModule�warningsrrrr��s 
����z.PythonPathTests.test_inconsistentImporterCachecCst��}|�d|�dS)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}.
        rON)rr6ZassertIn�rZthePathrrr�test_containsModule�sz#PythonPathTests.test_containsModulecCst��}|�d|�dS)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}.
        ZbogusModuleN)rr6ZassertNotInr�rrr�test_doesntContainModule�sz(PythonPathTests.test_doesntContainModuleN)rrrrr�r�r�r�rrrrr��s

r�)rZ
__future__rrr-r�rdr�rDZtwisted.pythonrZtwisted.python.compatrZtwisted.python.filepathrZtwisted.python.reflectrZtwisted.trial.unittestrZ#twisted.python.test.modules_helpersr	Z twisted.python.test.test_zippathr
rrrmr�r�r��__all__rrrr�<module>s2[	D�