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.opt-2.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@seZdZdd�ZdS)�PopenExecuteChildRaisescOstd��dS)NzForced Exception for Test)r'�r�args�kwargsrrr�_execute_childcsz&PopenExecuteChildRaises._execute_childN)r$r%r&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�assertIsInstancer/�io�BufferedIOBaser0r1�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)r/r0r1�bufsize)rr2rr3r4r/r5�	RawIOBaser0r1r7rr8rrr�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�TimeoutExpiredrBrCrDrrrr�test_call_timeout�s
�z!ProcessTestCase.test_call_timeoutcCst�t�}|�|d�dS�Nr)r�
check_callrrErFrrr�test_check_call_zero�s
z$ProcessTestCase.test_check_call_zerocCsR|�tj��"}t�tjddg�Wd�n1s40Y|�|jjd�dSr>)	rMr�CalledProcessErrorrQrCrDrE�	exceptionr�r�crrr�test_check_call_nonzero�s

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

�"z)ProcessTestCase.test_check_output_nonzerocCs(tjtjddgtjd�}|�d|�dS)Nr�$import sys; sys.stderr.write('BDFL')�r1rY)rr[rCrD�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())�r/�PEAR)�tempfile�
TemporaryFile�
addCleanupr7�write�seekrr[rCrDr\)r�tfr^rrr�test_check_output_stdin_arg�s

��z+ProcessTestCase.test_check_output_stdin_argcCs&tjtjddgdd�}|�d|�dS)Nrrgrf��inputrirZr]rrr�test_check_output_input_arg�s��z+ProcessTestCase.test_check_output_input_argcCs&tjtjddgdd�}|�d|�dS)Nr�3import sys; print('XX' if sys.stdin.read() else '')rqsXX�rr[rCrD�assertNotInr]rrr�test_check_output_input_none�s��z,ProcessTestCase.test_check_output_input_nonecCs(tjtjddgddd�}|�d|�dS)NrrtT)rr�text�XXrur]rrr�!test_check_output_input_none_text�s��z1ProcessTestCase.test_check_output_input_none_textcCs(tjtjddgddd�}|�d|�dS)NrrtT)rr�universal_newlinesryrur]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')�r0z-Expected ValueError when stdout arg supplied.r0r)rM�
ValueErrorrr[rCrDr0�failr\rTr*�rrVr^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)
Nrfrrr}�hare�r/rr�7Expected ValueError when stdin and input args supplied.r/rr)rjrkrlr7rmrnrMrrr[rCrDr�r\rTr*�rrorVr^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)�rKzExpected TimeoutExpired.rY)
rMrrNr[rCrDr�rErTr^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�copyrrBrCrDrE)r�newenvrGrrr�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_stderrrM�	TypeErrorrr2�__init__�__code__�co_argcountrE�getvalue)r�sZargcountZ
too_many_argsrrr�test_invalid_argss
4z!ProcessTestCase.test_invalid_argscCsRtjtjddgtjtjd�}|�|jj�|�|jj�|�	�|�
|jd�dS)Nr�print("banana")�r0r1)rr2rCrDr3rlr0r7r1rrEr/r8rrr�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�
rr2rCrDr3rlr0r7r1�communicaterEr�rstrip�r�coder9�out�errrrr�test_stdout_none!s�z ProcessTestCase.test_stdout_nonecCsRtjtjddgtjtjd�}|�|jj�|�|jj�|�	�|�
|jd�dS)Nrr��r/r0)rr2rCrDr3rlr0r7r/rrEr1r8rrr�test_stderr_none8s�z ProcessTestCase.test_stderr_nonecKs6|dg}tj|fi|��}|��|�d|j�dS�Nr?r@)rr2rrEr)r�pre_argsr+r*r9rrr�_assert_pythonAs
zProcessTestCase._assert_pythoncCs0tj�tj�tj�d�}|j|dgtjd�dS�N�doesnotexistr�rD)r�path�join�dirnamerCrDr��rr�rrr�test_executableIs�zProcessTestCase.test_executablecCs6tj�tj�tj�d�}|j|dgt�tj�d�dSr�)rr�r�r�rCrDr��fsencoder�rrr�test_bytes_executableTs�

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

�z0ProcessTestCase.test_executable_takes_precedencez"executable argument replaces shellcCs|jgtjdd�dS�NT)rD�shell)r�rCrDrrrr�test_executable_replaces_shelljsz.ProcessTestCase.test_executable_replaces_shellcCs|jgt�tj�dd�dSr�)r�rr�rCrDrrrr�$test_bytes_executable_replaces_shellps�z4ProcessTestCase.test_bytes_executable_replaces_shellcCs|jgttj�dd�dSr�)r�rrCrDrrrr�'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��realpathrCrD�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)r0r@)rr2r3rlr0r7rrErrr��normcase�read�decode)rZexpected_cwdZ
python_argr+r9r�rrr�_assert_cwd�s���
�zProcessTestCase._assert_cwdcCs(t��}|�|�}|j|tj|d�dS�N�r�)rj�
gettempdirr�r�rCrD�r�temp_dirrrr�test_cwd�s
zProcessTestCase.test_cwdcCs.t��}|�|�}|j|tjt�|�d�dSr�)rjr�r�r�rCrDrr�r�rrr�test_cwd_with_bytes�s
z#ProcessTestCase.test_cwd_with_bytescCs,t��}|�|�}|j|tjt|�d�dSr�)rjr�r�r�rCrDrr�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_cwdrM�FileNotFoundErrorrr2r�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��rDr�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�rMr�rr2r�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�rCrD)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�rCrDrrrr�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")rhrfr�)rr2rCrDr3r/rmr7rrErr8rrr�test_stdin_pipe�s
��
zProcessTestCase.test_stdin_pipecCsft��}|�|j�|��}t�|d�t�|dd�tj	t
jddg|d�}|��|�
|jd�dS�Nrfrrr�rhr�)rjrkrlr7�filenorrm�lseekrr2rCrDrrEr�rro�dr9rrr�test_stdin_filedes�s
��z"ProcessTestCase.test_stdin_filedescCsXt��}|�|j�|�d�|�d�tjtj	ddg|d�}|�
�|�|jd�dSr�)
rjrkrlr7rmrnrr2rCrDrrEr�rror9rrr�test_stdin_fileobjs


��z"ProcessTestCase.test_stdin_fileobjcCsRtjtjddgtjd�}|�"|�|j��d�Wd�n1sD0YdS)Nr�&import sys; sys.stdout.write("orange")r~�orange�rr2rCrDr3rEr0r�r8rrr�test_stdout_pipes
��z ProcessTestCase.test_stdout_pipecCs`t��}|�|j�|��}tjtjddg|d�}|�	�t
�|dd�|�t
�
|d�d�dS)Nrr�r~r�r��rjrkrlr7r�rr2rCrDrrr�rEr�r�rrr�test_stdout_filedess
��z#ProcessTestCase.test_stdout_filedescCsPt��}|�|j�tjtjddg|d�}|��|�	d�|�
|��d�dS)Nrr�r~rr��rjrkrlr7rr2rCrDrrnrEr�r�rrr�test_stdout_fileobj&s
��
z#ProcessTestCase.test_stdout_fileobjcCsRtjtjddgtjd�}|�"|�|j��d�Wd�n1sD0YdS)Nr�*import sys; sys.stderr.write("strawberry")rc�
strawberry)rr2rCrDr3rEr1r�r8rrr�test_stderr_pipe1s
��z ProcessTestCase.test_stderr_pipecCs`t��}|�|j�|��}tjtjddg|d�}|�	�t
�|dd�|�t
�
|d�d�dS)Nrr�rcrr�r�r�r�rrr�test_stderr_filedes9s
��z#ProcessTestCase.test_stderr_filedescCsPt��}|�|j�tjtjddg|d�}|��|�	d�|�
|��d�dS)Nrr�rcrr�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�rr2rCrDr3r�rEr�rr9r0r1rrr�,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)	rr2rCrDr3rdrEr0r�r8rrr�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~)rr2rCrD�DEVNULLrrEr0r8rrr�test_stdout_devnull�s
��z#ProcessTestCase.test_stdout_devnullcCs2tjtjddgtjd�}|��|�|jd�dS)Nrz=import sys
for i in range(10240):sys.stderr.write("x" * 1024)rc)rr2rCrDr�rrEr1r8rrr�test_stderr_devnull�s
��z#ProcessTestCase.test_stderr_devnullcCs2tjtjddgtjd�}|��|�|jd�dS)Nrzimport sys;sys.stdin.read(1)rh)rr2rCrDr�rrEr/r8rrr�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"))�r0r�r�)
rr�r�rr2rCrDr3r�rE�rr�r9r0r1rrr�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)NcSs*d|vp(d|vp(|dkp(|�d�p(|dkS)NZ	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�rrr�
<listcomp>�s�z2ProcessTestCase.test_empty_env.<locals>.<listcomp>)rr2rCrDr3r��eval�stripr4�listrE)rr9r0r1Zchild_env_namesrrr�test_empty_env�s	
��zProcessTestCase.test_empty_envcCs�tjd}|�t�� t�|ddg�Wd�n1s:0Y|�t��"t�tjddg�Wd�n1sv0YdS)N�rrzpass#)rCrDrMrrr2)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�rMrrr2rrCrDr3r�rErrrr�test_invalid_env�s.
,
,
,

��z ProcessTestCase.test_invalid_envcCs4tjtjddgtjd�}|�d�|�|jd�dS)Nrz/import sys;sys.exit(sys.stdin.read() == "pear")rhrfr�r�r8rrr�test_communicate_stdin�s
��
z&ProcessTestCase.test_communicate_stdincCs@tjtjddgtjd�}|��\}}|�|d�|�|d�dS)Nrz)import sys; sys.stdout.write("pineapple")r~�	pineapple�rr2rCrDr3r�rEr�rrr�test_communicate_stdouts
��z'ProcessTestCase.test_communicate_stdoutcCs@tjtjddgtjd�}|��\}}|�|d�|�|d�dS)Nrz)import sys; sys.stderr.write("pineapple")rcrrr�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)rr2rCrDr3rlr0r7r1r/r�rEr�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{r/r0r1r�g333333�?rKspineapple
pear
)
rr2rCrDr3rMrNr�rE�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));r~g�������?rKi)
rr2rCrDr3rMrNr�rE�len)rr9r0�_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�FTr/r0r1)
rr3r2rr�r/�
assertTrue�closedr0r1)rZ
stdin_pipeZstdout_pipeZstderr_pipe�optionsr9rrr�test_communicate_pipe_fd_leakGs(





z-ProcessTestCase.test_communicate_pipe_fd_leakcCs:t�tjddg�}|��\}}|�|d�|�|d�dS)Nrr?)rr2rCrDr�rEr�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�piper7rr2rCrDr�
PIPE_MAX_SIZEr3rlr0r1r/r�rE)r�x�yr9Zstring_to_writer0r1rrr�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�)
rr2rCrDr3rlr0r7r1r/rmr�rEr�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{rxr/r0Tzline1
zline3
zline2
�zline4
line5
line6
line7
line8)rCrD�	SETBINARYrr2r3r/rm�flushrEr0�readliner7rlr�)rr*Zextra_kwargr9rrr� test_universal_newlines_and_text�s4���

���z0ProcessTestCase.test_universal_newlines_and_textcCs^tjtjddtdgtjtjdd�}|�|jj�|�|j	j�|�
�\}}|�|d�dS)Nrr1z�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�)r1r0r{z#line2
line4
line5
line6
line7
line8)rr2rCrDr3r3rlr0r7r1r�rEr�rrr�#test_universal_newlines_communicate�s
����z3ProcessTestCase.test_universal_newlines_communicatecCsHtjtjddtt�d�gtjdd�}|�d�\}}|�	|j
d�dS)Nrr1a
                               s = sys.stdin.readline()
                               assert s == "line1\n", repr(s)
                               s = sys.stdin.read()
                               assert s == "line3\n", repr(s)
                              r�)r/r{�line1
line3
r)rr2rCrDr3�textwrap�dedentr3r�rErr�rrr�)test_universal_newlines_communicate_stdin�s
��	z9ProcessTestCase.test_universal_newlines_communicate_stdincCs0tjttjtjdd�}|��|�|jd�dS)NT)r/r0r{r)rr2rr3r�rErr8rrr�.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)
Nrr1a�
                               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)r/r1r0r{r8rzline1
line2
line3
line4
line5
zeline2
eline6
eline7
)rr2rCrDr3r9r:r3rlr0r7r1r�rErr%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)r/r0�encodingrrqz1
2
3
4)rCrDrr2r3r�rE)rr>r�r*�popenr0r1rrr�-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)r/r0r>�errorsrrqz[{}])rCrDrr2r3r�rE�format)rrF�expectedr�r*r?r0r1rrr�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)rrj�mkdtemp�rangerr�r�rZTESTFN�append�open�O_WRONLY�O_CREAT�OSError�errnoZEMFILE�skipTestr7�poprr2rCrDr3r�rEr
�rmtree)
rZmax_handles�handles�tmpdir�iZtmpfile�er9�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�rZz"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\"brVr�z
a\\\"b c d)za\\b cr�rZz"a\\b c" d e)za\\b\ cr�rZz
"a\\b\ c" d e�abrzab "")rEr�list2cmdlinerrrr�test_list2cmdlineTs0��������z!ProcessTestCase.test_list2cmdlinecCsbtjtjddgtjd�}|�|jj�|�|�	��t
�|j��d�|�
�|�|�	�d�dS)Nrzimport os; os.read(0, 1)rh�Ar)rr2rCrDr3rlr/r7�assertIsNone�pollrrmr�rrEr8rrr�	test_pollfs
��zProcessTestCase.test_pollcCs.t�t�}|�|��d�|�|��d�dSrP�rr2rrErr8rrr�	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?rKz0.0001r)
rr2rCrDrMrNrr\�strrTrEr�
SHORT_TIMEOUT)rr9rVrrr�test_wait_timeoutws�*z!ProcessTestCase.test_wait_timeoutcCs:|�t��t�td�Wd�n1s,0YdS)Nr)rMr�rr2rrrrr�test_invalid_bufsizesz$ProcessTestCase.test_invalid_bufsizecCs>t�td�}|�|��d�tjtdd�}|�|��d�dS)Nr)r;rgr8rrr�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�)r/r0r1r;r{r)rr2rCrDr3r�r/rmrr7r�r0r5r�SuppressCrashReportrRrEr)r�linerHr{r9Z	read_linerrr�_test_bufsize_equal_one�s&�

$$z'ProcessTestCase._test_bufsize_equal_onecCsd}|j||dd�dS)Nzline
T�r{)rq�rrprrr� test_bufsize_equal_one_text_mode�sz0ProcessTestCase.test_bufsize_equal_one_text_modecCsNdtj��}|�td�� |j|ddd�Wd�n1s@0YdS)Nslinezline bufferingr�Frr)r�lineseprZassertWarnsRegex�RuntimeWarningrqrsrrr�"test_bufsize_equal_one_binary_mode�sz2ProcessTestCase.test_bufsize_equal_one_binary_modec	CsPtd�D]B}|�t��$tjttjtjd�Wd�q1s@0YqdS)Nr�r�)rMrMr�rr2r�r3)rrYrrr�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)r1r{r�r)�msvcrtZCrtSetReportMode�AttributeError�ImportErrorrTr9r:r�rCrDrr2r3r�rEr)rryr�r�procr1rrr�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?)rM�extendrr+�time�sleep)rY��fdsrr�open_fds�sz<ProcessTestCase.test_double_close_on_error.<locals>.open_fds)�targetr.)
�	threading�Thread�startrM�EnvironmentErrorrr2r�r3r�rr7rR)rr��t�exc�fdrZrr�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|dd
�|�
�|�dd
|fd|fg��dS)Nrzimport time; time.sleep(12)csN��d���f���������d�jf������d�jf�dS)N�thread-start-poll-result�thread-after-kill-and-wait�thread-after-second-wait)rNre�killrrr�r|�resultsrr�kill_proc_timer_thread	szDProcessTestCase.test_threadsafe_wait.<locals>.kill_proc_timer_thread皙�����?r�i����rKz*unexpected result in wait from main thread��msgz&unexpected result in second main wait.)r�Nr�r�)rr2rCrDrErr��Timerr�rrrrkr�)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[rCrDr%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.)rj�mkstemprr2rRrr7�remover r��exists)rZifhandleZifnameZofhandleZofnameZefhandleZefnamerrr� test_handles_closed_on_exceptionAs"�





z0ProcessTestCase.test_handles_closed_on_exceptioncCsTtjttjtjtjd�}|�|jj�|�|jj�|�|jj�|�	dd�dS)Nr.�x�)
rr2rr3rlr0r7r1r/r�r8rrr�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�objectrr2rrE�repr)	rZpath_cmdZpathlib_clsZcasesrr�r�Zsxr9rrr�	test_repr`s

��	zProcessTestCase.test_reprcCs8tjttjd�}|�|jj�|��|�dd�dS)Nrhr�r�)	rr2rr3rlr/r7rr�r8rrr�!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�rlrCrDrr3r2r�)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)Nr�r.)
r�getpid�listdirrMr'r(rrr3rE)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�)rMr�rrBrErT�filenamerUrrr�%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�)rMr�rr2rErTr�rUrrr� test_file_not_found_with_bad_cwd�s0z0ProcessTestCase.test_file_not_found_with_bad_cwdcCs,|�tjttj�|�tjttj�dSr)r4rr2�bytes�types�GenericAlias�CompletedProcessrjrrrr�test_class_getitems�sz#ProcessTestCase.test_class_getitemsN)zr$r%r&r:r=rHrOrRrWr_rarerprsrwrzr|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�rC�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)r/r0r6r7r;r<r=r@rIr]rbrfrhrlrmrnrqrtrwrxr}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)Nr)rCrDrr)rr�r+�argvrrr�
run_python�szRunFuncTestCase.run_pythoncCsP|�d�}|�|jd�|�tj��|��Wd�n1sB0YdSr�)r�rErrMrrS�check_returncode�r�cprrr�test_returncode�s
zRunFuncTestCase.test_returncodecCsN|�tj��}|jddd�Wd�n1s00Y|�|jjd�dS)Nr?T��checkr@)rMrrSr�rErTrrUrrr�
test_check�s,zRunFuncTestCase.test_checkcCs tjtdd�}|�|jd�dS)NTr�r)rrrrErr�rrr�test_check_zero�szRunFuncTestCase.test_check_zerocCs>|�tj��|jddd�Wd�n1s00YdS)NrIrirK)rMrrNr�rrrr�test_timeout�szRunFuncTestCase.test_timeoutcCs"|jdtjd�}|�d|j�dS)NrXr~rY�r�rr3r\r0r�rrr�test_capture_stdout�sz#RunFuncTestCase.test_capture_stdoutcCs"|jdtjd�}|�d|j�dS)NrbrcrY)r�rr3r\r1r�rrr�test_capture_stderr�s�z#RunFuncTestCase.test_capture_stderrcCsLt��}|�|j�|�d�|�d�|jd|tjd�}|�	d|j
�dS)Nrfrrgr�ri)rjrkrlr7rmrnr�rr3r\r0)rror�rrrrp�s

�z+RunFuncTestCase.test_check_output_stdin_argcCs$|jddtjd�}|�d|j�dS)Nrgrf)rrr0rir�r�rrrrs�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)
Nrfrr�r�r}r�r�r/rr)rjrkrlr7rmrnrMrr�r\rTr*r�rrrr��s

��$z6RunFuncTestCase.test_check_output_stdin_with_input_argcCsb|�tj��"}|jddtjd�}Wd�n1s40Y|�|jjd�|�|jjd�dS)Nr�r�)rLr0rY)	rMrrNr�r3rErTr^r0)rrVr�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�rEr)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 testr~rT)r0r�)rr
rrTrrrr�rErrMr�)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�rCrDrrrEr�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�)rrCrDrrrErr�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_outputrYsFLUFL)r�r\r0r1r�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�r0r0rr��
rjrkrlr7rMrr�r\rTr*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�r1r1rr�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)
Nzsleep 3TrJ)r�rLr�zTimeoutExpired not raised.g�?z/TimeoutExpired was delayed! Bad traceback:
```
z```r�)	r��	monotonicrrrN�	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�rprsr�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�chdirrrRr�)rrZ�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)Nrrr��Expected OSError: %s�rrr2rCrDrrRrErS�strerrorr�r��rrr9rZrrr�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)Nrrr�rrrrrr�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)Nrrr)
rrr2rrRrErSrr�r�rrrr�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�sr
z%subprocess._posixsubprocess.fork_execc	Csvdd�}||_tjdtd��F|�t��|�dg�Wd�n1sJ0YWd�n1sh0YdS)NcWs0|d}d�tj���}t�|d|d�dS)N�
z{:x}sOSError:�:r)rGrSZEISDIRrrrm)r*�
errpipe_writeZerr_coderrr�proper_error�szHPOSIXProcessTestCase.test_exception_errpipe_normal.<locals>.proper_error�subprocess.os.waitpid��side_effect�non_existent_command)rrr��ChildProcessErrorrM�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)Ns�ޭcs|d}t�|��dS)Nrr)rrm)r*r�Z
error_datarr�	bad_error�szGPOSIXProcessTestCase.test_exception_errpipe_bad_data.<locals>.bad_errorrrr)rrr�rrMr�SubprocessErrorr
r\r�rjrT)rrrrZrrr�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	rT�assertNotEqual)rZdefault_proc_statusrpZdefault_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[rCrDrRrS�EPERMr�getsid�intr")rr^rZZ
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�geteuidrCr��pwd�getpwnamrNr��subTestrr[rD�PermissionErrorrRrSZEACCESr%�
isinstancerj�pw_uidr'rErMrrQr�
OverflowErrorr�r�)
r�uidZ
test_usersZname_uidr.r/r^rZZuser_uidZ
child_userrrr�	test_usersN

��
0.
�&zPOSIXProcessTestCase.test_userz setreuid() available on platformcCs<|�t��tjtdd�Wd�n1s.0YdS)N�r1�rMrrrQrrrrr�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$)�groupr/rzimport os; print(os.getgid())r0�rAr2)r�r�rA)r�getegidr�r�rNr6rr[rCrDr7r8rjr��gr_gidr'rErMrrQrr:r�r�)	r�gid�
group_listr�rAr/r^Z	group_gidZchild_grouprrr�
test_groupDs@
��

..
�&
zPOSIXProcessTestCase.test_groupz setregid() available on platformcCs<|�t��tjtdd�Wd�n1s.0YdS)Nr=rBr>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)r8rjr�r�rD)r�grrrr�s�z:POSIXProcessTestCase.test_extra_groups.<locals>.<listcomp>r0r2)r�r�rK)rrCr�r�rNrr[rCrDrRrSr%�	getgroups�json�loadsrE�setrMrrQrr�r�)
rrErFr�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)NrJr>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()�+)rTi�i�z != r�)rjrLrr�r�rrQrCrD�stat�st_moderE�octr
rV)rrX�namerWZ
expected_moderrr�
test_umask�s"���zPOSIXProcessTestCase.test_umaskcCsXt���*t�tjddg�}|��Wd�n1s80Y|�|jt	j
�dS)Nrzimport os; os.abort())rrorr2rCrDrrErr��SIGABRTr8rrr�test_run_abort�s

�&z#POSIXProcessTestCase.test_run_abortcCsNt�ttj�d�}t|�}|�d|���|�d|�|�ttj�|�dS)N�fake cmdr�ZSIG)rrSr'r�r[rjr\�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.�rrSrjr\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.rar_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>)r0�
preexec_fn�appler�r8rrr�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?�rrrrr�raise_it�sz=POSIXProcessTestCase.test_preexec_exception.<locals>.raise_itrr�rhz)Expected a ValueError from the preexec_fnZcoconutrzEException raised by preexec_fn did not make it to the parent process.)rr2rCrDrr%�_posixsubprocessrr\r*r�)rrlr9rZrrr�test_preexec_exception�s�
�(z+POSIXProcessTestCase.test_preexec_exceptionc@seZdZdd�Zdd�ZdS)z+POSIXProcessTestCase._TestExecuteChildPopencOs$||_tjj|g|�Ri|��dSr)�	_testcaserr2r�)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rO�O_RDONLY�rr"rrrrs�zNPOSIXProcessTestCase._TestExecuteChildPopen._execute_child.<locals>.<listcomp>�z!At least one fd was closed early.r�)rr2r,rMrprvr/r�r0r1rr7)rr*r+Zdevzero_fdsr�rrrr,�s@���
�����
�z:POSIXProcessTestCase._TestExecuteChildPopen._execute_childN)r$r%r&r�r,rrrr�_TestExecuteChildPopen�srurqz/dev/zero required.c	CsTdd�}|�tj��,|j|ttjtjtj|d�Wd�n1sF0YdS)NcSst�d��dS)Nz-force the _execute_child() errpipe_data path.)rrrrrrrls�zWPOSIXProcessTestCase.test_preexec_errpipe_does_not_double_close_pipes.<locals>.raise_it)r/r0r1rh)rMrrrurr3)rrlrrr�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_errorrrcSsdSrrrrrrrg*r�zEPOSIXProcessTestCase.test_preexec_gc_module_failure.<locals>.<lambda>rmz#Popen enabled gc when it shouldn't.cSsdSrrrrrrrg1r�zPopen left gc disabled.cSsdSrrrrrrrg7r�cSsdSrrrrrrrg<r�)�gc�	isenabled�disabler rrBrCrD�enabler%rMrwr2rz)rrxZenabledZorig_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_NPROCrrcSsdSrrrrrrrgRr�z@POSIXProcessTestCase.test_preexec_fork_failure.<locals>.<lambda>rmz.RLIMIT_NPROC had no effect; probably superuser)�resourcer~rr�r{rTrlrrBrCrD�BlockingIOError)rr~rr�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�wrC)rFz#!%s
z(exec '%s' -c 'import sys; sys.exit(47)'
i�r@)rjr�rOrmr�
unix_shellrCrDr�chmodrr2rr�rEr)rr��fname�fobjr9rrr�test_args_stringYs�$

z%POSIXProcessTestCase.test_args_stringcCs<|jttjtjddgdd�|jttjtjddgdd�dS)Nrr?r@��startupinfo��
creationflags�rMrrrBrCrDrrrrr�gs
��
��z&POSIXProcessTestCase.test_invalid_argscCshtj��}d|d<tjdgdtj|d�}|�(|�|j���	d�d�Wd�n1sZ0YdS�Nrer�zecho $FRUITr��r�r0r�s 	
ri�
rr�r�rr2r3rEr0r�r�rr�r9rrr�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�)rjr�rOrmrr�rCrDrr�rrBr�rE)rr�r�r�rGrrr�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)rDr�r0r�)rr�r��isfilerNrT�islinkrr2r3rEr0r�rr�)rZshellsr�rY�shr9rrr�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�r/r/r0r1r�)r��SIGINT�default_int_handlerrr2rCrDr3r0r��getattr)r�methodr*r�r9rrr�
_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�)rr2rCrDr3r0r�r�r�r�r�)rr�r*r9rrr�_kill_dead_process�s�
z'POSIXProcessTestCase._kill_dead_processcCs:|�dtj�}|��\}}|�d|�|�|��d�dS)N�send_signalsKeyboardInterruptr)r�r�r�r�r\r"r�rr9r"r1rrr�test_send_signal�sz%POSIXProcessTestCase.test_send_signalcCs:|�d�}|��\}}|�|d�|�|��tj�dS)Nr�r�)r�r�rErr��SIGKILLr�rrr�	test_kill�s
zPOSIXProcessTestCase.test_killcCs:|�d�}|��\}}|�|d�|�|��tj�dS)N�	terminater�)r�r�rErr��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�duprN)rZsave_fdsr�r��inheritable�savedrrr�	_save_fds�s

zPOSIXProcessTestCase._save_fdscCs.|D]$\}}}tj|||d�t�|�qdS)N)r�)r�dup2r7)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.rir�)r�rr7rr2rCrDr3r�rEr�)	rr�r/�	saved_fdsr�r�r�r�r�rrr�check_close_std_fds	s&

��z(POSIXProcessTestCase.check_close_std_fdscCs|�dg�dSrP�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)Nrcr�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)Nrrcr�rrrr�test_close_fds_0_2'	sz'POSIXProcessTestCase.test_close_fds_0_2cCs|�ddg�dS)Nr�rcr�rrrr�test_close_fds_1_2*	sz'POSIXProcessTestCase.test_close_fds_1_2cCs|�gd��dS)N)rr�rcr�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)Nrr�rz*print('AssertionError:0:CLOEXEC failure.'))	rr�r7rr2rCrDrr�)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�rjr��rrYrrrrI	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")rcr.r��	got STDIN�err)rMr�unlinkrmr�r��	enumerater�rr2rCrDrr�r�rrEr7)
r�temps�temp_fdsr�r�r��temp_fdr9r�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�)rMrr�r�r�r�rmr�rr2rCrDrr�rr�rEr7)
rZstdin_noZ	stdout_noZ	stderr_nor�r�r�r�r�r�r9r�r�rrr�check_swap_fdst	s<
���z#POSIXProcessTestCase.check_swap_fdscCsX|�ddd�|�ddd�|�ddd�|�ddd�|�ddd�|�ddd�dS)Nrr�rc)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�rMrjrkrr�r�rPrUr7�zipr9r:rrBrCrDrjrEr��SEEK_SETr�r�mapr'r�r�)r�from_fds�to_fdsr�Zfrom_fd�fZfd_to_close�	arg_namesr+Zto_fdr�Z
skipped_fdrG�
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�rc)�	itertools�combinationsrM�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:�rkrrrr�prepare�	szCPOSIXProcessTestCase.test_surrogates_error_message.<locals>.preparermr�z!Exception occurred in preexec_fn.z1Expected ValueError or subprocess.SubprocessError)rrBrrrdrnrErjr�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�rCz&import os; print(ascii(os.getenv(%s)))�C�LC_ALLrr�s

z'import os; print(ascii(os.getenvb(%s))))
rr�rr�r�rr[rCrDr�rEr�r�)r�key�valueZ
encoded_value�scriptr�Z
decoded_valuer0rrr�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� rETr��PATHr�sPATH)rr�rrr�r�rrBrEr�rr�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)r/r0r1r/r��r0r/�,z5Expected no fds from %r to be open in child, found %r)r�findfilerr2rCrDr3rlr�rPr�r'r�r/r�r0r1r )	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)r/r0r/cs$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_p2rJzThe child hung)rr�rr2rCrDr3r0rlrr/rmr7�selectr%rEr�)
rZqcatZqgrepZsubdatar[r�rZ	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)rUrs�r�rr�	<genexpr>�
r�z6POSIXProcessTestCase.test_close_fds.<locals>.<genexpr>rt�r0r/�pass_fdsz'Some fds not in pass_fds were left open)rr�rr+rlr7rPrMrO�devnullrr�add�set_inheritablerr2rCrDr3r�r�r'r�rEr r\)
rr�r�r"r�r9r^�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)Nr�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))
        r~rcz(expected exactly two lines of output:
%rr�rr�r�zSome fds were left open.)rr�rr2rCrDr9r:r3r�r!rEr!rPr�r'rr�r )rr�r9r^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_loweredrJr`c	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�r`rr�Trr�zfd to be passed not passedzfd to be closed passedF)r/rzoverriding close_fds)rr�rPrMrr+rlr7r
�updaterr2rCrDr3r�r�r'r�r\r ZassertWarnsrvrBrrj�warning)rr�r�r-r�r�r9r^rr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+rlr7r
rCrDrr�rjrr2r3r�rPr'r�rEr�)
rr�r�Znon_inheritablerr*r9r^rr�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�0Yd
d�|�d�D�}|�|hd
�t|�Bd|���dS)Nr�r�r�rcr�rT)r/r0r1r/rr�cSsh|]}t|��qSr)r')r�numrrr�	<setcomp>@r�z@POSIXProcessTestCase.test_pass_fds_redirected.<locals>.<setcomp>r�>rr�rczoutput=)rr�rMrrOr�O_RDWRrlr7rNr+�insertrr2rCrDr�r�rE�	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+)r0r/�r5rOrrrr2rr�rZinoutr9rrr�%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)r1r/rrrrr�%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:
%srE)rr�rr2rCrDr3r�rErr�)rZsigchild_ignorer9r0r1rrr�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)r0r;r�sapplg)r�
import_modulerr2rCrDr3r0rlr7rEr�r\rr)rrr9r�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>)rr2rCrDr3rlr0r7r1r%�pidr�check_warnings�ResourceWarningrrdrr\)rr9�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�)rr2rCrDr3rlr0r7r1r%r(rr)r*�
gc_collectrr�r�r�rrdrr\r�r�rMrRr��waitpidrv)rr9r+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�rrrg�r�zCPOSIXProcessTestCase.test_close_fds_after_preexec.<locals>.<lambda>)r0r/rhr�)rr�rr�rlr7rr2rCrDr3r�rPr�r'r�rv)rr�r9r^rrrr0r�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)NrcSsdSrrrrrrrg�r�z5POSIXProcessTestCase.test_fork_exec.<locals>.<lambda>r�sexe�envsargTrr0r�rcr�r�Fz
takes exactly)rnryrzr|rMr�rrvrjrTr{)	rrn�
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�rrrrr�__int__�szRPOSIXProcessTestCase.test_fork_exec_sorted_fd_sanity_check.<locals>.BadInt.__int__N)r$r%r&r7r�r8rrrr�BadInt�sr9)r0rcr�r�r`)rjr�)���*l)r`r�)r2r�r�rtr�rczfds_to_keep={}r�sfalseTr2r0r�r�r
)rnryrzr|rMrrGrr\rjrTr{)rrnr9r3r
rVrrr�%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)Nr/)rr2rrr�r��BrokenPipeErrorr7rr�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)Nr/�stuff)rr2rrr�r�r>rmrr��assert_called_once_withr7r?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�r/�wbrB)rr2rCrDr3rr�r�rOrrr>r4rr��return_valuer�rC)rr|r@Zdev_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)NrEr�r/i�rK)
rr2rCrDr3rr�r�r>r7rr�rCr?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)Nr�r�r�r)rG���)rrr2r�wait_processr(�	_testcapirJrr�rrE)rr*r|�statusrrrr�test_stoppedGs

&z!POSIXProcessTestCase.test_stoppedcCstt�t�}tj|jdd�|�|j�t�	d��}|�
tj�Wd�n1sR0Y|�
�|�|j�dS)NrrKzos.kill)rr2rrrMr(rdrrr�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)recSsdSrrrrrrrgwr�z=POSIXProcessTestCase.test_send_signal_race2.<locals>.<lambda>)�new)rr2rCrDrrerr�r�r�r�r�r8rrr�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)r~rJrK)rr2rCrDr3r�rN�rr|rrr�1test_communicate_repeated_call_after_stdout_close{s
��zFPOSIXProcessTestCase.test_communicate_repeated_call_after_stdout_close)nr$r%r&rrr	r
rrr2r
rr�rrr�r�rr�r�r#r(r�r�r<r?rGrHrRrSrrZr\r`rbrdrjrorurvr}rCr�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�rrrV�st_devrrZrequires_mac_verrrrrrr r!r#r,r/r1�cpython_onlyr6r=rArDrHrIrNrPrQrSrU�
__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�wShowWindowrBr)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�)rrZrBr)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)Nrcr�)r0r1r�r�handle_list)rrZr]r[ZSW_HIDEr\rMrrOrrr2rdr�rErrdZ	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�)rCr1rmrrBrD)rZCREATE_NEW_CONSOLErrr�test_creationflags�s��z'Win32ProcessTestCase.test_creationflagscCs$|jttjtjddgdd�d�dS)Nrr?cSsdSr�rrrrrrg�r�z8Win32ProcessTestCase.test_invalid_args.<locals>.<lambda>rmr�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�srgr�)�dictrMr�rr2r)rrgrrr�test_issue31471�sz$Win32ProcessTestCase.test_issue31471cCs&tjtjddgdd�}|�|d�dS)Nrr?T)r/r@rArFrrrr�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�)r0r1r/sOSErrorra)r0r1r�r/z.*overriding close_fds)ryrr+rlr7r
rNZ
get_osfhandlerr2rCrDrGr3r�rErr'rr\rZrbrr)rv)
rryr�rWr�r9r0r1rar�rrr�test_close_fds_with_stdio�s^
��
�
�

��
��z.Win32ProcessTestCase.test_close_fds_with_stdiocCs t��}i|_tjt|d�dS)Nr��rrZrbrBr�rr�rrr�test_empty_attribute_list
s
�z.Win32ProcessTestCase.test_empty_attribute_listcCs$t��}dgi|_tjt|d�dS)Nrar�rkrlrrr�test_empty_handle_list
s

�z+Win32ProcessTestCase.test_empty_handle_listcCsbtj��}d|d<tjdgdtj|d�}|�"|�d|j���Wd�n1sT0YdS�N�physalisr�rPr�r�sphysalis�	rr�r�rr2r3r\r0r�r�rrrr� 
s

�z(Win32ProcessTestCase.test_shell_sequencecCs`tj��}d|d<tjddtj|d�}|�"|�d|j���Wd�n1sR0YdSrorqr�rrrr�*
s
�z&Win32ProcessTestCase.test_shell_stringc	CsndD]d}tj��}d|d<tjddtj||d�}|�$|�d|j��|�Wd�q1s^0YqdS)N)�ansiZoemrpr�rPr�)r�r0r�r>rq)r�encr�r9rrr�test_shell_encodings4
s
�z)Win32ProcessTestCase.test_shell_encodingscCs t�tjd�}|�|d�dS)Nz -c "import sys; sys.exit(47)"r@rArFrrrr�@
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)rr2rCrDr3r0r�r�r�rErr")rr�r*r9r"r1rrrrr�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<)
rr2rCrDr3r0r�r�r�r�r�rEr)rr�r*r9r"r1rGrrrr�[
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�rurrrrr�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_r`rcrer�rrWrirrjrmrnr�r�rtr�r�r�r�r�r�r�r�r�rrrrrY�s.
1

rYc@s^eZdZGdd�dej�Zej�ejd�dd��Z	dd�Z
dd	�Zd
d�Zdd
�Z
dd�ZdS)�	MiscTestscs eZdZgZ�fdd�Z�ZS)zMiscTests.RecordingPopencs"t�j|i|��|j�|�dSr)r�r��instances_createdrNr)rrrr��
sz!MiscTests.RecordingPopen.__init__)r$r%r&rwr�rXrrrr�RecordingPopen�
srx�_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)N�_waitr2rzLimport time
time.sleep(9)
import sys
sys.stderr.write('\n!runaway child!\n')r0r�rKz.no open-ended wait() after the first allowed: g�?rcr�)�KeyboardInterruptrrr�r�rr2rxrMrCrDr�Zcall_args_listr"rBrNZassertLessEqualZ
call_countrEr!rwrUr�r)rZpopenerZmock__communicater+Z
mock__waitrBZsigint_callsr�rrr�_test_keyboardinterrupt_no_kill�
sP
����B��

��(�z)MiscTests._test_keyboardinterrupt_no_killcCs|jtjdd�dS�NgT㥛� @rK)r|rrBrrrr�#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)rr2r{)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)rEr�	getoutput�getstatusoutputrjrLrr�r�rr"�rmdir)r�dirrYrOr^rrr�test_getoutput�
s ���zMiscTests.test_getoutputcCsnhd�}ttj�}t�}ddl}tj��D].\}}|�d�r>q*t||jf�rNq*|�	|�q*|�
|||�dS)N>rar�r4�Handlerr")rPr�__all__r��__dict__�itemsr	r8�
ModuleTyper	rE)rZintentionally_excludedZexportedZpossible_exportsr�rYr�rrr�test__all__�
s

zMiscTests.test__all__N)r$r%r&rr2rxrr�r�r|r~rr�r�r�rrrrrv�
s
+rv�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])))	r�rrjr�r^r�rrmr7)rr�r�rrrr�
s


zCommandsWithSpaces.setUpcst�|j�t���dSr)rr�r�r�r"rrrrr"szCommandsWithSpaces.tearDowncOs`tj|d<tj|i|��}|�.|�|j���d�d|j�Wd�n1sR0YdS)Nr0�mbcsz2 [%r, 'ab cd'])rr3r2rEr0r�r�r�)rr*r+r9rrr�with_spacess
�zCommandsWithSpaces.with_spacescCs |jdtj|jdfdd�dS)N�"%s" "%s" "%s"�ab cdr�r��r�rCrDr�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�rXrrrrr��
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)rr2rCrDr3rEr0r�r1r%r&rTrrr�	test_pipe#s
��0zContextManagerTests.test_pipecCsDt�tjddg��}Wd�n1s(0Y|�|jd�dS)Nrzimport sys; sys.exit(100)�d)rr2rCrDrErrTrrrr�0s
�z#ContextManagerTests.test_returncodecCsTtjtjddgtjd��(}|�d�|�|jd�Wd�n1sF0YdS)Nrz2import sys;sys.exit(sys.stdin.read() == 'context')rhscontextr�r�rTrrrr7s
��
z*ContextManagerTests.test_communicate_stdinc	Csb|�t��Dtjttjtjd��}Wd�n1s60YWd�n1sT0YdS)Nr�)rMr�rr2r�r3rTrrrr�?s�z%ContextManagerTests.test_invalid_argscCsrtjttjtjdd�}|��}|j�dtj�|�	|j
�|�t|j
ddd�|�|j
d�|�|jj�dS)Nrc)r/r;r�r)rr2rr3rr,�	__enter__r/rmrdrrMrR�__exit__rEr%r&rTrrr�test_broken_pipe_cleanupFs�z,ContextManagerTests.test_broken_pipe_cleanupN)r$r%r&r�r�rr�r�rrrrr�!s

r��__main__)?r�rr�rrrCr�r5r�rrSrjr�r�r�r�r�rr
r�ryr9rNr�Ztest.supportrrNr{r4r�ZPGOr�r�rr3r�r��NotADirectoryErrorr7r�rDrrZTestCaser�	Exceptionr'r2r(r-r�r�r�r�r�rYrvr�r�r�r�r$�mainrrrr�<module>s�






Y0
!
}d�
+6


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