Your IP : 216.73.216.213


Current Path : /opt/alt/python35/lib64/python3.5/test/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib64/python3.5/test/__pycache__/test_cmd_line_script.cpython-35.opt-1.pyc



��Yf�p�@sjddlZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlm
Z
ddlmZmZmZmZmZmZmZmZe
jZdddgZdZedd	�Zed
dd�Zd
Zddd�ZGdd�dej�Zdd�Z e!dkrfe �dS)�N)�support)�make_pkg�make_script�make_zip_pkg�make_zip_script�assert_python_ok�assert_python_failure�spawn_python�kill_pythonZtest1Ztest2Ztest3a�# Script may be run with optimisation enabled, so don't rely on assert
# statements being executed
def assertEqual(lhs, rhs):
    if lhs != rhs:
        raise AssertionError('%r != %r' % (lhs, rhs))
def assertIdentical(lhs, rhs):
    if lhs is not rhs:
        raise AssertionError('%r is not %r' % (lhs, rhs))
# Check basic code execution
result = ['Top level assignment']
def f():
    result.append('Lower level reference')
f()
assertEqual(result, ['Top level assignment', 'Lower level reference'])
# Check population of magic variables
assertEqual(__name__, '__main__')
from importlib.machinery import BuiltinImporter
_loader = __loader__ if __loader__ is BuiltinImporter else type(__loader__)
print('__loader__==%a' % _loader)
print('__file__==%a' % __file__)
print('__cached__==%a' % __cached__)
print('__package__==%r' % __package__)
# Check PEP 451 details
import os.path
if __package__ is not None:
    print('__main__ was located through the import system')
    assertIdentical(__spec__.loader, __loader__)
    expected_spec_name = os.path.splitext(os.path.basename(__file__))[0]
    if __package__:
        expected_spec_name = __package__ + "." + expected_spec_name
    assertEqual(__spec__.name, expected_spec_name)
    assertEqual(__spec__.parent, __package__)
    assertIdentical(__spec__.submodule_search_locations, None)
    assertEqual(__spec__.origin, __file__)
    if __spec__.cached is not None:
        assertEqual(__spec__.cached, __cached__)
# Check the sys module
import sys
assertIdentical(globals(), sys.modules[__name__].__dict__)
if __spec__ is not None:
    # XXX: We're not currently making __main__ available under its real name
    pass # assertIdentical(globals(), sys.modules[__spec__.name].__dict__)
from test import test_cmd_line_script
example_args_list = test_cmd_line_script.example_args
assertEqual(sys.argv[1:], example_args_list)
print('sys.argv[0]==%a' % sys.argv[0])
print('sys.path[0]==%a' % sys.path[0])
# Check the working directory
import os
print('cwd==%a' % os.getcwd())
cCs t|||�}tj�|S)N)r�	importlib�invalidate_caches)�
script_dir�script_basename�source�	to_return�r�>/opt/alt/python35/lib64/python3.5/test/test_cmd_line_script.py�_make_test_scriptNs
r�cCs)t||||||�}tj�|S)N)rrr)Zzip_dirZzip_basenameZpkg_namerr�depthrrrr�_make_test_zip_pkgSs
rzPimport sys, os.path, runpy
sys.path.insert(0, %s)
runpy._run_module_as_main(%r)
cCsQ|dkrd}nt|�}t||f}t|||�}tj�|S)Nzos.path.dirname(__file__))�repr�
launch_sourcerrr)r
rZmodule_name�pathrrrrr�_make_launch_scriptds	
rc@s;eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zej	ddd
��Z
ddd�Zddd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zd2d3�Zd4d5�Zd6d7�Zd8d9�Z d:d;�Z!d<d=�Z"ej	d>d?��Z#d@dA�Z$dBdC�Z%dDdE�Z&dFdG�Z'dHdI�Z(dJdK�Z)dLdM�Z*dNdO�Z+dPdQ�Z,dRdS�Z-dTdU�Z.dVdW�Z/dXdY�Z0dZS)[�CmdLineTestc	CsTtdkr*td|�tt|��|j|d�d|}	d|}
d|}d|}d|}
d	tj�}tdkr�td
�t|
�t|�t|�t|�|j|	jd�|�|j|
jd�|�|j|jd�|�|j|jd�|�|j|
jd�|�|j|jd�|�dS)NrzOutput from test script %r:rz__loader__==%az__file__==%az__package__==%rzsys.argv[0]==%azsys.path[0]==%azcwd==%azExpected output:zutf-8)�verbose�printr�assertEqual�os�getcwd�assertIn�encode)�self�script_nameZ	exit_code�data�
expected_file�expected_argv0�expected_path0�expected_package�expected_loaderZprinted_loaderZprinted_fileZprinted_packageZ
printed_argv0Z
printed_path0Zprinted_cwdrrr�
_check_outputos,









zCmdLineTest._check_outputc	Gs}ts!|ddtjjf7}||ftt�}t|dd�\}	}
}|j||	|
||||||�dS)N�-�O�
__isolatedF)�	__debug__�sys�flags�optimize�tuple�example_argsrr+)r#r$r&r'r(r)r*�cmd_line_switches�run_args�rc�out�errrrr�
_check_script�szCmdLineTest._check_scriptcGsw||f}t|�\}}}tdkrZtd|�tt|��td|�|j|jd�|�dS)NrzOutput from test script %r:zExpected output: %rzutf-8)rrrrr!r")r#r$Zexpected_msgr5r6r7r8r9rrr�_check_import_error�s
zCmdLineTest._check_import_errorcCsGtdd�\}}}ttjj�jd�}|j||�dS)Nz-czprint(__loader__)zutf-8)rrr�	machinery�BuiltinImporterr"r!)r#r7r8r9�expectedrrr�test_dash_c_loader�szCmdLineTest.test_dash_c_loadercCsit�}z!|jjd�|jj�Wdt|�}Xttjj�j	d�}|j
||�dS)Nsprint(__loader__)
zutf-8)r	�stdin�write�flushr
rrr<r=r"r!)r#�pr8r>rrr�test_stdin_loader�s	
zCmdLineTest.test_stdin_loaderFccs�|r-tddddtj�}|j}n$tddddtj�}|j}z6x*|jd�}|dkrsP|j�qWW|VWdt|�|j	�XdS)Nz-i�bufsizer�stderr�s>>> )
r	�
subprocess�PIPErFZSTDOUT�stdout�read�readliner
�close)r#�separate_stderrrCrFr%rrr�interactive_python�s		
zCmdLineTest.interactive_pythonc
CsY|j|��C}|jjd�|jj�|jd|jj�j��WdQRXdS)Ns
print('foo')
sfoo)rOr@rArBrrJrL�strip)r#rNrCrrr�check_repl_stdout_flush�s
z#CmdLineTest.check_repl_stdout_flushc
Cs�|j|��~}|jjd�|jj�|r>|jn|j}|jd|j��|jd|j��|jd|j��WdQRXdS)Ns1/0
s
Traceback sFile "<stdin>"sZeroDivisionError)rOr@rArBrFrJr!rL)r#rNrCrFrrr�check_repl_stderr_flush�s
z#CmdLineTest.check_repl_stderr_flushcCs|j�dS)N)rQ)r#rrr�test_repl_stdout_flush�sz"CmdLineTest.test_repl_stdout_flushcCs|jd�dS)NT)rQ)r#rrr�&test_repl_stdout_flush_separate_stderr�sz2CmdLineTest.test_repl_stdout_flush_separate_stderrcCs|j�dS)N)rR)r#rrr�test_repl_stderr_flush�sz"CmdLineTest.test_repl_stderr_flushcCs|jd�dS)NT)rR)r#rrr�&test_repl_stderr_flush_separate_stderr�sz2CmdLineTest.test_repl_stderr_flush_separate_stderrcCsKtj��8}t|d�}|j||||dtjj�WdQRXdS)N�script)r�temp_dirrr:rr<�SourceFileLoader)r#r
r$rrr�test_basic_script�s
zCmdLineTest.test_basic_scriptcCsztj��g}t|d�}tj|dd�tj|�tj|�}|j||||dt	j
j�WdQRXdS)NrW�doraiseT)rrXr�
py_compile�compiler�remove�make_legacy_pycr:rr<�SourcelessFileLoader)r#r
r$�pyc_filerrr�test_script_compiled�s
	z CmdLineTest.test_script_compiledcCsKtj��8}t|d�}|j||||dtjj�WdQRXdS)N�__main__�)rrXrr:rr<rY)r#r
r$rrr�test_directory�s
zCmdLineTest.test_directorycCsztj��g}t|d�}tj|dd�tj|�tj|�}|j||||dt	j
j�WdQRXdS)Nrcr[Trd)rrXrr\r]rr^r_r:rr<r`)r#r
r$rarrr�test_directory_compiled�s
z#CmdLineTest.test_directory_compiledc
Cs4tj��!}d|}|j||�WdQRXdS)Nz"can't find '__main__' module in %r)rrXr;)r#r
�msgrrr�test_directory_errors
z CmdLineTest.test_directory_errorcCs`tj��M}t|d�}t|d|�\}}|j||||dtj�WdQRXdS)Nrc�test_ziprd)rrXrrr:�	zipimport�zipimporter)r#r
r$�zip_name�run_namerrr�test_zipfiles
zCmdLineTest.test_zipfilecCsutj��b}t|d�}tj|dd�}t|d|�\}}|j||||dtj�WdQRXdS)Nrcr[Trird)	rrXrr\r]rr:rjrk)r#r
r$�
compiled_namerlrmrrr�test_zipfile_compiledsz!CmdLineTest.test_zipfile_compiledcCs[tj��H}t|d�}t|d|�\}}d|}|j||�WdQRXdS)NZnot_mainriz"can't find '__main__' module in %r)rrXrrr;)r#r
r$rlrmrgrrr�test_zipfile_errors

zCmdLineTest.test_zipfile_errorcCs|tj��i}tjj|d�}t|�t|d�}t|dd�}|j||||dt	j
j�WdQRXdS)N�test_pkgrW�launchztest_pkg.script)rrXrr�joinrrrr:rr<rY)r#r
�pkg_dirr$�launch_namerrr�test_module_in_packages
z"CmdLineTest.test_module_in_packagecCsitj��V}t|ddd�\}}t|dd|�}|j||||dtj�WdQRXdS)NrirrrWrsztest_pkg.script)rrXrrr:rjrk)r#r
rlrmrvrrr�!test_module_in_package_in_zipfile(s
z-CmdLineTest.test_module_in_package_in_zipfilecCsotj��\}t|ddddd�\}}t|dd|�}|j||||dtj�WdQRXdS)	NrirrrWr�rsztest_pkg.test_pkg.scriptztest_pkg.test_pkg)rrXrrr:rjrk)r#r
rlrmrvrrr�$test_module_in_subpackage_in_zipfile/s!z0CmdLineTest.test_module_in_subpackage_in_zipfilecCs|tj��i}tjj|d�}t|�t|d�}t|dd�}|j||||dt	j
j�WdQRXdS)Nrrrcrs)rrXrrrtrrrr:rr<rY)r#r
rur$rvrrr�test_package7s
	zCmdLineTest.test_packagecCs�tj���}tjj|d�}t|�t|d�}tj|dd�}tj	|�tj
|�}t|dd�}|j||||dt
jj�WdQRXdS)Nrrrcr[Trs)rrXrrrtrrr\r]r^r_rr:rr<r`)r#r
rur$rorarvrrr�test_package_compiledAs

	z!CmdLineTest.test_package_compiledcCsatj��N}tjj|d�}t|�d}t|dd�}|j||�WdQRXdS)Nrrz7'test_pkg' is a package and cannot be directly executedrs)rrXrrrtrrr;)r#r
rurgrvrrr�test_package_errorNs
zCmdLineTest.test_package_errorcCs�tj��m}tjj|d�}t|�tjj|d�}t|�d}t|dd�}|j||�WdQRXdS)Nrrrcz^Cannot use package as __main__ module; 'test_pkg' is a package and cannot be directly executedrs)rrXrrrtrrr;)r#r
ruZmain_dirrgrvrrr�test_package_recursionWs

z"CmdLineTest.test_package_recursioncCs�tj���}tjd|���tjj|d�}t|d�t|d�}tddt	dd�\}}}t
d	kr�tt|��d
}|j
|jd�|�|j|||||ddtjj�WdQRXWdQRXdS)Nrrrz1import sys; print('init_argv0==%r' % sys.argv[0])rWz-mztest_pkg.scriptr.Frzinit_argv0==%rzutf-8rdzinit_argv0=='-m')rrX�
change_cwdrrrtrrrr4rrrr!r"r+rr<rY)r#r
rur$r7r8r9r>rrr�test_issue8202cs
!zCmdLineTest.test_issue8202cCs�tj���}tjd|���tdd��m}|jd�tdddd�\}}}tdkr{tt|��d}|j	|j
d�|�WdQRXWdQRXWdQRXdS)
Nrz-c�wr%z2import sys; print("sys.path[0]==%r" % sys.path[0])r.Frzsys.path[0]==%rrdzutf-8zsys.path[0]=='')rrXr�openrArrrrr!r")r#r
�fr7r8r9r>rrr�"test_issue8202_dash_c_file_ignoredts
z.CmdLineTest.test_issue8202_dash_c_file_ignoredcCs�tj���}t|d�}tjd|��ttdd��]}|jd�tddtdd�\}}}|j|||||ddt	j
j�WdQRXWdQRXWdQRXdS)	N�otherrz-mr�r%r.Frd)rrXrrr�rArr4r+rr<rY)r#r
r$r�r7r8r9rrr�"test_issue8202_dash_m_file_ignored�s
z.CmdLineTest.test_issue8202_dash_m_file_ignoredcgs[tj��H}tjd|��,tjj|d�}t||�|VWdQRXWdQRXdS)Nrrr)rrXrrrrtr)r#�argsr
rurrr�setup_test_pkg�s

zCmdLineTest.setup_test_pkgcGsNtd|dd�\}}}tdkr:tt|��|j|d�|S)Nz-mr.Fr)rrrrr)r#r�r7r8r9rrr�check_dash_m_failure�s
z CmdLineTest.check_dash_m_failurecCsN|j��;}t|dd�}|jdt�}|jd|�WdQRXdS)Nr�z+if __name__ == '__main__': raise ValueErrorztest_pkg.others
ValueError)r�rr�r4r!)r#rur$r9rrr�test_dash_m_error_code_is_one�s
		z)CmdLineTest.test_dash_m_error_code_is_onecCs�d}xs|D]k\}}|j|��Mtd|�\}}}|j|d�|j||�|jd|�WdQRXq
WdS)N�builtins�No code object available�
builtins.x�8Error while finding module specification.*AttributeError�builtins.x.y�UError while finding module specification.*ImportError.*No module named.*not a package�os.path�loader.*cannot handler�=No module named.*is a package and cannot be directly executed�importlib.nonexistant�No module named�	.unittest�#Relative module names not supportedz-mrs	Traceback�r�r��r�r��r�r��r�r��rr��r�r��r�r�)r�r�r�r�r�r�r�)�subTestrr�assertRegex�assertNotIn)r#Ztests�nameZregexr7�_r9rrr�test_dash_m_errors�szCmdLineTest.test_dash_m_errorscCs�tj���}tjd|��htjd�tdd��WdQRX|jd�}|j|d�|jd|�|jd|�WdQRXWdQRXdS)Nrrrztest_pkg/__init__.pyc�wbsGError while finding module specification.*ImportError.*bad magic numbersis a packages	Traceback)	rrXrr�mkdirr�r�r�r�)r#r
r9rrr�test_dash_m_bad_pyc�s
	zCmdLineTest.test_dash_m_bad_pyccCs�ttttf}x�|D]�}|j}dj|�}|j|��f|j|��O}|jd�}|j	|j
d�|�|j	d|�|j	d|�WdQRXWdQRXqWdS)Nz%raise {0}('Exception in __init__.py')rr�asciisException in __init__.pys	Traceback)�ImportError�AttributeError�	TypeError�
ValueError�__name__�formatr�r�r�r!r")r#�
exceptionsZ	exceptionZinitrur9rrr�test_dash_m_init_traceback�s
	z&CmdLineTest.test_dash_m_init_tracebackcCso|j��\}d}t|d|�|jd�}|jd|�|jd|�|jd|�WdQRXdS)Nz1raise ImportError('Exception in __main__ module')rcrrsImportErrorsException in __main__ modules	Traceback)r�rr�r!)r#ru�mainr9rrr�test_dash_m_main_traceback�sz&CmdLineTest.test_dash_m_main_tracebackcCs�tjd�}tj���}t|d|�}t|�\}}}|jd�jd�}|jt	|�d�|j
|djd��|j
|djd	��|j
|d
jd��WdQRXdS)Nz|            try:
                raise ValueError
            except:
                raise NameError from None
            rWr��
rGrZ	Tracebackrz  File ��	NameError)�textwrap�dedentrrXrr�decode�splitr�lenZ
assertTrue�
startswith)r#rWr
r$�exitcoderJrF�textrrr�test_pep_409_verbiage�s	z!CmdLineTest.test_pep_409_verbiagecCs�tjr-tjdkr-tjtj�}n"tjrBtj}n
|jd�d}ttj	||�}|j
tj|�t|�\}}}|j
t|�|j�jd�d||f�|j
d|�dS)	N�win32�darwinzneed support.TESTFN_NONASCIIzprint(ascii(__file__))
r�zstdout=%r stderr=%rr)r�r�)rZTESTFN_UNDECODABLEr0�platformr�fsdecodeZTESTFN_NONASCIIZskipTestr�curdirZ
addCleanup�unlinkrrr��rstripr�)r#r�rr$r7rJrFrrr�test_non_ascii�s		
	zCmdLineTest.test_non_asciicCsotjd�}tj��M}t|d|�}t|�\}}}|jd�}|j|d�WdQRXdS)Nz�            import sys
            error = None
            try:
                raise ValueError('some text')
            except ValueError as err:
                error = err

            if error:
                sys.exit(error)
            rWr�z	some text)r�r�rrXrrr�r)r#rWr
r$r�rJrFr�rrr�)test_issue20500_exit_with_exception_values
	z5CmdLineTest.test_issue20500_exit_with_exception_valuecCsxd}tj��_}t|d|�}t|�\}}}tjtj|�d�j�}|jd|�WdQRXdS)Nz
1 + 1 = 2
rWr�z
    1 + 1 = 2
    ^)	rrXrr�io�
TextIOWrapper�BytesIOrKr!)r#rWr
r$r�rJrFr�rrr�*test_syntaxerror_unindented_caret_position s!z6CmdLineTest.test_syntaxerror_unindented_caret_positioncCs�tjd�}tj���}t|d|�}t|�\}}}tjtj|�d�j	�}|j
d|�d}t|d|�}t|�\}}}tjtj|�d�j	�}|jd|�|j
d|�WdQRXdS)Nz;            if True:
                1 + 1 = 2
            rWr�z
    1 + 1 = 2
    ^zif True:
    1 + 1 = 2
�)r�r�rrXrrr�r�r�rKr!r�)r#rWr
r$r�rJrFr�rrr�(test_syntaxerror_indented_caret_position)s	!!z4CmdLineTest.test_syntaxerror_indented_caret_positionc	Cs(tjd�}d|_tj���}tj���}t|d|�}td|d|�}t|�j�j	�}|j
|d|�|j||�td|d|�}t|�j�j	�}|j
||�td|d|�}t|�j�j	�}|j
|||�WdQRXWdQRXdS)Nzc            import sys
            for entry in sys.path:
                print(entry)
            rcz-Es�cwdrz-I)r�r��maxDiffrrXrr	r
r��
splitlinesrr�)	r#rW�work_dirr
r$rCZout_by_nameZ
out_by_dirZout_by_dir_isolatedrrr�-test_consistent_sys_path_for_direct_execution@s		z9CmdLineTest.test_consistent_sys_path_for_direct_executioncCs8tjd�}d|_tj��
}tjj|d�}tj|�t	|d|�}t
ddd|�}t|�j�j
�}|j|dd�|j||�t
ddd|�}t|�j�j
�}|j||�td	dd|�\}}	}
|
j�j
�}|jd
|d�WdQRXdS)
Nzc            import sys
            for entry in sys.path:
                print(entry)
            Z
script_pkgrcz-smzscript_pkg.__main__r�rrdz-ImzNo module named script_pkgr���)r�r�r�rrXrrrtr�rr	r
r�r�rr�rr!)r#rWr�r
r$rCZ
out_by_moduleZout_by_packager�rJrFZtraceback_linesrrr�-test_consistent_sys_path_for_module_execution_s$		
z9CmdLineTest.test_consistent_sys_path_for_module_executionN)1r��
__module__�__qualname__r+r:r;r?rD�
contextlib�contextmanagerrOrQrRrSrTrUrVrZrbrerfrhrnrprqrwrxrzr{r|r}r~r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrrnsZ

	




	

	rcCstjt�tj�dS)N)rZrun_unittestrZ
reap_childrenrrrr�	test_main�s
r�rc)"r�r�importlib.machineryrjZunittestr0rZos.pathr\rHr�r�ZtestrZtest.support.script_helperrrrrrrr	r
rr4Ztest_sourcerrrrZTestCaserr�r�rrrr�<module>s4:	5
��