HEX
Server: LiteSpeed
System: Linux server.nevid-deploma.com 4.18.0-553.111.1.lve.el8.x86_64 #1 SMP Fri Mar 13 13:42:17 UTC 2026 x86_64
User: smilepac (1037)
PHP: 8.1.34
Disabled: NONE
Upload Files
File: //opt/cppython/lib/python3.8/test/test_import/__pycache__/__init__.cpython-38.pyc
U

/D�g���@s(ddlZddlZddlmZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlmZddlZddlZddlZddlZddlZddlmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%ddlm&Z&ddl'm(Z(e�)ej*d�Z+dd�Z,ej-d%d
d��Z.Gdd
�d
ej/�Z0e+Gdd�dej/��Z1Gdd�dej/�Z2Gdd�dej/�Z3Gdd�dej/�Z4Gdd�dej/�Z5Gdd�dej/�Z6Gdd�dej/�Z7e"Gdd�dej/��Z8e"Gdd�dej/��Z9Gd d!�d!ej/�Z:Gd"d#�d#ej/�Z;e<d$k�r$e�=�dS)&�N)�_get_sourcefile)�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)�uncachez*test meaningful only when writing bytecodecCs6|d|d|d|dfD]}t|�qtd�dS)N�.py�.pyc�.pyw�	$py.class�__pycache__)rr)�name�f�r�8/opt/cppython/lib/python3.8/test/test_import/__init__.py�remove_files%s�
r�c
cs�|pd}t��t}t�|||�}tj�|d�}z(tj�d|�||fVtj�|�W5|dk	rj|tj|<n|tjkr|tj|=XW5QRXdS)N�spamr)	rr�make_script�sys�modules�pop�path�insert�remove)r�source�tempdirr#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�setUpEszImportTests.setUpcCstt�dSr*)rrr-rrr�tearDownIszImportTests.tearDownc	Cs"|�t��ddl}W5QRXdS�Nr)�assertRaises�ModuleNotFoundError�(something_that_should_not_exist_anywhere�r.r4rrr�&test_import_raises_ModuleNotFoundErrorLsz2ImportTests.test_import_raises_ModuleNotFoundErrorc	Cs&|�t��ddlm}W5QRXdS)Nr)�blah)r2r3r4r7)r.r7rrr�:test_from_import_missing_module_raises_ModuleNotFoundErrorPszFImportTests.test_from_import_missing_module_raises_ModuleNotFoundErrorc	Cs&|�t��ddlm}W5QRXdS)Nr)r4)r2�ImportErrorr+r4r5rrr�0test_from_import_missing_attr_raises_ImportErrorTsz<ImportTests.test_from_import_missing_attr_raises_ImportErrorc	CsZ|�t��}ddlm}W5QRX|�|jjd�|�|jjtj�|�	t
|j�d�dS)Nr��i_dont_exist�osz7cannot import name 'i_dont_exist' from 'os' \(.*os.py\))r2r9r=r<�assertEqual�	exceptionrr#�__file__�assertRegex�str�r.�cmr<rrr�/test_from_import_missing_attr_has_name_and_pathXs
z;ImportTests.test_from_import_missing_attr_has_name_and_pathc	Csbddl}|�t��}ddlm}W5QRX|�|jjd�|�|jj|j�|�	t
|j�d�dS)Nrr;�	_testcapizCcannot import name 'i_dont_exist' from '_testcapi' \(.*\.(so|pyd)\))rFr2r9r<r>r?rr#r@rArB)r.rFrDr<rrr�2test_from_import_missing_attr_has_name_and_so_path_sz>ImportTests.test_from_import_missing_attr_has_name_and_so_pathc	CsD|�t��}ddlm}W5QRX|�|jjd�|�|jj�dS)Nrr;�_warning)	r2r9rHr<r>r?rZassertIsNoner#rCrrr�&test_from_import_missing_attr_has_namehsz2ImportTests.test_from_import_missing_attr_has_namec	CsF|�t��}ddlm}W5QRX|�|jjddh�|�|j�dS)Nrr;�	posixpathZntpath)r2r9Zos.pathr<�assertInr?r�assertIsNotNonerCrrr�/test_from_import_missing_attr_path_is_canonicalosz;ImportTests.test_from_import_missing_attr_path_is_canonicalc
Csddl}t��p\}}t|d��}|�d�W5QRXi}|�t|�|��d���td|�d�|�W5QRX|�d|�W5QRXt��p\}}t|d��}|�d�W5QRXi}|�t|�|��d	���td|�d�|�W5QRX|�d|�W5QRXdS)
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.rOrr#r�globalsrrr�"test_from_import_star_invalid_typeus*��z.ImportTests.test_from_import_star_invalid_typec	Cs"|�t��ddl}W5QRXdSr1)r2r9�RAnDoM)r.rYrrr�test_case_sensitivity�sz!ImportTests.test_case_sensitivitycCsddlm}dS)Nr)�double_const)�testr[)r.r[rrr�test_double_const�szImportTests.test_double_constcsV�fdd�}tj�dtj�z*|d�tj�d�rDdD]}||�q6W5tjd=XdS)Nc
st|}trtd}ntd}t|d��F}td|d|d�t�d�}t�d�}td||d�td	||d�W5QRXttjkr�tjt=t�	�znztt�}Wn4t
k
r�}z��d
||f�W5d}~XYnX��|j|d|���|j|d|�W5t
t�t|�t|�XdS)NrrrNz)# This tests Python's ability to import azfile.��filei�za =zb =zimport from %s failed: %sz'module loaded (%s) but contents invalid)rrrP�print�random�	randranger r!r+r,rr�
__import__r9�failr>�a�b)�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�longlistcCs�|d}t|d��2}|�d�td�D]}|�d�q&|�d�W5QRXzt�|�W5t|�Xtj�d�t	�
�i}zt|�td|d|�W5tjd=t|d	�t|d
�|��ztj|=Wnt
k
r�YnXXdS)NrrNzd = [
i�z"",
�]r����c�ozimport )rPrQ�ranger�
py_compile�compiler r#�appendr+r,�clearr!�KeyErrorrrU)r.�module�filenamer�i�	namespacerrr�test_module_with_large_stack�s.

z(ImportTests.test_module_with_large_stackc	Cs�td}t|d��}td|d�W5QRXtj�dtj�t�	�ttj
krTtj
t=z0dD]&}|�t
tt�|�ttj
d|�qZW5tjd=tt�XdS)NrrNza = 1/0r^r)���z'damaged module in sys.modules on %i try)rrPr`r r#r$r=rlr+r,r!rr2�ZeroDivisionErrorrcrV)r.r&rr}rrr�test_failing_import_sticks�s

�
z&ImportTests.test_failing_import_stickscCsTddl}ddl}|�|||j�|�t|jd��ddlm}|�||j|j�dS)Nrr@)r\�test.support�assertIs�__name__�
assertTrue�hasattr�support)r.�xr\�yrrr�test_import_name_binding�sz$ImportTests.test_import_name_bindingcCsjzd}W5ddlmm}Xd}|dkrFddlmm}|d7}q"td�D]}ddlmm}qNdS)Nr.�
r�r�)Ztest.support.script_helperr�rru)r.r�r}rrr�test_issue31286s
zImportTests.test_issue31286c	Csttjd}t|d��}|�d�W5QRXtj�dtj�z�tt�}|�ttj
�|�|jdd�|�|jdd�t	t�t|d��}|�d�W5QRX|�ttj|�tj
�t�}|�|d	�|�|jd
d�|�|jdd�W5tjd=t	t�t
t�XdS)N�pyrNz
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=�extseprPrQr r#r$rlrrrcrKr!r>rerfr2r�r+�reload�getrL)r.r&rrhrrr�test_failing_reloads(zImportTests.test_failing_reloadc	Cs�td}t|d��}|�d�W5QRXtj�dtj�zft
t�}|�|j�
d��t�|�tj	t=t|�t��t
t�}tj�|j�\}}|�|d�W5tjd=tt�ttj	kr�tj	t=XdS)NrrNztest = None
rr)rrPrQr r#r$r=rlrr!rcr�r@�endswithr%rr+r,�splitextr>)r.r&rrh�basergrrr�test_file_to_source?s$

zImportTests.test_file_to_sourcec	Csjtj�t�}t��}z|�|�Wn$tk
rF|�d�	|��YnX|�
t��}t|�W5QRXdS)Nzpath is not encodable to {})
r=r#�abspathrr �getfilesystemencoding�encode�UnicodeEncodeError�skipTest�formatr2r9rc)r.r#�encodingrsrrr�test_import_by_filenameWs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())
            )rrr�textwrap�dedent�assert_python_ok)r.Ztestfnrrr�!test_import_in_del_does_not_crashasz-ImportTests.test_import_in_del_does_not_crashc
Cs�tj�dtj�z�td}tj�	|�}t
|d��}W5QRXzt�|d�Wnntk
rj|�
d�YnRtk
r�}z4|jttdd�ttdd�fkr��|�
d�|��W5d}~XYnXtt�t�|�W5tjd=tt�XdS)	NrrrN)���r�z-cannot set modification time to large integerZ	EOVERFLOW�EINVALz2cannot set modification time to large integer ({}))r r#r$r=rlrrr+�util�cache_from_sourcerP�utime�
OverflowErrorr��OSError�errno�getattrr�rc�stat)r.r&Zcompiledr�errr�test_timestamp_overflowls(

�"z#ImportTests.test_timestamp_overflowcCs6ztddgd�Wntk
r0|�d�YnXdS)NZhttpr7)�fromlistzfromlist must allow bogus names)rcr9rdr-rrr�test_bogus_fromlist�szImportTests.test_bogus_fromlistcCs.ddg}tj|�}|��\}}|�d|�dS)N�-cz&del __builtins__.__import__; import ossImportError)rZspawn_python�communicaterK)r.�args�popen�stdout�stderrrrr�test_delete_builtins_import�s
z'ImportTests.test_delete_builtins_importc	Cs(|�td��ddlm}W5QRXdS)Nz^No module named 'bogus'r)�foo)rRr9�bogusr�)r.r�rrr�/test_from_import_message_for_nonexistent_module�sz;ImportTests.test_from_import_message_for_nonexistent_modulec	Cs(|�td��ddlm}W5QRXdS)Nz^cannot import name 'bogus'r)r�)rRr9rOr�)r.r�rrr�,test_from_import_message_for_existing_module�sz8ImportTests.test_from_import_message_for_existing_modulec	CsbGdd�d�}d}|�t|�|�tj|<|�t��}ddlm}W5QRX|�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!r2r9r�r�r>rBr?)r.r��module_namerDr�rrrr��s�z+ImportTests.test_from_import_AttributeErrorc
CsZttdd��D|�t��ddlm}W5QRX|�t��tjW5QRXW5QRXdS)Nr�rr�)rr=r2r9r�r�)r.r�rrr�test_issue31492�s
zImportTests.test_issue31492cs�tj�dtj�tj�t�d��z�d���fdd��td�D]x}t�	���fdd�td�D�}z*tj
�|�j��t�d�W5QRXW5tj
�d	d�tj
�d
d�X�dk	r<��q<W5tjd=XdS)Nr�datac
s@���zddl}Wn&tk
r:}z|�W5d}~XYnXdSr1)�wait�package�
BaseException)r�r�)�event�excrr�run�s
z)ImportTests.test_concurrency.<locals>.runr�csg|]}tj�d��qS))�target)�	threading�Thread)�.0r�)r�rr�
<listcomp>�sz0ImportTests.test_concurrency.<locals>.<listcomp>r�r�zpackage.submodule)r r#r$r=�join�dirnamer@rur��Eventr!r"r\r�Z
start_threads�set�time�sleep)r.r}�threadsr)r�r�r�r�test_concurrency�s 
zImportTests.test_concurrency�win32�Windows-specificc
Cs�ddlm}|tj�}tj�d�j}tj	�
tj	�|�d�d|krDdnd��}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�W5QRXdS)Nr)�GetModuleFileNameZ_sqlite3z
sqlite3{}.dllZ_drZDLLszvcruntime*.dllcSsi|]}|��tj|�qSr)�upperr=�environ)r��krrr�
<dictcomp>�sz:ImportTests.test_dll_dependency_import.<locals>.<dictcomp>�;Z
PYTHONPATHz-Scz	import oszp = os.add_dll_directory({!r})zimport _sqlite3zp.close)r��env�cwd)�_winapir�r Z	dllhandler+r��	find_spec�originr=r#r�r�r�r\r�r�mkdir�basename�
executable�shutil�copy�globrT�prefixr�r@�
subprocess�
check_call�STDOUT)
r.r�ZdllnameZpydnameZdepname�tmpZtmp2Zpyexerr�rrr�test_dll_dependency_import�sR

�
 
���
�

�z&ImportTests.test_dll_dependency_importN)rp)'r�r�r�r/r0r6r8r:rEr
rGrIrMrXrZr]rorr�r�r�r��skip_if_dont_write_bytecoder�r�r�r�r�r�r�r�r�r�r��unittest�
skipUnlessr rmr�rrrrr)CsH
,
(&




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|��Pt��>\}}tj�|�}t|�}tj�|�sD|�	d�t�
|�}W5QRXW5QRX|�tt
�
|j��td|@��dS)N��4__import__ did not result in creation of a .pyc filei�)r
r(r+r�r�rcr=r#�existsrdr�r>�oct�S_IMODE�st_mode)r.�maskrr#�cached_pathr{�	stat_inforrr�test_creation_modes
�z&FilePermissionTests.test_creation_modec
Cs�d}td��\t��J\}}tj�|�}t�||�t|�tj�	|�sP|�
d�t�|�}W5QRXW5QRX|�t
t�|j��t
|��dS)N�r�r��r
r(r+r�r�r=�chmodrcr#r�rdr�r>rrr)r.�moderr#rrrrr�test_cached_mode_issue_2051s
z/FilePermissionTests.test_cached_mode_issue_2051c
Cs�d}td��\t��J\}}tj�|�}t�||�t|�tj�	|�sP|�
d�t�|�}W5QRXW5QRX|dB}|�t
t�|j��t
|��dS)N�r�r��r)r.r
rr#rrZexpectedrrr�test_cached_readonly$s
z(FilePermissionTests.test_cached_readonlyc
Cst���
\}}t|d��}|�d�W5QRXt�|�}t�||j|jdf�t�|d�t	|�}|�
|jd�t�|d�t|d��}|�d�W5QRXt|�t
��t	|�}|�
|jd�t|�t|�t
��|d	}t�t
j�|�|�t	|�}|�
|jd�W5QRXdS)
NrNzx = 'original'
i�rZoriginalrzx = 'rewritten'
Z	rewrittenrs)r(rPrQr=r�r��st_atime�st_mtimer	rcr>r�rr+r,r�renamer�r�)r.rr#r�s�mZ
bytecode_onlyrrr�test_pyc_always_writable4s,
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�c	Csntjdd�|_tj�|jd�|_t�|j	�t
|jd��}|�|j
�W5QRXtj�d|j	�t��dS)NrNr)r r#�sys_pathr!r"r��orig_moduler=r��dir_namerP�	file_namerQ�
module_sourcer$r+r,)r.rrrrr/fszPycRewritingTests.setUpcCsV|jtjdd�<|jdk	r*|jtj|j<n
t|j�t|j�t|j	�t
|j�dSr*)rr r#rr!r�rrr�
compiled_namerrr-rrrr0os



zPycRewritingTests.tearDowncCs t�}t|j||�tj|jSr*)rWrcr�r r!)r.�nsrrr�
import_moduleyszPycRewritingTests.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_sourcec	Cs�t�|j�t|jd��}|�d�}t�|�}W5QRXt|j	�}t
jj}|�
d�}|||<|jt|�d�}t|jd��}|�|�t�||�W5QRX|��}|�|jj|j�dS)N�rb�r�)�	co_consts�wb)rvrwrrPr�read�marshal�load�listr*r+r�__code__�index�replace�tuplerQ�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�rr/r0rr"r%r'r9rrrrrSs		
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-rrrr/�szPathsTests.setUpcCst|j�|jtjdd�<dSr*)rr#r;r r-rrrr0�s
zPathsTests.tearDownc	Cs`ttj�|jd�d��}|�d�W5QRXtj�|jd�td�}|�|j	d�t
d�dS)Nztest_trailing_slash.pyrNz testdata = 'test_trailing_slash'�/�test_trailing_slash)rPr=r#r�rQr rxrcr>�testdatar)r.rrhrrrr=�szPathsTests.test_trailing_slashr�r�c	
CsRttj�|jd�d��}|�d�W5QRXt��tj�|j�}ddl}|�	�}|d}d||f}||dd�7}zt�
|�WnJtk
r�}z,|jtj
tjtjfkr�|�d|f��W5d}~XYnXtj�d|�ztd�}Wn6tk
�r }z|�d	||f�W5d}~XYnX|�|jd�|�|j�|�|j�td�dS)
Nztest_unc_path.pyrNztestdata = '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)rPr=r#r�rQr+r,r��socket�gethostname�listdirr�r��EPERMZEACCES�ENOENTr�r r$rcr9rdr>r>r�r@rnr)	r.rr#r?ZhnZdriveZuncr�rhrrr�
test_UNC_path�s2�zPathsTests.test_UNC_pathN)r�r�r�ZSAMPLESrr#r/r0r=r�r�r rmrDrrrrr:�s	r:c@sHeZdZdd�ZeZdd�Zdd�Zdd�Zd	d
�Zdd�Z	d
d�Z
dS)�RelativeImportTestscCstd�dS)Nztest.relimport)rr-rrrr0�szRelativeImportTests.tearDowncCs ddlm}|�t|d��dS)Nr�)�	relimportrE)rrFr�r�)r.rFrrr�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)rUr�rrr�check_relative�sz:RelativeImportTests.test_issue3221.<locals>.check_relativer\ztest.notarealmodule)�__package__r�znotarealpkg.notarealmoduler�)rJ)�dictr2r3�objectrS)r.rIrrHr�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_global	s
�z=RelativeImportTests.test_parentless_import_shadowed_by_globalc	Cs0|�t��ddlm}|�d�W5QRXdS)Nr�)�sepz>explicit relative import triggered an implicit absolute import)r2r9r=rPrd)r.rPrrr�#test_absolute_import_without_futuresz7RelativeImportTests.test_absolute_import_without_futurecCs�tj�tj�t�dd�}tdd��Tt|��@|�t��ddl	}W5QRX|�
dtj�|�
dtj�W5QRXW5QRXdS)Nr��package2�
submodule1�
submodule2r)
r=r#r�r�r@rrr2r9rSrVr r!)r.r#rSrrr�test_import_from_non_packagesz0RelativeImportTests.test_import_from_non_packagec
CsRtddd��<ttj�tj�t�d���ddl}|jj	W5QRXW5QRXdS)NrRzpackage2.submodule1zpackage2.submodule2r�r)
rrr=r#r�r�r@Zpackage2.submodule1rSrT)r.rRrrr�!test_import_from_unloaded_packages
�z5RelativeImportTests.test_import_from_unloaded_packageN)r�r�r�r0r/rGrMrOrQrUrVrrrrrE�s	rEc@seZdZdd�ZdS)�OverridingImportBuiltinTestsc	Csxddl}dd�}|�|�|�ttddd���|�|�d�W5QRXtt�ddd���|�|�|�W5QRXdS)	NrcSsddl}|Sr1�r=rXrrrr�+sz?OverridingImportBuiltinTests.test_override_builtin.<locals>.foorccWsdS�N�r�r�rrr�<lambda>0�zDOverridingImportBuiltinTests.test_override_builtin.<locals>.<lambda>rZcWsdSrYrr[rrrr\6r])r=r>r�builtinsr	rW)r.r=r�rrr�test_override_builtin'sz2OverridingImportBuiltinTests.test_override_builtinN)r�r�r�r_rrrrrW&srWc@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)rrrrr&r-rrr�_clean=szPycacheTests._cleanc	CsRtd|_|��t|jd��}td|d�W5QRXtj�dtj	�t
��dS)NrrNz/# This is a test file written by test_import.pyr^r)rr&rarPr`r r#r$r=rlr+r,)r.�fprrrr/Bs
zPycacheTests.setUpcCs,tjdtjkstd��tjd=|��dS)NrzUnexpected sys.path[0])r r#r=rl�AssertionErrorrar-rrrr0JszPycacheTests.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_pathOs��z!PycacheTests.test_import_pyc_pathr�r��geteuidrz=due to varying filesystem permission semantics (issue #11956)c	Cs\td��tt�W5QRX|�tj�d��tj�	|j
�}|�tj�|�d�|t��dS)N�rz"bytecode file {!r} for {!r} exists)
r
rcrr�r=r#r�r+r�r�r&rdr�rerrr�test_unwritable_directoryYs
��z&PycacheTests.test_unwritable_directorycCsVtt�tj�|j�}|�tj�	|��t�
|j�tt�t��|�
ttt�dSr*)rcrr+r�r�r&r�r=r#r�r%rr,r2r9)r.r&rrr�test_missing_sourceisz PycacheTests.test_missing_sourcec
Csttt�t|j�}t�|j�tt�t��tt�}z,|�	|j
tj�t�
�tjtj�|���W5t�|�XdSr*)rcrrr&r=r%rr+r,r>r@r#r��getcwdrl�relpath�r.r&rrrr�test_missing_source_legacyus
�z'PycacheTests.test_missing_source_legacycCs<tt�}tj�td�}|�|jtj�	t�
�tj|��dS)Nr)rcrr+r�r�r>�
__cached__r=r#r�rkrl)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>ror#r�rkrlrlrmrrr�test___cached___legacy_pyc�s
�z'PycacheTests.test___cached___legacy_pycc	Cs�dd�}t�d�|�|�ttj�dd�d��W5QRXttj�dd�d��W5QRXt��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>.cleanuprs�__init__.pyrN�foo.pyrt)r=r�r�rPr#r�r+r,rcr�r�r>rorkrlr r!�r.rvrZinit_pycZfoo_pycrrr�test_package___cached__�s"



� �z$PycacheTests.test_package___cached__c	Csdd�}t�d�|�|�ttj�dd�d��W5QRXttj�dd�d��W5QRXt��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�dSrrrurrrrrv�sz>PycacheTests.test_package___cached___from_pyc.<locals>.cleanuprsrwrNrxrt)r=r�r�rPr#r�r+r,rcrr�r�r>rorkrlr r!ryrrr� test_package___cached___from_pyc�s*



� �z-PycacheTests.test_package___cached___from_pycc	CsNtt�tt�t|jd��}td|d�W5QRXtt�}|�|jd�dS)Nrezx = 5r^rZ)rcrrrPr&r`r>r�)r.rbrrrr�test_recompute_pyc_same_second�sz+PycacheTests.test_recompute_pyc_same_secondN)r�r�r�rar/r0r�rfr�r�r=r�skipIfr�rgrirjrnrprqrzr{r|rrrrr`:s0
	��



r`c@sXeZdZdZedZdd�Zdd�Ze�e	e
d�p>e
��dkd	�ej
jd
d���ZdS)
�TestSymbolicallyLinkedPackage�samplez-taggedcCs�tj�|j�tj�|j�tjdd�|_t�	|j�|�
tjj|j�tj�|jd�}tj�|�tj�
|�svt�tj|j|jdd�|�
tjj|j�t��|�tj�|j�d�tj�tj�|jd��s�t�dS)NrwT)Ztarget_is_directory)r\r�r�tagged�package_namer r#�
orig_sys_pathr=r�r�r�Zcreate_empty_filer�rc�symlinkrr+r,r>�isdir�isfile)r.Z	init_filerrrr/�sz#TestSymbolicallyLinkedPackage.setUpcCs|jtjdd�<dSr*)r�r r#r-rrrr0�sz&TestSymbolicallyLinkedPackage.tearDown�getwindowsversion)�rzWindows Vista or later requiredcCsNdgtjdd�<tj�|j�s"t�tj�tj�|jd��s>t�t�|j�dS)N�.rw)	r r#r=r�r�rcr�r+rr-rrr�test_symlinked_dir_importable�sz;TestSymbolicallyLinkedPackage.test_symlinked_dir_importableN)r�r�r�r�r�r/r0r�r�r�r r�r\r�Zskip_unless_symlinkr�rrrrr~�s
�r~c@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�ImportlibBootstrapTestscCstjd}|�|�dS)N�_frozen_importlib)r r!r�r!rrr�test_frozen_importlib
s
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�rJr�r@r�)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�rJr�r@r�)r.r�rhrrr�4test_frozen_importlib_external_is_bootstrap_externals
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.

    c	Cs@t�d��,}d|_td}td}|�t|�|�W5QRXdS)N�*importlib._bootstrap_external._path_isfileTrr��mockZpatchZreturn_valuerr>r)r.�_path_isfiler#�expectrrr�test_get_sourcefile4s
z&GetSourcefileTests.test_get_sourcefilec	Cs8t�d��$}d|_td}|�t|�|�W5QRXdS)Nr�Frr�)r.r�r#rrr�test_get_sourcefile_no_source=sz0GetSourcefileTests.test_get_sourcefile_no_sourcecCstd}|�t|�|�dS)Nz.bad_ext)rr>r)r.r#rrr�test_get_sourcefile_bad_extEsz.GetSourcefileTests.test_get_sourcefile_bad_extN)r�r�r��__doc__r�r�r�rrrrr�*s	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�dSr1)r=r�rr r#�old_pathr$r-rrrr/Ns
zImportTracebackTests.setUpcCs|jtjdd�<tt�dSr*)r�r r#rrr-rrrr0SszImportTracebackTests.tearDownrc	CsJtj�t||�}t|d��}|�|�W5QRX|�t|�t�	�|S)NrN)
r=r#r�rrPrQr�rr+r,)r.rh�contentsrgZfnamerrrr�
create_moduleWsz"ImportTracebackTests.create_modulecCsrg}|r8|jj}|j}|r&||dkr0|�|�|j}q|�t|�t|�|�t||�D]\}}|�||�qXdS)Nrr)	�tb_frame�f_coder5rx�tb_nextr>�len�ziprK)r.�tb�filesZ
deduped_filesr7�fn�patrrr�assert_traceback_s
z%ImportTracebackTests.assert_tracebackc
CsRzddl}Wn(tk
r4}z
|j}W5d}~XYnX|�d�|�|tg�dS)Nr�#ImportError should have been raised)�nonexistent_xyzzyr9�
__traceback__rdr�r@)r.r�r�r�rrr�test_nonexistent_moduleks
z,ImportTracebackTests.test_nonexistent_modulec
Cs`|�dd�zddl}Wn(tk
r@}z
|j}W5d}~XYnX|�d�|�|tdg�dS)Nr�zimport nonexistent_xyzzyrr�rx)r�r�r9r�rdr�r@�r.r�r�r�rrr�test_nonexistent_module_nestedus
z3ImportTracebackTests.test_nonexistent_module_nestedc
Cs`|�dd�zddl}Wn(tk
r@}z
|j}W5d}~XYnX|�d�|�|tdg�dS)Nr��1/0r�)ZeroDivisionError should have been raisedrx�r�r�r�r�rdr�r@r�rrr�test_exec_failures
z&ImportTracebackTests.test_exec_failurec
Csn|�dd�|�dd�zddl}Wn(tk
rL}z
|j}W5d}~XYnX|�d�|�|tddg�dS)	Nr�z
import bar�barr�rr�rx�bar.pyr�r�rrr�test_exec_failure_nested�s
z-ImportTracebackTests.test_exec_failure_nestedc
Cs^|�dd�zddl}Wn(tk
r@}z
|j}W5d}~XYnX|�d�|�|tg�dS)Nr�zinvalid syntax is invalidrz#SyntaxError should have been raised)r�r��SyntaxErrorr�rdr�r@r�rrr�test_syntax_error�s
z&ImportTracebackTests.test_syntax_errorc	Cs�d}|�t|�tj�t|�}t�|�tj�|d�}t|d��}|�|�W5QRXtj�|d�}t|d��}|�|�W5QRXt	�
�||fS)N�_parent_foorwrNr�)r�rr=r#r�rr�rPrQr+r,)r.�parentZchildZpkg_nameZpkg_path�	init_pathr�bar_pathrrr�_setup_broken_package�s
z*ImportTracebackTests._setup_broken_packagec
Csd|�dd�\}}zddl}Wn(tk
rD}z
|j}W5d}~XYnX|�d�|�|t|g�dS)Nrr�rr��r�Z_parent_foo.barr�r�rdr�r@�r.r�r�r�r�r�rrr�test_broken_submodule�s
z*ImportTracebackTests.test_broken_submodulec
Csh|�dd�\}}zddlm}Wn(tk
rH}z
|j}W5d}~XYnX|�d�|�|t|g�dS)Nrr�r�r�r��r�r�r�r�r�rdr�r@�r.r�r�r�r�r�rrr�test_broken_from�s
z%ImportTracebackTests.test_broken_fromc
Csd|�dd�\}}zddl}Wn(tk
rD}z
|j}W5d}~XYnX|�d�|�|t|g�dS)Nr�rrr�r�r�rrr�test_broken_parent�s
z'ImportTracebackTests.test_broken_parentc
Csh|�dd�\}}zddlm}Wn(tk
rH}z
|j}W5d}~XYnX|�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�kr.|jj}nd}zfdd�}||j_zddl}Wn(tk
rx}z
|j}W5d}~XYnX|�	d�|�
|td	tg�W5|dkr�|j`n||j_XdS)
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�r@)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)rrrr�rr�)r.Zpynamerrrr�test_unencodable_filename�s�z.ImportTracebackTests.test_unencodable_filenameN)r)r�r�r�r/r0r�r�r�r�r�r�r�r�r�r�r�r�r
r�r�r�rr�rrrrr�Ls$










r�c@sXeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�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.�keyrrrr0s
zCircularImportTests.tearDowncCs0zddl}Wntk
r*|�d�YnXdS)Nrz/circular import through relative imports failed)Z,test.test_import.data.circular_imports.basicr9rd�r.r\rrr�test_directszCircularImportTests.test_directcCs0zddl}Wntk
r*|�d�YnXdS)Nrz@relative import in module contributing to circular import failed)Z/test.test_import.data.circular_imports.indirectr9rdr�rrr�
test_indirectsz!CircularImportTests.test_indirectcCs0zddl}Wntk
r*|�d�YnXdS)Nrz-circular import involving a subpackage failed)Z1test.test_import.data.circular_imports.subpackager9rdr�rrr�test_subpackagesz#CircularImportTests.test_subpackagecCsbz"ddlmmmm}Wntk
r@|�d�YnXddlm}|�	|j|j�dS)Nrz9circular import with rebinding of module attribute failed)r�)
Z0test.test_import.data.circular_imports.rebindingror�Zcircular_imports�	rebindingr9rdZ-test.test_import.data.circular_imports.subpkgr�r�)r.r�r�rrr�test_rebindings"z"CircularImportTests.test_rebindingcCs0zddl}Wntk
r*|�d�YnXdS)Nrz9circular import with binding a submodule to a name failed)Z.test.test_import.data.circular_imports.bindingr9rdr�rrr�test_binding#sz CircularImportTests.test_bindingcCsddl}ddl}dSr1)Z*test.test_import.data.circular_imports.use�-test.test_import.data.circular_imports.sourcer�rrr�test_crossreference1)sz(CircularImportTests.test_crossreference1c	Cs\|�t��}ddl}W5QRXt|j�}|�d|�|�d|�|�d|�|�d|�dS)Nrz-test.test_import.data.circular_imports.sourcerzpartially initialized modulezcircular import)r2r�r�rBr?rK)r.rDr\�errmsgrrr�test_crossreference2-s
z(CircularImportTests.test_crossreference2c	Cs4|�t��}ddl}W5QRX|�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))r2r9Z2test.test_import.data.circular_imports.from_cycle1rKrBr?)r.rDr\rrr�test_circular_from_import6s�z-CircularImportTests.test_circular_from_importN)
r�r�r�r�r0r�r�r�r�r�r�r�r�rrrrr��s	r��__main__)Nr)>r+�importlib.utilZimportlib._bootstrap_externalrr^r-r=rvrar�r�r�r r�r�r�Z
unittest.mockr�r�r��
contextlibr�r�r\rrrrrrr	r
rrr
rrrrZtest.test_importlib.utilrr}�dont_write_bytecoder�r�contextmanagerr(ZTestCaser)r�rr:rErWr`r~r�r�r�r�r��mainrrrr�<module>sh@�	?SY5E". !1E