a
    ¼3jE*  ć                   @   sh  d dl Z d dlZd dlZd dlZd dlZd dlmZ e jdd Ze jdd Z	G dd dej
ZG d	d
 d
eZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd  d eZG d!d" d"eZG d#d$ d$eZG d%d& d&eZed'krde ”  dS )(é    N)Śutilc               	   c   sZ   t j} t j ” }z&dV  W | t _t j ”  t j |” n| t _t j ”  t j |” 0 dS )zĻ
    Make sure sys.modules is the same object and has the same content
    when exiting the context as when entering.

    Similar to importlib.test.util.uncache, but doesn't require explicit
    names.
    N)ŚsysŚmodulesŚcopyŚclearŚupdate)Zsys_modules_savedZsys_modules_copy© r   śc/www/server/python_manager/versions/3.9.10/lib/python3.9/test/test_importlib/test_namespace_pkgs.pyŚsys_modules_context   s    	

ž
r
   c               	   k   s   |   dtj” |   dtj” tjf i | ¤}|< t  dV  W d   n1 sT0    Y  W d   n1 sr0    Y  dS )zÖ
    Save import state and sys.modules cache and restore it on exit.
    Typical usage:

    >>> with namespace_tree_context(path=['/tmp/xxyy/portion1',
    ...         '/tmp/xxyy/portion2']):
    ...     pass
    Ś	meta_pathŚ
path_hooksN)Ś
setdefaultr   r   r   r   Zimport_stater
   )ŚkwargsZimport_contextr   r   r	   Śnamespace_tree_context$   s
    r   c                   @   s6   e Zd ZdZej ej e”d”Z	dd Z
dd ZdS )ŚNamespacePackageTestzj
    Subclasses should define self.root and self.paths (under that root)
    to be added to sys.path.
    Znamespace_pkgsc                    s2    fdd j D  _t jd _ j ”  d S )Nc                    s   g | ]}t j  j|”qS r   )ŚosŚpathŚjoinŚroot)Ś.0r   ©Śselfr   r	   Ś
<listcomp>=   s   z.NamespacePackageTest.setUp.<locals>.<listcomp>)r   )ŚpathsZresolved_pathsr   ŚctxŚ	__enter__r   r   r   r	   ŚsetUp<   s
    
’zNamespacePackageTest.setUpc                 C   s   | j  d d d ” d S )N)r   Ś__exit__r   r   r   r	   ŚtearDownC   s    zNamespacePackageTest.tearDownN)Ś__name__Ś
__module__Ś__qualname__Ś__doc__r   r   r   ŚdirnameŚ__file__r   r   r   r   r   r   r	   r   5   s   r   c                   @   s*   e Zd ZdgZdd Zdd Zdd ZdS )	ŚSingleNamespacePackageŚportion1c                 C   s   dd l }|  |jjd” d S ©Nr   śportion1 foo one©Śfoo.oneŚassertEqualŚoneŚattr©r   Śfoor   r   r	   Śtest_simple_packageK   s    z*SingleNamespacePackage.test_simple_packagec                 C   s6   |   t” dd l}W d    n1 s(0    Y  d S ©Nr   ©ŚassertRaisesŚImportErrorŚfoo.twor.   r   r   r	   Śtest_cant_import_otherO   s    z-SingleNamespacePackage.test_cant_import_otherc                 C   s   dd l }|  t|d” d S )Nr   z<module 'foo' (namespace)>)r*   r+   Śreprr.   r   r   r	   Śtest_module_reprS   s    z'SingleNamespacePackage.test_module_reprN)r   r    r!   r   r0   r6   r8   r   r   r   r	   r%   H   s   r%   c                   @   s   e Zd ZdgZdd ZdS )ŚDynamicPathNamespacePackager&   c                 C   s~   dd l }|  |jjd” |  t” dd l}W d    n1 s@0    Y  tj 	t
j | jd”” dd l}|  |jjd” d S )Nr   r(   Śportion2śportion2 foo two)r*   r+   r,   r-   r3   r4   r5   r   r   Śappendr   r   r   Śtwor.   r   r   r	   Śtest_dynamic_path[   s    &z-DynamicPathNamespacePackage.test_dynamic_pathN)r   r    r!   r   r>   r   r   r   r	   r9   X   s   r9   c                   @   s   e Zd ZdgZdd ZdS )ŚCombinedNamespacePackagesŚboth_portionsc                 C   s4   dd l }dd l}|  |jjd” |  |jjd” d S ©Nr   zboth_portions foo oneśboth_portions foo two©r*   r5   r+   r,   r-   r=   r.   r   r   r	   Śtest_importsn   s    z&CombinedNamespacePackages.test_importsN©r   r    r!   r   rD   r   r   r   r	   r?   k   s   r?   c                   @   s   e Zd ZddgZdd ZdS )ŚSeparatedNamespacePackagesr&   r:   c                 C   s4   dd l }dd l}|  |jjd” |  |jjd” d S )Nr   r(   r;   rC   r.   r   r   r	   rD   x   s    z'SeparatedNamespacePackages.test_importsNrE   r   r   r   r	   rF   u   s   rF   c                   @   s,   e Zd ZddgZdd Zdd Zdd Zd	S )
Ś%SeparatedOverlappingNamespacePackagesr&   r@   c                 C   s4   dd l }dd l}|  |jjd” |  |jjd” d S ©Nr   r(   rB   rC   r.   r   r   r	   Śtest_first_path_wins   s    z:SeparatedOverlappingNamespacePackages.test_first_path_winsc                 C   s>   t j ”  dd l}dd l}|  |jjd” |  |jjd” d S rA   )	r   r   Śreverser*   r5   r+   r,   r-   r=   r.   r   r   r	   Śtest_first_path_wins_again   s
    
z@SeparatedOverlappingNamespacePackages.test_first_path_wins_againc                 C   s4   dd l }dd l}|  |jjd” |  |jjd” d S rH   )r5   r*   r+   r,   r-   r=   r.   r   r   r	   Ś+test_first_path_wins_importing_second_first   s    zQSeparatedOverlappingNamespacePackages.test_first_path_wins_importing_second_firstN)r   r    r!   r   rI   rK   rL   r   r   r   r	   rG      s   rG   c                   @   s"   e Zd ZdgZdd Zdd ZdS )ŚSingleZipNamespacePackageśtop_level_portion1.zipc                 C   s   dd l }|  |jjd” d S r'   r)   r.   r   r   r	   r0      s    z-SingleZipNamespacePackage.test_simple_packagec                 C   s6   |   t” dd l}W d    n1 s(0    Y  d S r1   r2   r.   r   r   r	   r6      s    z0SingleZipNamespacePackage.test_cant_import_otherN©r   r    r!   r   r0   r6   r   r   r   r	   rM      s   rM   c                   @   s   e Zd ZddgZdd ZdS )ŚSeparatedZipNamespacePackagesrN   r:   c                 C   sT   dd l }dd l}|  |jjd” |  |jjd” |  d|jj” |  d|jj” d S )Nr   r(   r;   rN   ś.zip)	r*   r5   r+   r,   r-   r=   ŚassertInr$   ŚassertNotInr.   r   r   r	   rD   „   s    z*SeparatedZipNamespacePackages.test_importsNrE   r   r   r   r	   rP   ¢   s   rP   c                   @   s"   e Zd ZdgZdd Zdd ZdS )ŚSingleNestedZipNamespacePackageś#nested_portion1.zip/nested_portion1c                 C   s   dd l }|  |jjd” d S r'   r)   r.   r   r   r	   r0   ±   s    z3SingleNestedZipNamespacePackage.test_simple_packagec                 C   s6   |   t” dd l}W d    n1 s(0    Y  d S r1   r2   r.   r   r   r	   r6   µ   s    z6SingleNestedZipNamespacePackage.test_cant_import_otherNrO   r   r   r   r	   rT   ®   s   rT   c                   @   s   e Zd ZddgZdd ZdS )Ś#SeparatedNestedZipNamespacePackagesrU   r:   c                 C   sb   dd l }dd l}|  |jjd” |  |jjd” tj dd”}|  	||jj
” |  d|jj
” d S )Nr   r(   r;   znested_portion1.zipZnested_portion1rQ   )r*   r5   r+   r,   r-   r=   r   r   r   rR   r$   rS   )r   r/   Śfnr   r   r	   rD   ½   s    z0SeparatedNestedZipNamespacePackages.test_importsNrE   r   r   r   r	   rV   ŗ   s   rV   c                   @   s   e Zd Zg d¢Zdd ZdS )ŚLegacySupport)Znot_a_namespace_pkgr&   r:   r@   c                 C   sb   dd l }|  t” dd l}W d    n1 s00    Y  |  d|j” |  dt|j 	” ” d S )Nr   Ś__init__Ś	namespace)
r*   r3   r4   r5   rR   r$   rS   ŚstrŚ
__loader__Ślowerr.   r   r   r	   Ś+test_non_namespace_package_takes_precedenceŹ   s
    &z9LegacySupport.test_non_namespace_package_takes_precedenceN)r   r    r!   r   r^   r   r   r   r	   rX   Ē   s   rX   c                   @   s$   e Zd ZddgZdd Zdd ZdS )ŚDynamicPathCalculationZproject1Zproject2c                 C   sÜ   dd l }|  t|jd” |  t|jjd” dd l}|  t|jd” |  t|jjd” |  |jjjd” |  |jjjd” |  	t
” dd l}W d    n1 sØ0    Y  |  t|jd” |  t|jjd” d S )Nr   é   śparent child oneśparent child two)Śparent.child.oner+   ŚlenŚ__path__ŚchildŚparent.child.twor,   r-   r=   r3   r4   Śparent.child.three©r   Śparentr   r   r	   Śtest_project3_failsÕ   s    &z*DynamicPathCalculation.test_project3_failsc                 C   s  dd l }|  t|jd” |  t|jjd” dd l}|  t|jd” |  t|jjd” |  |jjjd” |  |jjjd” |  	t
” dd l}W d    n1 sØ0    Y  tj tj | jd”” dd l}|  t|jd” |  t|jjd” |  |jjjd” d S )Nr   r`   ra   rb   Zproject3é   zparent child three)rc   r+   rd   re   rf   rg   r,   r-   r=   r3   r4   rh   r   r   r<   r   r   r   Zthreeri   r   r   r	   Śtest_project3_succeedsę   s    &z-DynamicPathCalculation.test_project3_succeedsN)r   r    r!   r   rk   rm   r   r   r   r	   r_   Ņ   s   r_   c                   @   s(   e Zd ZdgZejdd Zdd ZdS )ŚZipWithMissingDirectoryzmissing_directory.zipc                 C   s   dd l }d S r1   )r*   r.   r   r   r	   Śtest_missing_directory  s    z.ZipWithMissingDirectory.test_missing_directoryc                 C   s   dd l }|  |jjd” d S )Nr   zmissing_directory foo two)Zbar.twor+   r=   r-   )r   Zbarr   r   r	   Śtest_present_directory  s    z.ZipWithMissingDirectory.test_present_directoryN)r   r    r!   r   ŚunittestZexpectedFailurero   rp   r   r   r   r	   rn   ’   s   
rn   c                   @   s   e Zd ZdgZdd ZdS )Ś"ModuleAndNamespacePackageInSameDirZmodule_and_namespace_packagec                 C   s   dd l }|  |jd” d S )Nr   z	in module)Śa_testr+   r-   )r   rs   r   r   r	   Ś$test_module_before_namespace_package  s    zGModuleAndNamespacePackageInSameDir.test_module_before_namespace_packageN)r   r    r!   r   rt   r   r   r   r	   rr     s   rr   c                   @   s*   e Zd ZdgZdd Zdd Zdd ZdS )	ŚReloadTestsr&   c                 C   s&   dd l }t |”}|  |jjd” d S r'   )r*   Ś	importlibŚreloadr+   r,   r-   r.   r   r   r	   r0   %  s    
zReloadTests.test_simple_packagec                 C   sz   dd l }|  t” dd l}W d    n1 s00    Y  t |”}|  t” dd l}W d    n1 sl0    Y  d S r1   )r/   r3   r4   r5   rv   rw   r.   r   r   r	   r6   *  s    &
z"ReloadTests.test_cant_import_otherc                 C   sx   dd l }|  t” dd l}W d    n1 s00    Y  tj tj | j	d”” t
 |”}dd l}|  |jjd” d S )Nr   r:   r;   )r*   r3   r4   r5   r   r   r<   r   r   r   rv   rw   r+   r=   r-   r.   r   r   r	   r>   2  s    &
zReloadTests.test_dynamic_pathN)r   r    r!   r   r0   r6   r>   r   r   r   r	   ru   "  s   ru   c                   @   s*   e Zd ZdgZdd Zdd Zdd ZdS )	ŚLoaderTestsr&   c                 C   s*   dd l }|  |j|jj” |  |j” d S r1   )r/   r+   r\   Ś__spec__ŚloaderZassertIsNotNoner.   r   r   r	   Ś!test_namespace_loader_consistencyC  s    z-LoaderTests.test_namespace_loader_consistencyc                 C   s&   dd l }|  |jj” |  |j” d S r1   )r/   ZassertIsNonery   Śoriginr$   r.   r   r   r	   Ś!test_namespace_origin_consistencyI  s    z-LoaderTests.test_namespace_origin_consistencyc                 C   s0   dd l }tj | jdd”}|  |jd |” d S )Nr   r&   r/   )r/   r   r   r   r   r+   re   )r   r/   Zexpected_pathr   r   r	   Śtest_path_indexableO  s    zLoaderTests.test_path_indexableN)r   r    r!   r   r{   r}   r~   r   r   r   r	   rx   @  s   rx   Ś__main__)Ś
contextlibrv   r   r   rq   Ztest.test_importlibr   Ścontextmanagerr
   r   ZTestCaser   r%   r9   r?   rF   rG   rM   rP   rT   rV   rX   r_   rn   rr   ru   rx   r   Śmainr   r   r   r	   Ś<module>   s6   




-

