
    ;e                     <   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Zed	fd
Z G d dej                  Z d Z!e"dk    r ej#                     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)test1test2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                 N    t          | ||          }t          j                     |S N)r   	importlibinvalidate_caches)
script_dirscript_basenamesource	to_returns       0/usr/lib/python3.11/test/test_cmd_line_script.py_make_test_scriptr   P   s(    J@@I!!!       c                 T    t          | |||||          }t          j                     |S r   )r   r   r   )zip_dirzip_basenamepkg_namer   r   depthr   s          r   _make_test_zip_pkgr"   U   s3    WlHo#U, ,I!!!r   c                      e Zd Z	 dAdZdddZdddZd Zd Zej	        dBd	            Z
dBd
ZdBdZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d  Z!d! Z"d" Z#d# Z$d$ Z%d% Z&ej	        d&             Z'd' Z(d( Z)d) Z*d* Z+d+ Z,d, Z-d- Z.d. Z/d/ Z0d0 Z1d1 Z2d2 Z3d3 Z4d4 Z5d5 Z6d6 Z7d7 Z8d8 Z9d9 Z: e;j<        e=j>        ?                    d:          d;           e;j@        eAjB        C                    d<          o+ e=jD        d=          jE         e=jD        d>          jE        k    d?          d@                         ZFdS )CCmdLineTestNc
                    t           dk    r.t          d|z             t          t          |                     |                     |d           d|z  }
d|z  }d|z  }d|z  }d|z  }|	t	          j                    }	d	|	z  }t           dk    rKt          d
           t          |           t          |           t          |           t          |           |                     |
                    d          |           |                     |                    d          |           |                     |                    d          |           |                     |                    d          |           t          j	        j
        s)|                     |                    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sysflags	safe_path)selfscript_name	exit_codedataexpected_fileexpected_argv0expected_path0expected_packageexpected_loaderexpected_cwdprinted_loaderprinted_fileprinted_packageprinted_argv0printed_path0printed_cwds                   r   _check_outputzCmdLineTest._check_output]   s    Q;;/+=>>>$t**A&&&)O;%5+.>>)N:)N:9;;L,.Q;;$%%%,/"""-   +n++G44d;;;l))'22D999o,,W55t<<<m**733T:::y" 	?MM-..w77>>>k((11488888r   cwdc                    t          |t                    r|g}g t          j                    ||t          }
t          |
d|d|	\  }}}|                     ||||z   ||||||	  	         d S )NF
__isolated__cwd)
isinstancestrr   !optim_args_from_interpreter_flagsexample_argsr
   rB   )r2   script_exec_argsr6   r7   r8   r9   r:   rD   cmd_line_switchesenv_varsrun_argsrcouterrs                 r   _check_scriptzCmdLineTest._check_script|   s     &,, 	2 01JW>@@ J&J)9J<HJ'%s
 
6>
 
C 	+RsM)>+_c	C 	C 	C 	C 	Cr   c                p   t          |t                    r|f}nt          |          }||z   }t          |d|d|\  }}}	t          dk    rAt          d|            t          t          |	                     t          d|z             |                     |                    d          |	           d S )NFrF   r   zOutput from test script zExpected output: %rr&   )	rI   rJ   tupler   r'   r(   r)   r-   r.   )
r2   rM   expected_msgrD   rN   rO   rP   rQ   rR   rS   s
             r   _check_import_errorzCmdLineTest._check_import_error   s    &,, 	7 02$%566$'77,%s
 
6>
 
C Q;;B-=BBBCCC$s))',6777l))'22C88888r   c                     t          dd          \  }}}t          t          j        j                                      d          }|                     ||           d S )N-czprint(__loader__)r&   )r
   r)   r   	machineryBuiltinImporterr.   r-   )r2   rQ   rR   rS   expecteds        r   test_dash_c_loaderzCmdLineTest.test_dash_c_loader   sS    '.ABBC	+;<<CCGLLh$$$$$r   c                 `   t                      }	 |j                            d           |j                                         t	          |          }n# t	          |          }w xY wt          t          j        j                  	                    d          }| 
                    ||           d S )Ns   print(__loader__)
r&   )r   stdinwriteflushr   r)   r   r[   r\   r.   r-   )r2   prR   r]   s       r   test_stdin_loaderzCmdLineTest.test_stdin_loader   s     NN	!GMM0111GMMOOOa..CC+a..C    	+;<<CCGLLh$$$$$s   3A A$Fc              #     K   |r#t          dt          j                  }|j        }n"t          dt          j                  }|j        }	 	 |                    d          }|dk    rn|                                 1|V  t          |           |	                                 d S # t          |           |	                                 w xY w)Nz-i)stderrT   s   >>> )
r   
subprocessPIPErf   STDOUTstdoutreadreadliner   close)r2   separate_stderrrc   rf   r5   s        r   interactive_pythonzCmdLineTest.interactive_python   s       	T*/:::AXFFT**;<<<AXF
	"{{1~~7??!!!	"
 GGGNNNLLNNNNN NNNLLNNNNs   6B& &%Cc                 F   |                      |          5 }|j                            d           |j                                         |                     d|j                                                                                   d d d            d S # 1 swxY w Y   d S )Ns   print('foo')
s   foo)rp   r`   ra   rb   r*   rk   rm   strip)r2   ro   rc   s      r   check_repl_stdout_flushz#CmdLineTest.check_repl_stdout_flush   s    $$_55 	BGMM+,,,GMMOOOVQX%6%6%8%8%>%>%@%@AAA	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	Bs   A3BBBc                    |                      |          5 }|j                            d           |j                                         |r|j        n|j        }|                     d|                                           |                     d|                                           |                     d|                                           d d d            d S # 1 swxY w Y   d S )Ns   1/0
s
   Traceback s   File "<stdin>"s   ZeroDivisionError)rp   r`   ra   rb   rf   rk   r-   rm   )r2   ro   rc   rf   s       r   check_repl_stderr_flushz#CmdLineTest.check_repl_stderr_flush   s   $$_55 	CGMM(###GMMOOO!0>QXXahFMM-):):;;;MM+V__->->???MM.0A0ABBB	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	Cs   B<CC#&C#c                 .    |                                   d S r   rs   r2   s    r   test_repl_stdout_flushz"CmdLineTest.test_repl_stdout_flush       $$&&&&&r   c                 0    |                      d           d S NTrw   rx   s    r   &test_repl_stdout_flush_separate_stderrz2CmdLineTest.test_repl_stdout_flush_separate_stderr       $$T*****r   c                 .    |                                   d S r   ru   rx   s    r   test_repl_stderr_flushz"CmdLineTest.test_repl_stderr_flush   rz   r   c                 0    |                      d           d S r|   r   rx   s    r   &test_repl_stderr_flush_separate_stderrz2CmdLineTest.test_repl_stderr_flush_separate_stderr   r~   r   c           
          t          j                    5 }t          |d          }|                     ||||d t          j        j        |           d d d            d S # 1 swxY w Y   d S )Nscript)r;   r   temp_dirr   rT   r   r[   SourceFileLoaderr2   r   r3   s      r   test_basic_scriptzCmdLineTest.test_basic_script   s    !! 	8Z+JAAK{K)4(2C,6  8 8 8	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8s   <AA!$A!c           	      t   t          j                    5 }|                     t          j                            |          |           t          |d          }t          j                            |          }|                     ||||d t          j
        j                   d d d            d S # 1 swxY w Y   d S )Nr   )r   temp_cwd
assertTruer+   pathisabsr   basenamerT   r   r[   r   )r2   r   r3   relative_names       r   test_script_abspathzCmdLineTest.test_script_abspath   s     !! 	EZOOBGMM*55zBBB+JAAKG,,[99M}k=)4(2CE E E	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	Es   BB--B14B1c           	      L   t          j                    5 }t          |d          }t          j        |d           t          j        |           t          j        |          }| 	                    ||||d t          j        j                   d d d            d S # 1 swxY w Y   d S )Nr   Tdoraiser   r   r   
py_compilecompiler+   remover   make_legacy_pycrT   r   r[   SourcelessFileLoaderr2   r   r3   pyc_files       r   test_script_compiledz CmdLineTest.test_script_compiled   s    !! 	IZ+JAAK{D9999Ik"""$4[AAHx'T(2GI I I	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I   A8BB Bc           	          t          j                    5 }t          |d          }|                     ||||dt          j        j                   d d d            d S # 1 swxY w Y   d S )N__main__ r   r   s      r   test_directoryzCmdLineTest.test_directory   s    !! 	EZ+J
CCKz;
)2(2CE E E	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	Es   :AA"Ac           	      L   t          j                    5 }t          |d          }t          j        |d           t          j        |           t          j        |          }| 	                    ||||dt          j        j                   d d d            d S # 1 swxY w Y   d S )Nr   Tr   r   r   r   s       r   test_directory_compiledz#CmdLineTest.test_directory_compiled  s    !! 	IZ+J
CCK{D9999Ik"""$4[AAHz8Z)2(2GI I I	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	Ir   c                     t          j                    5 }d|z  }|                     ||           d d d            d S # 1 swxY w Y   d S )N"can't find '__main__' module in %r)r   r   rX   )r2   r   msgs      r   test_directory_errorz CmdLineTest.test_directory_error  s    !! 	6Z6CC$$Z555	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6s   =AAc           	          t          j                    5 }t          |d          }t          |d|          \  }}|                     ||||dt
          j                   d d d            d S # 1 swxY w Y   d S )Nr   test_zipr   )r   r   r   r	   rT   	zipimportzipimporter)r2   r   r3   zip_namerun_names        r   test_zipfilezCmdLineTest.test_zipfile  s    !! 	6Z+J
CCK!0Z!U!UHhx8Xr(46 6 6	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6s   A	A**A.1A.c           	      :   t          j                    5 }t          |d          }t          j        |dt          j        j                  }t          |d|          \  }}|                     ||||dt          j
                   d d d            d S # 1 swxY w Y   d S Nr   T)r   invalidation_moder   r   )r   r   r   r   r   PycInvalidationMode	TIMESTAMPr	   rT   r   r   r2   r   r3   compiled_namer   r   s         r   test_zipfile_compiled_timestampz+CmdLineTest.test_zipfile_compiled_timestamp  s    !! 	6Z+J
CCK&.T","@"JL L LM "1Z!W!WHhx8Xr(46 6 6	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6   A/BBBc           	      :   t          j                    5 }t          |d          }t          j        |dt          j        j                  }t          |d|          \  }}|                     ||||dt          j
                   d d d            d S # 1 swxY w Y   d S r   )r   r   r   r   r   r   CHECKED_HASHr	   rT   r   r   r   s         r   "test_zipfile_compiled_checked_hashz.CmdLineTest.test_zipfile_compiled_checked_hash!  s    !! 	6Z+J
CCK&.T","@"MO O OM "1Z!W!WHhx8Xr(46 6 6	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6r   c           	      :   t          j                    5 }t          |d          }t          j        |dt          j        j                  }t          |d|          \  }}|                     ||||dt          j
                   d d d            d S # 1 swxY w Y   d S r   )r   r   r   r   r   r   UNCHECKED_HASHr	   rT   r   r   r   s         r   $test_zipfile_compiled_unchecked_hashz0CmdLineTest.test_zipfile_compiled_unchecked_hash+  s    !! 	6Z+J
CCK&.T","@"OQ Q QM "1Z!W!WHhx8Xr(46 6 6	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6r   c                     t          j                    5 }t          |d          }t          |d|          \  }}d|z  }|                     ||           d d d            d S # 1 swxY w Y   d S )Nnot_mainr   r   )r   r   r   r	   rX   )r2   r   r3   r   r   r   s         r   test_zipfile_errorzCmdLineTest.test_zipfile_error5  s    !! 	4Z+J
CCK!0Z!U!UHh6AC$$Xs333		4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4s   A A!!A%(A%c           
      6   t          j                    5 }t          j                            |d          }t          |           t          |d          }|                     ddg|||dt          j	        j
        |           d d d            d S # 1 swxY w Y   d S )Ntest_pkgr   -mtest_pkg.scriptrC   r   r   r+   r   joinr   r   rT   r   r[   r   r2   r   pkg_dirr3   s       r   test_module_in_packagez"CmdLineTest.test_module_in_package<  s    !! 	/Zgll:z::GW+GX>>K&78+{):(2C#-  / / /		/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/   A-BBBc                     t          j                    5 }t          |ddd          \  }}|                     ddg|||dt          j        ||           d d d            d S # 1 swxY w Y   d S )Nr   r   r   r   r   
PYTHONPATHrD   r   r   r"   rT   r   r   r2   r   r   r   s       r   !test_module_in_package_in_zipfilez-CmdLineTest.test_module_in_package_in_zipfileF  s    !! 	DZ!3J
JX`!a!aHh&78(H):y7L*2
  D D D	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	Ds   ?A  A$'A$c                     t          j                    5 }t          |dddd          \  }}|                     ddg|||dt          j        ||	           d d d            d S # 1 swxY w Y   d S )
Nr   r   r      )r!   r   ztest_pkg.test_pkg.scriptztest_pkg.test_pkgr   r   r   s       r   $test_module_in_subpackage_in_zipfilez0CmdLineTest.test_module_in_subpackage_in_zipfileM  s    !! 	DZ!3J
JX`hi!j!j!jHh&@A8X)+>(4*2
  D D D	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	Ds   AA""A&)A&c           
      6   t          j                    5 }t          j                            |d          }t          |           t          |d          }|                     ddg|||dt          j	        j
        |           d d d            d S # 1 swxY w Y   d S )Nr   r   r   rC   r   r   s       r   test_packagezCmdLineTest.test_packageU  s    !! 	/Zgll:z::GW+GZ@@Kj1;*J
(2C#-  / / /		/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/r   c           
         t          j                    5 }t          j                            |d          }t          |           t          |d          }t          j        |d          }t          j	        |           t          j        |          }|                     ddg|||dt          j        j        |           d d d            d S # 1 swxY w Y   d S )Nr   r   Tr   r   rC   )r   r   r+   r   r   r   r   r   r   r   r   r   rT   r   r[   r   )r2   r   r   r3   r   r   s         r   test_package_compiledz!CmdLineTest.test_package_compiled_  s   !! 
	/Zgll:z::GW+GZ@@K&.{DIIIMIk"""$4[AAHj18'Z(2G#-  / / /
	/ 
	/ 
	/ 
	/ 
	/ 
	/ 
	/ 
	/ 
	/ 
	/ 
	/ 
	/ 
	/ 
	/ 
	/ 
	/ 
	/ 
	/s   B+CCCc                     t          j                    5 }t          j                            |d          }t          |           d}|                     ddg||           d d d            d S # 1 swxY w Y   d S )Nr   z7'test_pkg' is a package and cannot be directly executedr   rC   r   r   r+   r   r   r   rX   )r2   r   r   r   s       r   test_package_errorzCmdLineTest.test_package_errorl  s    !! 	NZgll:z::GW*C$$dJ%7*$MMM	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	Ns   AA--A14A1c                 R   t          j                    5 }t          j                            |d          }t          |           t          j                            |d          }t          |           d}|                     ddg||           d d d            d S # 1 swxY w Y   d S )Nr   r   z^Cannot use package as __main__ module; 'test_pkg' is a package and cannot be directly executedr   rC   r   )r2   r   r   main_dirr   s        r   test_package_recursionz"CmdLineTest.test_package_recursiont  s    !! 	NZgll:z::GWw||GZ88HX*C $$dJ%7*$MMM	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	Ns   A;BB #B c                 l   t          j                    5 }t          j        |          5  t          j                            |d          }t          |d           t          |d          }t          ddgt          R ddi\  }}}t          d	k    rt          t          |                     d
dz  }|                     |                    d          |           |                     ||||||dt           j        j                   d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nr   r   z1import sys; print('init_argv0==%r' % sys.argv[0])r   r   r   rG   Fr   zinit_argv0==%rr&   )r   r   
change_cwdr+   r   r   r   r   r
   rL   r'   r(   r)   r-   r.   rB   r   r[   r   )r2   r   r   r3   rQ   rR   rS   r]   s           r   test_issue8202zCmdLineTest.test_issue8202  s    !! 	IZ%:666 I I',,z:>>"UVVV/BB/6Gi,iiichiiCQ;;$s))$$$+d2hoog66<<<"";C#.Z#,#6#GI I II I I I I I I I I I I I I I I	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	Is5   D)CDD)D	D)D	D))D-0D-c                    t          j                    5 }t          j        |          5  t          ddd          5 }|                    d           t          ddd	          \  }}}t          d
k    rt          t          |                     ddz  }| 	                    |
                    d          |           d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nr   rZ   wr&   encodingr5   z2import sys; print("sys.path[0]==%r" % sys.path[0])F)rG   r   zsys.path[0]==%rr   )r   r   r   openra   r
   r'   r(   r)   r-   r.   )r2   r   frQ   rR   rS   r]   s          r   "test_issue8202_dash_c_file_ignoredz.CmdLineTest.test_issue8202_dash_c_file_ignored  s    !! 
	AZ%:666 	A 	A$g666 A!GGFOOO#3DL#($* $* $*LBS {{d3ii(((025HMM(//'":":C@@@A A A A A A A A A A A A A A A	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A
	A 
	A 
	A 
	A 
	A 
	A 
	A 
	A 
	A 
	A 
	A 
	A 
	A 
	A 
	A 
	A 
	A 
	AsX   C9C!BC
>C!
CC!CC!C9!C%	%C9(C%	)C99C= C=c                    t          j                    5 }t          |d          }t          j        |          5  t	          ddd          5 }|                    d           t          ddgt          R dd	i\  }}}|                     ||||||d
t          j
        j                   d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Notherr   r   r   r&   r   r5   rG   Fr   )r   r   r   r   r   ra   r
   rL   rB   r   r[   r   )r2   r   r3   r   rQ   rR   rS   s          r   "test_issue8202_dash_m_file_ignoredz.CmdLineTest.test_issue8202_dash_m_file_ignored  s    !! 		LZ+J@@K%:666 L L$g666 L!GGFOOO#3D' $FL $F $F $F?D$F $FLBS&&{B&1;
B&/&9&JL L L	L L L L L L L L L L L L L L LL L L L L L L L L L L L L L L		L 		L 		L 		L 		L 		L 		L 		L 		L 		L 		L 		L 		L 		L 		L 		L 		L 		LsY   &C#CAB4(C4B88C;B8<C?C#C	C#C	C##C'*C'c                    t          j                    5 }t          j                            |d          }t          |ddd          5 }|                    d           |                    d           t          d          D ]}|                    d	           |                    d           d d d            n# 1 swxY w Y   t          j        |
          5  t          |          \  }}}d d d            n# 1 swxY w Y   | 
                    d|           | 
                    d|           d d d            d S # 1 swxY w Y   d S )Nzissue20884.pyr   latin1
)r   newlinez#coding: iso-8859-1
z"""
   zQxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
r   r   )r   r   r+   r   r   r   ra   ranger   r
   r*   )r2   r   r3   r   _rQ   rR   rS   s           r   test_issue20884zCmdLineTest.test_issue20884  s    !! 	'Z',,z?CCKk34HHH !A/000   r + +AGGM****   ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! %:666 = =/<<C= = = = = = = = = = = = = = =S#&&&S#&&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	'sZ   4D?A'B;/D?;B?	?D?B?	D?C;/D?;C?	?D?C?	/D??EEc              '     K   t          j                    5 }t          j        |          5  t          j                            |d          }t          |g|R   |V  d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nr   r   )r   r   r   r+   r   r   r   )r2   argsr   r   s       r   setup_test_pkgzCmdLineTest.setup_test_pkg  s     !! 	Z$*555	 	gll:z::GW$t$$$$MMM		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s4   B2A*B*A.	.B1A.	2BB	Bc                     t          dg|R ddi\  }}}t          dk    rt          t          |                     |                     |d           |S )Nr   rG   Fr   )r   r'   r(   r)   r*   )r2   r   rQ   rR   rS   s        r   check_dash_m_failurez CmdLineTest.check_dash_m_failure  s]    ,TKDKKKUKKCQ;;$s))Q
r   c                     |                                  5 }t          |dd          } | j        dgt          R  }|                     d|           d d d            d S # 1 swxY w Y   d S )Nr   z+if __name__ == '__main__': raise ValueErrorztest_pkg.others
   ValueError)r   r   r   rL   r-   )r2   r   r3   rS   s       r   test_dash_m_error_code_is_onez)CmdLineTest.test_dash_m_error_code_is_one  s       "" 	.g+GW,Y[ [K+$+,<L|LLLCMM----		. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.s   ;AA!$A!c                     d}|D ]\  }}|                      |          5  t          d|          \  }}}|                     |d           |                     ||           |                     d|           d d d            n# 1 swxY w Y   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)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)r2   testsnameregexrQ   r   rS   s          r   test_dash_m_errorszCmdLineTest.test_dash_m_errors  s    

 ! 	4 	4KD%d## 4 424>>
As  Q'''  e,,,  s333	4 4 4 4 4 4 4 4 4 4 4 4 4 4 4	4 	4s   ABB		B	c                    t          j                    5 }t          j        |          5  t          j        d           t          dd          5  	 d d d            n# 1 swxY w Y   |                     d          }|                     |d           |                     d|           |                     d|           d d d            n# 1 swxY w Y   d d d            d S # 1 swxY 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   r   r   r+   mkdirr   r   r  r  r2   r   rS   s      r   test_dash_m_bad_pyczCmdLineTest.test_dash_m_bad_pyc  s   !! 	0Z$*555	0 	0HZ   -t44                ++J77CS23 3 3 _c222\3///	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0sY   C#%CACA!!C$A!%AC?C#C	C#C	C##C'*C'c                 `   t          j                    5 }t          j        |          5  t          dd          5  	 d d d            n# 1 swxY w Y   |                     d          }|                     d|           d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nr   z
asyncio.pyr  s>   Try using 'asyncio' instead of 'asyncio.py' as the module name)r   r   r   r   r   r-   r
  s      r   *test_hint_when_triying_to_import_a_py_filez6CmdLineTest.test_hint_when_triying_to_import_a_py_file  s   !! 	FZ$*555	F 	F lD))                ++L99CMM @ADF F F	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	FsV   B#BA	B	ABA.B?B#B	B#B	B##B'*B'c                    t           t          t          t          f}|D ]}|j        }d                    |          }|                     |          5  |                     |          5 }|                     d          }| 	                    |
                    d          |           | 	                    d|           | 	                    d|           d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   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.   )r2   
exceptions	exceptioninitr   rS   s         r   test_dash_m_init_tracebackz&CmdLineTest.test_dash_m_init_traceback  ss    ">9jI
# 	1 	1I!*I:AA)LLDi(( 1 1''--118//
;;i..w77===93???lC0001 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1	1 	1s7   C4&A+CC4C!!C4$C!%C44C8	;C8	c                 4   |                                  5 }d}t          |d|           |                     d          }|                     d|           |                     d|           |                     d|           d d d            d S # 1 swxY w Y   d S )Nz1raise ImportError('Exception in __main__ module')r   r   s   ImportErrors   Exception in __main__ moduler   )r   r   r   r-   )r2   r   mainrS   s       r   test_dash_m_main_tracebackz&CmdLineTest.test_dash_m_main_traceback  s      "" 	-gFDgz4888++J77CMM.#...MM93???MM,,,,	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-s   A+BBBc                 x   t          j        d          }t          j                    5 }t	          |d|          }t          |          \  }}}|                    d                              d          }|                     t          |          d           | 
                    |d                             d                     | 
                    |d                             d	                     | 
                    |d
                             d                     d d d            d S # 1 swxY w Y   d S )Nz|            try:
                raise ValueError
            except:
                raise NameError from None
            r   r  r      r   	Tracebackr   z  File    	NameError)textwrapdedentr   r   r   r   decodesplitr*   lenr   
startswithr2   r   r   r3   exitcoderk   rf   texts           r   test_pep_409_verbiagez!CmdLineTest.test_pep_409_verbiage  sX     "   !! 	=Z+J&IIK'<['I'I$Hff==))//55DSYY***OODG..{;;<<<OODG..y99:::OODG..{;;<<<	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	=s   C:D//D36D3c                 L   t           j        r-t          j        dvrt	          j        t           j                  }n.t           j        rt           j        }n|                     d           d}t          t	          j	                    ||          }| 
                    t           j        |           t          |          \  }}}|                     t          |          |                                                    d          d|d|           |                     d|           d S )N)win32darwin
emscriptenwasizneed os_helper.TESTFN_NONASCIIzprint(ascii(__file__))
r  zstdout=z stderr=r   )r   TESTFN_UNDECODABLEr/   platformr+   fsdecodeTESTFN_NONASCIIskipTestr   r,   
addCleanupunlinkr
   r*   r  rstripr$  )r2   r  r   r3   rQ   rk   rf   s          r   test_non_asciizCmdLineTest.test_non_ascii-  s   
 ( 	<L III;y;<<DD& 	<,DDMM:;;; ,'	T6BB	(+666-k::FF+MMOO""7+++%+VVVV4	6 	6 	6 	Br   c                 H   t          j        d          }t          j                    5 }t	          |d|          }t          |          \  }}}|                    d          }|                     |                                d           d d d            d S # 1 swxY 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)
            r   r  z	some text)	r"  r#  r   r   r   r   r$  r*   r8  r(  s           r   )test_issue20500_exit_with_exception_valuez5CmdLineTest.test_issue20500_exit_with_exception_valueE  s     
" 
 
 !! 	9Z+J&IIK'<['I'I$Hff==))DT[[]]K888		9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9s   A"BBBc                 H   d}t          j                    5 }t          |d|          }t          |          \  }}}t	          j        t	          j        |          d                                          }|                     d|           d d d            d S # 1 swxY w Y   d S )Nz
1 + 1 = 2
r   r  z
    ^^^^^
)	r   r   r   r   ioTextIOWrapperBytesIOrl   r-   r(  s           r   *test_syntaxerror_unindented_caret_positionz6CmdLineTest.test_syntaxerror_unindented_caret_positionW  s    !! 	1Z+J&IIK'<['I'I$Hff#BJv$6$6@@EEGGDMM/4000	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1s   A4BBBc                    t          j        d          }t          j                    5 }t	          |d|          }t          |          \  }}}t          j        t          j        |          d          	                                }| 
                    d|           d}t	          |d|          }t          |          \  }}}t          j        t          j        |          d          	                                }|                     d|           | 
                    d|           d d d            d S # 1 swxY w Y   d S )Nz;            if True:
                1 + 1 = 2
            r   r  z
    1 + 1 = 2
    ^^^^^
zif True:
    1 + 1 = 2
)r"  r#  r   r   r   r   r=  r>  r?  rl   r-   r  r(  s           r   (test_syntaxerror_indented_caret_positionz4CmdLineTest.test_syntaxerror_indented_caret_position`  s    "   !! 	@Z+J&IIK'<['I'I$Hff#BJv$6$6@@EEGGDMM8$???$  ,J&IIK'<['I'I$Hff#BJv$6$6@@EEGGDT4(((MM8$???!	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@s   C?D44D8;D8c                    d}t          j                    5 }t          |d|          }t          |          \  }}}|                     |                                dd          g d           d d d            d S # 1 swxY w Y   d S )Nzfoo = f"""{}
foo"""
r   )s
       foo"""s             ^s3   SyntaxError: f-string: empty expression not allowedr   r   r   r   r*   
splitlinesr2   r   r   r3   r)  rk   rf   s          r   #test_syntaxerror_multi_line_fstringz/CmdLineTest.test_syntaxerror_multi_line_fstringw  s    )!! 
	Z+J&IIK'<['I'I$Hff!!##BCC(    
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	   AA::A>A>c                    d}t          j                    5 }t          |d|          }t          d|          \  }}}|                     |                                dd          g d           d d d            d S # 1 swxY w Y   d S )Nzfoo = """\q"""
r   z-WerrorrE  )s       foo = """\q"""s             ^^^^^^^^s)   SyntaxError: invalid escape sequence '\q'rF  rH  s          r   3test_syntaxerror_invalid_escape_sequence_multi_linez?CmdLineTest.test_syntaxerror_invalid_escape_sequence_multi_line  s    $!! 	Z+J&IIK'<;( ($Hff !!##BCC(    	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   AA;;A?A?c                    d}t          j                    5 }t          |d|          }t          |          \  }}}|                     |                                dd          ddg           d d d            d S # 1 swxY w Y   d S )Nz?x = ' ' nothing to see here
';import os;os.system('echo pwnd')
r   s	       x = '2   SyntaxError: source code cannot contain null bytesrF  rH  s          r   test_syntaxerror_null_bytesz'CmdLineTest.test_syntaxerror_null_bytes  s    U!! 	Z+J&IIK'<['I'I$Hff!!##BCC( I  	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	rJ  c                    ddg}t          j                    5 }|D ]X}t          |d|          }t          |          \  }}}|                     |                                dd          ddg           Y	 d d d            d S # 1 swxY w Y   d S )Nz
'''
multilinestring 
'''z
f'''
multilinestring 
'''r   rN  s       multilinestringrO  rF  )r2   scriptsr   r   r3   r   rf   s          r   /test_syntaxerror_null_bytes_in_multiline_stringz;CmdLineTest.test_syntaxerror_null_bytes_in_multiline_string  s    24TU!! 		Z!  /
HfMM4[AA1f  %%'',.M   		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		s   ABB	Bc                 h   t          j        d          }d | _        t          j                    5 }t          j                    5 }t          |d|          }t          d||          }t          |                                          	                                }| 
                    |d         |           |                     ||           t          d||          }t          |                                          	                                }| 
                    ||           t          d||          }t          |                                          	                                }| 
                    |||           d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nc            import sys
            for entry in sys.path:
                print(entry)
            r   z-EsrC   r   z-I)r"  r#  maxDiffr   r   r   r   r   r$  rG  r*   r  )	r2   r   work_dirr   r3   rc   out_by_name
out_by_dirout_by_dir_isolateds	            r   -test_consistent_sys_path_for_direct_executionz9CmdLineTest.test_consistent_sys_path_for_direct_execution  s.     "   !! 	KXy/A/C/C 	Kz+J
FKKK UKX>>>A%a..//11<<>>K[^Z888X{333UJH===A$Q..00;;==JZ555T:8<<<A"-a.."7"7"9"9"D"D"F"F0*kJJJ	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	Ks6   F'E FF'F	F'F	F''F+.F+c                 r   t          j        d          }d | _        t          j                    5 }t
          j                            |d          }t          j        |           t          |d|          }t          dd|          }t          |                                                                          }|                     |d         |           |                     ||           t          dd|          }t          |                                                                          }|                     ||           t!          dd|          \  }}	}
|
                                                                }|                     d	|d
                    d d d            d S # 1 swxY w Y   d S )NrU  
script_pkgr   z-smzscript_pkg.__main__rC   r   z-ImzNo module named script_pkg)r"  r#  rV  r   r   r+   r   r   r	  r   r   r   r$  rG  r*   r  r   r-   )r2   r   rW  r   r3   rc   out_by_moduleout_by_packager)  rk   rf   traceback_liness               r   -test_consistent_sys_path_for_module_executionz9CmdLineTest.test_consistent_sys_path_for_module_execution  s     "   !! 	MXh==JHZ   +J
FKKK U$9xHHHA'NN1133>>@@M]1-x888Z777ULh???A(^^2244??AAN^];;;'<|( ( ($Hff %mmoo88::OMM68KLLL)	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	Ms   E0F,,F03F0c                 L   d}|                      t          j                            |                     t	          |dt
          j        t
          j                  }|                                \  }}|                     d|           | 	                    |j
        d           d S )Nznonexistingscript.pyT)r*  rk   rf   z: can't open file r   )assertFalser+   r   existsr   rh   ri   communicater-   assertNotEqual
returncode)r2   r   procrR   rS   s        r   test_nonexisting_scriptz#CmdLineTest.test_nonexisting_script  s    
 (//000F#-?#-?4 4 4 ##%%S*C000DOQ/////r   z	/dev/fd/0zrequires /dev/fd platformfreebsdz/devz/dev/fdz.Requires fdescfs mounted on /dev/fd on FreeBSDc                    d}t          j                    5 }t          |d|          }t          |d          5 }t	          d|                                 dddd|                                f	          }|                                \  }}|                     |d
           d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nz print("12345678912345678912345")z	script.pyrz/dev/fd/Tr   r   r   )	close_fdspass_fdss   12345678912345678912345
)r   r   r   r   r   filenorf  r*   )r2   r   rW  r3   fprc   rR   rS   s           r   test_script_as_dev_fdz!CmdLineTest.test_script_as_dev_fd   su    4!! 	DX+Hk6JJKk3'' D2 !9BIIKK!9!9TUVWXYZ[][d[d[f[fTghhh==??S  &BCCCD D D D D D D D D D D D D D D	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	Ds5   "CA+B/#C/B3	3C6B3	7CCCr   )F)Gr  
__module____qualname__rB   rT   rX   r^   rd   
contextlibcontextmanagerrp   rs   ru   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+  r9  r;  r@  rC  rI  rL  rP  rS  r[  rb  rj  unittest
skipUnlessr+   r   re  skipIfr/   r2  r'  statst_devrr   r   r   r$   r$   \   sp        <@9 9 9 9D 59C C C C C  599 9 9 9 9 % % %
% % %    &B B B BC C C C' ' '+ + +' ' '+ + +8 8 8
E 
E 
EI I IE E EI I I6 6 6
6 6 66 6 66 6 66 6 64 4 4/ / /D D DD D D/ / // / /N N N	N 	N 	NI I I"A A AL L L' ' '"     . . .4 4 4(0 0 0F F F1 1 1- - -= = =$     09 9 9$1 1 1@ @ @.    
 
 
  K K K>#M #M #MJ0 0 0 X446QRRX_S\,,Y77 IRWV__+wrwy/A/A/HHEG G
D 
DG G SR
D 
D 
Dr   r$   c                  ,    t          j                     d S r   )r   reap_childrenr|  r   r   tearDownModuler    s    r   r   )$ru  r   importlib.machineryr   rw  r/   r+   os.pathr   rh   r=  r"  testr   test.supportr   r   test.support.script_helperr   r   r   r	   r
   r   r   r   r'   rL   test_sourcer   r"   TestCaser$   r  r  r  r|  r   r   <module>r     s                    



 				          				        & & & & & & " " " " " "H H H H H H H H H H H H H H H H H H H H /***3j ;F     *   r
D r
D r
D r
D r
D(# r
D r
D r
Dl   zHMOOOOO r   