Your IP : 216.73.216.213


Current Path : /opt/alt/python34/lib64/python3.4/test/__pycache__/
Upload File :
Current File : //opt/alt/python34/lib64/python3.4/test/__pycache__/test_cmd_line_script.cpython-34.pyc

�
f f�P�@sgddlZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlmZddl
mZmZmZmZmZmZmZmZmZejZdddgZdZedd	�Zed
dd�Zd
Zddd�ZGdd�dej�Zdd�Z e!dkrce �ndS)�N)�support)	�make_pkg�make_script�make_zip_pkg�make_zip_script�assert_python_ok�assert_python_failure�temp_dir�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/python34/lib64/python3.4/test/test_cmd_line_script.py�_make_test_scriptNs
r�cCs)t||||||�}tj�|S)N)rrr
)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_sourcerrr
)rrZmodule_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"d>d?�Z#d@dA�Z$dBdC�Z%dDdE�Z&dFS)G�CmdLineTestc	CsZtdkr-td|�tt|��n|j|d�d|}	d|}
d|}d|}d|}
d	tj�}tdkr�td
�t|
�t|�t|�t|�n|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}n||ftt�}tdd|�\}	}
}|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_scriptcGsz||f}t|�\}}}tdkr]td|�tt|��td|�n|j|jd�|�dS)NrzOutput from test script %r:zExpected output: %rzutf-8)rrrrr"r#)r$r%Zexpected_msgr6r7r8r9r:rrr�_check_import_error�s
zCmdLineTest._check_import_errorcCsGtdd�\}}}ttjj�jd�}|j||�dS)Nz-czprint(__loader__)zutf-8)rrr�	machinery�BuiltinImporterr#r")r$r8r9r:�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�flushrrrr=r>r#r")r$�pr9r?rrr�test_stdin_loader�s	
zCmdLineTest.test_stdin_loaderFccs�|r-tddddtj�}|j}n$tddddtj�}|j}z9x-|jd�}|dkrvPn|j�qWW|VWdt|�|j	�XdS)Nz-i�bufsizer�stderr�s>>> )
r
�
subprocess�PIPErGZSTDOUT�stdout�read�readliner�close)r$�separate_stderrrDrGr&rrr�interactive_python�s		
zCmdLineTest.interactive_pythonc
CsX|j|��C}|jjd�|jj�|jd|jj�j��WdQXdS)Ns
print('foo')
sfoo)rPrArBrCrrKrM�strip)r$rOrDrrr�check_repl_stdout_flush�s
z#CmdLineTest.check_repl_stdout_flushc
Cs�|j|��~}|jjd�|jj�|r>|jn|j}|jd|j��|jd|j��|jd|j��WdQXdS)Ns1/0
s
Traceback sFile "<stdin>"sZeroDivisionError)rPrArBrCrGrKr"rM)r$rOrDrGrrr�check_repl_stderr_flush�s
z#CmdLineTest.check_repl_stderr_flushcCs|j�dS)N)rR)r$rrr�test_repl_stdout_flush�sz"CmdLineTest.test_repl_stdout_flushcCs|jd�dS)NT)rR)r$rrr�&test_repl_stdout_flush_separate_stderr�sz2CmdLineTest.test_repl_stdout_flush_separate_stderrcCs|j�dS)N)rS)r$rrr�test_repl_stderr_flush�sz"CmdLineTest.test_repl_stderr_flushcCs|jd�dS)NT)rS)r$rrr�&test_repl_stderr_flush_separate_stderr�sz2CmdLineTest.test_repl_stderr_flush_separate_stderrcCsGt��8}t|d�}|j||||dtjj�WdQXdS)N�script)r	rr;rr=�SourceFileLoader)r$rr%rrr�test_basic_script�s
zCmdLineTest.test_basic_scriptcCsvt��g}t|d�}tj|dd�tj|�tj|�}|j||||dt	j
j�WdQXdS)NrX�doraiseT)r	r�
py_compile�compiler �remover�make_legacy_pycr;rr=�SourcelessFileLoader)r$rr%�pyc_filerrr�test_script_compiled�s
	z CmdLineTest.test_script_compiledcCsGt��8}t|d�}|j||||dtjj�WdQXdS)N�__main__�)r	rr;rr=rY)r$rr%rrr�test_directory�s
zCmdLineTest.test_directorycCsvt��g}t|d�}tj|dd�tj|�tj|�}|j||||dt	j
j�WdQXdS)Nrcr[Trd)r	rr\r]r r^rr_r;rr=r`)r$rr%rarrr�test_directory_compiled�s
z#CmdLineTest.test_directory_compiledc
Cs0t��!}d|}|j||�WdQXdS)Nz"can't find '__main__' module in %r)r	r<)r$r�msgrrr�test_directory_errors
z CmdLineTest.test_directory_errorcCs\t��M}t|d�}t|d|�\}}|j||||dtj�WdQXdS)Nrc�test_ziprd)r	rrr;�	zipimport�zipimporter)r$rr%�zip_name�run_namerrr�test_zipfiles
zCmdLineTest.test_zipfilecCsqt��b}t|d�}tj|dd�}t|d|�\}}|j||||dtj�WdQXdS)Nrcr[Trird)r	rr\r]rr;rjrk)r$rr%�
compiled_namerlrmrrr�test_zipfile_compiledsz!CmdLineTest.test_zipfile_compiledcCsWt��H}t|d�}t|d|�\}}d|}|j||�WdQXdS)NZnot_mainriz"can't find '__main__' module in %r)r	rrr<)r$rr%rlrmrgrrr�test_zipfile_errors

zCmdLineTest.test_zipfile_errorcCsxt��i}tjj|d�}t|�t|d�}t|dd�}|j||||dtj	j
�WdQXdS)N�test_pkgrX�launchztest_pkg.script)r	r r�joinrrrr;rr=rY)r$r�pkg_dirr%�launch_namerrr�test_module_in_packages
z"CmdLineTest.test_module_in_packagecCset��V}t|ddd�\}}t|dd|�}|j||||dtj�WdQXdS)NrirrrXrsztest_pkg.script)r	rrr;rjrk)r$rrlrmrvrrr�!test_module_in_package_in_zipfile(s
z-CmdLineTest.test_module_in_package_in_zipfilecCskt��\}t|ddddd�\}}t|dd|�}|j||||dtj�WdQXdS)	NrirrrXr�rsztest_pkg.test_pkg.scriptztest_pkg.test_pkg)r	rrr;rjrk)r$rrlrmrvrrr�$test_module_in_subpackage_in_zipfile/s!z0CmdLineTest.test_module_in_subpackage_in_zipfilecCsxt��i}tjj|d�}t|�t|d�}t|dd�}|j||||dtj	j
�WdQXdS)Nrrrcrs)r	r rrtrrrr;rr=rY)r$rrur%rvrrr�test_package7s
	zCmdLineTest.test_packagecCs�t���}tjj|d�}t|�t|d�}tj|dd�}tj|�t	j
|�}t|dd�}|j||||dt
jj�WdQXdS)Nrrrcr[Trs)r	r rrtrrr\r]r^rr_rr;rr=r`)r$rrur%rorarvrrr�test_package_compiledAs

	z!CmdLineTest.test_package_compiledcCs]t��N}tjj|d�}t|�d}t|dd�}|j||�WdQXdS)Nrrz7'test_pkg' is a package and cannot be directly executedrs)r	r rrtrrr<)r$rrurgrvrrr�test_package_errorNs
zCmdLineTest.test_package_errorcCs|t��m}tjj|d�}t|�tjj|d�}t|�d}t|dd�}|j||�WdQXdS)Nrrrcz^Cannot use package as __main__ module; 'test_pkg' is a package and cannot be directly executedrs)r	r rrtrrr<)r$rruZmain_dirrgrvrrr�test_package_recursionWs

z"CmdLineTest.test_package_recursioncCs�t���}tjd|���tjj|d�}t|d�t|d�}tddddt	�\}}}t
d	kr�tt|��nd
}|j
|jd�|�|j|||||ddtjj�WdQXWdQXdS)Nrrrz1import sys; print('init_argv0==%r' % sys.argv[0])rXz-mztest_pkg.scriptr/Frzinit_argv0==%rzutf-8rdzinit_argv0=='-m')r	r�
change_cwdr rrtrrrr5rrrr"r#r,rr=rY)r$rrur%r8r9r:r?rrr�test_issue8202cs
!zCmdLineTest.test_issue8202cCs�t���}tjd|���tdd��p}|jd�tdddd�\}}}tdkr{tt|��nd}|j	|j
d�|�WdQXWdQXWdQXdS)
Nrz-c�wr&z2import sys; print("sys.path[0]==%r" % sys.path[0])r/Frzsys.path[0]==%rrdzutf-8zsys.path[0]=='')r	rr�openrBrrrrr"r#)r$r�fr8r9r:r?rrr�"test_issue8202_dash_c_file_ignoredts
z.CmdLineTest.test_issue8202_dash_c_file_ignoredcCs�t���}t|d�}tjd|��stdd��]}|jd�tddddt�\}}}|j|||||ddt	j
j�WdQXWdQXWdQXdS)	N�otherrz-mr�r&r/Frd)r	rrrr�rBrr5r,rr=rY)r$rr%r�r8r9r:rrr�"test_issue8202_dash_m_file_ignored�s
z.CmdLineTest.test_issue8202_dash_m_file_ignoredcCs�t���}tjd|���tjj|d�}t|�t|dd�}tddt	�\}}}t
dkr�tt|��n|j
|d�WdQXWdQXdS)Nrrrr�z+if __name__ == '__main__': raise ValueErrorz-mztest_pkg.otherr)r	rrr rrtrrrr5rrrr)r$rrur%r8r9r:rrr�test_dash_m_error_code_is_one�s
		z)CmdLineTest.test_dash_m_error_code_is_onecCs�tjd�}t���}t|d|�}t|�\}}}|jd�jd�}|jt|�d�|j	|dj
d��|j	|dj
d	��|j	|d
j
d��WdQXdS)Nz|            try:
                raise ValueError
            except:
                raise NameError from None
            rX�ascii�
rHrZ	Tracebackrz  File ��	NameError)�textwrap�dedentr	rr�decode�splitr�lenZ
assertTrue�
startswith)r$rXrr%�exitcoderKrG�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)zwin32zdarwin)rZTESTFN_UNDECODABLEr1�platformr �fsdecodeZTESTFN_NONASCIIZskipTestr�curdirZ
addCleanup�unlinkrrr��rstripr�)r$�namerr%r8rKrGrrr�test_non_ascii�s		
	zCmdLineTest.test_non_asciicCsktjd�}t��M}t|d|�}t|�\}}}|jd�}|j|d�WdQXdS)Nz�            import sys
            error = None
            try:
                raise ValueError('some text')
            except ValueError as err:
                error = err

            if error:
                sys.exit(error)
            rXr�z	some text)r�r�r	rrr�r)r$rXrr%r�rKrGr�rrr�)test_issue20500_exit_with_exception_value�s
	z5CmdLineTest.test_issue20500_exit_with_exception_valueN)'�__name__�
__module__�__qualname__r,r;r<r@rE�
contextlib�contextmanagerrPrRrSrTrUrVrWrZrbrerfrhrnrprqrwrxrzr{r|r}r~r�r�r�r�r�r�r�rrrrrnsF

	




	rcCstjt�tj�dS)N)rZrun_unittestrZ
reap_childrenrrrr�	test_main�s
r�rc)"r�r�importlib.machineryrjZunittestr1r Zos.pathr\rIr�ZtestrZtest.script_helperrrrrrrr	r
rrr5Ztest_sourcerrrrZTestCaserr�r�rrrr�<module>s0@	5
�o