Your IP : 216.73.216.213


Current Path : /opt/alt/python39/lib64/python3.9/test/test_import/__pycache__/
Upload File :
Current File : //opt/alt/python39/lib64/python3.9/test/test_import/__pycache__/__init__.cpython-39.pyc

a

XC?hM��@s,ddlZddlZddlZddlZddlZddlmZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlZddlZddlZddlZddlmZddlZddlmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$ddlm%Z%ddl&m'Z'ddl(m)Z)e�*ej+d�Z,d	d
�Z-ej.d'dd
��Z/Gdd�dej0�Z1e,Gdd�dej0��Z2Gdd�dej0�Z3Gdd�dej0�Z4Gdd�dej0�Z5Gdd�dej0�Z6Gdd�dej0�Z7Gdd�dej0�Z8e!Gdd�dej0��Z9e!Gd d!�d!ej0��Z:Gd"d#�d#ej0�Z;Gd$d%�d%ej0�Z<e=d&k�r(e�>�dS)(�N)�_get_sourcefile)�mock)�TESTFN�forget�	is_jython�make_legacy_pyc�rmtree�	swap_attr�	swap_item�
temp_umask�unlink�unload�cpython_only�TESTFN_UNENCODABLE�temp_dir�
DirsOnSysPath)�
script_helper)�uncache)�
ModuleTypez*test meaningful only when writing bytecodecCs6|d|d|d|dfD]}t|�qtd�dS)N�.py�.pyc�.pyw�	$py.class�__pycache__)rr)�name�f�r�>/opt/alt/python39/lib64/python3.9/test/test_import/__init__.py�remove_files$s�
r�c
cs�|pd}t���}t�|||�}tj�|d�}zNtj�d|�||fVtj�|�W|durh|tj|<q�|tjvr�tj|=n(|dur�|tj|<n|tjvr�tj|=0Wd�n1s�0YdS)N�spamr)	rr�make_script�sys�modules�pop�path�insert�remove)r�sourceZtempdirr%Z
old_modulerrr�_ready_to_import-s 


�
r)c@s$eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Ze	d
d��Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�ZdAdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zed&d'��Zd(d)�Zd*d+�Zed,d-��Zd.d/�Ze	d0d1��Zd2d3�Zd4d5�Zd6d7�Ze	d8d9��Z d:d;�Z!e"�#e$j%d<kd=�d>d?��Z&d@S)B�ImportTestscCstt�t��dS�N)rr�	importlib�invalidate_caches��selfrrr�setUpDszImportTests.setUpcCstt�dSr+)r
rr.rrr�tearDownHszImportTests.tearDowncCs6|�t��ddl}Wd�n1s(0YdS�Nr)�assertRaises�ModuleNotFoundError�(something_that_should_not_exist_anywhere�r/r5rrr�&test_import_raises_ModuleNotFoundErrorKsz2ImportTests.test_import_raises_ModuleNotFoundErrorcCs:|�t��ddlm}Wd�n1s,0YdS)Nr)�blah)r3r4r5r8)r/r8rrr�:test_from_import_missing_module_raises_ModuleNotFoundErrorOszFImportTests.test_from_import_missing_module_raises_ModuleNotFoundErrorcCs:|�t��ddlm}Wd�n1s,0YdS)Nr)r5)r3�ImportErrorr,r5r6rrr�0test_from_import_missing_attr_raises_ImportErrorSsz<ImportTests.test_from_import_missing_attr_raises_ImportErrorcCsn|�t��}ddlm}Wd�n1s,0Y|�|jjd�|�|jjtj�|�	t
|j�d�dS)Nr��i_dont_exist�osz7cannot import name 'i_dont_exist' from 'os' \(.*os.py\))r3r:r>r=�assertEqual�	exceptionrr%�__file__�assertRegex�str�r/�cmr=rrr�/test_from_import_missing_attr_has_name_and_pathWs
*z;ImportTests.test_from_import_missing_attr_has_name_and_pathcCsvddl}|�t��}ddlm}Wd�n1s40Y|�|jjd�|�|jj|j�|�	t
|j�d�dS)Nrr<�	_testcapizCcannot import name 'i_dont_exist' from '_testcapi' \(.*\.(so|pyd)\))rGr3r:r=r?r@rr%rArBrC)r/rGrEr=rrr�2test_from_import_missing_attr_has_name_and_so_path^s*z>ImportTests.test_from_import_missing_attr_has_name_and_so_pathcCsX|�t��}ddlm}Wd�n1s,0Y|�|jjd�|�|jj�dS)Nrr<�_warning)	r3r:rIr=r?r@rZassertIsNoner%rDrrr�&test_from_import_missing_attr_has_namegs*z2ImportTests.test_from_import_missing_attr_has_namecCsZ|�t��}ddlm}Wd�n1s,0Y|�|jjddh�|�|j�dS)Nrr<�	posixpathZntpath)r3r:Zos.pathr=�assertInr@r�assertIsNotNonerDrrr�/test_from_import_missing_attr_path_is_canonicalns*z;ImportTests.test_from_import_missing_attr_path_is_canonicalc	Cs�ddl}t���\}}t|d��}|�d�Wd�n1s>0Yi}|�t|�|��d���"td|�d�|�Wd�n1s�0Y|�d|�Wd�n1s�0Yt���\}}t|d��}|�d�Wd�n1s�0Yi}|�t|�|��d	���"td|�d�|�Wd�n1�sF0Y|�d|�Wd�n1�sr0YdS)
Nr�wz__all__ = [b'invalid_type']z\.__all__ must be strzfrom z	 import *sinvalid_typez%globals()[b'invalid_type'] = object()z\.__dict__ must be str)	�rer)�open�write�assertRaisesRegex�	TypeError�escape�exec�assertNotIn)r/rPrr%r�globalsrrr�"test_from_import_star_invalid_typets&(�0*(�2z.ImportTests.test_from_import_star_invalid_typecCs6|�t��ddl}Wd�n1s(0YdSr2)r3r:�RAnDoM)r/rZrrr�test_case_sensitivity�sz!ImportTests.test_case_sensitivitycCsddlm}dS)Nr)�double_const)�testr\)r/r\rrr�test_double_const�szImportTests.test_double_constcs^�fdd�}tj�dtj�z2|d�tj�d�rDdD]}||�q6Wtjd=n
tjd=0dS)Nc
sJt|}trtd}ntd}t|d��P}td|d|d�t�d�}t�d�}td||d�td	||d�Wd�n1s~0Yttjvr�tjt=t�	�z�zt
t�}Wn6ty�}z��d
||f�WYd}~n
d}~00��
|j|d|���
|j|d|�Wtt�t|�t|�ntt�t|�t|�0dS)NrrrOz)# This tests Python's ability to import azfile.��filei�za =zb =zimport from %s failed: %sz'module loaded (%s) but contents invalid)rrrQ�print�randomZ	randranger"r#r,r-�
__import__r:�failr?�a�brr)�extr(Zpycrrerf�mod�errr.rr�test_with_extension�s@
�

,
(
�
�
�z4ImportTests.test_import.<locals>.test_with_extensionrr�win)z.PYz.Pyz.pYrz.PYWz.pYw)r"r%r&r>�curdir�platform�
startswith)r/rjrgrr.r�test_import�s"zImportTests.test_import�longlistcCsN|d}t|d��<}|�d�td�D]}|�d�q&|�d�Wd�n1sT0Yzt�|�Wt|�n
t|�0tj�d�t	�
�i}zdt|�td|d|�Wtjd	=t|d
�t|d�|�
�ztj|=Wnty�Yn0nLtjd	=t|d
�t|d�|�
�ztj|=Wnt�yFYn00dS)NrrOzd = [
i�z"",
�]rzimport ����c�o)rQrR�range�
py_compile�compilerr"r%�appendr,r-rrV�clearr#�KeyError)r/�module�filenamer�i�	namespacerrr�test_module_with_large_stack�s>
(�z(ImportTests.test_module_with_large_stackcCs�td}t|d��}td|d�Wd�n1s40Ytj�dtj�t�	�ttj
vrhtj
t=z@dD]&}|�tt
t�|�ttj
d|�qnWtjd=tt�ntjd=tt�0dS)NrrOza = 1/0r_r)���z'damaged module in sys.modules on %i try)rrQrar"r%r&r>rlr,r-r#r3�ZeroDivisionErrorrcrWr)r/r(rr}rrr�test_failing_import_sticks�s"*

�
�z&ImportTests.test_failing_import_stickscCsTddl}ddl}|�|||j�|�t|jd��ddlm}|�||j|j�dS)NrrA)r]�test.support�assertIs�__name__�
assertTrue�hasattr�support)r/�xr]�yrrr�test_import_name_binding�sz$ImportTests.test_import_name_bindingcCs|zd}Wddlmm}nddlmm}0d}|dkrXddlmm}|d7}q4td�D]}ddlmm}q`dS)N.r�
r�r�)Ztest.support.script_helperr�rru)r/r�r}rrr�test_issue31286s(
zImportTests.test_issue31286cCsHttjd}t|d��}|�d�Wd�n1s80Ytj�dtj�z�t	t�}|�
ttj�|�|j
dd�|�|jdd�tt�t|d��}|�d�Wd�n1s�0Y|�ttj|�tj�t�}|�|d	�|�|j
d
d�|�|jdd�Wtjd=tt�tt�ntjd=tt�tt�0dS)N�pyrOz
a = 1
b=2
rr�z!module has wrong attribute valuesr�za = 10
b=20//0
z$expected module to be in sys.modulesr�)rr>�extseprQrRr"r%r&rlrcrLr#r?rerfrr3r�r,�reload�getrMr
)r/r(rrhrrr�test_failing_reloads.((
�zImportTests.test_failing_reloadcCs�td}t|d��}|�d�Wd�n1s20Ytj�dtj�z�tt�}|�	|j
�d��t�|�tj
t=t|�t��tt�}tj�|j
�\}}|�|d�Wtjd=tt�ttj
vr�tj
t=n$tjd=tt�ttj
vr�tj
t=0dS)NrrOztest = None
rr)rrQrRr"r%r&r>rlrcr�rA�endswithr'r#rr,r-�splitextr?r)r/r(rrh�basergrrr�test_file_to_source>s,(


�
zImportTests.test_file_to_sourcecCs|tj�t�}t��}z|�|�Wn"tyD|�d�	|��Yn0|�
t��}t|�Wd�n1sn0YdS)Nzpath is not encodable to {})
r>r%�abspathrr"�getfilesystemencoding�encode�UnicodeEncodeError�skipTest�formatr3r:rc)r/r%�encodingrsrrr�test_import_by_filenameVsz#ImportTests.test_import_by_filenamecCs"t�dtt�d��}t�|�dS)Nrz�            import sys
            class C:
               def __del__(self):
                  import importlib
            sys.argv.insert(0, C())
            )rr!r�textwrap�dedent�assert_python_ok)r/Ztestfnrrr�!test_import_in_del_does_not_crash`sz-ImportTests.test_import_in_del_does_not_crashc
Cstj�dtj�z�td}tj�|�}t	|d��}Wd�n1sF0Yzt�
|d�Wnnty||�d�YnTt
y�}z<|jttdd�ttdd�fvr��|�d�|��WYd}~n
d}~00tt�t�|�Wtjd=tt�ntjd=tt�0dS)	NrrrO)���r�z-cannot set modification time to large integerZ	EOVERFLOWZEINVALz2cannot set modification time to large integer ({}))r"r%r&r>rlrr,�util�cache_from_sourcerQ�utime�
OverflowErrorr��OSError�errno�getattrr�rc�statr)r/r(Zcompiledr�errr�test_timestamp_overflowks,
�&
�z#ImportTests.test_timestamp_overflowcCs4ztddgd�Wnty.|�d�Yn0dS)NZhttpr8)�fromlistzfromlist must allow bogus names)rcr:rdr.rrr�test_bogus_fromlist�szImportTests.test_bogus_fromlistcCs.ddg}tj|�}|��\}}|�d|�dS)N�-cz&del __builtins__.__import__; import ossImportError)rZspawn_pythonZcommunicaterL)r/�args�popen�stdout�stderrrrr�test_delete_builtins_import�s
z'ImportTests.test_delete_builtins_importcCs<|�td��ddlm}Wd�n1s.0YdS)Nz^No module named 'bogus'r)�foo)rSr:�bogusr�)r/r�rrr�/test_from_import_message_for_nonexistent_module�sz;ImportTests.test_from_import_message_for_nonexistent_modulecCs<|�td��ddlm}Wd�n1s.0YdS)Nz^cannot import name 'bogus'r)r�)rSr:rPr�)r/r�rrr�,test_from_import_message_for_existing_module�sz8ImportTests.test_from_import_message_for_existing_modulecCsvGdd�d�}d}|�t|�|�tj|<|�t��}ddlm}Wd�n1sV0Y|�t	|j
�d�dS)Nc@seZdZdd�ZdS)zIImportTests.test_from_import_AttributeError.<locals>.AlwaysAttributeErrorcSst�dSr+)�AttributeError)r/�_rrr�__getattr__�szUImportTests.test_from_import_AttributeError.<locals>.AlwaysAttributeError.__getattr__N)r��
__module__�__qualname__r�rrrr�AlwaysAttributeError�sr��test_from_import_AttributeErrorr��does_not_existzScannot import name 'does_not_exist' from '<unknown module name>' (unknown location))�
addCleanupr
r"r#r3r:r�r�r?rCr@)r/r��module_namerEr�rrrr��s*�z+ImportTests.test_from_import_AttributeErrorc	Cs�ttdd��v|�t��ddlm}Wd�n1s:0Y|�t��tjWd�n1sj0YWd�n1s�0YdS)Nr�rr�)r	r>r3r:r�r�)r/r�rrr�test_issue31492�s
*zImportTests.test_issue31492cs
dd��tj�dtj�tj�t�d��z�d����fdd��td�D]�}t�	���fdd	�td
�D�}zZt
j�|�j
��t�d�Wd�n1s�0YWtj�dd�tj�dd�ntj�dd�tj�dd�0�durF��qFWtjd=n
tjd=0dS)
NcSs"|dkr|jjdkrt�d�dS)NZcall�has_deadlockg�������?)�f_code�co_name�time�sleep)�frame�event�argrrr�delay_has_deadlock�sz8ImportTests.test_concurrency.<locals>.delay_has_deadlockr�datac
sVt������zddl}Wn(tyF}z|�WYd}~n
d}~00t�d�dSr2)r"�settrace�wait�package�
BaseException)r�r�)r�r��excrr�run�s
z)ImportTests.test_concurrency.<locals>.runr�csg|]}tj�d��qS))�target)�	threadingZThread)�.0r�)r�rr�
<listcomp>��z0ImportTests.test_concurrency.<locals>.<listcomp>r�r�zpackage.submodule)r"r%r&r>�join�dirnamerArur�ZEventr]r�Z
start_threads�setr�r�r#r$)r/r}Zthreadsr)r�r�r�r�r�test_concurrency�s$ 
*�zImportTests.test_concurrency�win32�Windows-specificc
Cs�ddlm}|tj�}tj�d�j}tj	�
tj	�|�d�d|vrDdnd��}t
j����.}tj	�
|d�}t�|�tj	�
|tj	�tj��}t�tj|�t�||�t�tj	�
t�tj�d��D]}t�||�q�t�||�d}	d	d
�tjD�}	|dtj	�tj�|	d<tj|d
d�
dd�tj	�|��ddg�gtj|	tj	�|�d�t�||�tj|d
dgtj|	tj	�|�d�Wd�n1�s�0YdS)Nr)�GetModuleFileNameZ_sqlite3z
sqlite3{}.dllZ_drZDLLszvcruntime*.dllcSsi|]}|��tj|�qSr)�upperr>�environ)r��krrr�
<dictcomp>�r�z:ImportTests.test_dll_dependency_import.<locals>.<dictcomp>�;Z
PYTHONPATHz-Scz	import oszp = os.add_dll_directory({!r})zimport _sqlite3zp.close)r��env�cwd)Z_winapir�r"Z	dllhandler,r��	find_spec�originr>r%r�r�r�r]r�r�mkdir�basename�
executable�shutil�copy�globrU�prefixr�rA�
subprocessZ
check_callZSTDOUT)
r/r�ZdllnameZpydnameZdepname�tmpZtmp2Zpyexerr�rrr�test_dll_dependency_import�sN

�
 
���
�

�z&ImportTests.test_dll_dependency_importN)rp)'r�r�r�r0r1r7r9r;rFrrHrJrNrYr[r^rorr�r�r�r��skip_if_dont_write_bytecoder�r�r�r�r�r�r�r�r�r�r��unittest�
skipUnlessr"rmr�rrrrr*BsH
,
(&




"r*c@sbeZdZe�ejdkd�dd��Ze�ejdkd�dd��Ze�ejdkd�dd��Z	d	d
�Z
dS)�FilePermissionTests�posix�%test meaningful only on posix systemsc	Cs�d}t|��nt��H\}}tj�|�}t|�}tj�|�sD|�	d�t�
|�}Wd�n1sb0YWd�n1s�0Y|�tt
�
|j��td|@��dS)N��4__import__ did not result in creation of a .pyc filei�)rr)r,r�r�rcr>r%�existsrdr�r?�oct�S_IMODE�st_mode)r/�maskrr%�cached_pathr{�	stat_inforrr�test_creation_mode
s
F�z&FilePermissionTests.test_creation_modec	Cs�d}td��zt��T\}}tj�|�}t�||�t|�tj�	|�sP|�
d�t�|�}Wd�n1sn0YWd�n1s�0Y|�t
t�|j��t
|��dS)N�r�r��rr)r,r�r�r>�chmodrcr%r�rdr�r?rrr)r/�moderr%rrrrr�test_cached_mode_issue_2051s
Fz/FilePermissionTests.test_cached_mode_issue_2051c	Cs�d}td��zt��T\}}tj�|�}t�||�t|�tj�	|�sP|�
d�t�|�}Wd�n1sn0YWd�n1s�0Y|dB}|�t
t�|j��t
|��dS)N�r�r��r)r/r
rr%rrZexpectedrrr�test_cached_readonly+s
Fz(FilePermissionTests.test_cached_readonlyc	CsZt���<\}}t|d��}|�d�Wd�n1s80Yt�|�}t�||j|jdf�t�|d�t	|�}|�
|jd�t�|d�t|d��}|�d�Wd�n1s�0Yt|�t
��t	|�}|�
|jd�t|�t|�t
��|d	}t�t
j�|�|�t	|�}|�
|jd�Wd�n1�sL0YdS)
NrOzx = 'original'
i�rZoriginalrzx = 'rewritten'
Z	rewrittenrs)r)rQrRr>r�r��st_atime�st_mtimer	rcr?r�r
r,r-r�renamer�r�)r/rr%r�s�mZ
bytecode_onlyrrr�test_pyc_always_writable;s,(
(z,FilePermissionTests.test_pyc_always_writableN)r�r�r�r�r�r>rrrrrrrrrr�s�
�
�
r�c@s|eZdZdZdZej�e�Z	ej�
e	e�ejdZe
j�e�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Zdd�ZdS)�PycRewritingTestsZunlikely_module_namez�
import sys
code_filename = sys._getframe().f_code.co_filename
module_filename = __file__
constant = 1
def func():
    pass
func_filename = func.__code__.co_filename
r�cCs�tjdd�|_tj�|jd�|_t�|j	�t
|jd��}|�|j
�Wd�n1s\0Ytj�d|j	�t��dS)NrOr)r"r%�sys_pathr#r$r��orig_moduler>r��dir_namerQ�	file_namerR�
module_sourcer&r,r-)r/rrrrr0ms*zPycRewritingTests.setUpcCsV|jtjdd�<|jdur*|jtj|j<n
t|j�t|j�t|j	�t
|j�dSr+)rr"r%rr#r�r
rr�
compiled_namerrr.rrrr1vs



zPycRewritingTests.tearDowncCs t�}t|j||�tj|jSr+)rXrcr�r"r#)r/�nsrrr�
import_module�szPycRewritingTests.import_modulecCs~|��}|�|j|j�|�|j|j�|�|j|j�tj|j=|��}|�|j|j�|�|j|j�|�|j|j�dSr+)	rr?�module_filenamer�
code_filename�
func_filenamer"r#r��r/rhrrr�test_basics�s
zPycRewritingTests.test_basicscCsLtj|jdd�|��}|�|j|j�|�|j|j�|�|j|j�dS�Nzanother_module.py)�dfile)rvrwrrr?rrr r!rrr�test_incorrect_code_name�s
z*PycRewritingTests.test_incorrect_code_namecCshd}tj|j|d�t�|j�t|j�}t��|��}|�	|j
|�|�	|j|�|�	|j|�dSr#)
rvrwrr>r'rr,r-rr?rrr )r/r��pyc_filerhrrr�test_module_without_source�s
z,PycRewritingTests.test_module_without_sourcecCs�t�|j�t|jd��$}|�d�}t�|�}Wd�n1sB0Yt|j	�}t
jj}|�
d�}|||<|jt|�d�}t|jd��&}|�|�t�||�Wd�n1s�0Y|��}|�|jj|j�dS)N�rb�r�)�	co_consts�wb)rvrwrrQr�read�marshal�load�listr*r,r�__code__�index�replace�tuplerR�dumpr?Zconstant�co_filename)r/r�header�codeZ	constantsZforeign_code�posrhrrr�test_foreign_code�s
(


*z#PycRewritingTests.test_foreign_codeN)r�r�r�r�rr>r%r�rrr�r�rr,r�r�rr0r1rr"r%r'r9rrrrrZs		
rc@sFeZdZdZeZdd�Zdd�Zdd�Ze	�
ejdkd	�d
d��Z
dS)
�
PathsTests)r]utestäöüßutestéèu
test°³²cCs t�|j�tjdd�|_dSr+)r>r�r%r"�syspathr.rrrr0�szPathsTests.setUpcCst|j�|jtjdd�<dSr+)rr%r;r"r.rrrr1�s
zPathsTests.tearDowncCstttj�|jd�d��}|�d�Wd�n1s60Ytj�|jd�td�}|�|j	d�t
d�dS)Nztest_trailing_slash.pyrOz testdata = 'test_trailing_slash'�/�test_trailing_slash)rQr>r%r�rRr"rxrcr?�testdatar
)r/rrhrrrr=�s(zPathsTests.test_trailing_slashr�r�c	
Csjttj�|jd�d��}|�d�Wd�n1s60Yt��tj�|j�}ddl}|�	�}|d}d||f}||dd�7}zt�
|�WnLty�}z4|jtj
tjtjfvr�|�d|f��WYd}~n
d}~00tj�d|�ztd�}Wn8t�y8}z|�d	||f�WYd}~n
d}~00|�|jd�|�|j�|�|j�td�dS)
Nztest_unc_path.pyrOztestdata = 'test_unc_path'rz\\%s\%s$r�z%cannot access administrative share %rZ
test_unc_pathz,could not import 'test_unc_path' from %r: %r)rQr>r%r�rRr,r-r��socketZgethostname�listdirr�r�ZEPERMZEACCESZENOENTr�r"r&rcr:rdr?r>r�rArnr
)	r/rr%r?ZhnZdriveZuncr�rhrrr�
test_UNC_path�s2(�zPathsTests.test_UNC_pathN)r�r�r�ZSAMPLESrr%r0r1r=r�r�r"rmrArrrrr:�s	r:c@sHeZdZdd�ZeZdd�Zdd�Zdd�Zd	d
�Zdd�Z	d
d�Z
dS)�RelativeImportTestscCstd�dS)Nztest.relimport)r
r.rrrr1�szRelativeImportTests.tearDowncCs ddlm}|�t|d��dS)Nr�)�	relimportrB)rrCr�r�)r/rCrrr�test_relimport_star�sz'RelativeImportTests.test_relimport_starcs|�fdd�}tddd��|�tddd��|�tddd��|�t|�tddd��|�t|�tt�d��|�t|�dS)	Ncstd��dS)Nzfrom . import relimport)rVr�rrr�check_relative�sz:RelativeImportTests.test_issue3221.<locals>.check_relativer]ztest.notarealmodule)�__package__r�znotarealpkg.notarealmoduler�)rG)�dictr3r4�objectrT)r/rFrrEr�test_issue3221�sz"RelativeImportTests.test_issue3221cCst�dddd�dS)Nz-W�ignorer�zfoo = 1; from . import foo)rZassert_python_failurer.rrr�)test_parentless_import_shadowed_by_globals
�z=RelativeImportTests.test_parentless_import_shadowed_by_globalcCsD|�t��&ddlm}|�d�Wd�n1s60YdS)Nr�)�sepz>explicit relative import triggered an implicit absolute import)r3r:r>rMrd)r/rMrrr�#test_absolute_import_without_futuresz7RelativeImportTests.test_absolute_import_without_futurec
Cs�tj�tj�t�dd�}tdd���t|��^|�t��ddl	}Wd�n1sV0Y|�
dtj�|�
dtj�Wd�n1s�0YWd�n1s�0YdS)Nr��package2�
submodule1�
submodule2r)
r>r%r�r�rArrr3r:rPrWr"r#)r/r%rPrrr�test_import_from_non_packages&z0RelativeImportTests.test_import_from_non_packagec	Csztddd��Zttj�tj�t�d��� ddl}|jj	Wd�n1sN0YWd�n1sl0YdS)NrOzpackage2.submodule1zpackage2.submodule2r�r)
rrr>r%r�r�rAZpackage2.submodule1rPrQ)r/rOrrr�!test_import_from_unloaded_package&s
�z5RelativeImportTests.test_import_from_unloaded_packageN)r�r�r�r1r0rDrJrLrNrRrSrrrrrB�s	rBc@seZdZdd�ZdS)�OverridingImportBuiltinTestscCs�ddl}dd�}|�|�|�ttddd���|�|�d�Wd�n1sR0Ytt�ddd���|�|�|�Wd�n1s�0YdS)	NrcSsddl}|Sr2�r>rUrrrr�2sz?OverridingImportBuiltinTests.test_override_builtin.<locals>.foorccWsdS�N�r�r�rrr�<lambda>7r�zDOverridingImportBuiltinTests.test_override_builtin.<locals>.<lambda>rWcWsdSrVrrXrrrrY=r�)r>r?r	�builtinsr
rX)r/r>r�rrr�test_override_builtin.s,z2OverridingImportBuiltinTests.test_override_builtinN)r�r�r�r[rrrrrT-srTc@s�eZdZdd�Zdd�Zdd�Zedd��Ze�	e
jd	kd
�e�e
e
d�oTe
��dkd
�edd����Zedd��Zedd��Zdd�Zedd��Zedd��Zdd�Zdd�ZdS)�PycacheTestscCstt�td�t|j�dS)Nr)rrrrr(r.rrr�_cleanDszPycacheTests._cleancCsftd|_|��t|jd��}td|d�Wd�n1s@0Ytj�dtj	�t
��dS)NrrOz/# This is a test file written by test_import.pyr_r)rr(r]rQrar"r%r&r>rlr,r-)r/�fprrrr0Is
*zPycacheTests.setUpcCs,tjdtjksJd��tjd=|��dS)NrzUnexpected sys.path[0])r"r%r>rlr]r.rrrr1QszPycacheTests.tearDowncCsZ|�tj�d��tt�|�tj�d��tj�	|j
�}|�tj�|�d�|t��dS)Nrz*bytecode file {!r} for {!r} does not exist)�assertFalser>r%r�rcrr�r,r�r�r(r��r/Zpyc_pathrrr�test_import_pyc_pathVs��z!PycacheTests.test_import_pyc_pathr�r��geteuidrz=due to varying filesystem permission semantics (issue #11956)cCsptd��tt�Wd�n1s&0Y|�tj�d��tj�	|j
�}|�tj�|�d�|t��dS)N�rz"bytecode file {!r} for {!r} exists)
rrcrr�r>r%r�r,r�r�r(r_r�r`rrr�test_unwritable_directory`s
&��z&PycacheTests.test_unwritable_directorycCsVtt�tj�|j�}|�tj�	|��t�
|j�tt�t��|�
ttt�dSr+)rcrr,r�r�r(r�r>r%r�r'rr-r3r:)r/r&rrr�test_missing_sourcepsz PycacheTests.test_missing_sourcec
Cs~tt�t|j�}t�|j�tt�t��tt�}z6|�	|j
tj�t�
�tjtj�|���Wt�|�nt�|�0dSr+)rcrrr(r>r'r
r,r-r?rAr%r��getcwdrl�relpath�r/r&rrrr�test_missing_source_legacy|s
�z'PycacheTests.test_missing_source_legacycCs<tt�}tj�td�}|�|jtj�	t�
�tj|��dS)Nr)rcrr,r�r�r?�
__cached__r>r%r�rfrl)r/rr&rrr�test___cached__�szPycacheTests.test___cached__c
Csbtt�t|j�}t�|j�tt�t��tt�}|�	|j
tj�t�
�tjtj�|���dSr+)rcrrr(r>r'r
r,r-r?rjr%r�rfrlrgrhrrr�test___cached___legacy_pyc�s
�z'PycacheTests.test___cached___legacy_pyccCs
dd�}t�d�|�|�ttj�dd�d��Wd�n1sF0Yttj�dd�d��Wd�n1sz0Yt��td�}tj	�
tj�dd��}|�|jtj�t�
�tj|��tj	�
tj�dd��}|�tjdjtj�t�
�tj|��dS)NcSstd�td�td�dS�N�pep3147�pep3147.foo�rr
rrrr�cleanup�sz5PycacheTests.test_package___cached__.<locals>.cleanuprn�__init__.pyrO�foo.pyro)r>r�r�rQr%r�r,r-rcr�r�r?rjrfrlr"r#�r/rqrZinit_pycZfoo_pycrrr�test_package___cached__�s"

� �z$PycacheTests.test_package___cached__cCs*dd�}t�d�|�|�ttj�dd�d��Wd�n1sF0Yttj�dd�d��Wd�n1sz0Yt��td�}t	d�t	d�t��td�}tj
�tj�dd��}|�|j
tj�t��tj|��tj
�tj�dd��}|�tjdj
tj�t��tj|��dS)NcSstd�td�td�dSrmrprrrrrq�sz>PycacheTests.test_package___cached___from_pyc.<locals>.cleanuprnrrrOrsro)r>r�r�rQr%r�r,r-rcr
r�r�r?rjrfrlr"r#rtrrr� test_package___cached___from_pyc�s*

� �z-PycacheTests.test_package___cached___from_pyccCsbtt�tt�t|jd��}td|d�Wd�n1s>0Ytt�}|�|jd�dS)Nrezx = 5r_rW)rcrr
rQr(rar?r�)r/r^rrrr�test_recompute_pyc_same_second�s*z+PycacheTests.test_recompute_pyc_same_secondN)r�r�r�r]r0r1r�rar�r�r>r�skipIfr�rbrdrerirkrlrurvrwrrrrr\As0
	��



r\c@sXeZdZdZedZdd�Zdd�Ze�e	e
d�p>e
��dkd	�ej
jd
d���ZdS)
�TestSymbolicallyLinkedPackageZsamplez-taggedcCs�tj�|j�tj�|j�tjdd�|_t�	|j�|�
tjj|j�tj�|jd�}tj�|�tj�
|�svJ�tj|j|jdd�|�
tjj|j�t��|�tj�|j�d�tj�tj�|jd��s�J�dS)NrrT)Ztarget_is_directory)r]r�r�tagged�package_namer"r%�
orig_sys_pathr>r�r�r�Zcreate_empty_filer��symlinkrr,r-r?�isdir�isfile)r/Z	init_filerrrr0�sz#TestSymbolicallyLinkedPackage.setUpcCs|jtjdd�<dSr+)r|r"r%r.rrrr1�sz&TestSymbolicallyLinkedPackage.tearDown�getwindowsversion)�rzWindows Vista or later requiredcCsNdgtjdd�<tj�|j�s"J�tj�tj�|jd��s>J�t�|j�dS)N�.rr)r"r%r>r�r{r�r,rr.rrr�test_symlinked_dir_importablesz;TestSymbolicallyLinkedPackage.test_symlinked_dir_importableN)r�r�r�r{rzr0r1r�r�r�r"r�r]r�Zskip_unless_symlinkr�rrrrry�s
�ryc@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�ImportlibBootstrapTestscCstjd}|�|�dS)N�_frozen_importlib)r"r#r�r!rrr�test_frozen_importlibs
z-ImportlibBootstrapTests.test_frozen_importlibcCsXddlm}tjd}|�||�|�|jd�|�|jd�|�|j	�
d�|j	�dS)Nr)�
_bootstrapr�zimportlib._bootstrapr,z
_bootstrap.py)r,r�r"r#r�r?r�rGr�rAr�)r/r�rhrrr�"test_frozen_importlib_is_bootstraps
z:ImportlibBootstrapTests.test_frozen_importlib_is_bootstrapcCsXddlm}tjd}|�||�|�|jd�|�|jd�|�|j	�
d�|j	�dS)Nr)�_bootstrap_external�_frozen_importlib_externalzimportlib._bootstrap_externalr,z_bootstrap_external.py)r,r�r"r#r�r?r�rGr�rAr�)r/r�rhrrr�4test_frozen_importlib_external_is_bootstrap_external s
zLImportlibBootstrapTests.test_frozen_importlib_external_is_bootstrap_externalcCs*ddlm}tjd}|�|j|j�dS)Nr)�	machineryr�)r,r�r"r#r��
ModuleSpec)r/r�rhrrr�test_there_can_be_only_one(s
z2ImportlibBootstrapTests.test_there_can_be_only_oneN)r�r�r�r�r�r�r�rrrrr�sr�c@s(eZdZdZdd�Zdd�Zdd�ZdS)	�GetSourcefileTestsz�Test importlib._bootstrap_external._get_sourcefile() as used by the C API.

    Because of the peculiarities of the need of this function, the tests are
    knowingly whitebox tests.

    cCsTt�d��6}d|_td}td}|�t|�|�Wd�n1sF0YdS)N�*importlib._bootstrap_external._path_isfileTrr�rZpatchZreturn_valuerr?r)r/�_path_isfiler%Zexpectrrr�test_get_sourcefile;s
z&GetSourcefileTests.test_get_sourcefilecCsLt�d��.}d|_td}|�t|�|�Wd�n1s>0YdS)Nr�Frr�)r/r�r%rrr�test_get_sourcefile_no_sourceDsz0GetSourcefileTests.test_get_sourcefile_no_sourcecCstd}|�t|�|�dS)Nz.bad_ext)rr?r)r/r%rrr�test_get_sourcefile_bad_extLsz.GetSourcefileTests.test_get_sourcefile_bad_extN)r�r�r��__doc__r�r�r�rrrrr�1s	r�c@s�eZdZdd�Zdd�Zd$dd�Zdd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zedd��Ze�ed �d!d"��Zd#S)%�ImportTracebackTestscCs,t�t�tjdd�|_tj�dt�dSr2)r>r�rr"r%�old_pathr&r.rrrr0Us
zImportTracebackTests.setUpcCs|jtjdd�<tt�dSr+)r�r"r%rrr.rrrr1ZszImportTracebackTests.tearDownrcCs^tj�t||�}t|d��}|�|�Wd�n1s<0Y|�t|�t�	�|S)NrO)
r>r%r�rrQrRr�r
r,r-)r/rh�contentsrgZfnamerrrr�
create_module^s(z"ImportTracebackTests.create_modulecCsrg}|r8|jj}|j}|r&||dkr0|�|�|j}q|�t|�t|�|�t||�D]\}}|�||�qXdS)Nrr)	�tb_framer�r5rx�tb_nextr?�len�ziprL)r/�tb�filesZ
deduped_filesr7�fnZpatrrr�assert_tracebackfs
z%ImportTracebackTests.assert_tracebackc
CsTzddl}Wn*ty6}z|j}WYd}~nd}~00|�d�|�|tg�dS)Nr�#ImportError should have been raised)�nonexistent_xyzzyr:�
__traceback__rdr�rA)r/r�r�r�rrr�test_nonexistent_modulers
z,ImportTracebackTests.test_nonexistent_modulec
Csb|�dd�zddl}Wn*tyB}z|j}WYd}~nd}~00|�d�|�|tdg�dS)Nr�zimport nonexistent_xyzzyrr�rs)r�r�r:r�rdr�rA�r/r�r�r�rrr�test_nonexistent_module_nested|s
z3ImportTracebackTests.test_nonexistent_module_nestedc
Csb|�dd�zddl}Wn*tyB}z|j}WYd}~nd}~00|�d�|�|tdg�dS)Nr��1/0r�)ZeroDivisionError should have been raisedrs�r�r�r�r�rdr�rAr�rrr�test_exec_failure�s
z&ImportTracebackTests.test_exec_failurec
Csp|�dd�|�dd�zddl}Wn*tyN}z|j}WYd}~nd}~00|�d�|�|tddg�dS)	Nr�z
import bar�barr�rr�rs�bar.pyr�r�rrr�test_exec_failure_nested�s
z-ImportTracebackTests.test_exec_failure_nestedc
Cs`|�dd�zddl}Wn*tyB}z|j}WYd}~nd}~00|�d�|�|tg�dS)Nr�zinvalid syntax is invalidrz#SyntaxError should have been raised)r�r��SyntaxErrorr�rdr�rAr�rrr�test_syntax_error�s
z&ImportTracebackTests.test_syntax_errorcCs�d}|�t|�tj�t|�}t�|�tj�|d�}t|d��}|�|�Wd�n1s`0Ytj�|d�}t|d��}|�|�Wd�n1s�0Yt	�
�||fS)N�_parent_foorrrOr�)r�r
r>r%r�rr�rQrRr,r-)r/�parent�childZpkg_nameZpkg_path�	init_pathr�bar_pathrrr�_setup_broken_package�s
((z*ImportTracebackTests._setup_broken_packagec
Csf|�dd�\}}zddl}Wn*tyF}z|j}WYd}~nd}~00|�d�|�|t|g�dS)Nrr�rr��r�Z_parent_foo.barr�r�rdr�rA�r/r�r�r�r�r�rrr�test_broken_submodule�s
z*ImportTracebackTests.test_broken_submodulec
Csj|�dd�\}}zddlm}Wn*tyJ}z|j}WYd}~nd}~00|�d�|�|t|g�dS)Nrr�r�r�r��r�r�r�r�r�rdr�rA�r/r�r�r�r�r�rrr�test_broken_from�s
z%ImportTracebackTests.test_broken_fromc
Csf|�dd�\}}zddl}Wn*tyF}z|j}WYd}~nd}~00|�d�|�|t|g�dS)Nr�rrr�r�r�rrr�test_broken_parent�s
z'ImportTracebackTests.test_broken_parentc
Csj|�dd�\}}zddlm}Wn*tyJ}z|j}WYd}~nd}~00|�d�|�|t|g�dS)Nr�rrr�r�r�r�rrr�test_broken_parent_from�s
z,ImportTracebackTests.test_broken_parent_fromc
Cs�|�dd�tjd}dt|j�vr.|jj}nd}z�dd�}||j_zddl}Wn*tyz}z|j}WYd}~nd}~00|�	d�|�
|td	tg�W|dur�|j`q�||j_n|dur�|j`n||j_0dS)
Nr�rr��load_modulecWsdddS)Nr�rr)r�rrr�exec_module�sz9ImportTracebackTests.test_import_bug.<locals>.exec_modulerr�z<frozen importlib)r�r"r#�vars�SourceLoaderr�r�r�r�rdr�rA)r/r,Zold_exec_moduler�r�r�r�rrr�test_import_bug�s(



�z$ImportTracebackTests.test_import_bugzneed TESTFN_UNENCODABLEcCs>t�dtd�}|�t|�|dd�}tjdd|dd�dS)Nr�pass���r�zmod = __import__(%a)F)Z
__isolated)rr!rr�rr�)r/Zpynamerrrr�test_unencodable_filename�s�z.ImportTracebackTests.test_unencodable_filenameN)r)r�r�r�r0r1r�r�r�r�r�r�r�r�r�r�r�r�rr�r�r�rr�rrrrr�Ss$










r�c@s`eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dS)�CircularImportTestszQSee the docstrings of the modules being imported for the purpose of the
    test.cCs*ttj���D]}|�d�rtj|=qdS)zSMake sure no modules pre-exist in sys.modules which are being used to
        test.z&test.test_import.data.circular_importsN)r/r"r#�keysrn)r/�keyrrrr1s
zCircularImportTests.tearDowncCs.zddl}Wnty(|�d�Yn0dS)Nrz/circular import through relative imports failed)Z,test.test_import.data.circular_imports.basicr:rd�r/r]rrr�test_directszCircularImportTests.test_directcCs.zddl}Wnty(|�d�Yn0dS)Nrz@relative import in module contributing to circular import failed)Z/test.test_import.data.circular_imports.indirectr:rdr�rrr�
test_indirectsz!CircularImportTests.test_indirectcCs.zddl}Wnty(|�d�Yn0dS)Nrz-circular import involving a subpackage failed)Z1test.test_import.data.circular_imports.subpackager:rdr�rrr�test_subpackagesz#CircularImportTests.test_subpackagecCs`z"ddlmmmm}Wnty>|�d�Yn0ddlm}|�	|j|j�dS)Nrz9circular import with rebinding of module attribute failed)r�)
Z0test.test_import.data.circular_imports.rebindingror�Zcircular_imports�	rebindingr:rdZ-test.test_import.data.circular_imports.subpkgr�r�)r/r�r�rrr�test_rebinding"s"z"CircularImportTests.test_rebindingcCs.zddl}Wnty(|�d�Yn0dS)Nrz9circular import with binding a submodule to a name failed)Z.test.test_import.data.circular_imports.bindingr:rdr�rrr�test_binding*sz CircularImportTests.test_bindingcCsddl}ddl}dSr2)Z*test.test_import.data.circular_imports.use�-test.test_import.data.circular_imports.sourcer�rrr�test_crossreference10sz(CircularImportTests.test_crossreference1cCsp|�t��}ddl}Wd�n1s(0Yt|j�}|�d|�|�d|�|�d|�|�d|�dS)Nrz-test.test_import.data.circular_imports.sourcer zpartially initialized modulezcircular import)r3r�r�rCr@rL)r/rEr]�errmsgrrr�test_crossreference24s&
z(CircularImportTests.test_crossreference2cCsH|�t��}ddl}Wd�n1s(0Y|�dt|j��dS)Nrz�cannot import name 'b' from partially initialized module 'test.test_import.data.circular_imports.from_cycle1' (most likely due to a circular import))r3r:Z2test.test_import.data.circular_imports.from_cycle1rLrCr@)r/rEr]rrr�test_circular_from_import=s&�z-CircularImportTests.test_circular_from_importcCs�|�td�|�td�ddlmmm}|�t��ddlm}Wd�n1s\0Y|�	t
|�t�|�t
|�t�|�
t��d|_Wd�n1s�0YdS)Nz test.test_import.data.unwritablez"test.test_import.data.unwritable.xrrX�*)r�r
Z test.test_import.data.unwritableror��
unwritableZassertWarns�
ImportWarningr�ZassertNotEqual�typerr?r3r�)r/r�r�rrr�test_unwritable_moduleGs*z*CircularImportTests.test_unwritable_moduleN)r�r�r�r�r1r�r�r�r�r�r�r�r�r�rrrrr�s	
r��__main__)Nr)?rZ�
contextlibr�r��importlib.utilr,Zimportlib._bootstrap_externalrr-r>rvrbr�r�r�r"r�r�r�r�rr�r]rrrrrr	r
rrr
rrrrrZtest.test_importlib.utilr�typesrrx�dont_write_bytecoder�r�contextmanagerr)ZTestCaser*r�rr:rBrTr\ryr�r�r�r�r��mainrrrr�<module>sh@�	GSY5E". !1R