o
    g                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlmZ d dlmZ d dlmZ d dlmZmZmZmZmZmZmZmZ ejZg dZdZefdd	Zed
fddZG dd dejZ dd Z!e"dkre!  dS dS )    N)support)import_helper)	os_helper)make_pkgmake_scriptmake_zip_pkgmake_zip_scriptassert_python_okassert_python_failurespawn_pythonkill_python)Ztest1Ztest2Ztest3a  # 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())
c                 C   s   t | ||}t  |S N)r   	importlibinvalidate_caches)
script_dirscript_basenamesource	to_return r   O/Users/admin/.pyenv/versions/3.10.0/lib/python3.10/test/test_cmd_line_script.py_make_test_scriptP   s   r      c                 C   s   t | |||||}t  |S r   )r   r   r   )Zzip_dirZzip_basenamepkg_namer   r   depthr   r   r   r   _make_test_zip_pkgU   s
   
r   c                   @   s  e Zd Z	dlddZddddZddddZd	d
 Zdd Zej	dmddZ
dmddZdmddZdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Z d<d= Z!d>d? Z"d@dA Z#dBdC Z$dDdE Z%dFdG Z&ej	dHdI Z'dJdK Z(dLdM Z)dNdO Z*dPdQ Z+dRdS Z,dTdU Z-dVdW Z.dXdY Z/dZd[ Z0d\d] Z1d^d_ Z2d`da Z3dbdc Z4ddde Z5dfdg Z6dhdi Z7djdk Z8dS )nCmdLineTestNc
                 C   s  t dkrtd|  tt| | |d d| }
d| }d| }d| }d| }|	d u r2t }	d	|	 }t dkrNtd
 t| t| t| t| | |
d| | |d| | |d| | |d| | |d| | |d| d S )Nr   zOutput from test script %r:r   z__loader__==%az__file__==%az__package__==%rzsys.argv[0]==%azsys.path[0]==%azcwd==%azExpected output:utf-8)verboseprintreprassertEqualosgetcwdassertInencode)selfscript_nameZ	exit_codedataexpected_fileexpected_argv0expected_path0expected_packageexpected_loaderexpected_cwdZprinted_loaderZprinted_fileZprinted_packageZprinted_argv0Zprinted_path0Zprinted_cwdr   r   r   _check_output]   s0   zCmdLineTest._check_outputcwdc                O   sd   t |tr|g}g t ||t}
t|
d|d|	\}}}| |||| ||||||	 d S )NF
__isolatedZ__cwd)
isinstancestrr   Z!optim_args_from_interpreter_flagsexample_argsr	   r.   )r%   script_exec_argsr(   r)   r*   r+   r,   r0   cmd_line_switchesenv_varsrun_argsrcouterrr   r   r   _check_scriptz   s$   

zCmdLineTest._check_scriptc          
      O   s   t |tr	|f}nt|}|| }t|d|d|\}}}	tdkr6td|  tt|	 td|  | |d|	 d S )NFr1   r   zOutput from test script zExpected output: %rr   )	r3   r4   tupler
   r   r   r   r#   r$   )
r%   r6   Zexpected_msgr0   r7   r8   r9   r:   r;   r<   r   r   r   _check_import_error   s   
zCmdLineTest._check_import_errorc                 C   s2   t dd\}}}ttjjd}| || d S )N-czprint(__loader__)r   )r	   r   r   	machineryBuiltinImporterr$   r#   )r%   r:   r;   r<   expectedr   r   r   test_dash_c_loader   s   zCmdLineTest.test_dash_c_loaderc                 C   sV   t  }z|jd |j  W t|}nt|}w ttjj	d}| 
|| d S )Ns   print(__loader__)
r   )r   stdinwriteflushr   r   r   rA   rB   r$   r#   )r%   pr;   rC   r   r   r   test_stdin_loader   s   zCmdLineTest.test_stdin_loaderFc                 c   s    |rt dtjd}|j}n
t dtjd}|j}z	 |d}|dkr$q)|  q|V  W t| |	  d S t| |	  w )Nz-i)stderrT   s   >>> )
r   
subprocessPIPErJ   STDOUTstdoutreadreadliner   close)r%   separate_stderrrH   rJ   r'   r   r   r   interactive_python   s$   

zCmdLineTest.interactive_pythonc                 C   s\   |  |}|jd |j  | d|j   W d    d S 1 s'w   Y  d S )Ns   print('foo')
s   foo)rT   rE   rF   rG   r    rO   rQ   strip)r%   rS   rH   r   r   r   check_repl_stdout_flush   s
   
"z#CmdLineTest.check_repl_stdout_flushc                 C   s   |  |4}|jd |j  |r|jn|j}| d|  | d|  | d|  W d    d S 1 s<w   Y  d S )Ns   1/0
s
   Traceback s   File "<stdin>"s   ZeroDivisionError)rT   rE   rF   rG   rJ   rO   r#   rQ   )r%   rS   rH   rJ   r   r   r   check_repl_stderr_flush   s   
"z#CmdLineTest.check_repl_stderr_flushc                 C      |    d S r   rV   r%   r   r   r   test_repl_stdout_flush      z"CmdLineTest.test_repl_stdout_flushc                 C      |  d d S NTrY   rZ   r   r   r   &test_repl_stdout_flush_separate_stderr      z2CmdLineTest.test_repl_stdout_flush_separate_stderrc                 C   rX   r   rW   rZ   r   r   r   test_repl_stderr_flush   r\   z"CmdLineTest.test_repl_stderr_flushc                 C   r]   r^   ra   rZ   r   r   r   &test_repl_stderr_flush_separate_stderr   r`   z2CmdLineTest.test_repl_stderr_flush_separate_stderrc              
   C   sT   t  }t|d}| j||||d tjj|d W d    d S 1 s#w   Y  d S )Nscript)r-   r   temp_dirr   r=   r   rA   SourceFileLoaderr%   r   r&   r   r   r   test_basic_script   s   


"zCmdLineTest.test_basic_scriptc              	   C   sp   t  *}| tj|| t|d}tj|}| ||||d t	j
j W d    d S 1 s1w   Y  d S )Nrd   )r   Ztemp_cwd
assertTruer!   pathisabsr   basenamer=   r   rA   rg   )r%   r   r&   Zrelative_namer   r   r   test_script_abspath   s   


"zCmdLineTest.test_script_abspathc              	   C   sr   t  +}t|d}tj|dd t| t|}| 	||||d t
jj W d    d S 1 s2w   Y  d S )Nrd   Tdoraiser   rf   r   
py_compilecompiler!   remover   make_legacy_pycr=   r   rA   SourcelessFileLoaderr%   r   r&   pyc_filer   r   r   test_script_compiled   s   



"z CmdLineTest.test_script_compiledc              	   C   sP   t  }t|d}| ||||dtjj W d    d S 1 s!w   Y  d S )N__main__ re   rh   r   r   r   test_directory   s   


"zCmdLineTest.test_directoryc              	   C   sr   t  +}t|d}tj|dd t| t|}| 	||||dt
jj W d    d S 1 s2w   Y  d S )Nrz   Tro   r{   rq   rw   r   r   r   test_directory_compiled   s   




"z#CmdLineTest.test_directory_compiledc                 C   sB   t  }d| }| || W d    d S 1 sw   Y  d S )N"can't find '__main__' module in %r)r   rf   r?   )r%   r   msgr   r   r   test_directory_error	  s   
"z CmdLineTest.test_directory_errorc              	   C   s^   t  !}t|d}t|d|\}}| ||||dtj W d    d S 1 s(w   Y  d S )Nrz   test_zipr{   )r   rf   r   r   r=   	zipimportzipimporter)r%   r   r&   zip_namerun_namer   r   r   test_zipfile  s   

"zCmdLineTest.test_zipfilec              	   C   r   t  +}t|d}tj|dtjjd}t|d|\}}| ||||dt	j
 W d    d S 1 s2w   Y  d S Nrz   T)rp   invalidation_moder   r{   )r   rf   r   rr   rs   PycInvalidationMode	TIMESTAMPr   r=   r   r   r%   r   r&   compiled_namer   r   r   r   r   test_zipfile_compiled_timestamp     

"z+CmdLineTest.test_zipfile_compiled_timestampc              	   C   r   r   )r   rf   r   rr   rs   r   CHECKED_HASHr   r=   r   r   r   r   r   r   "test_zipfile_compiled_checked_hash  r   z.CmdLineTest.test_zipfile_compiled_checked_hashc              	   C   r   r   )r   rf   r   rr   rs   r   UNCHECKED_HASHr   r=   r   r   r   r   r   r   $test_zipfile_compiled_unchecked_hash)  r   z0CmdLineTest.test_zipfile_compiled_unchecked_hashc                 C   s\   t   }t|d}t|d|\}}d| }| || W d    d S 1 s'w   Y  d S )NZnot_mainr   r~   )r   rf   r   r   r?   )r%   r   r&   r   r   r   r   r   r   test_zipfile_error3  s   

"zCmdLineTest.test_zipfile_errorc              
   C   sn   t  )}tj|d}t| t|d}| jddg|||dtj	j
|d W d    d S 1 s0w   Y  d S )Ntest_pkgrd   -mtest_pkg.scriptr/   r   rf   r!   rk   joinr   r   r=   r   rA   rg   r%   r   pkg_dirr&   r   r   r   test_module_in_package:  s   

"z"CmdLineTest.test_module_in_packagec                 C   s`   t  "}t|ddd\}}| jddg|||dtj||d W d    d S 1 s)w   Y  d S )Nr   r   rd   r   r   Z
PYTHONPATHr0   r   rf   r   r=   r   r   r%   r   r   r   r   r   r   !test_module_in_package_in_zipfileD  s   
"z-CmdLineTest.test_module_in_package_in_zipfilec                 C   sd   t  $}t|ddddd\}}| jddg|||dtj||d	 W d    d S 1 s+w   Y  d S )
Nr   r   rd      )r   r   ztest_pkg.test_pkg.scriptztest_pkg.test_pkgr   r   r   r   r   r   $test_module_in_subpackage_in_zipfileK  s   
"z0CmdLineTest.test_module_in_subpackage_in_zipfilec              
   C   sn   t  )}tj|d}t| t|d}| jddg|||dtj	j
|d W d    d S 1 s0w   Y  d S )Nr   rz   r   r/   r   r   r   r   r   test_packageS  s   

"zCmdLineTest.test_packagec              
   C   s   t  :}tj|d}t| t|d}tj|dd}t	| t
|}| jddg|||dtjj|d W d    d S 1 sAw   Y  d S )Nr   rz   Tro   r   r/   )r   rf   r!   rk   r   r   r   rr   rs   rt   r   ru   r=   r   rA   rv   )r%   r   r   r&   r   rx   r   r   r   test_package_compiled]  s   



"z!CmdLineTest.test_package_compiledc                 C   s\   t   }tj|d}t| d}| jddg||d W d    d S 1 s'w   Y  d S )Nr   z7'test_pkg' is a package and cannot be directly executedr   r/   r   rf   r!   rk   r   r   r?   )r%   r   r   r   r   r   r   test_package_errorj  s   
"zCmdLineTest.test_package_errorc                 C   sr   t  +}tj|d}t| tj|d}t| d}| jddg||d W d    d S 1 s2w   Y  d S )Nr   rz   z^Cannot use package as __main__ module; 'test_pkg' is a package and cannot be directly executedr   r/   r   )r%   r   r   Zmain_dirr   r   r   r   test_package_recursionr  s   
"z"CmdLineTest.test_package_recursionc                 C   s   t  l}t j|dM tj|d}t|d t|d}tddgt	R ddi\}}}t
d	kr6tt| d
d }| |d| | ||||||dtjj W d    n1 s[w   Y  W d    d S W d    d S 1 ssw   Y  d S )Nrk   r   z1import sys; print('init_argv0==%r' % sys.argv[0])rd   r   r   r2   Fr   zinit_argv0==%rr   )r   rf   
change_cwdr!   rk   r   r   r   r	   r5   r   r   r   r#   r$   r.   r   rA   rg   )r%   r   r   r&   r:   r;   r<   rC   r   r   r   test_issue8202}  s$   



"zCmdLineTest.test_issue8202c              
   C   s   t  d}t j|dE tdddd.}|d tdddd	\}}}td
kr-tt| dd }| 	|
d| W d    n1 sDw   Y  W d    n1 sSw   Y  W d    d S W d    d S 1 skw   Y  d S )Nr   r@   wr   encodingr'   z2import sys; print("sys.path[0]==%r" % sys.path[0])F)r2   r   zsys.path[0]==%rr{   )r   rf   r   openrF   r	   r   r   r   r#   r$   )r%   r   fr:   r;   r<   rC   r   r   r   "test_issue8202_dash_c_file_ignored  s$   

"z.CmdLineTest.test_issue8202_dash_c_file_ignoredc                 C   s   t  e}t|d}t j|dA tdddd*}|d tddgtR dd	i\}}}| ||||||d
t	j
j W d    n1 sEw   Y  W d    n1 sTw   Y  W d    d S W d    d S 1 slw   Y  d S )Notherr   r   r   r   r   r'   r2   Fr{   )r   rf   r   r   r   rF   r	   r5   r.   r   rA   rg   )r%   r   r&   r   r:   r;   r<   r   r   r   "test_issue8202_dash_m_file_ignored  s$   



"z.CmdLineTest.test_issue8202_dash_m_file_ignoredc              	   C   s   t  l}tj|d}t|dddd#}|d |d tdD ]}|d	 q#|d W d    n1 s:w   Y  t j|d
 t	|\}}}W d    n1 sWw   Y  | 
d| | 
d| W d    d S 1 ssw   Y  d S )Nzissue20884.pyr   latin1
)r   newlinez#coding: iso-8859-1
z"""
   zQxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
r       )r   rf   r!   rk   r   r   rF   ranger   r	   r    )r%   r   r&   r   _r:   r;   r<   r   r   r   test_issue20884  s   


"zCmdLineTest.test_issue20884c              	   g   s    t  9}t j|d tj|d}t|g|R   |V  W d    n1 s)w   Y  W d    d S W d    d S 1 sAw   Y  d S )Nr   r   )r   rf   r   r!   rk   r   r   )r%   argsr   r   r   r   r   setup_test_pkg  s   

PzCmdLineTest.setup_test_pkgc                 G   s@   t dg|R ddi\}}}tdkrtt| | |d |S )Nr   r2   Fr   )r
   r   r   r   r    )r%   r   r:   r;   r<   r   r   r   check_dash_m_failure  s
   z CmdLineTest.check_dash_m_failurec                 C   sX   |   }t|dd}| jdgtR  }| d| W d    d S 1 s%w   Y  d S )Nr   z+if __name__ == '__main__': raise ValueErrorztest_pkg.others
   ValueError)r   r   r   r5   r#   )r%   r   r&   r<   r   r   r   test_dash_m_error_code_is_one  s   
"z)CmdLineTest.test_dash_m_error_code_is_onec              	   C   st   d}|D ]3\}}|  |" td|\}}}| |d | || | d| W d    q1 s2w   Y  qd S )N))builtinss   No code object available)z
builtins.xs=   Error while finding module specification.*ModuleNotFoundError)zbuiltins.x.ys]   Error while finding module specification.*ModuleNotFoundError.*No module named.*not a package)zos.paths   loader.*cannot handle)r   s=   No module named.*is a package and cannot be directly executed)zimportlib.nonexistents   No module named)z	.unittests#   Relative module names not supportedr   r   	   Traceback)subTestr
   r    assertRegexassertNotIn)r%   ZtestsnameZregexr:   r   r<   r   r   r   test_dash_m_errors  s   zCmdLineTest.test_dash_m_errorsc              
   C   s   t  X}t j|d9 td tdd W d    n1 s!w   Y  | d}| |d | d| | d| W d    n1 sGw   Y  W d    d S W d    d S 1 s_w   Y  d S )Nr   r   ztest_pkg/__init__.pycwbsG   Error while finding module specification.*ImportError.*bad magic numbers   is a packager   )	r   rf   r   r!   mkdirr   r   r   r   r%   r   r<   r   r   r   test_dash_m_bad_pyc  s   



PzCmdLineTest.test_dash_m_bad_pycc              
   C   s   t  G}t j|d( tdd W d    n1 sw   Y  | d}| d| W d    n1 s6w   Y  W d    d S W d    d S 1 sNw   Y  d S )Nr   z
asyncio.pyr   s>   Try using 'asyncio' instead of 'asyncio.py' as the module name)r   rf   r   r   r   r#   r   r   r   r   *test_hint_when_triying_to_import_a_py_file  s   


Pz6CmdLineTest.test_hint_when_triying_to_import_a_py_filec              
   C   s   t tttf}|D ]N}|j}d|}| |7 | |"}| d}| 	|
d| | 	d| | 	d| W d    n1 sBw   Y  W d    q1 sQw   Y  qd S )Nz%raise {0}('Exception in __init__.py')r   asciis   Exception in __init__.pyr   )ImportErrorAttributeError	TypeError
ValueError__name__formatr   r   r   r#   r$   )r%   
exceptions	exceptionZinitr   r<   r   r   r   test_dash_m_init_traceback  s    

 z&CmdLineTest.test_dash_m_init_tracebackc                 C   sl   |   (}d}t|d| | d}| d| | d| | d| W d    d S 1 s/w   Y  d S )Nz1raise ImportError('Exception in __main__ module')rz   r   s   ImportErrors   Exception in __main__ moduler   )r   r   r   r#   )r%   r   mainr<   r   r   r   test_dash_m_main_traceback  s   

"z&CmdLineTest.test_dash_m_main_tracebackc                 C   s   t d}t D}t|d|}t|\}}}|dd}| t	|d | 
|d d | 
|d d	 | 
|d
 d W d    d S 1 sPw   Y  d S )Nz|            try:
                raise ValueError
            except:
                raise NameError from None
            rd   r   r      r   Z	Tracebackr   z  File    	NameError)textwrapdedentr   rf   r   r
   decodesplitr    lenrj   
startswithr%   rd   r   r&   exitcoderO   rJ   textr   r   r   test_pep_409_verbiage  s   

"z!CmdLineTest.test_pep_409_verbiagec                 C   s   t jrtjdvrtt j}nt jrt j}n| d d}tt	 ||}| 
t j| t|\}}}| t|| dd||f  | d| d S )N)win32darwinzneed os_helper.TESTFN_NONASCIIzprint(ascii(__file__))
r   zstdout=%r stderr=%rr   )r   ZTESTFN_UNDECODABLEsysplatformr!   fsdecodeZTESTFN_NONASCIIZskipTestr   r"   Z
addCleanupunlinkr	   r    r   rstripr   )r%   r   r   r&   r:   rO   rJ   r   r   r   test_non_ascii,  s"   

zCmdLineTest.test_non_asciic                 C   sl   t d}t #}t|d|}t|\}}}|d}| | d W d    d S 1 s/w   Y  d S )Nz            import sys
            error = None
            try:
                raise ValueError('some text')
            except ValueError as err:
                error = err

            if error:
                sys.exit(error)
            rd   r   z	some text)	r   r   r   rf   r   r
   r   r    r   r   r   r   r   )test_issue20500_exit_with_exception_valueC  s   


"z5CmdLineTest.test_issue20500_exit_with_exception_valuec                 C   sn   d}t  '}t|d|}t|\}}}tt|d }| d| W d    d S 1 s0w   Y  d S )Nz
1 + 1 = 2
rd   r   z
    ^^^^^
)	r   rf   r   r
   ioTextIOWrapperBytesIOrP   r#   r   r   r   r   *test_syntaxerror_unindented_caret_positionU  s   
"z6CmdLineTest.test_syntaxerror_unindented_caret_positionc                 C   s   t d}t M}t|d|}t|\}}}tt|d	 }| 
d| d}t|d|}t|\}}}tt|d	 }| d| | 
d| W d    d S 1 sYw   Y  d S )Nz;            if True:
                1 + 1 = 2
            rd   r   z
    1 + 1 = 2
    ^^^^^
zif True:
    1 + 1 = 2
)r   r   r   rf   r   r
   r   r   r   rP   r#   r   r   r   r   r   (test_syntaxerror_indented_caret_position^  s   

"z4CmdLineTest.test_syntaxerror_indented_caret_positionc                 C   sh   d}t  $}t|d|}t|\}}}| | dd  g d W d    d S 1 s-w   Y  d S )Nzfoo = f"""{}
foo"""
rd   )s
       foo"""s             ^s3   SyntaxError: f-string: empty expression not allowedr   rf   r   r
   r    
splitlinesr%   rd   r   r&   r   rO   rJ   r   r   r   #test_syntaxerror_multi_line_fstringu  s   
"z/CmdLineTest.test_syntaxerror_multi_line_fstringc                 C   sj   d}t  %}t|d|}td|\}}}| | dd  g d W d    d S 1 s.w   Y  d S )Nzfoo = """\q"""
rd   z-Werrorr   )s       foo = """\q"""s             ^^^^^^^^s)   SyntaxError: invalid escape sequence '\q'r   r   r   r   r   3test_syntaxerror_invalid_escape_sequence_multi_line  s   

"z?CmdLineTest.test_syntaxerror_invalid_escape_sequence_multi_linec           	   	   C   s  t d}d | _t s}t V}t|d|}td||d}t| 	 }| 
|d | | || td||d}t| 	 }| 
|| td||d}t| 	 }| 
||| W d    n1 sjw   Y  W d    d S W d    d S 1 sw   Y  d S )Nc            import sys
            for entry in sys.path:
                print(entry)
            rz   z-Esr/   r   z-I)r   r   maxDiffr   rf   r   r   r   r   r   r    r   )	r%   rd   work_dirr   r&   rH   Zout_by_nameZ
out_by_dirZout_by_dir_isolatedr   r   r   -test_consistent_sys_path_for_direct_execution  s   
Pz9CmdLineTest.test_consistent_sys_path_for_direct_executionc                 C   s   t d}d | _t e}tj|d}t| t	|d|}t
dd|d}t|  }| |d | | || t
dd|d}t|  }| || tdd|d\}}	}
|
  }| d	|d
  W d    d S 1 stw   Y  d S )Nr   Z
script_pkgrz   z-smzscript_pkg.__main__r/   r   z-ImzNo module named script_pkg)r   r   r   r   rf   r!   rk   r   r   r   r   r   r   r   r    r   r
   r#   )r%   rd   r   r   r&   rH   Zout_by_moduleZout_by_packager   rO   rJ   Ztraceback_linesr   r   r   -test_consistent_sys_path_for_module_execution  s&   


"z9CmdLineTest.test_consistent_sys_path_for_module_executionc                 C   sT   d}|  tj| t|dtjtjd}| \}}| d| | 	|j
d d S )Nznonexistingscript.pyT)r   rO   rJ   z: can't open file r   )ZassertFalser!   rk   existsr   rL   rM   communicater#   ZassertNotEqual
returncode)r%   rd   procr;   r<   r   r   r   test_nonexisting_script  s   z#CmdLineTest.test_nonexisting_scriptr   )F)9r   
__module____qualname__r.   r=   r?   rD   rI   
contextlibcontextmanagerrT   rV   rW   r[   r_   rb   rc   ri   rn   ry   r|   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r   r   r   r   \   st    
 

	










	%r   c                   C   s   t t t   d S r   )r   Zrun_unittestr   reap_childrenr   r   r   r   	test_main  s   
r	  rz   )#r  r   importlib.machineryr   Zunittestr   r!   Zos.pathrr   rL   r   r   testr   Ztest.supportr   r   Ztest.support.script_helperr   r   r   r   r	   r
   r   r   r   r5   Ztest_sourcer   r   ZTestCaser   r	  r   r   r   r   r   <module>   s@   (5
     
