
    <eWL                        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 d dl	m
Z
 d dl	mZ d dl	mZ d dl	mZ d dlZd dlZ ej        d          Zd dlZej        j        Zd Z G d	 d
ej                  Z G d dej                  Z G d dej                  Z G d dej                  Z G d dej                  Zedk    r ej                     dS dS )    N)support)import_helper)	os_helper)script_helper)warnings_helperimpc                     t          j        |           }  t          j        t	          t
          dd          du d          |           S )zXDecorator to skip a test if not running under CPython or lacking
    imp.load_dynamic().load_dynamicNzimp.load_dynamic() required)r   cpython_onlyunittestskipIfgetattrr   )meths    $/usr/lib/python3.11/test/test_imp.pyrequires_load_dynamicr      sR     %%D:8?73==E8: ::>@ @ @    c                       e Zd ZdZd Zd ZdS )	LockTestsz)Very basic test of import lock functions.c                 h    |                      t                                          |d|z             d S )Nz!expected imp.lock_held() to be %r)assertEqualr   	lock_held)selfexpecteds     r   verify_lock_statezLockTests.verify_lock_state"   s=    (@8K	M 	M 	M 	M 	Mr   c                    d}t                                           }|                     |           t          |          D ]0}t                                            |                     d           1t          |          D ]}t                                            |                     |           |sB	 t                                            |                     d           d S # t          $ r Y d S w xY wd S )N2   Tz5release_lock() without lock should raise RuntimeError)r   r   r   rangeacquire_lockrelease_lockfailRuntimeError)r   LOOPSlock_held_at_startis       r   testLockzLockTests.testLock%   s    !]]__1222u 	) 	)A""4((((u 	 	A 	1222! 	,,  """ 		 + , , , , ,     	, 	,s   4C$ $
C21C2N)__name__
__module____qualname____doc__r   r%    r   r   r   r      s;        33M M M, , , , ,r   r   c                   N   e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
ed	             Zed
             Zed             Zed             Zed             Z ej        ej        d          d             Zd Zd Zej        d             Zd Zd Zd Zd Zej        d             ZdS )ImportTestsc                 ^    t          j        d          }|j        | _        |j        | _        d S )Nztest.encoded_modules)	importlibimport_moduletest_strings__path__	test_path)r   mods     r   setUpzImportTests.setUpA   s+    %&<==,r   c                     | j         D ]8\  }}}t          j        d|z             }|                     ||j                   9d S )Nztest.encoded_modules.module_)r0   r.   r/   r   test)r   modnameencodingteststrr3   s        r   test_import_encoded_modulez&ImportTests.test_import_encoded_moduleF   s_    *.*; 	0 	0&GXw) +46=+> ? ?CWch////	0 	0r   c                    | j         D ]b\  }}}t                              d|z   | j                  d         5 }|                     |j        |           d d d            n# 1 swxY w Y   ct          j                            t                    g}| 
                    t                    5  t                              d|           d d d            d S # 1 swxY w Y   d S )Nmodule_r   badsyntax_pep3120)r0   r   find_moduler2   r   r8   ospathdirname__file__assertRaisesSyntaxError)r   r3   r8   _fdr@   s         r   test_find_module_encodingz%ImportTests.test_find_module_encodingL   sJ    $ 1 	8 	8C1S$.AA!D 8  h7778 8 8 8 8 8 8 8 8 8 8 8 8 8 8 ))*{++ 	7 	7OO/666	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7s#   AA#	&A#	*CCCc                     | j         D ]\  }}}t                              d|z   | j                  \  }}}|5  |                     |d            |                     |j        |           |                     |                                d           |                     |                                d|z             d d d            n# 1 swxY w Y   t                              d          \  }}}|5  |                     |d            |                     |j        d           |                     |                                d           |                     |                                d           d d d            d S # 1 swxY w Y   d S )Nr<   r   z# test %s encoding
tokenizeutf-8z*"""Tokenization help for Python programs.
)	r0   r   r>   r2   assertNotEqualr   r8   tellreadline)r   r3   r8   rE   fpfilenameinfos          r   test_issue1267zImportTests.test_issue1267U   s#    $ 1 	- 	-C1"%//)c/26.#B #BB$ - -##B---  h777  A...  0F#+1, - - -	- - - - - - - - - - - - - - - !__Z88Hd 	L 	LD)))R['222RWWYY***R[[]]JL L L		L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	Ls%   BCC	C	4BFF
Fc                 `   d}t           j                            dd           	 t          |dz   dd          5 }|                    d           d d d            n# 1 swxY w Y   t
                              |          \  }}}|                                 |                     |j	        d	           t           j        d= t          j        |dz              t          j        |d
z              d S # t           j        d= t          j        |dz              t          j        |d
z              w xY w)Ntest_imp_helperr   ..pywzlatin-1r8   z%# coding: cp1252
u = 'test.test_imp'
cp1252.pyc)sysr@   insertopenwriter   r>   closer   r8   r   unlink)r   temp_mod_namefilerO   rP   s        r   test_issue3594zImportTests.test_issue3594h   sg   )3		5me+S9EEE F

DEEEF F F F F F F F F F F F F F F#&??=#A#A D(DJJLLLT]H555]U2333]V344444 ]U2333]V34444s/   C0 AC0 AC0 "A#AC0 0=D-c                 
   t          j                    }ddddddddddd
}t           j        dk    r|                     |d           d	}n|                    |          }|s|                     d
|z             |                    |          }d|z   }d|z   }t          j        	                    |d          }	 t           j        
                    dt          j                   t          |dz   dd          5 }|                    d           d d d            n# 1 swxY w Y   t                              |          \  }}	}
|5  |                     |           |                     |	d d                             |                     |                     |
d         d           |                     |
d         d           |                     |
d         t          j                   t                              |||	|
          }|                     |j        d           d d d            n# 1 swxY w Y   t-          j                    5  t-          j        d           t                              ||dz             }d d d            n# 1 swxY w Y   |                     |j        d           t-          j                    5  t-          j        d           t           j        s6t                              |t                              |dz                       }d d d            n# 1 swxY w Y   |                     |j        d           t          j                            |          st          j        |           t          |dd          5 }|                    d           d d d            n# 1 swxY w Y   t-          j                    5  t-          j        d           t                              ||          }d d d            n# 1 swxY w Y   |                     |j         d           t           j        d= dD ]0}tC          j"        ||z              tC          j"        ||z              1tC          j#        |           tC          j#        d           d S # t           j        d= dD ]0}tC          j"        ||z              tC          j"        ||z              1tC          j#        |           tC          j#        d           w xY w)Ns   ä            )
rJ   cp1250cp1251rX   cp1253cp1254cp1255cp1256cp1257cp1258darwinrJ   s   äz2can't run this test with %s as filesystem encodingtest_imp_helper_test_imp_helper_package_z__init__.pyr   rU   rV   rW   za = 1
   r   ignorezb = 2
)rU   rY   __pycache__)$rZ   getfilesystemencodingplatformr   getskipTestdecoder?   r@   joinr[   curdirr\   r]   r   r>   assertIsNotNone
assertTrueendswith	PY_SOURCEload_moduleawarningscatch_warningssimplefilterload_sourcedont_write_bytecodeload_compiledcache_from_sourceexistsmkdirload_packagebr   r_   rmtree)r   fs_encodingknown_localesspecial_chardecoded_charr`   test_package_nameinit_file_namera   rO   rP   r3   packageexts                 r   test_issue5604zImportTests.test_issue5604v   s    /11 "  <8##['222 (LL(,,[99L 	)MMN'( ) ) )#**;77*\96E&7GG,	, HOOAry)))me+S7CCC &t

9%%%& & & & & & & & & & & & & & &#&??=#A#A D(D + +$$T***" 6 6} E EFFF  a%000  a#...  a#-888oomT8TJJ  ***+ + + + + + + + + + + + + + + (** L L%h///oom]U5JKKL L L L L L L L L L L L L L L SUA&&&(** F F%h///. F++%--me.CDDF FCF F F F F F F F F F F F F F F SUA&&&7>>"344 ,*+++ncG<<< &

9%%%& & & & & & & & & & & & & & &(** Q Q%h///**+<>OPPQ Q Q Q Q Q Q Q Q Q Q Q Q Q Q WY***& 7 7 !4555 #!56666.///]+++++ & 7 7 !4555 #!56666.///]++++s   9?R 8DR DR !D"#R CH-!R -H11R 4H15R 3J
>R 
JR J1R AL&R &L**R -L*.A#R N3'R 3N77R :N7;R 0PR PR PR A*Tc                     t           j                            t                    }|                     t
          t          j        d|g           d S )Nr=   )r?   r@   rA   rB   rC   rD   r   r>   )r   r@   s     r   test_issue9319zImportTests.test_issue9319   sJ    wx((+/+>	H 	H 	H 	H 	Hr   c                    t          j        ddt                    5  dd l}|j        }|j        }t          j                    5  t          	                    d          }| 
                    |d         j                   t          j        dg|R  }|                     ||           |                     ||j                   |                     ||j                   d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nr?   zos.pathr   )r   CleanImportOS_PATH_NAMEr?   r@   getenvr   EnvironmentVarGuardr   r>   
addCleanupr^   r   assertIsassertIsNot)r   r?   	orig_pathorig_getenvxnew_oss         r   test_load_from_sourcez!ImportTests.test_load_from_source   sz    &tYEE 
	= 
	=IIII)K.00 = =OOD))!
+++2222b&)))i555  fm<<<= = = = = = = = = = = = = = =	
	= 
	= 
	= 
	= 
	= 
	= 
	= 
	= 
	= 
	= 
	= 
	= 
	= 
	= 
	= 
	= 
	= 
	=s6   &C?BC'C?'C+	+C?.C+	/C??DDc                     d}t                               |          }|d         }||                     |j                   t          j        |g|R  }|                     |j        |           d S )N_heapqr   )r   r>   r   r^   r   r   r&   )r   exampler   file_r3   s        r   test_issue15828_load_extensionsz+ImportTests.test_issue15828_load_extensions   sr     OOG$$!OOEK(((og****w/////r   c                 N   d}t                               |          \  }}}|                                 t                               ||          }t                               d|          }t                               d|          }|                     |j        |           |                     |j        d           |                     |j        d           |                     t                    5  t                               d|           d d d            d S # 1 swxY w Y   d S )N_testimportmultiple_testimportmultiple_foo_testimportmultiple_barnonexistent)r   r>   r^   r
   r   r&   rC   ImportError)r   mfileobjpathnamedescriptionmod0mod1mod2s           r   +test_issue16421_multiple_modules_in_one_dllz7ImportTests.test_issue16421_multiple_modules_in_one_dll   sG    "),););&;8,, 98DD 98DD***(ABBB(ABBB{++ 	6 	6]H555	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6s   1DD!Dc                 (   d}d}|                      t                    5 }t                              ||           d d d            n# 1 swxY w Y   |                     ||j        j                   |                     ||j        j                   d S )Nzbogus file path	extension)	rC   r   r   r
   assertIn	exceptionr@   r   name)r   r@   r   errs       r   "test_load_dynamic_ImportError_pathz.ImportTests.test_load_dynamic_ImportError_path  s    
 !{++ 	)sT4(((	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)dCM.///s}122222s   AAAc                    d}t                               |          }|d         |d                                          |d         d         t           j        k    r|                     d           t          j        |d g|dd          R   d S )Nr   r   rv   z/found module doesn't appear to be a C extensionrt   )r   r>   r^   C_EXTENSIONr|   r   )r   r   founds      r   'test_load_module_extension_file_is_Nonez3ImportTests.test_load_module_extension_file_is_None  s    
 %%%8!HNN8A;#/))MMKLLLd/U122Y//////r   c                    d}	 t           j        |= n# t          $ r Y nw xY w	 t          j        |          }t          j                            d          }t                              ||j	                  }| 
                    |j        |           | 
                    |j        j        |           | 
                    |j        j	        |j	                   |                     t          t           |d           | 
                    |j        d           |                     t           j        |         |           	 t           j        |= d S # t          $ r Y d S w xY w# 	 t           j        |= w # t          $ r Y w w xY wxY w)Nztest.imp_dummy_testmultiphase
dummy_namei  )rZ   modulesKeyErrorr.   r/   util	find_specr   r
   originr   r&   __spec__r   rC   AttributeErrorr   	int_constr   )r   r   modulespecs       r   3test_issue24748_load_module_skips_sys_modules_checkz?ImportTests.test_issue24748_load_module_skips_sys_modules_check  s   	D!! 	 	 	D		,T22F>++,=>>D%%dDK88FV_d333V_14888V_3T[AAAngv|LLLV-t444MM#+d+V444K%%%   K%%   sN    
DE :E	 	
EEE:E*)E:*
E74E:6E77E:z*test meaningful only when writing bytecodec                 
   t          j                    5  t           j        dz   }t          j        |           |                     t          dt          j        t           j        dg           d d d            d S # 1 swxY w Y   d S )NrU   z
^No modulerT   )	r   temp_cwdTESTFNr?   r   assertRaisesRegexr   r   r>   )r   sources     r   test_bug7732zImportTests.test_bug77322  s     !! 	: 	:%-FHV"";!1C5: : :	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	:s   AA88A<?A<c                     t                               dt           j        t          t           j        d                    }|                    t           j                   |                    t           j                   d S )Nr   rJ   rW   )r   _LoadSourceCompatibilityrB   r\   get_data)r   loaders     r   test_multiple_calls_to_get_dataz+ImportTests.test_multiple_calls_to_get_data;  s_    --eS\.23<'.R.R.RT T%%%%%%%%r   c                    dt            } t          t          j        t                              |          }t	          j        t          j        ||          5  |                     t          d          5  t          	                    |t          dz              d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Ntmpzembedded null )r&   typerZ   r   r   	swap_itemr   
ValueErrorr   r   rB   )r   r7   r3   s      r   test_load_sourcezImportTests.test_load_sourceB  s-    #"")d3;x())'22s{GS99 	: 	:''
ODD : :D999: : : : : : : : : : : : : : :	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	:s6   B<4$B$B<$B(	(B<+B(	,B<<C C c                     t          j        t          d          } G d d          }|                     t                    5   | |                       d d d            d S # 1 swxY w Y   d S )Ncreate_dynamicc                       e Zd ZdZdZdS ),ImportTests.test_issue31315.<locals>.BadSpecNfoo)r&   r'   r(   r   r   r*   r   r   BadSpecr   P  s        DFFFr   r   )r   get_attributer   rC   	TypeError)r   r   r   s      r   test_issue31315zImportTests.test_issue31315K  s     !.s4DEE	 	 	 	 	 	 	 	 y)) 	& 	&N7799%%%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&s   A##A'*A'c                     dd l }|                     |j        j        d           dd l}|                     |j        j        d           d S )Nr   frozen)_frozen_importlib_externalr   r   r   _frozen_importlib)r   r   r   s      r   test_issue_35321zImportTests.test_issue_35321V  s\    
 	*)))3<CXNNN    *3:HEEEEEr   c                     |                      t          j        dd          d           |                      t          j        dd          d           d S )N*   s   his   G$~+   s   /CC)r   _impsource_hash)r   s    r   test_source_hashzImportTests.test_source_hasha  sP    )"e446PQQQ)"e446PQQQQQr   c                     g dfddgdfddgdfddgdfg}|D ]Z\  }}|ddgz   }t          j        | }|                     |j                                                            d          |           [d S )Ndefaultz--check-hash-based-pycsalwaysneverz-cz.import _imp; print(_imp.check_hash_based_pycs)rJ   )r   assert_python_okr   outstripr}   )r   casesinterp_argsr   argsress         r   'test_pyc_invalidation_mode_from_cmdlinez3ImportTests.test_pyc_invalidation_mode_from_cmdlinee  s    O'3Y?'2H='17;	
 &+ 	H 	H!K@" D  0$7CSW]]__33G<<hGGGG	H 	Hr   c                    t          j                    5  t          dd          5 }|                    d           d d d            n# 1 swxY w Y   t	          j        ddt          j        j                   t          	                    ddg          \  }}}t          
                    d|||          }d d d            n# 1 swxY w Y   |                     |j        d	           d S )
Nzmymod.pywbs   x = 42
T)doraiseinvalidation_modemymodrT   )r@   r   )r   r   r\   r]   
py_compilecompilePycInvalidationModeCHECKED_HASHr   r>   r   r   r   )r   rN   ra   r@   r   r3   s         r   test_find_and_load_checked_pycz*ImportTests.test_find_and_load_checked_pyct  sk   !! 		D 		Dj$'' &2%%%& & & & & & & & & & & & & & &","@"M   
 '*oogSEo&J&J#D$//'4{CCC		D 		D 		D 		D 		D 		D 		D 		D 		D 		D 		D 		D 		D 		D 		D 	#####s4   CACA	CA	A'CC	Cc                      dd l t          j        t          d          } G d d          } |             fd} |             t          j        d          }                     |d            |             d S )Nr   create_builtinc                       e Zd ZdZdS )7ImportTests.test_create_builtin_subinterp.<locals>.SpecbuiltinsN)r&   r'   r(   r   r*   r   r   Specr    s        DDDr   r  c                      t          j                  } t          j                  }                    |                               t          j                  | dz              t          j                     d S )Nrt   )rZ   getrefcountr   r  r   r   gccollect)refcntr3   r  r   r   s     r   check_get_builtinszEImportTests.test_create_builtin_subinterp.<locals>.check_get_builtins  si    _X..F%d++CMM#x(((S_X66
CCCJLLLLLr   zimport builtins)r  r   r   r   run_in_subinterpr   )r   r  r  r  retr  r   s   `    @@r   test_create_builtin_subinterpz)ImportTests.test_create_builtin_subinterp  s     	 .t5EFF	 	 	 	 	 	 	 	tvv	 	 	 	 	 	 	 	&'899a   r   N) r&   r'   r(   r4   r:   rG   rQ   rb   r   r   r   r   r   r   r   r   r   r   r   rZ   r   r   r   r   r   r   r   r   r   r  r  r   r*   r   r   r,   r,   @   s       & & &
0 0 07 7 7L L L&5 5 5U, U, U,nH H H
= = =* 	0 	0 	0 6 6 6 	3 	3 	3 
0 
0 
0   , X_S,46 6: :6 6:& & &: : : & & &	F 	F 	FR R RH H H$ $ $     r   r,   c                   *    e Zd ZdZd Zd Zd Zd ZdS )ReloadTestszPVery basic tests to make sure that imp.reload() operates just like
    reload().c                     t          j                    5  dd l}t                              |           d d d            d S # 1 swxY w Y   d S )Nr   )r   r   r?   r   reload)r   r?   s     r   test_sourcezReloadTests.test_source  s     *,, 	 	IIIJJrNNN	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   A  AAc                     t          j        d          5  dd l}t                              |           d d d            d S # 1 swxY w Y   d S )Ntimer   )r   r   r'  r   r$  )r   r'  s     r   test_extensionzReloadTests.test_extension  s    &v.. 	 	KKKJJt	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	   AAAc                     t          j        d          5  dd l}t                              |           d d d            d S # 1 swxY w Y   d S )Nmarshalr   )r   r   r+  r   r$  )r   r+  s     r   test_builtinzReloadTests.test_builtin  s    &y11 	  	 NNNJJw	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	 r)  c                 "   ddl m} t          j                            d          fd}|                     |           |                     t          d          5  t          	                    |           d d d            d S # 1 swxY w Y   d S )Nr   )parserhtmlc                  &     t           j        d<   d S )Nr/  )rZ   r   )r/  s   r   cleanupz5ReloadTests.test_with_deleted_parent.<locals>.cleanup  s    "&CKr   )
r/  r.  rZ   r   popr   r   r   r   r$  )r   r.  r1  r/  s      @r   test_with_deleted_parentz$ReloadTests.test_with_deleted_parent  s    {v&&	' 	' 	' 	' 	'   ##K88 	 	JJv	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   BBBN)r&   r'   r(   r)   r%  r(  r,  r3  r*   r   r   r"  r"    sZ         
 
 
  
     
    r   r"  c                       e Zd ZdZe                                Z ej        e	j
        j        dud          d             Z ej        e	j
        j        dud          d             ZdS )PEP3147TestszTests of PEP 3147.Nz1requires sys.implementation.cache_tag not be Nonec           	         t           j                            dddd          }t           j                            ddddd                    | j                            }|                     t                              |d          |           d S )Nr   barbazqux.pyrx   
qux.{}.pycT)r?   r@   r~   formattagr   r   r   r   r@   expects      r   test_cache_from_sourcez#PEP3147Tests.test_cache_from_source  sy    
 w||E5%::eUE=*11$(;;= =..tT::FCCCCCr   z4requires sys.implementation.cache_tag to not be Nonec           	         t           j                            ddddd                    | j                            }t           j                            dddd          }|                     t                              |          |           d S )Nr   r7  r8  rx   r:  r9  )r?   r@   r~   r;  r<  r   r   source_from_cacher=  s      r   test_source_from_cachez#PEP3147Tests.test_source_from_cache  sw     w||E5%(//99; ;eUE8<<..t44f=====r   )r&   r'   r(   r)   r   get_tagr<  r   
skipUnlessrZ   implementation	cache_tagr?  rB  r*   r   r   r5  r5    s        
++--CX+5TALN ND DN ND X+5TA ! !> >! !> > >r   r5  c                   P    e Zd Z ej        ej        du d          d             ZdS )NullImporterTestsNzNeed an undecodeable filenamec                     t           j        }t          j        |           	 |                     t
          t          j        |           t          j        |           d S # t          j        |           w xY w)N)	r   TESTFN_UNENCODABLEr?   r   rC   r   r   NullImporterrmdir)r   r   s     r   test_unencodeablez#NullImporterTests.test_unencodeable  s\     +
	k3+;TBBBHTNNNNNBHTNNNNs   &A A4)r&   r'   r(   r   r   r   rJ  rM  r*   r   r   rH  rH    sK        X_Y1T946 6 6 6  r   rH  __main__)r  r.   importlib.utilr?   os.pathr  rZ   r6   r   test.supportr   r   r   r   r   r   import_deprecatedr   r   r@   r&   r   r   TestCaser   r,   r"  r5  rH  mainr*   r   r   <module>rU     s   				         				      



       & & & & & & " " " " " " & & & & & & ( ( ( ( ( (  'o'..  w@ @ @ ,  ,  ,  ,  ,!  ,  ,  ,D] ] ] ] ](# ] ] ]@# # # # #(# # # #L> > > > >8$ > > >6	 	 	 	 	) 	 	 	 zHMOOOOO r   