Sindbad~EG File Manager

Current Path : /usr/local/lib/python3.9/test/__pycache__/
Upload File :
Current File : //usr/local/lib/python3.9/test/__pycache__/test_subprocess.cpython-39.pyc

a

��g�b�@s�ddlZddlmZddlmZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlmZzddlZWney�dZYn0zddlZWne�ydZYn0zddlZWne�y:dZYn0ej�rNe�d��ej dkZ!e!�rddZ"ndZ"d	Z#e$e%e&fZ'ej(d
dfa)dd
�Z*Gdd�dej+�Z,Gdd�de-�Z.Gdd�dej/�Z0Gdd�de,�Z1Gdd�de,�Z2dd�Z3e�4e!d�Gdd�de,��Z5e�6e!d�Gdd�de,��Z7Gd d!�d!ej+�Z8e�6e9ed"�d#�Gd$d%�d%e1��Z:e�6e!d&�Gd'd(�d(e,��Z;Gd)d*�d*e,�Z<e=d+k�r�e�>�dS),�N)�mock)�support)�FakePathztest is not helpful for PGO�win32z@import msvcrt; msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY);�)Znonexisting_i_hope�-c�passcCs@t�d�}|durdSt�|tj�r<t�|g�jdkr<|fadS)N�truer)	�shutil�which�os�access�X_OK�
subprocess�run�
returncode�ZERO_RETURN_CMD)Z
shell_true�r�0/usr/local/lib/python3.9/test/test_subprocess.py�setUpModule>s
�rc@seZdZdd�Zdd�ZdS)�BaseTestCasecCst��dS�N)r�
reap_children��selfrrr�setUpIszBaseTestCase.setUpcCsBts.tjD]}|��q
t��|�tjd�|��t��dS)Nzsubprocess._active not empty)	�	mswindowsr�_active�wait�_cleanup�assertFalseZ
doCleanupsrr)r�instrrr�tearDownNs

�zBaseTestCase.tearDownN��__name__�
__module__�__qualname__rr"rrrrrHsrc@seZdZdS)�PopenTestExceptionN)r$r%r&rrrrr'[sr'c@seZdZdZdd�ZdS)�PopenExecuteChildRaiseszePopen subclass for testing cleanup of subprocess.PIPE filehandles when
    _execute_child fails.
    cOstd��dS)NzForced Exception for Test)r'�r�args�kwargsrrr�_execute_childcsz&PopenExecuteChildRaises._execute_childN)r$r%r&�__doc__r,rrrrr(_sr(c@sBeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3d4�Zd5d6�Ze�e d7�d8d9��Z!e�e d7�d:d;��Z"e�e d7�d<d=��Z#d>d?�Z$d@dA�Z%dBdC�Z&dDdE�Z'dFdG�Z(dHdI�Z)e�e dJ�dKdL��Z*e�e dJ�dMdN��Z+dOdP�Z,e�e-j.e-j/kdQ�dRdS��Z0e�e-j.e-j/kdQ�e�e1�2�dT�dUdV���Z3dWdX�Z4dYdZ�Z5d[d\�Z6d]d^�Z7d_d`�Z8dadb�Z9dcdd�Z:dedf�Z;dgdh�Z<didj�Z=dkdl�Z>dmdn�Z?dodp�Z@dqdr�ZAdsdt�ZBdudv�ZCdwdx�ZDe�e-jEdykdz�e�e1�Fd{�d|kd}�d~d���ZGd�d��ZHd�d��ZId�d��ZJd�d��ZKd�d��ZLd�d��ZMd�d��ZNd�d��ZOd�d��ZPd�d��ZQd�d��ZRd�d��ZSd�d��ZTd�d��ZUd�d��ZVd�d��ZWd�d��ZXd�d��ZYd�d��ZZd�d��Z[d�d��Z\d�d��Z]d�d��Z^d�d��Z_d�d��Z`d�d��Zad�d��Zbd�d��Zcd�d��Zdd�d��Zed�d��Zfd�d��Zgd�d��Zhd�dÄZid�dńZjd�dDŽZkd�dɄZld�d˄Zme�neoepd̃d͡e�neoeqd΃dϡe�neoeqdЃdѡd�dӄ���Zre�neqjs�td�eq�u��dաd�dׄ�Zve�e dءd�dڄ�Zwe�e dءd�d܄�Zxd�dބZyd�S)��ProcessTestCasecCs�tjttjtjtjd�}zZ|�|jtj�|�|jtj�|�|j	tj�W|j�
�|j�
�|j	�
�|��n(|j�
�|j�
�|j	�
�|��0dS)N��stdin�stdout�stderr)r�Popenr�PIPE�assertIsInstancer0�io�BufferedIOBaser1r2�closer�r�prrr�test_io_buffered_by_defaultis �



�


z+ProcessTestCase.test_io_buffered_by_defaultcCs�tjttjtjtjdd�}zZ|�|jtj�|�|jtj�|�|j	tj�W|j�
�|j�
�|j	�
�|��n(|j�
�|j�
�|j	�
�|��0dS)Nr)r0r1r2�bufsize)rr3rr4r5r0r6�	RawIOBaser1r2r8rr9rrr�test_io_unbuffered_worksws �



�


z(ProcessTestCase.test_io_unbuffered_workscCs"t�tjddg�}|�|d�dS�Nr�import sys; sys.exit(47)�/�r�call�sys�
executable�assertEqual�r�rcrrr�
test_call_seq�s
�zProcessTestCase.test_call_seqcCs"|jtjtjtjddgdd�dS)Nr�while True: pass皙�����?��timeout)�assertRaisesr�TimeoutExpiredrCrDrErrrr�test_call_timeout�s
�z!ProcessTestCase.test_call_timeoutcCst�t�}|�|d�dS�Nr)r�
check_callrrFrGrrr�test_check_call_zero�s
z$ProcessTestCase.test_check_call_zerocCsR|�tj��"}t�tjddg�Wd�n1s40Y|�|jjd�dSr?)	rNr�CalledProcessErrorrRrDrErF�	exceptionr�r�crrr�test_check_call_nonzero�s

�$z'ProcessTestCase.test_check_call_nonzerocCs"t�tjddg�}|�d|�dS)Nr�
print('BDFL')�BDFL�r�check_outputrDrE�assertIn�r�outputrrr�test_check_output�s
�z!ProcessTestCase.test_check_outputcCsR|�tj��"}t�tjddg�Wd�n1s40Y|�|jjd�dS)Nrzimport sys; sys.exit(5)�)	rNrrTr\rDrErFrUrrVrrr�test_check_output_nonzero�s

�"z)ProcessTestCase.test_check_output_nonzerocCs(tjtjddgtjd�}|�d|�dS)Nr�$import sys; sys.stderr.write('BDFL')�r2rZ)rr\rDrE�STDOUTr]r^rrr�test_check_output_stderr�s

�z(ProcessTestCase.test_check_output_stderrcCsNt��}|�|j�|�d�|�d�tjtj	ddg|d�}|�
d|�dS)N�pearrr�6import sys; sys.stdout.write(sys.stdin.read().upper())�r0�PEAR)�tempfile�
TemporaryFile�
addCleanupr8�write�seekrr\rDrEr])r�tfr_rrr�test_check_output_stdin_arg�s

��z+ProcessTestCase.test_check_output_stdin_argcCs&tjtjddgdd�}|�d|�dS)Nrrhrg��inputrjr[r^rrr�test_check_output_input_arg�s��z+ProcessTestCase.test_check_output_input_argcCs&tjtjddgdd�}|�d|�dS)z<input=None has a legacy meaning of input='' on check_output.r�3import sys; print('XX' if sys.stdin.read() else '')NrrsXX�rr\rDrE�assertNotInr^rrr�test_check_output_input_none�s��z,ProcessTestCase.test_check_output_input_nonecCs(tjtjddgddd�}|�d|�dS)NrruT)rs�text�XXrvr^rrr�!test_check_output_input_none_text�s��z1ProcessTestCase.test_check_output_input_none_textcCs(tjtjddgddd�}|�d|�dS)NrruT)rs�universal_newlinesrzrvr^rrr�/test_check_output_input_none_universal_newlines�s��z?ProcessTestCase.test_check_output_input_none_universal_newlinescCsd|�t��2}tjtjddgtjd�}|�d�Wd�n1sB0Y|�d|j	j
d�dS)Nr�print('will not be run')�r1z-Expected ValueError when stdout arg supplied.r1r)rN�
ValueErrorrr\rDrEr1�failr]rUr*�rrWr_rrr�test_check_output_stdout_arg�s
�(z,ProcessTestCase.test_check_output_stdout_argcCs�t��}|�|j�|�d�|�d�|�t��2}tj	t
jddg|dd�}|�d�Wd�n1sj0Y|�
d|jjd�|�
d	|jjd�dS)
Nrgrrr~�hare�r0rs�7Expected ValueError when stdin and input args supplied.r0rs)rkrlrmr8rnrorNr�rr\rDrEr�r]rUr*�rrprWr_rrr�&test_check_output_stdin_with_input_arg�s


�(z6ProcessTestCase.test_check_output_stdin_with_input_argcCs`|�tj��0}tjtjddgdd�}|�d�Wd�n1sB0Y|�|jj	d�dS)Nr�Mimport sys, time
sys.stdout.write('BDFL')
sys.stdout.flush()
time.sleep(3600)�rLzExpected TimeoutExpired.rZ)
rNrrOr\rDrEr�rFrUr_r�rrr�test_check_output_timeout�s��	(z)ProcessTestCase.test_check_output_timeoutcCs8tj��}d|d<tjtjddg|d�}|�|d�dS)N�banana�FRUITrz5import sys, os;sys.exit(os.getenv("FRUIT")=="banana")��env�)r�environ�copyrrCrDrErF)r�newenvrHrrr�test_call_kwargss

��z ProcessTestCase.test_call_kwargscCs~t���R}|jttjdd�tjjjj}dg|d}|jttjg|�R�Wd�n1s`0Y|�	|�
�d�dS)Nr�)Zinvalid_arg_namerr)rZcaptured_stderrrN�	TypeErrorrr3�__init__�__code__�co_argcountrF�getvalue)r�sZargcountZ
too_many_argsrrr�test_invalid_argss
4z!ProcessTestCase.test_invalid_argscCsRtjtjddgtjtjd�}|�|jj�|�|jj�|�	�|�
|jd�dS)Nr�print("banana")�r1r2)rr3rDrEr4rmr1r8r2rrFr0r9rrr�test_stdin_nones�zProcessTestCase.test_stdin_nonecCsld}tjtjd|gtjtjd�}|�|jj�|�|jj�|�	�\}}|�
|jd|�|�
|��d�dS)Nz�import sys; from subprocess import Popen, PIPE;p = Popen([sys.executable, "-c", "print('test_stdout_none')"],          stdin=PIPE, stderr=PIPE);p.wait(); assert p.stdout is None;rr�rstest_stdout_none�
rr3rDrEr4rmr1r8r2�communicaterFr�rstrip�r�coder:�out�errrrr�test_stdout_none!s�z ProcessTestCase.test_stdout_nonecCsRtjtjddgtjtjd�}|�|jj�|�|jj�|�	�|�
|jd�dS)Nrr��r0r1)rr3rDrEr4rmr1r8r0rrFr2r9rrr�test_stderr_none8s�z ProcessTestCase.test_stderr_nonecKs6|dg}tj|fi|��}|��|�d|j�dS�Nr@rA)rr3rrFr)r�pre_argsr+r*r:rrr�_assert_pythonAs
zProcessTestCase._assert_pythoncCs0tj�tj�tj�d�}|j|dgtjd�dS�N�doesnotexistr�rE)r�path�join�dirnamerDrEr��rr�rrr�test_executableIs�zProcessTestCase.test_executablecCs6tj�tj�tj�d�}|j|dgt�tj�d�dSr�)rr�r�r�rDrEr��fsencoder�rrr�test_bytes_executableTs�

�z%ProcessTestCase.test_bytes_executablecCs4tj�tj�tj�d�}|j|dgttj�d�dSr�)rr�r�r�rDrEr�rr�rrr�test_pathlike_executableZs�
�z(ProcessTestCase.test_pathlike_executablecCs0tjdg}|�|�|jt|j|tdd�dS)Nrrr�)rDrEr�rN�NONEXISTING_ERRORS�NONEXISTING_CMD)rr�rrr� test_executable_takes_precedence`s

�z0ProcessTestCase.test_executable_takes_precedencez"executable argument replaces shellcCs|jgtjdd�dS�NT)rE�shell)r�rDrErrrr�test_executable_replaces_shelljsz.ProcessTestCase.test_executable_replaces_shellcCs|jgt�tj�dd�dSr�)r�rr�rDrErrrr�$test_bytes_executable_replaces_shellps�z4ProcessTestCase.test_bytes_executable_replaces_shellcCs|jgttj�dd�dSr�)r�rrDrErrrr�'test_pathlike_executable_replaces_shellus�z7ProcessTestCase.test_pathlike_executable_replaces_shellcCs6t�|��t��Wd�S1s(0YdSr)rZ
change_cwdr�getcwd)r�cwdrrr�_normalize_cwd{szProcessTestCase._normalize_cwdcCstj�tj�}tj�|�Sr)rr��realpathrDrE�split)rZpython_pathrrr�_split_python_path�sz"ProcessTestCase._split_python_pathcKsltj|ddgfdtji|��}|�|jj�|��|�d|j�t	j
j}|�||�||j���
���dS)Nrzcimport os, sys; buf = sys.stdout.buffer; buf.write(os.getcwd().encode()); buf.flush(); sys.exit(47)r1rA)rr3r4rmr1r8rrFrrr��normcase�read�decode)rZexpected_cwdZ
python_argr+r:r�rrr�_assert_cwd�s���
�zProcessTestCase._assert_cwdcCs(t��}|�|�}|j|tj|d�dS�N�r�)rk�
gettempdirr�r�rDrE�r�temp_dirrrr�test_cwd�s
zProcessTestCase.test_cwdcCs.t��}|�|�}|j|tjt�|�d�dSr�)rkr�r�r�rDrErr�r�rrr�test_cwd_with_bytes�s
z#ProcessTestCase.test_cwd_with_bytescCs,t��}|�|�}|j|tjt|�d�dSr�)rkr�r�r�rDrErr�rrr�test_cwd_with_pathlike�s
z&ProcessTestCase.test_cwd_with_pathlikez"pending resolution of issue #15533cCs�|��\}}tj�tj|�}t���R}|�tt	j
|g�|jtt	j
|g|d�|�|�}|j|||d�Wd�n1s|0YdSr��
r�rr�r��curdirrZtemp_cwdrN�FileNotFoundErrorrr3r�r�)r�
python_dir�python_base�
rel_python�	wrong_dirrrr�test_cwd_with_relative_arg�s

�
�
z*ProcessTestCase.test_cwd_with_relative_argcCs�|��\}}tj�tj|�}d}t���Z}|jtt	j
|g|d�|jtt	j
|g||d�|�|�}|j||||d�Wd�n1s�0YdS)N�somethingyoudonthaver��rEr�r�)rr�r�r�Zdoesntexistr�rrr�!test_cwd_with_relative_executable�s

�
�

�z1ProcessTestCase.test_cwd_with_relative_executablecCs�|��\}}tj�||�}tj�tj|�}t���@}|jtt	j
|g|d�|�|�}|j|||d�Wd�n1sx0YdSr�)
r�rr�r�r�rr�rNr�rr3r�r�)rr�r�Z
abs_pythonr�r�rrr�test_cwd_with_absolute_arg�s

�
z*ProcessTestCase.test_cwd_with_absolute_argzTest is not venv-compatiblecCs.|��\}}|�|�}|j|dtj|d�dS)Nr�r�)r�r�r�rDrE)rr�r�rrr�test_executable_with_cwd�s

�z(ProcessTestCase.test_executable_with_cwdz#need an installed Python. See #7774cCs|jt��dtjd�dS)Nr�r�)r�rr�rDrErrrr�test_executable_without_cwd�s�z+ProcessTestCase.test_executable_without_cwdcCsHtjtjddgtjd�}|j�d�|j��|��|�	|j
d�dS)Nr�0import sys; sys.exit(sys.stdin.read() == "pear")rirgr�)rr3rDrEr4r0rnr8rrFrr9rrr�test_stdin_pipe�s
��
zProcessTestCase.test_stdin_pipecCsft��}|�|j�|��}t�|d�t�|dd�tj	t
jddg|d�}|��|�
|jd�dS�Nrgrrr�rir�)rkrlrmr8�filenorrn�lseekrr3rDrErrFr�rrp�dr:rrr�test_stdin_filedes�s
��z"ProcessTestCase.test_stdin_filedescCsXt��}|�|j�|�d�|�d�tjtj	ddg|d�}|�
�|�|jd�dSr�)
rkrlrmr8rnrorr3rDrErrFr�rrpr:rrr�test_stdin_fileobjs


��z"ProcessTestCase.test_stdin_fileobjcCsRtjtjddgtjd�}|�"|�|j��d�Wd�n1sD0YdS)Nr�&import sys; sys.stdout.write("orange")r�orange�rr3rDrEr4rFr1r�r9rrr�test_stdout_pipes
��z ProcessTestCase.test_stdout_pipecCs`t��}|�|j�|��}tjtjddg|d�}|�	�t
�|dd�|�t
�
|d�d�dS)Nrr�rr�r��rkrlrmr8r�rr3rDrErrr�rFr�r�rrr�test_stdout_filedess
��z#ProcessTestCase.test_stdout_filedescCsPt��}|�|j�tjtjddg|d�}|��|�	d�|�
|��d�dS)Nrr�rrr��rkrlrmr8rr3rDrErrorFr�r�rrr�test_stdout_fileobj&s
��
z#ProcessTestCase.test_stdout_fileobjcCsRtjtjddgtjd�}|�"|�|j��d�Wd�n1sD0YdS)Nr�*import sys; sys.stderr.write("strawberry")rd�
strawberry)rr3rDrEr4rFr2r�r9rrr�test_stderr_pipe1s
��z ProcessTestCase.test_stderr_pipecCs`t��}|�|j�|��}tjtjddg|d�}|�	�t
�|dd�|�t
�
|d�d�dS)Nrr�rdrr�r�r�r�rrr�test_stderr_filedes9s
��z#ProcessTestCase.test_stderr_filedescCsPt��}|�|j�tjtjddg|d�}|��|�	d�|�
|��d�dS)Nrr�rdrr�r�r�rrr�test_stderr_fileobjEs
��
z#ProcessTestCase.test_stderr_fileobjcCsRtjtjddgtjtjd�}|��\}}|�|d�|�|d�|�|jd�dS)Nrz�import sys, subprocess;rc = subprocess.call([sys.executable, "-c",    "import sys;"    "sys.stderr.write('42')"],    stderr=subprocess.STDOUT);sys.exit(rc)r�s42�r�rr3rDrEr4r�rFr�rr:r1r2rrr�,test_stderr_redirect_with_no_stdout_redirectPs
��	z<ProcessTestCase.test_stderr_redirect_with_no_stdout_redirectcCsVtjtjddgtjtjd�}|�"|�|j��d�Wd�n1sH0YdS)Nr�Rimport sys;sys.stdout.write("apple");sys.stdout.flush();sys.stderr.write("orange")r��appleorange)	rr3rDrEr4rerFr1r�r9rrr�test_stdout_stderr_pipees
��z'ProcessTestCase.test_stdout_stderr_pipecCsRt��}|�|j�tjtjddg||d�}|��|�	d�|�
|��d�dS)Nrr�r�rr�r�r�rrr�test_stdout_stderr_fileqs
��
z'ProcessTestCase.test_stdout_stderr_filecCsld}tjtjd|gtjtjd�}|�|jj�|�|jj�|�	�\}}|�
|jd|�|�
|��d�dS)Nz�import sys, subprocess; rc = subprocess.call([sys.executable, "-c",     "import os, sys; sys.exit(os.write(sys.stdout.fileno(), b'test with stdout=1'))"], stdout=1); assert rc == 18rr�rstest with stdout=1r�r�rrr�test_stdout_filedes_of_stdout�s�z-ProcessTestCase.test_stdout_filedes_of_stdoutcCs2tjtjddgtjd�}|��|�|jd�dS)Nrz'for i in range(10240):print("x" * 1024)r)rr3rDrE�DEVNULLrrFr1r9rrr�test_stdout_devnull�s
��z#ProcessTestCase.test_stdout_devnullcCs2tjtjddgtjd�}|��|�|jd�dS)Nrz=import sys
for i in range(10240):sys.stderr.write("x" * 1024)rd)rr3rDrErrrFr2r9rrr�test_stderr_devnull�s
��z#ProcessTestCase.test_stderr_devnullcCs2tjtjddgtjd�}|��|�|jd�dS)Nrzimport sys;sys.stdin.read(1)ri)rr3rDrErrrFr0r9rrr�test_stdin_devnull�s
��z"ProcessTestCase.test_stdin_devnullcCshtj��}d|d<tjtjddgtj|d��(}|��\}}|�	|d�Wd�n1sZ0YdS)N�oranger�r�2import sys,os;sys.stdout.write(os.getenv("FRUIT"))�r1r�r�)
rr�r�rr3rDrEr4r�rF�rr�r:r1r2rrr�test_env�s

��zProcessTestCase.test_envrz#cannot test an empty env on WindowsZPy_ENABLE_SHAREDr�zEThe Python shared library cannot be loaded with an empty environment.cs�dd��tjtjddgtjid��R}|��\}}t|���}|�|t	��fdd�|D�}|�
|g�Wd�n1sz0YdS)	z+Verify that env={} is as empty as possible.cSs*d|vp(d|vp(|dkp(|�d�p(|dkS)z:Determine if an environment variable is under our control.Z	VERSIONERZ__CFZ
LD_PRELOADZSANDBOX�LC_CTYPE)�
startswith)�nrrr�is_env_var_to_ignore�s���z<ProcessTestCase.test_empty_env.<locals>.is_env_var_to_ignorerz)import os; print(list(os.environ.keys()))rcsg|]}�|�s|�qSrr)�.0�k�rrr�
<listcomp>�s�z2ProcessTestCase.test_empty_env.<locals>.<listcomp>N)rr3rDrEr4r��eval�stripr5�listrF)rr:r1r2Zchild_env_namesrrr�test_empty_env�s	
��zProcessTestCase.test_empty_envcCs�tjd}|�t�� t�|ddg�Wd�n1s:0Y|�t��"t�tjddg�Wd�n1sv0YdS)N�rrzpass#)rDrErNr�rr3)r�cmdrrr�test_invalid_cmd�s

.z ProcessTestCase.test_invalid_cmdcCsHtj��}d|d<|�t��tjt|d�Wd�n1s@0Ytj��}d|d<|�t��tjt|d�Wd�n1s�0Ytj��}d|d<|�t��tjt|d�Wd�n1s�0Ytj��}d|d<tjtj	d	d
gtj
|d��(}|��\}}|�|d�Wd�n1�s:0YdS)
NZcabbagezFRUITVEGETABLEr�zorangeVEGETABLE=cabbager�ZlemonzFRUIT=ORANGEzorange=lemonrz3import sys, os;sys.stdout.write(os.getenv("FRUIT"))rsorange=lemon)
rr�r�rNr�rr3rrDrEr4r�rFrrrr�test_invalid_env�s.
,
,
,

��z ProcessTestCase.test_invalid_envcCs4tjtjddgtjd�}|�d�|�|jd�dS)Nrz/import sys;sys.exit(sys.stdin.read() == "pear")rirgr�r�r9rrr�test_communicate_stdin�s
��
z&ProcessTestCase.test_communicate_stdincCs@tjtjddgtjd�}|��\}}|�|d�|�|d�dS)Nrz)import sys; sys.stdout.write("pineapple")r�	pineapple�rr3rDrEr4r�rFr�rrr�test_communicate_stdouts
��z'ProcessTestCase.test_communicate_stdoutcCs@tjtjddgtjd�}|��\}}|�|d�|�|d�dS)Nrz)import sys; sys.stderr.write("pineapple")rdrrr�rrr�test_communicate_stderrs
��z'ProcessTestCase.test_communicate_stderrcCsttjtjddgtjtjtjd�}|�|jj�|�|jj�|�|j	j�|�
d�\}}|�|d�|�|d�dS)NrzNimport sys,os;sys.stderr.write("pineapple");sys.stdout.write(sys.stdin.read())r/�bananar)rr3rDrEr4rmr1r8r2r0r�rFr�rrr�test_communicates
��z ProcessTestCase.test_communicatecCsdtjtjddgdtjtjtjd�}|jtj|jddd�|��\}}|�|d�|�|�	�d�dS)	Nrz~import sys,os,time;sys.stderr.write("pineapple\n");time.sleep(1);sys.stderr.write("pear\n");sys.stdout.write(sys.stdin.read())T)r|r0r1r2r�g333333�?rLspineapple
pear
)
rr3rDrEr4rNrOr�rF�encoder�rrr�test_communicate_timeout#s
��
�z(ProcessTestCase.test_communicate_timeoutcCsLtjtjddgtjd�}|jtj|jdd�|��\}}|�t	|�d�dS)Nrz�import sys,os,time;sys.stdout.write("a" * (64 * 1024));time.sleep(0.2);sys.stdout.write("a" * (64 * 1024));time.sleep(0.2);sys.stdout.write("a" * (64 * 1024));time.sleep(0.2);sys.stdout.write("a" * (64 * 1024));rg�������?rLi)
rr3rDrEr4rNrOr�rF�len)rr:r1�_rrr�%test_communicate_timeout_large_output6s
�	�
z5ProcessTestCase.test_communicate_timeout_large_outputcCs�dD]�}dD]�}dD]�}i}|r*tj|d<|r8tj|d<|rFtj|d<|sLqtjtfi|��}|��|jdur~|�|jj�|jdur�|�|jj�|j	dur|�|j	j�qqqdS)N�FTr0r1r2)
rr4r3rr�r0�
assertTrue�closedr1r2)rZ
stdin_pipeZstdout_pipeZstderr_pipe�optionsr:rrr�test_communicate_pipe_fd_leakGs(





z-ProcessTestCase.test_communicate_pipe_fd_leakcCs:t�tjddg�}|��\}}|�|d�|�|d�dS)Nrr@)rr3rDrEr�rFr�rrr�test_communicate_returns]s
�z(ProcessTestCase.test_communicate_returnscCs�t��\}}t�|�t�|�tjtjddtjgtj	tj	tj	d�}|�
|jj�|�
|jj�|�
|j
j�dtj}|�|�\}}|�||�dS)Nrzpimport sys,os;sys.stdout.write(sys.stdin.read(47));sys.stderr.write("x" * %d);sys.stdout.write(sys.stdin.read())r/�a)r�piper8rr3rDrEr�
PIPE_MAX_SIZEr4rmr1r2r0r�rF)r�x�yr:Zstring_to_writer1r2rrr�test_communicate_pipe_bufes$


���	
z)ProcessTestCase.test_communicate_pipe_bufcCs�tjtjddgtjtjtjd�}|�|jj�|�|jj�|�|j	j�|j	�
d�|�d�\}}|�|d�|�|d�dS)Nrz0import sys,os;sys.stdout.write(sys.stdin.read())r/rssplitsbananasplitr�)
rr3rDrEr4rmr1r8r2r0rnr�rFr�rrr�test_writes_before_communicate|s
��z.ProcessTestCase.test_writes_before_communicatec
Cs�tjddtdg}dD]�}tj|fidtjdtj|di��}|��|j�d�|j��|�	|j
��d�|j�d	�|j��|�
|j
j�|�	|j
��d
�|�	|j
�d�d	�|�	|j
��d�Wd�q1s�0YqdS)
Nr�import sys,os;a@buf = sys.stdout.buffer;buf.write(sys.stdin.readline().encode());buf.flush();buf.write(b"line2\n");buf.flush();buf.write(sys.stdin.read().encode());buf.flush();buf.write(b"line4\n");buf.flush();buf.write(b"line5\r\n");buf.flush();buf.write(b"line6\r");buf.flush();buf.write(b"\nline7");buf.flush();buf.write(b"\nline8");)r|ryr0r1Tzline1
zline3
zline2
�zline4
line5
line6
line7
line8)rDrE�	SETBINARYrr3r4r0rn�flushrFr1�readliner8rmr�)rr*Zextra_kwargr:rrr� test_universal_newlines_and_text�s4���

���z0ProcessTestCase.test_universal_newlines_and_textcCs^tjtjddtdgtjtjdd�}|�|jj�|�|j	j�|�
�\}}|�|d�dS)Nrr2z�buf = sys.stdout.buffer;buf.write(b"line2\n");buf.flush();buf.write(b"line4\n");buf.flush();buf.write(b"line5\r\n");buf.flush();buf.write(b"line6\r");buf.flush();buf.write(b"\nline7");buf.flush();buf.write(b"\nline8");r�)r2r1r|z#line2
line4
line5
line6
line7
line8)rr3rDrEr4r4rmr1r8r2r�rFr�rrr�#test_universal_newlines_communicate�s
����z3ProcessTestCase.test_universal_newlines_communicatecCsHtjtjddtt�d�gtjdd�}|�d�\}}|�	|j
d�dS)Nrr2a
                               s = sys.stdin.readline()
                               assert s == "line1\n", repr(s)
                               s = sys.stdin.read()
                               assert s == "line3\n", repr(s)
                              r�)r0r|�line1
line3
r)rr3rDrEr4�textwrap�dedentr4r�rFrr�rrr�)test_universal_newlines_communicate_stdin�s
��	z9ProcessTestCase.test_universal_newlines_communicate_stdincCs0tjttjtjdd�}|��|�|jd�dS)NT)r0r1r|r)rr3rr4r�rFrr9rrr�.test_universal_newlines_communicate_input_none�s�z>ProcessTestCase.test_universal_newlines_communicate_input_nonecCs�tjtjddtt�d�gtjtjtjdd�}|�|j	j
�|�|jj
�|�d�\}}|�
|jd�|�
d|�|�|�d	��dS)
Nrr2a�
                               s = sys.stdin.buffer.readline()
                               sys.stdout.buffer.write(s)
                               sys.stdout.buffer.write(b"line2\r")
                               sys.stderr.buffer.write(b"eline2\n")
                               s = sys.stdin.buffer.read()
                               sys.stdout.buffer.write(s)
                               sys.stdout.buffer.write(b"line4\n")
                               sys.stdout.buffer.write(b"line5\r\n")
                               sys.stderr.buffer.write(b"eline6\r")
                               sys.stderr.buffer.write(b"eline7\r\nz")
                              T)r0r2r1r|r9rzline1
line2
line3
line4
line5
zeline2
eline6
eline7
)rr3rDrEr4r:r;r4rmr1r8r2r�rFrr&r
r�rrr�7test_universal_newlines_communicate_stdin_stdout_stderr�s
�
�zGProcessTestCase.test_universal_newlines_communicate_stdin_stdout_stderrcCsTdD]J}d|}tjd|g}tj|tjtj|d�}|jdd�\}}|�|d�qdS)N)zutf-16z	utf-32-bez@import sys; sys.stdout.buffer.write('1\r\n2\r3\n4'.encode('%s'))r)r0r1�encodingrrrz1
2
3
4)rDrErr3r4r�rF)rr?r�r*�popenr1r2rrr�-test_universal_newlines_communicate_encodingss��z=ProcessTestCase.test_universal_newlines_communicate_encodingscCs\dD]R\}}d}tjd|g}tj|tjtjd|d�}|jdd�\}}|�|d�|��qdS)	N))�ignorer)�replaceu��)�surrogateescapeu��)�backslashreplacez\x80\x80z2import sys; sys.stdout.buffer.write(b'[\x80\x80]')r�utf-8)r0r1r?�errorsrrrz[{}])rDrErr3r4r�rF�format)rrG�expectedr�r*r@r1r2rrr�test_communicate_errorss�z'ProcessTestCase.test_communicate_errorsc
CsXts
d}nd}g}t��}�zt|�D]t}z.tj�|tj�}|�	t�
|tjtjB��Wq&t
y�}z(|jtjkrv�WYd}~q�WYd}~q&d}~00q&|�d|�td�D]}t�|���q�td�D]>}tjtjddgtjtjtjd�}|�d	�d
}|�|d	�q�W|D]}	t�|	��qt�|�n"|D]}	t�|	��q6t�|�0dS)Niiz4failed to reach the file descriptor limit (tried %d)�
�rz-import sys;sys.stdout.write(sys.stdin.read())r/slimer)rrk�mkdtemp�rangerr�r�rZTESTFN�append�open�O_WRONLY�O_CREAT�OSError�errnoZEMFILE�skipTestr8�poprr3rDrEr4r�rFr
�rmtree)
rZmax_handles�handles�tmpdir�iZtmpfile�er:�data�hrrr�test_no_leaking-sF&�
���zProcessTestCase.test_no_leakingcCs�|�t�gd��d�|�t�gd��d�|�t�gd��d�|�t�gd��d�|�t�gd	��d
�|�t�gd��d�|�t�gd
��d�|�t�ddg�d�dS)N)za b cr�r[z"a b c" d e)�ab"c�\r�z	ab\"c \ d)r_z \r�z
ab\"c " \\" d)za\\\bzde fgr]za\\\b "de fg" h)za\"brWr�z
a\\\"b c d)za\\b cr�r[z"a\\b c" d e)za\\b\ cr�r[z
"a\\b\ c" d e�abrzab "")rFr�list2cmdlinerrrr�test_list2cmdlineTs0��������z!ProcessTestCase.test_list2cmdlinecCsbtjtjddgtjd�}|�|jj�|�|�	��t
�|j��d�|�
�|�|�	�d�dS)Nrzimport os; os.read(0, 1)ri�Ar)rr3rDrEr4rmr0r8�assertIsNone�pollrrnr�rrFr9rrr�	test_pollfs
��zProcessTestCase.test_pollcCs.t�t�}|�|��d�|�|��d�dSrQ�rr3rrFrr9rrr�	test_waitqs
zProcessTestCase.test_waitcCsvt�tjddg�}|�tj��}|jdd�Wd�n1s@0Y|�dt|j	��|�
|jtjd�d�dS)Nrzimport time; time.sleep(0.3)�-C��6?rLz0.0001r)
rr3rDrErNrOrr]�strrUrFr�
SHORT_TIMEOUT)rr:rWrrr�test_wait_timeoutws�*z!ProcessTestCase.test_wait_timeoutcCs:|�t��t�td�Wd�n1s,0YdS)Nr)rNr�rr3rrrrr�test_invalid_bufsizesz$ProcessTestCase.test_invalid_bufsizecCs>t�td�}|�|��d�tjtdd�}|�|��d�dS)Nr)r<rhr9rrr�test_bufsize_is_none�sz$ProcessTestCase.test_bufsize_is_nonec
Cs�tjtjddgtjtjtjd|d���}|j�|�t�	|j�
��|j��}t
���2z|j�	�WntyvYn0Wd�n1s�0Yd|_Wd�n1s�0Y|�|jd�|�||�dS)NrzDimport sys;sys.stdout.write(sys.stdin.readline());sys.stdout.flush()r�)r0r1r2r<r|r)rr3rDrEr4rr0rnrr8r�r1r6r�SuppressCrashReportrSrFr)r�linerIr|r:Z	read_linerrr�_test_bufsize_equal_one�s&�

$$z'ProcessTestCase._test_bufsize_equal_onecCsd}|j||dd�dS)Nzline
T�r|)rr�rrqrrr� test_bufsize_equal_one_text_mode�sz0ProcessTestCase.test_bufsize_equal_one_text_modecCsNdtj��}|�td�� |j|ddd�Wd�n1s@0YdS)Nslinezline bufferingr�Frs)r�linesepr ZassertWarnsRegex�RuntimeWarningrrrtrrr�"test_bufsize_equal_one_binary_mode�sz2ProcessTestCase.test_bufsize_equal_one_binary_modec	CsPtd�D]B}|�t��$tjttjtjd�Wd�q1s@0YqdS)Nr�r�)rNrNr�rr3r�r4)rrZrrr�test_leaking_fds_on_error�s�z)ProcessTestCase.test_leaking_fds_on_errorc	Cs�zddl}|jWn ttfy2|�d�Yn0t�dt�d��}tj	d|g}t
j|t
jdd�}|�|�
�d}Wd�n1s�0Y|�|d	�|�|jd�dS)
Nrzneed msvcrt.CrtSetReportModezL
            import msvcrt
            import subprocess

            cmd = a

            for report_type in [msvcrt.CRT_WARN,
                                msvcrt.CRT_ERROR,
                                msvcrt.CRT_ASSERT]:
                msvcrt.CrtSetReportMode(report_type, msvcrt.CRTDBG_MODE_FILE)
                msvcrt.CrtSetReportFile(report_type, msvcrt.CRTDBG_FILE_STDERR)

            try:
                subprocess.Popen(cmd,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)
            except OSError:
                pass
        rT)r2r|r�r)�msvcrtZCrtSetReportMode�AttributeError�ImportErrorrUr:r;r�rDrErr3r4r�rFr)rrzr�r�procr2rrr�test_nonexisting_with_pipes�s"
�
�*z+ProcessTestCase.test_nonexisting_with_pipescs*g��fdd�}tj|d�}|��z�|�t��(tjttjtjtjd�Wd�n1s^0YW|�	�d}�D]<}zt
�|�Wqzty�}z|}WYd}~qzd}~00qz|dur�|�n`|�	�d}�D]>}zt
�|�Wq�t�y}z|}WYd}~q�d}~00q�|du�r$|�0dS)Ncs*td�D]}��t���t�d�qdS)N�g����MbP?)rN�extendrr,�time�sleep)rZ��fdsrr�open_fds�sz<ProcessTestCase.test_double_close_on_error.<locals>.open_fds)�targetr/)
�	threading�Thread�startrN�EnvironmentErrorrr3r�r4r�rr8rS)rr��t�exc�fdr[rr�r�test_double_close_on_error�s:�&�
z*ProcessTestCase.test_double_close_on_errorcs�t�tjddg��|��jd�g���fdd�}t�d|�}|��t	rPd}nd}�j
tjd	�|j�j|d
d���
�|j�j|dd�|�
�|�d
d|fd|fg��dS)z?Issue21291: Popen.wait() needs to be threadsafe for returncode.rzimport time; time.sleep(12)NcsN��d���f���������d�jf������d�jf�dS)N�thread-start-poll-result�thread-after-kill-and-wait�thread-after-second-wait)rOrf�killrrr�r}�resultsrr�kill_proc_timer_thread	szDProcessTestCase.test_threadsafe_wait.<locals>.kill_proc_timer_thread皙�����?r�i����rLz*unexpected result in wait from main thread��msgz&unexpected result in second main wait.)r�Nr�r�)rr3rDrErFrr��Timerr�rrrrlr�)rr�r�Zexpected_errorcoderr�r�test_threadsafe_waits4
�
�
���z$ProcessTestCase.test_threadsafe_waitcCs6d�d�}t�tjd|g�}|�|�d�t|��dS)N�;)zimport subprocess, syszJretcode = subprocess.call([sys.executable, '-c', 'print("Hello World!")'])zassert retcode == 0rsHello World!)r�rr\rDrEr&r
�ascii)rr�r_rrr�test_issue87806s
zProcessTestCase.test_issue8780cCs�t��\}}t��\}}t��\}}ztjdg|||d�WnNty�t�|�t�|�t�|�t�|�t�|�t�|�Yn0|�tj	�
|��|�tj	�
|��|�tj	�
|��dS)N�*r/)rk�mkstemprr3rSrr8�remover r��exists)rZifhandleZifnameZofhandleZofnameZefhandleZefnamerrr� test_handles_closed_on_exceptionAs"�





z0ProcessTestCase.test_handles_closed_on_exceptioncCsTtjttjtjtjd�}|�|jj�|�|jj�|�|jj�|�	dd�dS)Nr/�x�)
rr3rr4rmr1r8r2r0r�r9rrr�test_communicate_epipeUs�z&ProcessTestCase.test_communicate_epipec	
Cs�t�d�}|jj}dddgdddfgd�dddf|dd	d
|�d�fg}tjj�tj	d��F|D]0\}}}}tj	||d
�}||_
|�t|�|�q^Wd�n1s�0YdS)Nz
my-tool.py)�lsT�{z#<Popen: returncode: 123 args: 'ls'>)�daaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaTrzP<Popen: returncode: 0 args: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...>r�Fz&<Popen: returncode: None args: ['ls']>)r�z	--my-optsr�zP<Popen: returncode: None args: ['ls', '--my-opts', 'aaaaaaaaaaaaaaaaaaaaaaaa...>�z<Popen: returncode: 7 args: z('my-tool.py')>r,�r�)
�pathlib�Path�	__class__r$�unittestr�patch�objectrr3rrF�repr)	rZpath_cmdZpathlib_clsZcasesrr�r�Zsxr:rrr�	test_repr`s

��	zProcessTestCase.test_reprcCs8tjttjd�}|�|jj�|��|�dd�dS)Nrir�r�)	rr3rr4rmr0r8rr�r9rrr�!test_communicate_epipe_only_stdinss�z1ProcessTestCase.test_communicate_epipe_only_stdin�SIGUSR1zRequires signal.SIGUSR1r�zRequires os.kill�getppidzRequires os.getppidc	Cs�dd�}t�tj|�}|�tjtj|�tjddg}dD]H}|tji}tj|fi|���}|��Wd�q81sv0Yq8dS)NcSsdSrr)�signum�framerrr�handler�sz7ProcessTestCase.test_communicate_eintr.<locals>.handlerrz7import os, signal;os.kill(os.getppid(), signal.SIGUSR1)r�)	�signalr�rmrDrErr4r3r�)rr��old_handlerr*�stream�kw�processrrr�test_communicate_eintr{s�
z&ProcessTestCase.test_communicate_eintr�/proc/%d/fdzLinux specificcCspdt��}t�|�}|�t��&tttjtjtjd�Wd�n1sL0Yt�|�}|�	||�dS)z;Test for the fork() failure fd leak reported in issue16327.r�r/N)
r�getpid�listdirrNr'r(rrr4rF)rZfd_directoryZfds_before_popenZfds_after_exceptionrrr�!test_failed_child_execute_fd_leak�s
�$
z1ProcessTestCase.test_failed_child_execute_fd_leakz+behavior currently not supported on WindowscCsL|�t��}t�gd��Wd�n1s.0Y|�|jjd�dS)N)�/opt/nonexistent_binary�withZsomer*r�)rNr�rrCrFrU�filenamerVrrr�%test_file_not_found_includes_filename�s,z5ProcessTestCase.test_file_not_found_includes_filenamecCsP|�t��"}tjddgdd�Wd�n1s20Y|�|jjd�dS)N�exit�0z/some/nonexistent/directoryr�)rNr�rr3rFrUr�rVrrr� test_file_not_found_with_bad_cwd�s0z0ProcessTestCase.test_file_not_found_with_bad_cwdcCs,|�tjttj�|�tjttj�dSr)r5rr3�bytes�types�GenericAlias�CompletedProcessrkrrrr�test_class_getitems�sz#ProcessTestCase.test_class_getitemsN)zr$r%r&r;r>rIrPrSrXr`rbrfrqrtrxr{r}r�r�r�r�r�r�r�r�r�r�r�r�r�r��skipIfrr�r�r�r�r�r�r�r�r�r�r�r�rD�base_prefix�prefixr��	sysconfigZis_python_buildr�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrr�platformZget_config_varrrrrrrrr!r$r)r*r0r1r7r8r<r=r>rArJr^rcrgrirmrnrorrrurxryr~r�r�r�r�r�r�r��
skipUnless�hasattrr�rr�r��isdirr�r�r�r�r�rrrrr.gs		

		










�
�
�

	
��
'')4����




r.c@s�eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Ze�ed%�d&d'��Zd(S))�RunFuncTestCasecKstjd|g}tj|fi|��S)z4Run Python code in a subprocess using subprocess.runr)rDrErr)rr�r+�argvrrr�
run_python�szRunFuncTestCase.run_pythoncCsP|�d�}|�|jd�|�tj��|��Wd�n1sB0YdSr�)r�rFrrNrrT�check_returncode�r�cprrr�test_returncode�s
zRunFuncTestCase.test_returncodecCsN|�tj��}|jddd�Wd�n1s00Y|�|jjd�dS)Nr@T��checkrA)rNrrTr�rFrUrrVrrr�
test_check�s,zRunFuncTestCase.test_checkcCs tjtdd�}|�|jd�dS)NTr�r)rrrrFrr�rrr�test_check_zero�szRunFuncTestCase.test_check_zerocCs>|�tj��|jddd�Wd�n1s00YdS)NrJrjrL)rNrrOr�rrrr�test_timeout�szRunFuncTestCase.test_timeoutcCs"|jdtjd�}|�d|j�dS)NrYrrZ�r�rr4r]r1r�rrr�test_capture_stdout�sz#RunFuncTestCase.test_capture_stdoutcCs"|jdtjd�}|�d|j�dS)NrcrdrZ)r�rr4r]r2r�rrr�test_capture_stderr�s�z#RunFuncTestCase.test_capture_stderrcCsLt��}|�|j�|�d�|�d�|jd|tjd�}|�	d|j
�dS)Nrgrrhr�rj)rkrlrmr8rnror�rr4r]r1)rrpr�rrrrq�s

�z+RunFuncTestCase.test_check_output_stdin_argcCs$|jddtjd�}|�d|j�dS)Nrhrg)rsr1rjr�r�rrrrt�s
�z+RunFuncTestCase.test_check_output_input_argcCs�t��}|�|j�|�d�|�d�|jtdd�� }|jd|dd�}Wd�n1s\0Y|�	d|j
jd�|�	d	|j
jd�dS)
Nrgrr�r�r~r�r�r0rs)rkrlrmr8rnrorNr�r�r]rUr*r�rrrr��s

��$z6RunFuncTestCase.test_check_output_stdin_with_input_argcCsb|�tj��"}|jddtjd�}Wd�n1s40Y|�|jjd�|�|jjd�dS)Nr�r�)rMr1rZ)	rNrrOr�r4rFrUr_r1)rrWr�rrrr��s�$z)RunFuncTestCase.test_check_output_timeoutcCs2tj��}d|d<|jd|d�}|�|jd�dS)Nr�r�zCimport sys, os;sys.exit(33 if os.getenv("FRUIT")=="banana" else 31)r��!)rr�r�r�rFr)rr�r�rrr�test_run_kwargss
�zRunFuncTestCase.test_run_kwargscCs�trdnd}t�|�}|dur.|�|�d��t|�}tj|tjd�}|�|j	d�|�
t��"tj|tjdd�Wd�n1s�0YdS)Nztree.comr�z required for this testrrT)r1r�)rr
rrUrrrrrFrrNr�)r�progr��resrrr�test_run_with_pathlike_paths
z+RunFuncTestCase.test_run_with_pathlike_pathcCs2t�tj�}|ddg}t�|�}|�|jd�dS)Nrsimport sys; sys.exit(57)�9)rr�rDrErrrFr�rr�r*r�rrr�&test_run_with_bytes_path_and_arguments"s

z6RunFuncTestCase.test_run_with_bytes_path_and_argumentscCs0ttj�}|ddg}t�|�}|�|jd�dS)Nrzimport sys; sys.exit(57)r�)rrDrErrrFrr�rrr�)test_run_with_pathlike_path_and_arguments)s


z9RunFuncTestCase.test_run_with_pathlike_path_and_argumentscCs.|jddd�}|�d|j�|�d|j�dS)Nz>import sys;sys.stdout.write('BDFL'); sys.stderr.write('FLUFL')T)�capture_outputrZsFLUFL)r�r]r1r2r�rrr�test_capture_output0s
�z#RunFuncTestCase.test_capture_outputcCs~t��}|�|j�|jtdd�� }|jdd|d�}Wd�n1sH0Y|�d|jj	d�|�d|jj	d�dS)	NzAExpected ValueError when stdout and capture_output args supplied.r�r~T)r�r1r1rr��
rkrlrmr8rNr�r�r]rUr*r�rrr�#test_stdout_with_capture_output_arg8s��$z3RunFuncTestCase.test_stdout_with_capture_output_argcCs~t��}|�|j�|jtdd�� }|jdd|d�}Wd�n1sH0Y|�d|jj	d�|�d|jj	d�dS)	NzAExpected ValueError when stderr and capture_output args supplied.r�r~T)r�r2r2rr�r�r�rrr�#test_stderr_with_capture_output_argDs��$z3RunFuncTestCase.test_stderr_with_capture_output_argz)requires posix like 'sleep' shell commandc
Cs�t��}ztjddddd�Wn6tjyT}zt��}t��}WYd}~nd}~00|�d�|j||dd|�d	�d
�dS)zJOutput capturing after a timeout mustn't hang forever on open filehandles.zsleep 3TrK)r�rMr�NzTimeoutExpired not raised.g�?z/TimeoutExpired was delayed! Bad traceback:
```
z```r�)	r��	monotonicrrrO�	traceback�
format_excr�Z
assertLess)rZbefore_secsr�Z
after_secsZstacksrrr�.test_run_with_shell_timeout_and_capture_outputTs
�

��z>RunFuncTestCase.test_run_with_shell_timeout_and_capture_outputN)r$r%r&r�r�r�r�r�r�r�rqrtr�r�r�r�r�r�r�r�r�r�r�rr�rrrrr��s(

r�c	CsJdD]6}trzt�|�Wnty0YqYn0|Sqt�d��dS)N)ZstaffZnogroup�grp�nobodyZ	nfsnobodyz?No identified group name to use for this test on this platform.)r��getgrnam�KeyErrorr��SkipTest)�
name_grouprrr�_get_test_grp_namees

r�zPOSIX specific testscseZdZ�fdd�Zdd�Zdd�Zdd�Zd	d
�ZGdd�dej	�Z
e�d
�dd��Z
e�d
�dd��Ze�ej�d�d�dd��Zdd�Ze�eed�d�dd��Ze�eed�d�dd��Ze�eed�d �d!d"��Ze�eed�d#�d$d%��Ze�eed&�d'�d(d)��Ze�eed&�d*�d+d,��Ze�e�p@eed-�d.�d/d0��Zd1d2�Z d3d4�Z!d5d6�Z"d7d8�Z#d9d:�Z$d;d<�Z%Gd=d>�d>ej	�Z&e�ej�d?�d@�dAdB��Z'dCdD�Z(e�e)j*dEkdF�dGdH��Z+dIdJ�Z,dKdL�Z-dMdN�Z.dOdP�Z/dQdR�Z0dSdT�Z1dUdV�Z2e�e)j*�3dW�dX�dYdZ��Z4d[d\�Z5d]d^�Z6d_d`�Z7dadb�Z8dcdd�Z9dedf�Z:dgdh�Z;didj�Z<dkdl�Z=dmdn�Z>dodp�Z?dqdr�Z@dsdt�ZAdudv�ZBdwdx�ZCdydz�ZDd{d|�ZEd}d~�ZFdd��ZGd�d��ZHd�d��ZId�d��ZJd�d��ZKd�d��ZLd�d��ZMd�d��ZNd�d��ZOd�d��ZPe�e)j*�3d���o,e�Qd��jRe�Qd��jRkd��d�d���ZSeT�Ud�d��d�d���ZVd�d��ZWd�d��ZXd�d��ZYd�d��ZZd�d��Z[d�d��Z\d�d��Z]d�d��Z^d�d��Z_d�d��Z`eTjad�d���ZbeTjad�d���Zcd�d��Zdd�d��Zed�d��Zfd�d��Zge�ehd�u�o�eehd��d��d�d���Zid�dÄZjd�dńZkd�dDŽZl�ZmS)��POSIXProcessTestCasecst���d|_dS)Nz/_this/pa.th/does/not/exist)�superr�_nonexistent_dirr�r�rrrts
zPOSIXProcessTestCase.setUpc
CsNzt�|j�Wn(ty8}z|}WYd}~nd}~00|�d|j�|S)Nz,chdir to nonexistent directory %s succeeded.)r�chdirrrSr�)rr[�desired_exceptionrrr�_get_chdir_exceptionxs�z)POSIXProcessTestCase._get_chdir_exceptionc
Cs�|��}ztjtjddg|jd�}WnTtyx}z<|�|j|j�|�|j	|j	�|�|j
|j
�WYd}~nd}~00|�d|�dS)z;Test error in the child raised in the parent for a bad cwd.rrr�N�Expected OSError: %s�rrr3rDrErrSrFrT�strerrorr�r��rrr:r[rrr�test_exception_cwd�s�
&z'POSIXProcessTestCase.test_exception_cwdc
Cs�|��}ztjtjddg|jd�}WnTtyx}z<|�|j|j�|�|j	|j	�|�|j
|j
�WYd}~nd}~00|�d|�dS)zBTest error in the child raised in the parent for a bad executable.rrr�Nrrr	rrr�test_exception_bad_executable�s�
&z2POSIXProcessTestCase.test_exception_bad_executablec
Cs�|��}zt�|jddg�}WnTtyr}z<|�|j|j�|�|j|j�|�|j|j�WYd}~nd}~00|�	d|�dS)z?Test error in the child raised in the parent for a bad args[0].rrNr)
rrr3rrSrFrTrr�r�r	rrr�test_exception_bad_args_0�s&z.POSIXProcessTestCase.test_exception_bad_args_0c@seZdZdd�ZdS)z&POSIXProcessTestCase.PopenNoDestructorcCsdSrrrrrr�__del__�sz.POSIXProcessTestCase.PopenNoDestructor.__del__N)r$r%r&r
rrrr�PopenNoDestructor�srz%subprocess._posixsubprocess.fork_execc	Csvdd�}||_tjdtd��F|�t��|�dg�Wd�n1sJ0YWd�n1sh0YdS)z>Test error passing done through errpipe_write in the good casecWs0|d}d�tj���}t�|d|d�dS)N�
z{:x}sOSError:�:r)rHrTZEISDIRr rrn)r*�
errpipe_writeZerr_coderrr�proper_error�szHPOSIXProcessTestCase.test_exception_errpipe_normal.<locals>.proper_error�subprocess.os.waitpid��side_effect�non_existent_commandN)rrr��ChildProcessErrorrN�IsADirectoryErrorr)r�	fork_execrrrr�test_exception_errpipe_normal�s�z2POSIXProcessTestCase.test_exception_errpipe_normalc	s�d��fdd�}||_tjdtd��H|�tj��}|�dg�Wd�n1sT0YWd�n1sr0Y|�t	��t
|j��dS)zZTest error passing done through errpipe_write where its not
        in the expected formats�ޭcs|d}t�|��dS)Nrr)rrn)r*r�Z
error_datarr�	bad_error�szGPOSIXProcessTestCase.test_exception_errpipe_bad_data.<locals>.bad_errorrrrN)rrr�rrNr�SubprocessErrorrr]r�rkrU)rrrr[rrr�test_exception_errpipe_bad_data�s�Hz4POSIXProcessTestCase.test_exception_errpipe_bad_data�/proc/self/statuszneed /proc/self/statuscCs�tjddgdd�}|��D]}|�d�r|}q<q|�d�tjddgdd�}|��D]}|�d�rV|}qnqV|j||dd	�dS)
N�catrF)�restore_signalssSigIgnz&SigIgn not found in /proc/self/status.Tz=restore_signals=True should've unblocked SIGPIPE and friends.r�)rr\�
splitlinesr
rU�assertNotEqual)rZdefault_proc_statusrqZdefault_sig_ign_maskZrestored_proc_statusZrestored_sig_ign_maskrrr�test_restore_signals�s(�

�
�z)POSIXProcessTestCase.test_restore_signalsc
Cspztjtjddgdd�}Wn2tyL}z|jtjkr8�WYd}~n(d}~00t�d�}t	|�}|�
||�dS)Nrzimport os; print(os.getsid(0))T)�start_new_sessionr)rr\rDrErSrT�EPERMr�getsid�intr#)rr_r[Z
parent_sidZ	child_sidrrr�test_start_new_session�s
�

z+POSIXProcessTestCase.test_start_new_session�setreuidzno setreuid on platformc

Cst��}|dkrdnd|g}tjdkr*dnd}tdurfzt�|�|�|�Wntydd}Yn0|D]�}dD]�}|j||d���zt	j
tjdd	g||d�}WnHty�Ynjt
y�}z |jtjtjfvrւWYd}~n<d}~00t|t��rt�|�j}n|}t|�}	|�|	|�Wd�qr1�s40Yqrqj|�t��t	jtd
d�Wd�n1�sr0Y|�t��&t	jttjtjdd
�Wd�n1�s�0Ytdu�r|du�r|�t��t	jt|d�Wd�n1�s0YdS)N���darwinr��unknownr%)�user�	close_fdsrzimport os; print(os.getuid())����r/�)r�r�r/)r�geteuidrDr��pwd�getpwnamrOr��subTestrr\rE�PermissionErrorrSrTZEACCESr&�
isinstancerk�pw_uidr(rFrNr�rRr�
OverflowErrorr�r�)
r�uidZ
test_usersZname_uidr/r0r_r[Zuser_uidZ
child_userrrr�	test_usersN

��
0.
�&zPOSIXProcessTestCase.test_userz setreuid() available on platformcCs<|�t��tjtdd�Wd�n1s.0YdS)N�r2�rNr�rrRrrrrr�test_user_error?sz$POSIXProcessTestCase.test_user_error�setregidzno setregid() on platformc	Cs�t��}|dkrdndg}t�}tdur2|�|�||gD]�}dD]�}|j||d��pztjtj	ddg||d�}Wnt
y�Yn20t|t�r�t�
|�j}n|}t|�}|�||�Wd�qD1s�0YqDq<|�t��tjtdd�Wd�n1�s
0Y|�t��&tjttjtjd	d
�Wd�n1�sL0Ytdu�r�|�t��tjt|d�Wd�n1�s�0YdS)Nr+r,r%)�groupr0rzimport os; print(os.getgid())r1�rBr3)r�r�rB)r�getegidr�r�rOr7rr\rDrEr8r9rkr��gr_gidr(rFrNr�rRrr;r�r�)	r�gid�
group_listr�rBr0r_Z	group_gidZchild_grouprrr�
test_groupDs@
��

..
�&
zPOSIXProcessTestCase.test_groupz setregid() available on platformcCs<|�t��tjtdd�Wd�n1s.0YdS)Nr>rCr?rrrr�test_group_errornsz%POSIXProcessTestCase.test_group_error�	setgroupszno setgroups() on platformc

Cs�t��}|dkrdndg}t�}d}tdur6|�|�ztjtjddg|d�}Wn6t	y�}z|j
t
jkrn�d}WYd}~nfd}~00t��}t
�|�}tdur�dd	�|D�}	n|}	|r�|�t|�t|��n|�t|	�t|��|�t�� tjtd
gd�Wd�n1�s0Y|�t��(tjttjtjdgd�Wd�n1�sZ0Ytdu�r�|�t�� tjt|gd�Wd�n1�s�0YdS)
Nr+r,Frz;import os, sys, json; json.dump(os.getgroups(), sys.stdout)��extra_groupsTcSs&g|]}t|t�rt�|�jn|�qSr)r9rkr�r�rE)r
�grrrr�s�z:POSIXProcessTestCase.test_extra_groups.<locals>.<listcomp>r1r3)r�r�rL)rrDr�r�rOrr\rDrErSrTr&�	getgroups�json�loadsrF�setrNr�rRrr�r�)
rrFrGr�Z
perm_errorr_�exZ
parent_groupsZchild_groupsZdesired_gidsrrr�test_extra_groupsssN
��

�0�&
�z&POSIXProcessTestCase.test_extra_groupsz!setgroups() available on platformcCs<|�t��tjtgd�Wd�n1s.0YdS)NrKr?rrrr�test_extra_groups_error�sz,POSIXProcessTestCase.test_extra_groups_error�umaskzPOSIX umask() is not available.c	Cs�d}z�t��}tj�|d�}tjtjdd|�d�gdd�t�	|�j
d@}d}|j||t|��d	t|���d
�W|dur�t
�|�n|dur�t
�|�0dS)NZbeansrzopen(z, 'w').close()�+)rUi�i�z != r�)rkrMrr�r�rrRrDrE�stat�st_moderF�octr
rW)rrY�namerXZ
expected_moderrr�
test_umask�s"���zPOSIXProcessTestCase.test_umaskcCsXt���*t�tjddg�}|��Wd�n1s80Y|�|jt	j
�dS)Nrzimport os; os.abort())rrprr3rDrErrFrr��SIGABRTr9rrr�test_run_abort�s

�&z#POSIXProcessTestCase.test_run_abortcCsNt�ttj�d�}t|�}|�d|���|�d|�|�ttj�|�dS)N�fake cmdr�ZSIG)rrTr(r�r\rkr]�lower�rr�Zerror_stringrrr�"test_CalledProcessError_str_signal�s
z7POSIXProcessTestCase.test_CalledProcessError_str_signalcCs$t�dd�}t|�}|�d|�dS)Ni�Ki�r^zunknown signal 9876543.�rrTrkr]r`rrr�*test_CalledProcessError_str_unknown_signal�sz?POSIXProcessTestCase.test_CalledProcessError_str_unknown_signalcCs$t�dd�}t|�}|�d|�dS)N�r^znon-zero exit status 2.rbr`rrr�$test_CalledProcessError_str_non_zero�sz9POSIXProcessTestCase.test_CalledProcessError_str_non_zerocCsXtjtjddgtjdd�d�}|�"|�|j��d�Wd�n1sJ0YdS)NrrcSst�dd�S)Nr��apple)r�putenvrrrr�<lambda>�r�z3POSIXProcessTestCase.test_preexec.<locals>.<lambda>)r1�
preexec_fn�appler�r9rrr�test_preexec�s
��z!POSIXProcessTestCase.test_preexecc
Cs�dd�}ztjtjddg|d�}WnhtjyV}z|�tjd�WYd}~nHd}~0ty�}z|�d|j	d�WYd}~nd}~00|�
d	�dS)
NcSstd��dS)Nz'What if two swallows carried a coconut?�r�rrrr�raise_it�sz=POSIXProcessTestCase.test_preexec_exception.<locals>.raise_itrr�riz)Expected a ValueError from the preexec_fnZcoconutrzEException raised by preexec_fn did not make it to the parent process.)rr3rDrErr&�_posixsubprocessr�r]r*r�)rrmr:r[rrr�test_preexec_exception�s�
�(z+POSIXProcessTestCase.test_preexec_exceptionc@s eZdZdZdd�Zdd�ZdS)z+POSIXProcessTestCase._TestExecuteChildPopenz3Used to test behavior at the end of _execute_child.cOs$||_tjj|g|�Ri|��dSr)�	_testcaserr3r�)rZtestcaser*r+rrrr��sz4POSIXProcessTestCase._TestExecuteChildPopen.__init__cOsz�tjj|g|�Ri|��Wdd�td�D�}zL|D].}|jj||j��|j��|j	��fdd�q6W|D]}t
�|�qln|D]}t
�|�q�0nxdd�td�D�}zL|D].}|jj||j��|j��|j	��fdd�q�W|D]}t
�|�q�n|D]}t
�|�q�00dS)NcSsg|]}t�dtj��qS)�	/dev/zero)rrP�O_RDONLY�r
r#rrrrs�zNPOSIXProcessTestCase._TestExecuteChildPopen._execute_child.<locals>.<listcomp>�z!At least one fd was closed early.r�)rr3r,rNrqrwr0r�r1r2rr8)rr*r+Zdevzero_fdsr�rrrr,�s@���
�����
�z:POSIXProcessTestCase._TestExecuteChildPopen._execute_childN)r$r%r&r-r�r,rrrr�_TestExecuteChildPopen�srvrrz/dev/zero required.c	CsTdd�}|�tj��,|j|ttjtjtj|d�Wd�n1sF0YdS)z6Issue16140: Don't double close pipes on preexec error.cSst�d��dS)Nz-force the _execute_child() errpipe_data path.)rrrrrrrms�zWPOSIXProcessTestCase.test_preexec_errpipe_does_not_double_close_pipes.<locals>.raise_it)r0r1r2riN)rNrrrvrr4)rrmrrr�0test_preexec_errpipe_does_not_double_close_pipess�zEPOSIXProcessTestCase.test_preexec_errpipe_does_not_double_close_pipescCs&dd�}t��}tj}tj}z�t��|�t���tjtjddgdd�d�|�t��d�t��|�	t���tjtjddgd	d�d�|�	t��d
�|t_|j
ttjtjddgdd�d�t`|j
t
tjtjddgdd�d�W|t_|t_|�s"t��n|t_|t_|�s t��0dS)
NcSstd��dS)Nzthis shouldn't escape)�RuntimeErrorrrrr�raise_runtime_error!szPPOSIXProcessTestCase.test_preexec_gc_module_failure.<locals>.raise_runtime_errorrrcSsdSrrrrrrrh*r�zEPOSIXProcessTestCase.test_preexec_gc_module_failure.<locals>.<lambda>rnz#Popen enabled gc when it shouldn't.cSsdSrrrrrrrh1r�zPopen left gc disabled.cSsdSrrrrrrrh7r�cSsdSrrrrrrrh<r�)�gc�	isenabled�disabler rrCrDrE�enabler&rNrxr3r{)rryZenabledZorig_gc_disableZorig_gc_isenabledrrr�test_preexec_gc_module_failuresJ�
��

�

�
�z3POSIXProcessTestCase.test_preexec_gc_module_failurer-z!setrlimit() seems to fail on OS Xc
Cs�zddlm}m}m}Wn.tyF}z|�|�WYd}~n
d}~00||�}|\}}||d|f�|�|||�ztjt	j
ddgdd�d�Wnty�Yn0|�d�dS)	Nr)�	getrlimit�	setrlimit�RLIMIT_NPROCrrcSsdSrrrrrrrhRr�z@POSIXProcessTestCase.test_preexec_fork_failure.<locals>.<lambda>rnz.RLIMIT_NPROC had no effect; probably superuser)�resourcerr�r�r|rUrmrrCrDrE�BlockingIOError)rrr�r�r�Zlimitsr#Zhardrrr�test_preexec_fork_failureCs �
z.POSIXProcessTestCase.test_preexec_fork_failurecCs�t��\}}t|ddd��0}|�dtj�|�dtj�Wd�n1sP0Yt�	|d�t
�|�}|��t�
|�|�|jd�dS�N�wrD)rGz#!%s
z(exec '%s' -c 'import sys; sys.exit(47)'
i�rA)rkr�rPrnr�
unix_shellrDrEr�chmodrr3rr�rFr)rr��fname�fobjr:rrr�test_args_stringYs�$

z%POSIXProcessTestCase.test_args_stringcCs<|jttjtjddgdd�|jttjtjddgdd�dS)Nrr@rA��startupinfo��
creationflags�rNr�rrCrDrErrrrr�gs
��
��z&POSIXProcessTestCase.test_invalid_argscCshtj��}d|d<tjdgdtj|d�}|�(|�|j���	d�d�Wd�n1sZ0YdS�Nrfr�zecho $FRUITr��r�r1r�s 	
rj�
rr�r�rr3r4rFr1r�r�rr�r:rrr�test_shell_sequencers

�z(POSIXProcessTestCase.test_shell_sequencecCsftj��}d|d<tjddtj|d�}|�(|�|j���	d�d�Wd�n1sX0YdSr�r�r�rrr�test_shell_string|s
�z&POSIXProcessTestCase.test_shell_stringcCs�t��\}}t|ddd��0}|�dtj�|�dtj�Wd�n1sP0Yt�	|d�t
�|�}t�|�|�
|d�dSr�)rkr�rPrnrr�rDrErr�rrCr�rF)rr�r�r�rHrrr�test_call_string�s�$

z%POSIXProcessTestCase.test_call_stringc	Cs�g}dD]2}dD](}tj�||�}tj�|�r|�|�qq|sJ|�d�d}tj�|�rptj�|�sp|�|�|D]X}tjd|dtj	d�}|�,|�
|j���
�t|d��Wd�qt1s�0YqtdS)	N)z/binz	/usr/bin/z/usr/local/bin)ZbashZkshz"bash or ksh required for this testz/bin/shzecho $0T)rEr�r1r�)rr�r��isfilerOrU�islinkrr3r4rFr1r�rr�)rZshellsr�rZ�shr:rrr�test_specific_shell�s"


�z(POSIXProcessTestCase.test_specific_shellc
Gstt�tjtj�}z4tjtjddgdtjtjtjd�}Wt�tj|�nt�tj|�0|j�	d�t
||�|�|S)Nrz�if 1:
                                 import sys, time
                                 sys.stdout.write('x\n')
                                 sys.stdout.flush()
                                 time.sleep(30)
                                 T�r0r0r1r2r�)r��SIGINT�default_int_handlerrr3rDrEr4r1r��getattr)r�methodr*r�r:rrr�
_kill_process�s� z"POSIXProcessTestCase._kill_process)ZnetbsdZopenbsdz"Due to known OS bug (issue #16762)cGsRtjtjddgdtjtjtjd�}|j�d�t�d�t	||�|�|�
�dS)Nrz�if 1:
                             import sys, time
                             sys.stdout.write('x\n')
                             sys.stdout.flush()
                             Tr�r�)rr3rDrEr4r1r�r�r�r�r�)rr�r*r:rrr�_kill_dead_process�s�
z'POSIXProcessTestCase._kill_dead_processcCs:|�dtj�}|��\}}|�d|�|�|��d�dS)N�send_signalsKeyboardInterruptr)r�r�r�r�r]r#r�rr:r#r2rrr�test_send_signal�sz%POSIXProcessTestCase.test_send_signalcCs:|�d�}|��\}}|�|d�|�|��tj�dS)Nr�r�)r�r�rFrr��SIGKILLr�rrr�	test_kill�s
zPOSIXProcessTestCase.test_killcCs:|�d�}|��\}}|�|d�|�|��tj�dS)N�	terminater�)r�r�rFrr��SIGTERMr�rrr�test_terminate�s
z#POSIXProcessTestCase.test_terminatecCs|�dtj�dS�Nr�)r�r�r�rrrr�test_send_signal_dead�sz*POSIXProcessTestCase.test_send_signal_deadcCs|�d�dS�Nr��r�rrrr�test_kill_dead�sz#POSIXProcessTestCase.test_kill_deadcCs|�d�dS�Nr�r�rrrr�test_terminate_dead�sz(POSIXProcessTestCase.test_terminate_deadcCs6g}|D](}t�|�}t�|�}|�|||f�q|Sr)r�get_inheritable�duprO)rZsave_fdsr�r��inheritable�savedrrr�	_save_fds�s

zPOSIXProcessTestCase._save_fdscCs.|D]$\}}}tj|||d�t�|�qdS)N)r�)r�dup2r8)rr�r�r�r�rrr�_restore_fds�sz!POSIXProcessTestCase._restore_fdsc		Cs�d}|�|�}|D]\}}}|dkr|}q.qz`|D]}t�|�q4tjtjddg|tjtjd���\}}|�	|d�|�	|d�W|�
|�n|�
|�0dS)Nrrr�r/rjr�)r�rr8rr3rDrEr4r�rFr�)	rr�r0�	saved_fdsr�r�r�r�r�rrr�check_close_std_fds	s&

��z(POSIXProcessTestCase.check_close_std_fdscCs|�dg�dSrQ�r�rrrr�test_close_fd_0	sz$POSIXProcessTestCase.test_close_fd_0cCs|�dg�dS�Nr�r�rrrr�test_close_fd_1	sz$POSIXProcessTestCase.test_close_fd_1cCs|�dg�dS)Nrdr�rrrr�test_close_fd_2!	sz$POSIXProcessTestCase.test_close_fd_2cCs|�ddg�dS)Nrr�r�rrrr�test_close_fds_0_1$	sz'POSIXProcessTestCase.test_close_fds_0_1cCs|�ddg�dS)Nrrdr�rrrr�test_close_fds_0_2'	sz'POSIXProcessTestCase.test_close_fds_0_2cCs|�ddg�dS)Nr�rdr�rrrr�test_close_fds_1_2*	sz'POSIXProcessTestCase.test_close_fds_1_2cCs|�gd��dS)N)rr�rdr�rrrr�test_close_fds_0_1_2-	sz)POSIXProcessTestCase.test_close_fds_0_1_2c
Cs�t�d�}t�d�}zZt�d�t�d�t�tjddg���Wt�|d�t�|d�t�|�t�|�n.t�|d�t�|d�t�|�t�|�0dS)z=Issue #15798: Popen should work when stdio fds are available.rr�rz*print('AssertionError:0:CLOEXEC failure.')N)	rr�r8rr3rDrErr�)rZ	new_stdinZ
new_stdoutrrr�test_small_errpipe_write_fd2	s"



�
�
z0POSIXProcessTestCase.test_small_errpipe_write_fdc

Cs^dd�td�D�}�z,dd�|D�}|D]\}}t�|�q(t�|dd�t�|ddd�|�td��}zZt|�D]\}}t�||�qvtj	t
jdd	g|d|d
|dd�}|��W|�
|�n|�
|�0|D]}t�|dd�q�t�|d
d�}t�|dd���}	|�|d
�|�|	d�W|D]}t�|��q.n|D]}t�|��qF0dS)NcSsg|]}t���qSr�rkr��r
rZrrrrI	r�z?POSIXProcessTestCase.test_remapping_std_fds.<locals>.<listcomp>r�cSsg|]\}}|�qSrr�r
r�r�rrrrK	r�r��STDINrr�Zimport sys; got = sys.stdin.read();sys.stdout.write("got %s"%got); sys.stderr.write("err")rdr/r��	got STDIN�err)rNr�unlinkrnr�r��	enumerater�rr3rDrErr�r�rrFr8)
r�temps�temp_fdsr�r�r��temp_fdr:r�r�rrr�test_remapping_std_fdsG	s<
��
�z+POSIXProcessTestCase.test_remapping_std_fdsc

CsBdd�td�D�}dd�|D�}�z|D]\}}t�|�q(|�td��}z�t|�D]\}}	t�|	|�qTt�|d�t�|dd�tj	t
jddg|||d	�}
|
��|D]}t�|dd�q�t�
|d
�}t�
|d
���}W|�|�n|�|�0|�|d�|�|d�W|D]}t�|��qn|D]}t�|��q*0dS)
NcSsg|]}t���qSrr�r�rrrrv	r�z7POSIXProcessTestCase.check_swap_fds.<locals>.<listcomp>r�cSsg|]\}}|�qSrrr�rrrrw	r�r�rrr�r/r�r�r�)rNrr�r�r�r�rnr�rr3rDrErr�rr�rFr8)
rZstdin_noZ	stdout_noZ	stderr_nor�r�r�r�r�r�r:r�r�rrr�check_swap_fdst	s<
���z#POSIXProcessTestCase.check_swap_fdscCsX|�ddd�|�ddd�|�ddd�|�ddd�|�ddd�|�ddd�dS)Nrr�rd)r�rrrr�
test_swap_fds�	sz"POSIXProcessTestCase.test_swap_fdscCs�|�td��}�zj|D]<}t��� }t�|��|�Wd�q1sH0Yqttd��t|���}t�	|�gd�}i}t
||�D]\}}	||||	<q�t�d�}
ttd��t|���}t
jtjd|
t|�gfi|��}|�|d�t
||�D]r\}}	t�|dtj�t�|d�}
ttt|
�d���}t�d|�d	|�d
|�d|�d|	�d
��}|�|	g||�q�W|�|�n|�|�0dS)Nr�r/z�
                import os, sys
                skipped_fd = int(sys.argv[1])
                for fd in range(3):
                    if fd != skipped_fd:
                        os.write(fd, str(fd).encode('ascii'))
            rrr�r�z"
                    When testing z to z4 redirection,
                    parent descriptor z5 got redirected
                    to descriptor(s) z instead of descriptor z.
                )r�rNrkrlrr�r�rQrVr8�zipr:r;rrCrDrErkrFr��SEEK_SETr�r�mapr(r�r�)r�from_fds�to_fdsr�Zfrom_fd�fZfd_to_close�	arg_namesr+Zto_fdr�Z
skipped_fdrH�
read_bytesZread_fdsr�rrr�#_check_swap_std_fds_with_one_closed�	sD
0

������
z8POSIXProcessTestCase._check_swap_std_fds_with_one_closedcCs<t�td�d�D]&}t�td�d�D]}|�||�q$qdS)Nr�rd)�	itertools�combinationsrN�permutationsr�)rr�r�rrr�!test_swap_std_fds_with_one_closed�	sz6POSIXProcessTestCase.test_swap_std_fds_with_one_closedc
Cs�dd�}ztjt|d�Wn�tyZ}z(|�tj�|�t|�d�WYd}~nTd}~0tjy�}z(|�	tj�|�t|�d�WYd}~nd}~00|�
d�dS)NcSstd��dS)N�
surrogate:�rlrrrr�prepare�	szCPOSIXProcessTestCase.test_surrogates_error_message.<locals>.preparernr�z!Exception occurred in preexec_fn.z1Expected ValueError or subprocess.SubprocessError)rrCrr�rerorFrkr�assertIsNotNoner�)rr�r�rrr�test_surrogates_error_message�	s�
$&z2POSIXProcessTestCase.test_surrogates_error_messagecCs�dD]�\}}|�dd�}dt|�}tj��}|||<d|d<|}tjtjd|g|d�}|�	d	�}|�
|�d�t|��|�dd�}d
t|�}tj��}|||<tjtjd|g|d�}|�	d	�}|�
|�d�t|��qdS)N))�testuabc�)utest�Z42r�rDz&import os; print(ascii(os.getenv(%s)))�C�LC_ALLrr�s

z'import os; print(ascii(os.getenvb(%s))))
r r�rr�r�rr\rDrEr�rFr�r�)r�key�valueZ
encoded_value�scriptr�Z
decoded_valuer1rrr�test_undecodable_env�	s.

�


�
z)POSIXProcessTestCase.test_undecodable_envc	Cs�t�td�}ttdd��}tj�td�\}}t�|�}t�|g|�}|�|d�d|d�	|��
d�f}tj|dd�}|�|d�tj��}||d<tj|g||d	�}|�|d�tj
��}t�|�|d
<tj|g||d	�}|�|d�dS)Nrr�s'%s' %s� rFTr��PATHr�sPATH)rr�rrr�r�rrCrFr�r r�r��environb)	rZabs_programr*r�Zprogram�exitcoderr�Zenvbrrr�test_bytes_program
s"


z'POSIXProcessTestCase.test_bytes_programc	Cs�tjddd�}tjddd�}tjtj|gtjtjtjdd�}|�|jd�tjtj|gtjdd�}|��\}}t	t
t|�d	���}t	|j
��|j��|j��g�}|�||@d
|||@f�dS)Nzinput_reader.py�subprocessdata�Zsubdir�fd_status.pyF)r0r1r2r0r��r1r0�,z5Expected no fds from %r to be open in child, found %r)r�findfilerr3rDrEr4rmr�rQr�r(r�r0r�r1r2r )	rZsleeper�	fd_status�p1�p2r_�errorZ
result_fdsZunwanted_fdsrrr�test_pipe_cloexec(
s(���

��z&POSIXProcessTestCase.test_pipe_cloexecc
stjddd�}tjddd�}d}|dd}tjtj|gtjtjdd	��tjtj||g�jtjdd	��|��j	�|��j	��fd
d�}�fdd
�}|�|�|�|��j
�|��j
��t
�
�jgggd�\}}}	|�|d�|��j��|��j���j��dS)Nzqcat.pyr�r�zqgrep.pyszxcvbn��
F)r0r1r0cs$z���WntyYn0dSr�r��ProcessLookupErrorr)r�rr�kill_p1P
szBPOSIXProcessTestCase.test_pipe_cloexec_real_tools.<locals>.kill_p1cs$z���WntyYn0dSrr�r)r�rr�kill_p2U
szBPOSIXProcessTestCase.test_pipe_cloexec_real_tools.<locals>.kill_p2rKzThe child hung)rr�rr3rDrEr4r1rmrr0rnr8�selectr&rFr�)
rZqcatZqgrepZsubdatar\rrZ	readfilesZignored1Zignored2r)r�r�r�test_pipe_cloexec_real_tools?
s2��



z1POSIXProcessTestCase.test_pipe_cloexec_real_toolsc
s�tjddd�}t��}|�tj|d�|�tj|d�t|��td�D],}t�tj	tj
�}|�tj|���|�qJ�D]}t�|d�q|t
jtj|gt
jdd	�}|��\}}ttt|�d
���}|�|�@�d�t
jtj|gt
jdd	�}|��\}}ttt|�d
���}|�|�@d�|�d|d
�t�fdd�td�D��}	t
jtj|gt
jd|	d�}|��\}}ttt|�d
���}|�||	�@d�|�d|d
�dS)Nr�r�r�rr��	TFr�r�zSome fds were closedzSome fds were left openzSubprocess failedc3s|]}���VqdSr)rVrt�r�rr�	<genexpr>�
r�z6POSIXProcessTestCase.test_close_fds.<locals>.<genexpr>ru�r1r0�pass_fdsz'Some fds not in pass_fds were left open)rr�rr,rmr8rQrNrP�devnullrs�add�set_inheritablerr3rDrEr4r�r�r(r�rFr r])
rr�r�r#r�r:r_�ignored�
remaining_fds�fds_to_keeprrr�test_close_fdsh
sN���
���z#POSIXProcessTestCase.test_close_fdsZfreebsdz/devz/dev/fdz/Requires fdescfs mounted on /dev/fd on FreeBSD.cCs�tjddd�}tjtjdt�d|�gtjd�}|�	�\}}|�
�}|jt|�dd|d	�t
tt|d
���d���}t
tt|d���d���}|j||@d
d	�dS)z;Confirm that issue21618 is fixed (may fail under valgrind).r�r�r�raK
        import os, resource, subprocess, sys, textwrap
        open_fds = set()
        # Add a bunch more fds to pass down.
        for _ in range(40):
            fd = os.open(os.devnull, os.O_RDONLY)
            open_fds.add(fd)

        # Leave a two pairs of low ones available for use by the
        # internal child error pipe and the stdout pipe.
        # We also leave 10 more open as some Python buildbots run into
        # "too many open files" errors during the test if we do not.
        for fd in sorted(open_fds)[:14]:
            os.close(fd)
            open_fds.remove(fd)

        for fd in open_fds:
            #self.addCleanup(os.close, fd)
            os.set_inheritable(fd, True)

        max_fd_open = max(open_fds)

        # Communicate the open_fds to the parent unittest.TestCase process.
        print(','.join(map(str, sorted(open_fds))))
        sys.stdout.flush()

        rlim_cur, rlim_max = resource.getrlimit(resource.RLIMIT_NOFILE)
        try:
            # 29 is lower than the highest fds we are leaving open.
            resource.setrlimit(resource.RLIMIT_NOFILE, (29, rlim_max))
            # Launch a new Python interpreter with our low fd rlim_cur that
            # inherits open fds above that limit.  It then uses subprocess
            # with close_fds=True to get a report of open fds in the child.
            # An explicit list of fds to check is passed to fd_status.py as
            # letting fd_status rely on its default logic would miss the
            # fds above rlim_cur as it normally only checks up to that limit.
            subprocess.Popen(
                [sys.executable, '-c',
                 textwrap.dedent("""
                     import subprocess, sys
                     subprocess.Popen([sys.executable, %r] +
                                      [str(x) for x in range({max_fd})],
                                      close_fds=True).wait()
                     """.format(max_fd=max_fd_open+1))],
                close_fds=False).wait()
        finally:
            resource.setrlimit(resource.RLIMIT_NOFILE, (rlim_cur, rlim_max))
        rrdz(expected exactly two lines of output:
%rr�rr�r�zSome fds were left open.N)rr�rr3rDrEr:r;r4r�r"rFr"rQr�r(rr�r )rr�r:r_Z
unused_stderrZoutput_linesZ
opened_fdsr
rrr�%test_close_fds_when_max_fd_is_lowered�
s$
/��0�2�
�z:POSIXProcessTestCase.test_close_fds_when_max_fd_is_loweredrKrac	Cs@tjddd�}t�}td�D]Z}t��}|�tj|d�|�tj|d�t�|dd�t�|dd�|�	|�q|D]�}t
jtj
|gt
jd|fd�}|��\}}ttt|�d	���}	||h}
|�||	d
�|�|	|
@d�|�t��(}|�t
jtd|fd
��Wd�n1�s0Y|�dt|j��q|dS)Nr�r�r�rarr�Trr�zfd to be passed not passedzfd to be closed passedF)r0rzoverriding close_fds)rr�rQrNrr,rmr8r�updaterr3rDrEr4r�r�r(r�r]r ZassertWarnsrwrCrrk�warning)rr�r�r.r�r�r:r_rr
Zto_be_closed�contextrrr�
test_pass_fds�
s6�

��(z"POSIXProcessTestCase.test_pass_fdsc
Cs�tjddd�}t��\}}|�tj|�|�tj|�t�|d�t�|d�||f}tj|g}|t	t
t|��7}tj
|tjd|d�}|��\}}tt
t|�d���}	|�|	t|�d|�|�t�|�d�|�t�|�d�dS)	Nr�r�r�TFrr�z	output=%a)rr�rr,rmr8rrDrErr�rkrr3r4r�rQr(r�rFr�)
rr�r�Znon_inheritablerr*r:r_rr�rrr�test_pass_fds_inheritable
s$
�z.POSIXProcessTestCase.test_pass_fds_inheritablec	Cstjddd�}g}td�D],}t�tjtj�}|�tj|�|�	|�qt�
�\}}|�tj|�|�tj|�|�d|�tj
tj|g|d|d|dd|d��t�|d	�}Wd
�n1s�0Ydd�|�d
�D�}|�|hd�t|�Bd|���d
S)z7Regression test for https://bugs.python.org/issue32270.r�r�r�rdr�rT)r0r1r2r0rr�NcSsh|]}t|��qSr)r()r
�numrrr�	<setcomp>@r�z@POSIXProcessTestCase.test_pass_fds_redirected.<locals>.<setcomp>r�>rr�rdzoutput=)rr�rNrrPr	�O_RDWRrmr8rOr,�insertrr3rDrEr�r�rF�	frozenset)	rr�rr#r�Zstdout_rZstdout_wr_r�rrr�test_pass_fds_redirected+s(�*z-POSIXProcessTestCase.test_pass_fds_redirectedcCsJt�tjd��(}tjt||d�}|��Wd�n1s<0YdS)N�r+)r1r0�r6rPrr	rr3rr�rZinoutr:rrr�%test_stdout_stdin_are_single_inout_fdEs
�z:POSIXProcessTestCase.test_stdout_stdin_are_single_inout_fdcCsJt�tjd��(}tjt||d�}|��Wd�n1s<0YdS)Nrr�rrrrr�&test_stdout_stderr_are_single_inout_fdKs
�z;POSIXProcessTestCase.test_stdout_stderr_are_single_inout_fdcCsJt�tjd��(}tjt||d�}|��Wd�n1s<0YdS)Nr)r2r0rrrrr�%test_stderr_stdin_are_single_inout_fdQs
�z:POSIXProcessTestCase.test_stderr_stdin_are_single_inout_fdcCsRtjddd�}tjtj|gtjtjd�}|��\}}|�d|j	d|�
d��dS)Nzsigchild_ignore.pyr�r�r�rz6sigchild_ignore.py exited non-zero with this error:
%srF)rr�rr3rDrEr4r�rFrr�)rZsigchild_ignorer:r1r2rrr�test_wait_when_sigchild_ignoredWs���z4POSIXProcessTestCase.test_wait_when_sigchild_ignoredc	Cs�t�d�}tjtjddgtjdd�}|j}|�|j	�z<|�
|�d�d�|�||�
|gggd�d�W|��n
|��0dS)	Nrrz$import sys;sys.stdout.write("apple")r)r1r<r�sapplg)r�
import_modulerr3rDrEr4r1rmr8rFr�r]rr)rrr:r�rrr�test_select_unbufferedbs

�� z+POSIXProcessTestCase.test_select_unbufferedcCs�tjtjddgtjtjd�}|�|jj�|�|jj�t	|�}|j
}t�dt
f��d}Wd�n1sn0Ytr�|�tj�n|�|dd�tjD��dS)Nrz import sys, time;time.sleep(0.2)r�rcSsg|]}t|��qSr��id�r
�orrrr�r�zEPOSIXProcessTestCase.test_zombie_fast_process_del.<locals>.<listcomp>)rr3rDrEr4rmr1r8r2r&�pidr�check_warnings�ResourceWarningrrerr])rr:�identr)rrr�test_zombie_fast_process_delss
��"z1POSIXProcessTestCase.test_zombie_fast_process_delc	Csdtjtjddgtjtjd�}|�|jj�|�|jj�t	|�}|j
}t�dt
f��d}t��Wd�n1sv0Yt�|tj�tr�|�tj�n|�|dd�tjD��t�d�|�t��Dtjttjtjd��}Wd�n1s�0YWd�n1�s0Y|�ttj|d�t�rH|�tj�n|�|d	d�tjD��dS)
Nrzimport time;time.sleep(3)r�rcSsg|]}t|��qSrr%r'rrrr�r�zJPOSIXProcessTestCase.test_leak_fast_process_del_killed.<locals>.<listcomp>r�rcSsg|]}t|��qSrr%r'rrrr�r�)rr3rDrEr4rmr1r8r2r&r)rr*r+�
gc_collectrr�r�r�rrerr]r�r�rNrSr��waitpidrw)rr:r,r)r}rrr�!test_leak_fast_process_del_killed�s:
��&
�>z6POSIXProcessTestCase.test_leak_fast_process_del_killedcsxtjddd�}t�d��|�tj��tjtj	|gtj
d�fdd�d�}|��\}}tt
t|�d	���}|��|�dS)
Nr�r�r�r�Tcst�d��Sr�)rr�r�r�rrrh�r�zCPOSIXProcessTestCase.test_close_fds_after_preexec.<locals>.<lambda>)r1r0rir�)rr�rr�rmr8rr3rDrEr4r�rQr�r(r�rw)rr�r:r_rr
rr1r�test_close_fds_after_preexec�s

�z1POSIXProcessTestCase.test_close_fds_after_preexecc	Cs�ddl}t��}z�dd�}t��ddgddgfdgdddgfdgdgddgfdgdgddffD]z\}}}}|�t��B}|�||dd	||d
d
d
d
ddd
ddddgdd
|�Wd�n1s�0Y|�dt|j	��q`W|s�t�
�n|s�t�
�0dS)NrcSsdSrrrrrrrh�r�z5POSIXProcessTestCase.test_fork_exec.<locals>.<lambda>r�sexe�envsargTrr1r�rdr�r�Fz
takes exactly)rorzr{r}rNr�rrwrkrUr|)	rro�
gc_enabled�funcr*Zexe_listr��env_listr�rrr�test_fork_exec�s2��"
�z#POSIXProcessTestCase.test_fork_execcCs�ddl}Gdd�d�}t��}z�t��ddddd|d	�|d
�ffD]�}|jtd�|�d��H}|�d
gd
gd|ddgddddd	d
ddddddddd�Wd�n1s�0Y|�dt	|j
��qDW|s�t��n|s�t��0dS)Nrc@s eZdZdZdd�Zdd�ZdS)zJPOSIXProcessTestCase.test_fork_exec_sorted_fd_sanity_check.<locals>.BadIntTcSs
||_dSr)r�)rr�rrrr��szSPOSIXProcessTestCase.test_fork_exec_sorted_fd_sanity_check.<locals>.BadInt.__init__cSs|jrd|_|jSt�dS)NF)�firstr�r�rrrr�__int__�szRPOSIXProcessTestCase.test_fork_exec_sorted_fd_sanity_check.<locals>.BadInt.__int__N)r$r%r&r8r�r9rrrr�BadInt�sr:)r1rdr�r�ra)rkr�)���*l)rar�)r3r�r�rur�rdzfds_to_keep={}r�sfalseTr3r1r�r�r)rorzr{r}rNr�rHrr]rkrUr|)rror:r4rrWrrr�%test_fork_exec_sorted_fd_sanity_check�s>
��
�"
�z:POSIXProcessTestCase.test_fork_exec_sorted_fd_sanity_checkc	Cszt�t�}|�Xtj�|d��*}t|j_|�	�|j�
�Wd�n1sN0YWd�n1sl0YdS)Nr0)rr3rrr�r��BrokenPipeErrorr8rr�Zassert_called_with�rr}�mock_proc_stdinrrr�,test_communicate_BrokenPipeError_stdin_closes

zAPOSIXProcessTestCase.test_communicate_BrokenPipeError_stdin_closec	Cs�t�t�}|�ftj�|d��8}t|j_|�	d�|j�
d�|j�
�Wd�n1s\0YWd�n1sz0YdS)Nr0�stuff)rr3rrr�r�r?rnrr��assert_called_once_withr8r@rrr�,test_communicate_BrokenPipeError_stdin_write"s

zAPOSIXProcessTestCase.test_communicate_BrokenPipeError_stdin_writec
Cs�tjtjdgtjtjd�}|��tj�|d��d}tt	j
d��8}t|j_
|��|j_|�d�|j��Wd�n1sz0YWd�n1s�0YWd�n1s�0YdS)N�-hr�r0�wbrC)rr3rDrEr4rr�r�rPrr	r?r5rr��return_valuer�rD)rr}rAZdev_nullrrr�,test_communicate_BrokenPipeError_stdin_flush,s�
�
zAPOSIXProcessTestCase.test_communicate_BrokenPipeError_stdin_flushc	Cs�tjtjdgtjtjd�}|�\tj�|d��.}t|j	_
|jdd�|j	��Wd�n1sb0YWd�n1s�0YdS)NrFr�r0i�rL)
rr3rDrEr4rr�r�r?r8rr�rDr@rrr�9test_communicate_BrokenPipeError_stdin_close_with_timeout;s�zNPOSIXProcessTestCase.test_communicate_BrokenPipeError_stdin_close_with_timeoutN�
W_STOPCODEzneed _testcapi.W_STOPCODEcCstt}t�|�}tj|jdd�t�d�}tj	d|j|fd��|�
�}Wd�n1sZ0Y|�|d�dS)zATest wait() behavior when waitpid returns WIFSTOPPED; issue29335.r�r�r�r)rHN���)rrr3r�wait_processr)�	_testcapirKrr�rrF)rr*r}�statusrrrr�test_stoppedGs

&z!POSIXProcessTestCase.test_stoppedcCstt�t�}tj|jdd�|�|j�t�	d��}|�
tj�Wd�n1sR0Y|�
�|�|j�dS)NrrLzos.kill)rr3rrrNr)rerrr�r�r�r�Zassert_not_calledr�)rr}Z	mock_killrrr�test_send_signal_raceXs
*z*POSIXProcessTestCase.test_send_signal_racecCsnt�tjddg�}|js"|��qtjj|ddd�d��"d|_|�	t
j�Wd�n1s`0YdS)Nrzexit(1)rfcSsdSrrrrrrrhwr�z=POSIXProcessTestCase.test_send_signal_race2.<locals>.<lambda>)�new)rr3rDrErrfrr�r�r�r�r�r9rrr�test_send_signal_race2ns
z+POSIXProcessTestCase.test_send_signal_race2cCsFtjtjddgtjd�}z|jdd�WdStjy>Yq0qdS)Nrz+import os, time; os.close(1), time.sleep(2)rrKrL)rr3rDrEr4r�rO�rr}rrr�1test_communicate_repeated_call_after_stdout_close{s
��zFPOSIXProcessTestCase.test_communicate_repeated_call_after_stdout_close)nr$r%r&rrr
rrrr3rrr�rrr�r�rr�r�r$r)r�r�r=r@rHrIrSrTrr[r]rarcrerkrprvrwr~rDr�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�rrrW�st_devrrZrequires_mac_verrrrrr r!r"r$r-r0r2�cpython_onlyr7r>rBrErIrJrOrQrRrTrV�
__classcell__rrrrr�qs�


�

3

)

0
�


%
�



�
-0,)0�
P

!+
'
)	
�

r�zWindows specific testsc@s�eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zej	dd��Z
d
d�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-S).�Win32ProcessTestCasecCs.d}d}t��}||_||_tjt|d�dS)Nr�r�r�)r�STARTUPINFO�dwFlags�wShowWindowrCr)r�STARTF_USESHOWWINDOW�SW_MAXIMIZEr�rrr�test_startupinfo�s�z%Win32ProcessTestCase.test_startupinfocCs(d}d}tj||d�}tjt|d�dS)Nr�r�)r\r]r�)rr[rCr)rZSTARTF_USERSHOWWINDOWr_r�rrr�test_startupinfo_keywords�s��z.Win32ProcessTestCase.test_startupinfo_keywordsc
Cs�t��}tj|_tj|_td�D]�}t}tt	j
d��^}tj||tj|d�}|�|�
�Wd�n1sl0Y|�|jd�Wd�n1s�0Y|�|jtj�|�|j�|�|j�|�|j�|�|jtj�|�|jdgi�q dS)Nrdr�)r1r2r�r�handle_list)rr[r^r\ZSW_HIDEr]rNrrPrr	r3rer�rFrreZ	hStdInputZ
hStdOutputZ	hStdError�lpAttributeList)rr�r#rZnullr}rrr�test_startupinfo_copy�s,�&,�z*Win32ProcessTestCase.test_startupinfo_copycCs(d}tj�d�tjtjd|d�dS)N�z'    a DOS box should flash briefly ...
z# -c "import time; time.sleep(0.25)"r�)rDr2rnrrCrE)rZCREATE_NEW_CONSOLErrr�test_creationflags�s��z'Win32ProcessTestCase.test_creationflagscCs$|jttjtjddgdd�d�dS)Nrr@cSsdSr�rrrrrrh�r�z8Win32ProcessTestCase.test_invalid_args.<locals>.<lambda>rnr�rrrrr��s
��z&Win32ProcessTestCase.test_invalid_argscCsNGdd�dt�}|�t�� tjt|�d�Wd�n1s@0YdS)Nc@seZdZdZdS)z4Win32ProcessTestCase.test_issue31471.<locals>.BadEnvN)r$r%r&�keysrrrr�BadEnv�srhr�)�dictrNr�rr3r)rrhrrr�test_issue31471�sz$Win32ProcessTestCase.test_issue31471cCs&tjtjddgdd�}|�|d�dS)Nrr@T)r0rArBrGrrrr�s
��z#Win32ProcessTestCase.test_close_fdsc
Cs�ddl}t��}|�tj|d�|�tj|d�g}|D] }t�|d�|�|�|��q<tj	t
jdd�|d�gtj
dd�}|��\}}|�|jd�t|���tj	t
jdd�|d�gtj
tj
dd�}|��\}}|�|jd�|�d	|�g}t��}	d
|i|	_tj	t
jdd�|d�gtj
tj
|	dd�}|��\}}|�|jd�|�d	|�t�dtf��nt��}	d
|dd�i|	_tj	t
jdd�|d�gtj
tj
|	dd�}|��\}}|�|jd�Wd�n1�s�0YdS)
Nrr�Trz2import msvcrt; print(msvcrt.open_osfhandle({}, 0))Fr�)r1r2r0sOSErrorrb)r1r2r�r0z.*overriding close_fds)rzrr,rmr8rrOZ
get_osfhandlerr3rDrErHr4r�rFrr(rr]r[rcrr*rw)
rrzr�rXr�r:r1r2rbr�rrr�test_close_fds_with_stdio�s^
��
�
�

��
��z.Win32ProcessTestCase.test_close_fds_with_stdiocCs t��}i|_tjt|d�dS)Nr��rr[rcrCr�rr�rrr�test_empty_attribute_list
s
�z.Win32ProcessTestCase.test_empty_attribute_listcCs$t��}dgi|_tjt|d�dS)Nrbr�rlrmrrr�test_empty_handle_list
s

�z+Win32ProcessTestCase.test_empty_handle_listcCsbtj��}d|d<tjdgdtj|d�}|�"|�d|j���Wd�n1sT0YdS�N�physalisr�rQr�r�sphysalis�	rr�r�rr3r4r]r1r�r�rrrr� 
s

�z(Win32ProcessTestCase.test_shell_sequencecCs`tj��}d|d<tjddtj|d�}|�"|�d|j���Wd�n1sR0YdSrprrr�rrrr�*
s
�z&Win32ProcessTestCase.test_shell_stringc	CsndD]d}tj��}d|d<tjddtj||d�}|�$|�d|j��|�Wd�q1s^0YqdS)N)�ansiZoemrqr�rQr�)r�r1r�r?rr)r�encr�r:rrr�test_shell_encodings4
s
�z)Win32ProcessTestCase.test_shell_encodingscCs t�tjd�}|�|d�dS)Nz -c "import sys; sys.exit(47)"rArBrGrrrr�@
s�z%Win32ProcessTestCase.test_call_stringcGs�tjtjddgtjtjtjd�}|�J|j�d�t||�|�|��\}}|�	|d�|�
�}Wd�n1st0Y|�|d�dS)Nrz�if 1:
                             import sys, time
                             sys.stdout.write('x\n')
                             sys.stdout.flush()
                             time.sleep(30)
                             r/r�r�r)rr3rDrEr4r1r�r�r�rFrr#)rr�r*r:r#r2rrrrr�F
s�	&z"Win32ProcessTestCase._kill_processcGs�tjtjddgtjtjtjd�}|�T|j�d�t�d�t	||�|�|�
�\}}|�|d�|��}Wd�n1s~0Y|�|d�dS)Nrz�if 1:
                             import sys, time
                             sys.stdout.write('x\n')
                             sys.stdout.flush()
                             sys.exit(42)
                             r/r�r�r=)
rr3rDrEr4r1r�r�r�r�r�rFr)rr�r*r:r#r2rHrrrr�[
s�	
&z'Win32ProcessTestCase._kill_dead_processcCs|�dtj�dSr�)r�r�r�rrrrr�r
sz%Win32ProcessTestCase.test_send_signalcCs|�d�dSr��r�rrrrr�u
szWin32ProcessTestCase.test_killcCs|�d�dSr�rvrrrrr�x
sz#Win32ProcessTestCase.test_terminatecCs|�dtj�dSr�)r�r�r�rrrrr�{
sz*Win32ProcessTestCase.test_send_signal_deadcCs|�d�dSr�r�rrrrr�~
sz#Win32ProcessTestCase.test_kill_deadcCs|�d�dSr�r�rrrrr��
sz(Win32ProcessTestCase.test_terminate_deadN)r$r%r&r`rardrfr�rrXrjrrkrnror�r�rur�r�r�r�r�r�r�r�r�rrrrrZ�s.
1

rZc@s^eZdZGdd�dej�Zej�ejd�dd��Z	dd�Z
dd	�Zd
d�Zdd
�Z
dd�ZdS)�	MiscTestscs$eZdZdZgZ�fdd�Z�ZS)zMiscTests.RecordingPopenz<A Popen that saves a reference to each instance for testing.cs"t�j|i|��|j�|�dSr)rr��instances_createdrOr)rrrr��
sz!MiscTests.RecordingPopen.__init__)r$r%r&r-rxr�rYrrrr�RecordingPopen�
sry�_communicatec
Ks�t|_�zjtj�tjd���}t|_tj�td|j��X|�t��.|t	j
ddgfdtji|��Wd�n1sv0YWd�n1s�0Y|jdd�D]"}|�
|tjdd�d	|j���q�g}|jD]}|tjd
d�kr�|�|�q�|j|jd|jd�|jt|�d|jd�Wd�n1�s:0YW|jj��}|��|��|�g|jj�n.|jj��}|��|��|�g|jj�0dS)
a�Fake a SIGINT happening during Popen._communicate() and ._wait().

        This avoids the need to actually try and get test environments to send
        and receive signals reliably across platforms.  The net effect of a ^C
        happening during a blocking subprocess execution which we want to clean
        up from is a KeyboardInterrupt coming out of communicate() or wait().
        �_waitr3rzLimport time
time.sleep(9)
import sys
sys.stderr.write('\n!runaway child!\n')r1Nr�rLz.no open-ended wait() after the first allowed: g�?rdr�)�KeyboardInterruptrrr�r�rr3ryrNrDrErZcall_args_listr#rCrOZassertLessEqualZ
call_countrFr"rxrVr�r)rZpopenerZmock__communicater+Z
mock__waitrCZsigint_callsr�rrr�_test_keyboardinterrupt_no_kill�
sP
����B��

��(�z)MiscTests._test_keyboardinterrupt_no_killcCs|jtjdd�dS�NgT㥛� @rL)r}rrCrrrr�#test_call_keyboardinterrupt_no_kill�
sz-MiscTests.test_call_keyboardinterrupt_no_killcCs|jtjdd�dSr~)r}rrrrrr�"test_run_keyboardinterrupt_no_kill�
sz,MiscTests.test_run_keyboardinterrupt_no_killcCsdd�}|�|�dS)Nc_s8tj|i|���}t�Wd�n1s*0YdSr)rr3r|)r*r+Zunused_processrrr�popen_via_context_manager�
sz[MiscTests.test_context_manager_keyboardinterrupt_no_kill.<locals>.popen_via_context_manager)r})rr�rrr�.test_context_manager_keyboardinterrupt_no_kill�
sz8MiscTests.test_context_manager_keyboardinterrupt_no_killc	Cs�|�t�d�d�|�t�d�d�d}zRt��}tj�|d�}t�t	rLdnd|�\}}|�
|d�W|dur�t�|�n|dur�t�|�0dS)Nz
echo xyzzy�xyzzy)rr�Zfooztype zcat r)rFr�	getoutput�getstatusoutputrkrMrr�r�rr#�rmdir)r�dirrZrPr_rrr�test_getoutput�
s ���zMiscTests.test_getoutputcCsnhd�}ttj�}t�}ddl}tj��D].\}}|�d�r>q*t||jf�rNq*|�	|�q*|�
|||�dS)z*Ensure that __all__ is populated properly.>rbr5r��HandlerNr#)rQr�__all__r��__dict__�itemsr
r9�
ModuleTyper
rF)rZintentionally_excludedZexportedZpossible_exportsr�rZr�rrr�test__all__�
s

zMiscTests.test__all__N)r$r%r&rr3ryrr�r�r}rr�r�r�r�rrrrrw�
s
+rw�PollSelectorz!Test needs selectors.PollSelectorc@seZdZdd�Zdd�ZdS)�ProcessTestCaseNoPollcCstj|_tjt_t�|�dSr)r�_PopenSelector�
orig_selector�	selectors�SelectSelectorr.rrrrrr�
szProcessTestCaseNoPoll.setUpcCs|jt_t�|�dSr)r�rr�r.r"rrrrr"�
szProcessTestCaseNoPoll.tearDownNr#rrrrr��
sr�zWindows-specific testscsPeZdZ�fdd�Z�fdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	�Z
S)�CommandsWithSpacescs>t���t�dd�\}}|��|_t�|d�t�|�dS)Nz.pyzte stsVimport sys;sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv])))	rrrkr�r_r�rrnr8)rr�r�rrrr�
s


zCommandsWithSpaces.setUpcst�|j�t���dSr)rr�r�rr"rrrrr"szCommandsWithSpaces.tearDowncOs`tj|d<tj|i|��}|�.|�|j���d�d|j�Wd�n1sR0YdS)Nr1�mbcsz2 [%r, 'ab cd'])rr4r3rFr1r�r�r�)rr*r+r:rrr�with_spacess
�zCommandsWithSpaces.with_spacescCs |jdtj|jdfdd�dS)N�"%s" "%s" "%s"�ab cdr�r��r�rDrEr�rrrr�test_shell_string_with_spacess
��z0CommandsWithSpaces.test_shell_string_with_spacescCs|jtj|jdgdd�dS)Nr�r�r�r�rrrr�test_shell_sequence_with_spacessz2CommandsWithSpaces.test_shell_sequence_with_spacescCs|�dtj|jdf�dS)Nr�r�r�rrrr�test_noshell_string_with_spacess�z2CommandsWithSpaces.test_noshell_string_with_spacescCs|�tj|jdg�dS)Nr�r�rrrr�!test_noshell_sequence_with_spacessz4CommandsWithSpaces.test_noshell_sequence_with_spaces)r$r%r&rr"r�r�r�r�r�rYrrrrr��
s		r�c@s4eZdZdd�Zdd�Zdd�Zdd�Zd	d
�ZdS)�ContextManagerTestscCs�tjtjddgtjtjd��4}|�|j��d�|�|j��d�Wd�n1sV0Y|�	|jj
�|�	|jj
�dS)NrzAimport sys;sys.stdout.write('stdout');sys.stderr.write('stderr');r�sstdoutsstderr)rr3rDrEr4rFr1r�r2r&r'rUrrr�	test_pipe#s
��0zContextManagerTests.test_pipecCsDt�tjddg��}Wd�n1s(0Y|�|jd�dS)Nrzimport sys; sys.exit(100)�d)rr3rDrErFrrUrrrr�0s
�z#ContextManagerTests.test_returncodecCsTtjtjddgtjd��(}|�d�|�|jd�Wd�n1sF0YdS)Nrz2import sys;sys.exit(sys.stdin.read() == 'context')riscontextr�r�rUrrrr7s
��
z*ContextManagerTests.test_communicate_stdinc	Csb|�t��Dtjttjtjd��}Wd�n1s60YWd�n1sT0YdS)Nr�)rNr�rr3r�r4rUrrrr�?s�z%ContextManagerTests.test_invalid_argscCsrtjttjtjdd�}|��}|j�dtj�|�	|j
�|�t|j
ddd�|�|j
d�|�|jj�dS)z9Broken pipe error should not prevent wait() (Issue 21619)rd)r0r<r�Nr)rr3rr4rr-�	__enter__r0rnrerrNrS�__exit__rFr&r'rUrrr�test_broken_pipe_cleanupFs�z,ContextManagerTests.test_broken_pipe_cleanupN)r$r%r&r�r�rr�r�rrrrr�!s

r��__main__)?r�rr�rrrDr�r6r�rrTrkr�r�r�r�r�rr
r�rzr:rOr�Ztest.supportrrOr|r5r�ZPGOr�r�rr4r�r��NotADirectoryErrorr8r�rErrZTestCaser�	Exceptionr'r3r(r.r�r�r�r�r�rZrwr�r�r�r�r$�mainrrrr�<module>s�






Y0
!
}d�
+6


Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists