Your IP : 216.73.216.213


Current Path : /opt/alt/python38/lib64/python3.8/test/__pycache__/
Upload File :
Current File : //opt/alt/python38/lib64/python3.8/test/__pycache__/test_compileall.cpython-38.pyc

U

i�f�j�@s�ddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddl
mZmZzddlmZdZWnek
r�dZYnXddlmZddlmZdd	lmZdd
lmZGdd�d�ZGd
d�dee
jedd�ZGdd�dee
jedd�ZGdd�de
j�ZGdd�d�Z Gdd�de e
jedd�Z!Gdd�de e
jedd�Z"e#dk�r~e
�$�dS)�N)�mock�
skipUnless)�ProcessPoolExecutorTF)�support)�
script_helper�)�without_source_date_epoch)�SourceDateEpochTestMetac@s�eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Ze�d�dd��Zd d!�Ze�d�d"d#��Ze�d�e�d$�d%d&���Zejdd'd(�e�d$�d)d*���Zd'S)+�CompileallTestsBasec	Cs�t��|_tj�|jd�|_tj�	|j�|_
t|jd��}|�d�W5QRXtj�|jd�|_
tj�	|j
�|_t�|j|j
�tj�|jd�|_t�|j�tj�|jd�|_t�|j|j�dS)N�_test.py�wzx = 123
z	_test2.pyZ_subdirz	_test3.py)�tempfile�mkdtemp�	directory�os�path�join�source_path�	importlib�util�cache_from_source�bc_path�open�write�source_path2�bc_path2�shutilZcopyfileZsubdirectory�mkdir�source_path3��self�file�r"�9/opt/alt/python38/lib64/python3.8/test/test_compileall.py�setUps
zCompileallTestsBase.setUpcCst�|j�dS�N�r�rmtreer�r r"r"r#�tearDown-szCompileallTestsBase.tearDownc	Cs8tj�|jd�|_t|jd��}|�d�W5QRXdS)Nz_test_bad.pyrzx (
)rrrr�bad_source_pathrrrr"r"r#�add_bad_source_file0sz'CompileallTestsBase.add_bad_source_filec	CsPt|jd��}|�d�}W5QRXtt�|j�j�}t�	dt
jjd|�}||fS)N�rb��<4sllr)
rr�read�intr�statr�st_mtime�struct�packrr�MAGIC_NUMBER)r r!�data�mtimeZcomparer"r"r#�timestamp_metadata5s
z&CompileallTestsBase.timestamp_metadatac	Cs�tj�d�rt�d��t�|j�|j|�	��t
|jd��}|��t
|�d�}W5QRXt
|jd��}|�|�|�|�W5QRX|j|�	��tj|jddd�|j|�	��dS)	zOCheck that compileall recreates bytecode when the new metadata is
        used.�SOURCE_DATE_EPOCHzSOURCE_DATE_EPOCH is setr,N�wbFT��force�quiet)r�environ�get�unittest�SkipTest�
py_compile�compiler�assertEqualr8rrr/�lenr�assertNotEqual�
compileall�compile_dirr�
assertTrue)r Zmetadatar!Zbcr"r"r#�recreation_check<s

z$CompileallTestsBase.recreation_checkcCs|�t�dtjjdd��dS)Nr.rr)rJr3r4rrr5r(r"r"r#�
test_mtimeLs�zCompileallTestsBase.test_mtimecCs|�d�dS)Ns)rJr(r"r"r#�test_magic_numberQsz%CompileallTestsBase.test_magic_numbercCs|j|jfD] }zt�|�WqYqXq|�tj|jddd��|�tj�	|j�oftj�	|j��t�|j�|�tj
|jddd��|�tj�	|j�o�tj�	|j��t�|j�t�|j�|��|�
tj|jddd��|�
tj
|jddd��dS)NFTr;�)rrr�unlinkrIrG�compile_filerr�isfilerHrr+�assertFalser*)r �fnr"r"r#�test_compile_filesUs<������z&CompileallTestsBase.test_compile_filesc	Csh|�tj�|j��t���}|�t�	t
�|j���W5QRX|�
|��d�|�tj�|j��dS)Nz&Compiling ([^WindowsPath|PosixPath].*))rQrrrPrr�captured_stdoutrIrGrO�pathlib�Pathr�assertRegex�getvalue)r �stdoutr"r"r#�test_compile_file_pathlikens

"z.CompileallTestsBase.test_compile_file_pathlikecCsP|�tj�|j��|�tjt�	|j
�t�	d�dd��|�tj�|j��dS)NZ	ddir_pathrM)�ddirr=)rQrrrPrrIrGrOrUrVrr(r"r"r#�test_compile_file_pathlike_ddirvs�z3CompileallTestsBase.test_compile_file_pathlike_ddirc	Csttjjj|jgd��|�tjdd��W5QRXtjjj|jgd��$|��|�	tjdddd��W5QRXdS)N)rrM�r=FT)Zskip_curdirr<r=)
�test�test_importlibrZimport_staterrIrGZcompile_pathr+rQr(r"r"r#�test_compile_path}s
�z%CompileallTestsBase.test_compile_pathc	Cshtj�|jd�}tj�|d�}t�|�t|d��W5QRXt�|�|�tj�	tj�|d���dS)Nr6r!r�__pycache__)
rrrrrrrGrOrQ�exists)r Zdata_dirZ	data_filer"r"r#�test_no_pycache_in_non_package�s


z2CompileallTestsBase.test_no_pycache_in_non_packagecCs�dnd\}}tj|jd|d�tjj|j|d�}|�tj	�
|��tjj|j|d�}|�tj	�
|��tjj|j|d�}|�tj	�
|��dS)NT)rr)r�)r=�optimize)�optimization)
rGrHrrrrrrIrrrPrr)r re�opt�cachedZcached2Zcached3r"r"r#�
test_optimize�s
�
�
�z!CompileallTestsBase.test_optimizec	Csn|�tj�|j��t���}t�t	�
|j��W5QRX|���
�d}|�|d�|�tj�|j��dS)Nrz$Listing ([^WindowsPath|PosixPath].*))rQrrrPrrrTrGrHrUrVrrX�
splitlinesrWrI)r rY�liner"r"r#�test_compile_dir_pathlike�s
z-CompileallTestsBase.test_compile_dir_pathlikez&concurrent.futures.ProcessPoolExecutorcCs"tj|jddd�|�|j�dS�NT��r=�workers�rGrHrrI�called�r �	pool_mockr"r"r#�test_compile_pool_called�sz,CompileallTestsBase.test_compile_pool_calledc	Cs,|�td��tj|jdd�W5QRXdS)Nz%workers must be greater or equal to 0���)rp)ZassertRaisesRegex�
ValueErrorrGrHrr(r"r"r#�!test_compile_workers_non_positive�s�z5CompileallTestsBase.test_compile_workers_non_positivecCs,tj|jddd�|�|jddd�dS)NTrrorZmax_workers)rGrHrrD�	call_argsrsr"r"r#�test_compile_workers_cpu_count�sz2CompileallTestsBase.test_compile_workers_cpu_countzcompileall.compile_filecCs,tj|jdd�|�|j�|�|j�dS)NTr])rGrHrrQrrrI)r �compile_file_mockrtr"r"r#�test_compile_one_worker�sz+CompileallTestsBase.test_compile_one_workerN��newcCs"tj|jddd�|�|j�dSrmrq)r r{r"r"r#�$test_compile_missing_multiprocessing�sz8CompileallTestsBase.test_compile_missing_multiprocessing)�__name__�
__module__�__qualname__r$r)r+r8rJrKrLrSrZr\r`rcrirlr�patchrurxrzr|rr"r"r"r#r
s2	

r
c@seZdZdS)�CompileallTestsWithSourceEpochN�r�r�r�r"r"r"r#r��sr�)�	metaclassZsource_date_epochc@seZdZdS)�!CompileallTestsWithoutSourceEpochNr�r"r"r"r#r��sr�c@s(eZdZdZdd�Zdd�Zdd�ZdS)	�EncodingTestzTIssue 6716: compileall should escape source code when printing errors
    to stdout.c	CsPt��|_tj�|jd�|_t|jddd��}|�d�|�d�W5QRXdS)Nrrzutf-8��encodingz# -*- coding: utf-8 -*-
u
print u"€"
)	r
rrrrrrrrrr"r"r#r$�s


zEncodingTest.setUpcCst�|j�dSr%r&r(r"r"r#r)�szEncodingTest.tearDowncCs8z*tj}tjt��dd�t_t�|j�W5|t_XdS)N�asciir�)�sysrY�io�
TextIOWrapper�BytesIOrGrHr)r Zorig_stdoutr"r"r#�
test_error�s
zEncodingTest.test_errorN)r�r�r��__doc__r$r)r�r"r"r"r#r��sr�c@s�eZdZdZedd��Zdd�Zdd�Zdd	�Zd
d�Z	dd
�Z
dd�Zdd�Zdd�Z
edd��Zdd�ZddgfdddgfdddgffD]&\ZZZeefd d!�Zee�d"e<q�d#d$�Zd%d&�Zed'd(��Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3d4�Zd5d6�Zd7d8�Z d9d:�Z!d;d<�Z"d=d>�Z#d?d@�Z$dAdB�Z%dCdD�Z&dEdF�Z'e(e)dG�dHdI��Z*e+�,dJ�dKdL��Z-dMdN�dOdP�Z.dQdR�Z/dSdT�Z0dUdV�Z1dWdX�Z2dYS)Z�CommandLineTestsBasezTest compileall's CLI.cCs�ttjjtj�D]�}d}t�|�d}|d}z`z8|��sF|��d}|�
d��}|�d�W5QRXWn"tk
r�d}YW�q�YnXW5t�t	|��|r�|�
�Xqd}||_dS)NFraztest.tryTrz# for test_compileall)�filterrr�isdirr�rUrVrrN�str�rmdir�is_dirrrr�OSError�_sys_path_writable)�clsrZdirectory_createdrr!Zsys_path_writabler"r"r#�
setUpClass�s$zCommandLineTestsBase.setUpClasscCs|jst�d��dS)Nz(not all entries on sys.path are writable)r�r@rAr(r"r"r#�_skip_if_sys_path_not_writablesz3CommandLineTestsBase._skip_if_sys_path_not_writablecCst��d|�S)N�-S�-mrG)r�r�rG)rZ!optim_args_from_interpreter_flags)r �argsr"r"r#�
_get_run_args	s��z"CommandLineTestsBase._get_run_argscOs(tj|�|�|�\}}}|�d|�|S)N�)r�assert_python_okr�rD�r r�Zenv_vars�rc�out�errr"r"r#�assertRunOKs�
z CommandLineTestsBase.assertRunOKcOs"tj|�|�|�\}}}|||fSr%)r�assert_python_failurer�r�r"r"r#�assertRunNotOKs
�
z#CommandLineTestsBase.assertRunNotOKcCs"tj�|�}|�tj�|��dSr%)rrrrIrrrb�r rRrr"r"r#�assertCompiledsz#CommandLineTestsBase.assertCompiledcCs"tj�|�}|�tj�|��dSr%)rrrrQrrrbr�r"r"r#�assertNotCompiledsz&CommandLineTestsBase.assertNotCompiledcCsrt��|_|�tj|j�tj�|jd�|_	t�
|j	�tj�|j	d�|_t�
|j	dd�|_t�
|j	dd�|_dS)N�foora�__init__rdZbar)r
rrZ
addCleanuprr'rrr�pkgdirr�pkgdir_cachedirr�make_script�initfn�barfnr(r"r"r#r$!s
zCommandLineTestsBase.setUpcCsL|��t�|jdd�}|j|jd�|�|�|�|j�|�|j�dS)N�bazrd�Z
PYTHONPATH)	r�rr�rr�r�r�r�r�)r �bazfnr"r"r#�test_no_args_compiles_path+s
z/CommandLineTestsBase.test_no_args_compiles_pathcCs�|��t�|jdd�}|j|jd�tj�|�}t�	|t
�
�dfd�t�|�j}|j|jd�t�|�j}|�
||�|jd|jd�t�|�j}|�||�dS)Nr�rdr��<rM�-f)r�rr�rr�rrrr�utime�timer1r2rDrF)r r��pycpathr7�mtime2r"r"r#� test_no_args_respects_force_flag4sz5CommandLineTestsBase.test_no_args_respects_force_flagcCsR|��t�|jdd�|j|jd�}|�d|�|jd|jd�}|�d|�dS)Nr�rdr�sListing �-q)r�rr�rr��assertInZassertNotIn�r Znoisyr=r"r"r#� test_no_args_respects_quiet_flagFsz5CommandLineTestsBase.test_no_args_respects_quiet_flagZnormal�pycrez	opt-1.pycz-OZdoubleoptimizez	opt-2.pycz-OOcs~tj|ddd|jg�|�tj�|j��t�fdd�dD��}|�	tt�
|j��|�|��fdd�t�
|j�D��dS)	Nr�rGr�c3s|]}|�tjj��VqdSr%)�formatr��implementation�	cache_tag)�.0�base��extr"r#�	<genexpr>Zs�z)CommandLineTestsBase.f.<locals>.<genexpr>)z__init__.{}.{}z	bar.{}.{}csg|]}|���r|�qSr")�endswith)r�rRr�r"r#�
<listcomp>^s
�z*CommandLineTestsBase.f.<locals>.<listcomp>)rr�r�rIrrrbr��sortedrD�listdirrQ)r r��switch�expectedr"r�r#�fUs��zCommandLineTestsBase.fZtest_pep3147_paths_cCsP|�dd|j�|�tj�|j��tddddg�}|�tt�	|j��|�dS)Nz-br��__init__.pyz__init__.pyczbar.pyzbar.pyc)
r�r�rQrrrbr�r�rDr�)r r�r"r"r#�test_legacy_pathsbs�z&CommandLineTestsBase.test_legacy_pathscCs||�d|j�|�tj�|j��tj�|jd�}|�tj�|��|�d|j�|�tj�|j��|�tj�|��dS)Nr�ra)	r�r�rIrrrbr�rrQ)r Z
cachecachedirr"r"r#�test_multiple_runslsz'CommandLineTestsBase.test_multiple_runscCs�|�d|j�tj�|j�}t�|t��dfd�t�	|�j
}|�d|j�t�	|�j
}|�||�|�dd|j�t�	|�j
}|�||�dS)Nr�r�rMr�)
r�r�rrrr�rr�r�r1r2rDrF)r r�r7r�r"r"r#�
test_forceyszCommandLineTestsBase.test_forcecCs�tj�|jd�}t�|�t�|dd�}t�|dd�}|�dd|j�|�|�|�	tj�
tj�|d���|�d|j�|�|�|�|�dS)N�spamr�rd�hamr�z-lra)rrrr�rrr�r�r�rQrbr�)r �
subpackage�	subinitfn�hamfnr"r"r#�test_recursion_control�s


z+CommandLineTestsBase.test_recursion_controlc	CsVtj�|jd�}tj�|d�}tj�|d�}|||fD]}t�|�q6tj�|d�}t�|dd�}t�|dd�}t�|dd�}|�dd|j�|�|�|�	tj�
tj�|d	���|�dd
|j�|�|�|�|�|�|�|�dd|j�|�|�|�|�|�|�|�|�|�dd|j�|�|�|�|�|�|�|�|�dS)
Nr�r�Zeggsr�rdZeggr�z-r 0raz-r 1z-r 2z-r 5)rrrr�rZmake_pkgr�r�r�rQrbr�)	r r�Zsubpackage2Zsubpackage3Zpkgr�r�ZspamfnZeggfnr"r"r#�test_recursion_limit�s8
�









z)CommandLineTestsBase.test_recursion_limitcCs6|�|j�}|�d|j�}|�d|�|�d|�dS)Nr�r�)r�r�rFrDr�r"r"r#�
test_quiet�szCommandLineTestsBase.test_quietcCsTt�|jdd�|�d|j�\}}}|�d|j�\}}}|�d|�|�d|�dS)N�crunchyfrog�
bad(syntaxr�z-qqr�)rr�r�r�rFrD)r �_r=Zsilentr"r"r#�test_silent�s
z CommandLineTestsBase.test_silentcCs.|�ddd|j�|�|j�|�|j�dS)Nr�z-xz
ba[^\\/]*$)r�r�r�r�r�r�r(r"r"r#�test_regexp�sz CommandLineTestsBase.test_regexpcCsvtj�|jd�}t�|�t�|dd�}t�|dd�}|�d|j|�|�	|j
�|�	|j�|�	|�|�	|�dS)NZfoo2r�rd�bar2r�)rrrrrrr�r�r�r�r�r�)r Zpkgdir2Zinit2fn�bar2fnr"r"r#�test_multiple_dirs�s

z'CommandLineTestsBase.test_multiple_dirscCs8t�|jdd�|�ddd|j�\}}}|�|d�dS)Nr�r�r��-d�dinsdale�File "dinsdale)rr�r�r�rW�r r�r�r�r"r"r#�test_d_compile_error�sz)CommandLineTestsBase.test_d_compile_errorcCs�t�|jdd�}|�ddd|j�t�|jdd�}tj�|�}t�|tj	�
|jd��t�|�tj|d	d
�\}}}|�
|d�dS)Nr�zraise Exceptionr�r�r��bingz
import bazzbaz.pycF)Z
__isolatedr�)rr�r�r�rrrr�renamerr�remover�rW)r r�rRr�r�r�r�r"r"r#�test_d_runtime_error�s
z)CommandLineTestsBase.test_d_runtime_errorcCsZ|�dtj�|jd�|j�\}}}|�|d�|�|d�|�tj�	t
j�|j
���dS)N�-i�
nosuchfilesrror.*nosuchfiles	Traceback)r�rrrrr�rWZassertNotRegexrQrbrrrr�r�r"r"r#�test_include_bad_file�s�
�z*CommandLineTestsBase.test_include_bad_filec	Cs�t�|jdd�}t�|jdd�}t�|jdd�}t�|jdd�}ttj�|jd�d��>}|�tj�|jd�tj	�|�tj�|jd	�tj	�W5QRX|�
d
tj�|jd�|�|�|�|�|�|�|�|�|�dS)N�f1rd�f2�f3�f4�l1r�f1.py�f2.pyr�)
rr�r�rrrrrr�linesepr�r�r��r r�r�r�r�r�r"r"r#�test_include_file_with_arg�s&


z/CommandLineTestsBase.test_include_file_with_argc	Cs�t�|jdd�}t�|jdd�}t�|jdd�}t�|jdd�}ttj�|jd�d��"}|�tj�|jd�tj	�W5QRX|�
d	tj�|jd��|�|�|�|�|�|�|�|�dS)
Nr�rdr�r�r�r�rr�r�)
rr�r�rrrrrrr�r�r�r�r�r"r"r#�test_include_file_no_arg�s&


z-CommandLineTestsBase.test_include_file_no_argcCs�t�|jdd�}t�|jdd�}t�|jdd�}t�|jdd�}tj|�d�ddg�}|j�|tj�	d	��t�
|�|�|�|�|�|�|�|�|�dS)
Nr�rdr�r�r�r"r��-r�)
rr�r�Zspawn_pythonr��stdinrrr��encodeZkill_pythonr�r�)r r�r�r�r��pr"r"r#�test_include_on_stdins



z*CommandLineTestsBase.test_include_on_stdincCs\t�|jdd�}|�d|j||j�\}}}|�|d�|�|�|�|j�|�|j�dS)Nr�zsyntax(errorr�srror)	rr�r�r�r�r�rWr�r�)r Zbingfnr�r�r�r"r"r#�!test_compiles_as_much_as_possibles
�

z6CommandLineTestsBase.test_compiles_as_much_as_possiblecCs|�d�}|�|d�dS)NZbadfilenamesCan't list 'badfilename')r�rW)r r�r"r"r#�!test_invalid_arg_produces_messages
z6CommandLineTestsBase.test_invalid_arg_produces_messagec	Cs�t�|jdd�tj�tj�|jd��}|�	d|j�t
|d��}|��}W5QRX|�t
�|dd�d�d	�|�	d
|j�t
|d��}|��}W5QRX|�t
�|dd�d�d�dS)Nr�rdr�z --invalidation-mode=checked-hashr,���little�z"--invalidation-mode=unchecked-hashr)rr�r�rrrrrrr�rr/rDr0�
from_bytes)r r��fpr6r"r"r#�test_pyc_invalidation_mode s�z/CommandLineTestsBase.test_pyc_invalidation_modezrequires multiprocessingcCs�t�|jdd�}g}td�D]F}tj�|jd�|��}t�|�t�|dd�}|�	t�|dd��q|�
|jdd�|�|�|D]}|�|�q�dS)Nr�rdrnzfoo{}r�z-j�0)rr�r�rangerrrr�r�appendr�r�)r r��files�suffixr�rRr!r"r"r#�test_workers-s

z!CommandLineTestsBase.test_workerszcompileall.compile_dirc	CsRtjdtj|jdgd��0t��|�|j�|�	|j
ddd�W5QRXdS)Nzsys.argvz-j0r}rvrpr)rr�r��
executablerrG�mainrIrrrDry)r rHr"r"r#�test_workers_available_cores<s�z1CommandLineTestsBase.test_workers_available_coresT)�parallelcCsddg}|j}g}|D]>}tj�||�}t�|�t�|dd�|�t�|dd��qtj	|jd||rjdnd	d
�|�
|�|D]�}|�
|�|j�|�tj
�|�}|t|jtj�d�}	t�|�\}
}
}tj�||	�}tjj
�|�}
|�|
j|�|�d|�d�t�|��q�dS)
z@Recursive compile_dir ddir must contain package paths; bpo39769.r^r�r�rd�modzdef fn(): 1/0
fn()
TrMr)r=r[rpN�")rrrrrrr�rrGrHrI�
startswithrrrrE�sepr�r^r_Z_get_code_from_pycrD�co_filenamer��fsdecode)r r[r�fullpathrZmodsZsubdirrZmodcode�modpathr�r�Zexpected_inZmod_code_objr"r"r#�_test_ddir_onlyDs4
�
�
z$CommandLineTestsBase._test_ddir_onlycCs|jddd�S)�=Recursive compile_dir ddir= contains package paths; bpo39769.�
<a prefix>F�r[r�rr(r"r"r#�test_ddir_only_one_worker]sz.CommandLineTestsBase.test_ddir_only_one_workercCs|jddd�S)rr Tr!r"r(r"r"r#�test_ddir_multiple_workersasz/CommandLineTestsBase.test_ddir_multiple_workerscCs|jddd�S)�?Recursive compile_dir ddir='' contains package paths; bpo39769.rdFr!r"r(r"r"r#�test_ddir_empty_only_one_workeresz4CommandLineTestsBase.test_ddir_empty_only_one_workercCs|jddd�S)r%rdTr!r"r(r"r"r#� test_ddir_empty_multiple_workersisz5CommandLineTestsBase.test_ddir_empty_multiple_workersN)3r�r�r�r��classmethodr�r�r�r�r�r�r�r$r�rr�r��namer�r�r��localsr�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrr�_have_multiprocessingrrr�rrr#r$r&r'r"r"r"r#r��s`

	


�


"


	


r�c@seZdZdS)� CommmandLineTestsWithSourceEpochNr�r"r"r"r#r,nsr,c@seZdZdS)�CommmandLineTestsNoSourceEpochNr�r"r"r"r#r-usr-�__main__)%r�rG�importlib.utilrZtest.test_importlib.utilr^rrUrBrr3r
r�r@r�rrZconcurrent.futuresrr+�ImportErrorrZtest.supportrZtest_py_compilerr	r
ZTestCaser�r�r�r�r,r-r�rr"r"r"r#�<module>sf
*����