
    dafa0                     X   d dl Z d dlZd dlZd dlZ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        Ze j                  d        Z G d dej                        Z G d de      Z G d	 d
e      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d d e      Z G d! d"e      Z G d# d$e      Z G d% d&e      Ze d'k(  r ejB                          yy)(    N)utilc               #     K   t         j                  } t         j                  j                         }	 d | t         _        t         j                  j                          t         j                  j	                  |       y# | t         _        t         j                  j                          t         j                  j	                  |       w xY ww)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)sys_modules_savedsys_modules_copys     B/root/Python-3.12.4/Lib/test/test_importlib/test_namespace_pkgs.pysys_modules_contextr      s      {{'')-'+, (+,s   /CA? A	C?A
C		Cc               +   *  K   | j                  dt        j                         | j                  dt        j                         t	        j
                  di | }|5  t               5  d ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY ww)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   import_stater   )kwargsimport_contexts     r   namespace_tree_contextr   (   sx      k3==1
lCNN3&&00N	 ,.      s6   ABB%A;*B2	B;B	 BBBc                       e Zd ZdZej
                  j                  ej
                  j                  e      d      Z	d Z
y)NamespacePackageTestzj
    Subclasses should define self.root and self.paths (under that root)
    to be added to sys.path.
    namespace_pkgsc                     | j                   D cg c],  }t        j                  j                  | j                  |      . c}| _        | j                  t        | j
                               y c c}w )N)path)pathsosr   joinrootresolved_pathsenterContextr   )selfr   s     r   setUpzNamespacePackageTest.setUp@   sR    6:jj
.2BGGLLD)
 	0d6I6IJK
s   1A.N)__name__
__module____qualname____doc__r   r   r   dirname__file__r   r#   r       r   r   r   9   s1     77<<13CDDLr*   r   c                   $    e Zd ZdgZd Zd Zd Zy)SingleNamespacePackageportion1c                 X    dd l }| j                  |j                  j                  d       y Nr   portion1 foo onefoo.oneassertEqualoneattrr"   foos     r   test_simple_packagez*SingleNamespacePackage.test_simple_packageJ       '9:r*   c                 `    | j                  t              5  dd l}d d d        y # 1 sw Y   y xY wNr   assertRaisesImportErrorfoo.twor6   s     r   test_cant_import_otherz-SingleNamespacePackage.test_cant_import_otherN   (    {+ 		 	 	   $-c                 ^    dd l }| j                  t        |      j                  d             y )Nr   z <module 'foo' (namespace) from [)r2   
assertTruerepr
startswithr6   s     r   test_simple_reprz'SingleNamespacePackage.test_simple_reprR   s!    S	,,-OPQr*   N)r$   r%   r&   r   r8   r@   rG   r   r*   r   r,   r,   G   s    LE;Rr*   r,   c                       e Zd ZdgZd Zy)DynamicPathNamespacePackager-   c                    dd l }| j                  |j                  j                  d       | j	                  t
              5  dd l}d d d        t        j                  j                  t        j                  j                  | j                  d             dd l}| j                  |j                  j                  d       y # 1 sw Y   {xY w)Nr   r0   portion2portion2 foo two)r2   r3   r4   r5   r=   r>   r?   r   r   appendr   r   r   twor6   s     r   test_dynamic_pathz-DynamicPathNamespacePackage.test_dynamic_pathZ   s    '9:{+ 		 	TYY
;< 	'9:	 	s    B??CN)r$   r%   r&   r   rO   r   r*   r   rI   rI   W   s    LE;r*   rI   c                       e Zd ZdgZd Zy)CombinedNamespacePackagesboth_portionsc                     dd l }dd l}| j                  |j                  j                  d       | j                  |j
                  j                  d       y Nr   zboth_portions foo oneboth_portions foo twor2   r?   r3   r4   r5   rN   r6   s     r   test_importsz&CombinedNamespacePackages.test_importsm   s:    '>?'>?r*   Nr$   r%   r&   r   rW   r   r*   r   rQ   rQ   j   s    E@r*   rQ   c                       e Zd ZddgZd Zy)SeparatedNamespacePackagesr-   rK   c                     dd l }dd l}| j                  |j                  j                  d       | j                  |j
                  j                  d       y )Nr   r0   rL   rV   r6   s     r   rW   z'SeparatedNamespacePackages.test_importsw   s:    '9:'9:r*   NrX   r   r*   r   rZ   rZ   t   s    $E;r*   rZ   c                       e Zd ZdgZd Zy)-SeparatedNamespacePackagesCreatedWhileRunningr-   c                 .   t        j                         5 }t        j                  j	                  |       dd l}| j                  |j                  j                  d       | j                  t              5  dd l}d d d        t        j                  j                  |d      }t        j                  |       t        j                  j                  |d      }t        |dd      5 }|j!                  d       d d d        | j                  t              5  dd l}d d d        t#        j$                          dd l}| j                  |j&                  j                  d	       d d d        y # 1 sw Y   xY w# 1 sw Y   ~xY w# 1 sw Y   hxY w# 1 sw Y   y xY w)
Nr   r0   r7   zjust_created.pywzutf-8)encodingzattr = "just_created foo"zjust_created foo)tempfileTemporaryDirectoryr   r   rM   r2   r3   r4   r5   r=   r>   foo.just_createdr   r   mkdiropenwrite	importlibinvalidate_cachesjust_created)r"   temp_dirr7   namespace_pathmodule_pathfiles         r   test_invalidate_cacheszDSeparatedNamespacePackagesCreatedWhileRunning.test_invalidate_caches   sB   ((* 	Hh HHOOH%SWW\\+=> "";/ ('(
  WW\\(E:NHH^$'',,~7HIKk39 8T

678 "";/ ('( '')#S--224FG7	H 	H( (8 8( (+	H 	HsV   AF4E'9A+F$E36FE?AF'E0	,F3E<	8F?F	FFN)r$   r%   r&   r   rn   r   r*   r   r]   r]   ~   s    LEHr*   r]   c                   &    e Zd ZddgZd Zd Zd Zy)%SeparatedOverlappingNamespacePackagesr-   rR   c                     dd l }dd l}| j                  |j                  j                  d       | j                  |j
                  j                  d       y Nr   r0   rU   rV   r6   s     r   test_first_path_winsz:SeparatedOverlappingNamespacePackages.test_first_path_wins   :    '9:'>?r*   c                     t         j                  j                          dd l}dd l}| j                  |j                  j                  d       | j                  |j                  j                  d       y rT   )	r   r   reverser2   r?   r3   r4   r5   rN   r6   s     r   test_first_path_wins_againz@SeparatedOverlappingNamespacePackages.test_first_path_wins_again   sJ    '>?'>?r*   c                     dd l }dd l}| j                  |j                  j                  d       | j                  |j
                  j                  d       y rr   )r?   r2   r3   r4   r5   rN   r6   s     r   +test_first_path_wins_importing_second_firstzQSeparatedOverlappingNamespacePackages.test_first_path_wins_importing_second_first   rt   r*   N)r$   r%   r&   r   rs   rw   ry   r   r*   r   rp   rp      s    )E@@@r*   rp   c                       e Zd ZdgZd Zd Zy)SingleZipNamespacePackagetop_level_portion1.zipc                 X    dd l }| j                  |j                  j                  d       y r/   r1   r6   s     r   r8   z-SingleZipNamespacePackage.test_simple_package   r9   r*   c                 `    | j                  t              5  dd l}d d d        y # 1 sw Y   y xY wr;   r<   r6   s     r   r@   z0SingleZipNamespacePackage.test_cant_import_other   rA   rB   Nr$   r%   r&   r   r8   r@   r   r*   r   r{   r{      s    %&E;r*   r{   c                       e Zd ZddgZd Zy)SeparatedZipNamespacePackagesr|   rK   c                 D   dd l }dd l}| j                  |j                  j                  d       | j                  |j
                  j                  d       | j                  d|j                  j                         | j                  d|j
                  j                         y )Nr   r0   rL   r|   .zip)	r2   r?   r3   r4   r5   rN   assertInr)   assertNotInr6   s     r   rW   z*SeparatedZipNamespacePackages.test_imports   sm    '9:'9:.0@0@A!1!12r*   NrX   r   r*   r   r   r      s    %z2E3r*   r   c                       e Zd ZdgZd Zd Zy)SingleNestedZipNamespacePackage#nested_portion1.zip/nested_portion1c                 X    dd l }| j                  |j                  j                  d       y r/   r1   r6   s     r   r8   z3SingleNestedZipNamespacePackage.test_simple_package   r9   r*   c                 `    | j                  t              5  dd l}d d d        y # 1 sw Y   y xY wr;   r<   r6   s     r   r@   z6SingleNestedZipNamespacePackage.test_cant_import_other   rA   rB   Nr   r   r*   r   r   r      s    23E;r*   r   c                       e Zd ZddgZd Zy)#SeparatedNestedZipNamespacePackagesr   rK   c                    dd l }dd l}| j                  |j                  j                  d       | j                  |j
                  j                  d       t        j                  j                  dd      }| j                  ||j                  j                         | j                  d|j
                  j                         y )Nr   r0   rL   znested_portion1.zipnested_portion1r   )r2   r?   r3   r4   r5   rN   r   r   r   r   r)   r   )r"   r7   fns      r   rW   z0SeparatedNestedZipNamespacePackages.test_imports   s    '9:'9:WW\\/1BCb#''**+!1!12r*   NrX   r   r*   r   r   r      s    2J?E3r*   r   c                       e Zd Zg dZd Zy)LegacySupport)not_a_namespace_pkgr-   rK   rR   c                    dd l }| j                  t              5  dd l}d d d        | j	                  d|j
                         | j                  dt        |j                        j                                y # 1 sw Y   YxY w)Nr   __init__	namespace)
r2   r=   r>   r?   r   r)   r   str
__loader__lowerr6   s     r   +test_non_namespace_package_takes_precedencez9LegacySupport.test_non_namespace_package_takes_precedence   s_    {+ 		j#,,/c#..&9&?&?&AB	 	s   A77B N)r$   r%   r&   r   r   r   r*   r   r   r      s    LECr*   r   c                        e Zd ZddgZd Zd Zy)DynamicPathCalculationproject1project2c                 (   dd l }| j                  t        |j                        d       | j                  t        |j                  j                        d       dd l}| j                  t        |j                        d       | j                  t        |j                  j                        d       | j                  |j                  j                  j                  d       | j                  |j                  j                  j                  d       | j                  t              5  dd l}d d d        | j                  t        |j                        d       | j                  t        |j                  j                        d       y # 1 sw Y   ^xY w)Nr      parent child oneparent child two)parent.child.oner3   len__path__childparent.child.twor4   r5   rN   r=   r>   parent.child.threer"   parents     r   test_project3_failsz*DynamicPathCalculation.test_project3_fails   s   V__-q1V\\223Q7V__-q1V\\223Q7))..0BC))..0BC{+ 	&%	& 	V__-q1V\\223Q7		& 	&s   &FFc                    dd l }| j                  t        |j                        d       | j                  t        |j                  j                        d       dd l}| j                  t        |j                        d       | j                  t        |j                  j                        d       | j                  |j                  j                  j                  d       | j                  |j                  j                  j                  d       | j                  t              5  dd l}d d d        t        j                  j                  t        j                  j!                  | j"                  d             dd l}| j                  t        |j                        d       | j                  t        |j                  j                        d       | j                  |j                  j$                  j                  d       y # 1 sw Y   xY w)Nr   r   r   r   project3   zparent child three)r   r3   r   r   r   r   r4   r5   rN   r=   r>   r   r   r   rM   r   r   r   threer   s     r   test_project3_succeedsz-DynamicPathCalculation.test_project3_succeeds  sc   V__-q1V\\223Q7V__-q1V\\223Q7))..0BC))..0BC{+ 	&%	& 	TYY
;<! 	V__-q1V\\223Q7++002FG	& 	&s   &HHN)r$   r%   r&   r   r   r   r   r*   r   r   r      s    $E8"Hr*   r   c                   <    e Zd ZdgZej
                  d        Zd Zy)ZipWithMissingDirectoryzmissing_directory.zipc                     dd l }y r;   )r2   r6   s     r   test_missing_directoryz.ZipWithMissingDirectory.test_missing_directory#  s     	r*   c                 X    dd l }| j                  |j                  j                  d       y )Nr   zmissing_directory foo two)bar.twor3   rN   r5   )r"   bars     r   test_present_directoryz.ZipWithMissingDirectory.test_present_directory3  s    'BCr*   N)r$   r%   r&   r   unittestexpectedFailurer   r   r   r*   r   r   r      s(    $%E Dr*   r   c                       e Zd ZdgZd Zy)"ModuleAndNamespacePackageInSameDirmodule_and_namespace_packagec                 D    dd l }| j                  |j                  d       y )Nr   z	in module)a_testr3   r5   )r"   r   s     r   $test_module_before_namespace_packagezGModuleAndNamespacePackageInSameDir.test_module_before_namespace_package<  s     	k2r*   N)r$   r%   r&   r   r   r   r*   r   r   r   9  s    +,E3r*   r   c                   $    e Zd ZdgZd Zd Zd Zy)ReloadTestsr-   c                     dd l }t        j                  |      }| j                  |j                  j
                  d       y r/   )r2   rg   reloadr3   r4   r5   r6   s     r   r8   zReloadTests.test_simple_packageF  s.    s#'9:r*   c                     dd l }| j                  t              5  dd l}d d d        t	        j
                  |      }| j                  t              5  dd l}d d d        y # 1 sw Y   AxY w# 1 sw Y   y xY wr;   )r7   r=   r>   r?   rg   r   r6   s     r   r@   z"ReloadTests.test_cant_import_otherK  sf    {+ 		s#{+ 		 		 		 	s   AA+A(+A4c                 t   dd l }| j                  t              5  dd l}d d d        t        j
                  j                  t        j
                  j                  | j                  d             t        j                  |      }dd l}| j                  |j                  j                  d       y # 1 sw Y   xY w)Nr   rK   rL   )r2   r=   r>   r?   r   r   rM   r   r   r   rg   r   r3   rN   r5   r6   s     r   rO   zReloadTests.test_dynamic_pathS  s~    {+ 		 	TYY
;<s# 	'9:	 	s   B..B7N)r$   r%   r&   r   r8   r@   rO   r   r*   r   r   r   C  s    LE;
;r*   r   c                   *    e Zd ZdgZd Zd Zd Zd Zy)LoaderTestsr-   c                     dd l }| j                  |j                  |j                  j                         | j                  |j                         y r;   )r7   r3   r   __spec__loaderassertIsNotNoner6   s     r   !test_namespace_loader_consistencyz-LoaderTests.test_namespace_loader_consistencyd  s5    )<)<=S^^,r*   c                     dd l }| j                  |j                  j                         | j                  |j                         y r;   )r7   assertIsNoner   originr)   r6   s     r   !test_namespace_origin_consistencyz-LoaderTests.test_namespace_origin_consistencyj  s/    #,,--.#,,'r*   c                     dd l }t        j                  j                  | j                  dd      }| j                  |j                  d   |       y )Nr   r-   r7   )r7   r   r   r   r   r3   r   )r"   r7   expected_paths      r   test_path_indexablezLoaderTests.test_path_indexablep  s7    TYY
EBa-8r*   c                     dd l }| j                  t        |j                  t        j
                  j                               | j                  t        |j                  t        j                  j                               y r;   )	r7   rD   
isinstancer   rg   abcLoader	machineryNamespaceLoaderr6   s     r   test_loader_abczLoaderTests.test_loader_abcv  sI    
3>>9==3G3GHI
3>>93F3F3V3VWXr*   N)r$   r%   r&   r   r   r   r   r   r   r*   r   r   r   a  s    LE-(9Yr*   r   __main__)"
contextlibrg   importlib.abcimportlib.machineryr   r   ra   r   warningstest.test_importlibr   contextmanagerr   r   TestCaser   r,   rI   rQ   rZ   r]   rp   r{   r   r   r   r   r   r   r   r   r   r$   mainr   r*   r   <module>r      sg       	 
    $ - -$   L8,, LR1 R ;"6 ;&@ 4 @;!5 ;H4H HD@,@ @.	 4 		3$8 	3	&: 	
3*> 
3C( C*H1 *HZD2 D23)= 3;& ;<Y& Y6 zHMMO r*   