HEX
Server: Apache
System: Linux zacp120.webway.host 4.18.0-553.50.1.lve.el8.x86_64 #1 SMP Thu Apr 17 19:10:24 UTC 2025 x86_64
User: govancoz (1003)
PHP: 8.3.26
Disabled: exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname
Upload Files
File: //usr/local/lib/python3.10/test/__pycache__/test_subprocess.cpython-310.opt-2.pyc
o

�iVq�@s�ddlZddlmZddlmZddlmZddlmZddlmZddlmZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlmZzddlZWne y�dZYnwzddl!Z!Wne y�dZ!Ynwzddl"Z"Wne y�dZ"Ynwzddl#Z#WndZ#Yej$r�e�%d	��e
j&d
kZ'e'r�dZ(ndZ(d
Z)e*e+e,fZ-e
j.ddfa/dd�Z0Gdd�dej1�Z2Gdd�de3�Z4Gdd�de	j5�Z6Gdd�de2�Z7Gdd�de2�Z8dd�Z9e�:e'd�Gdd �d e2��Z;e�<e'd!�Gd"d#�d#e2��Z=Gd$d%�d%ej1�Z>e�<e?ed&�d'�Gd(d)�d)e7��Z@e�<e'd*�Gd+d,�d,e2��ZAGd-d.�d.e2�ZBeCd/k�rke�D�dSdS)0�N)�mock)�support)�check_sanitizer)�
import_helper)�	os_helper)�warnings_helper)�FakePathztest is not helpful for PGO�win32z@import msvcrt; msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY);�)Znonexisting_i_hope�-c�passcCsHt�d�}|durdSt�|tj�r t�|g�jdkr"|fadSdSdS)N�truer)	�shutil�which�os�access�X_OK�
subprocess�run�
returncode�ZERO_RETURN_CMD)Z
shell_true�r�1/usr/local/lib/python3.10/test/test_subprocess.py�setUpModuleGs

�rc@�eZdZdd�Zdd�ZdS)�BaseTestCasecCst��dS�N)r�
reap_children��selfrrr�setUpRszBaseTestCase.setUpcCsBtstjD]}|��qt��|�tjd�|��t��dS)Nzsubprocess._active not empty)	�	mswindowsr�_active�wait�_cleanup�assertFalseZ
doCleanupsrr)r�instrrr�tearDownWs

�zBaseTestCase.tearDownN��__name__�
__module__�__qualname__r r'rrrrrQsrc@seZdZdS)�PopenTestExceptionN)r)r*r+rrrrr,dsr,c@seZdZ	dd�ZdS)�PopenExecuteChildRaisescO�td��)NzForced Exception for Test)r,�r�args�kwargsrrr�_execute_childl�z&PopenExecuteChildRaises._execute_childN)r)r*r+r2rrrrr-hs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$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3d4�Zd5d6�Zd7d8�Ze� e!d9�d:d;��Z"e� e!d9�d<d=��Z#e� e!d9�d>d?��Z$d@dA�Z%dBdC�Z&dDdE�Z'dFdG�Z(dHdI�Z)dJdK�Z*e� e!dL�dMdN��Z+e� e!dL�dOdP��Z,dQdR�Z-e� e.j/e.j0kdS�dTdU��Z1e� e.j/e.j0kdS�e� e2�3�dV�dWdX���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�EeF�o/eGeFdy�dz�d{d|��ZHe�EeF�o?eGeFdy�dz�d}d~��ZIdd��ZJe� e.jKd�kd��e� e2�Ld��d�kd��e� eMd�d��d��d�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�dĄZkd�dƄZld�dȄZmd�dʄZnd�d̄Zod�d΄Zpd�dЄZqd�d҄Zrd�dԄZsd�dքZte�EeGeud׃dءe�EeGevdكdڡe�EeGevdۃdܡd�dބ���Zwe�Eevjx�yd�ev�z��d�d�d��Z{e� e!d�d�d��Z|e� e!d�d�d��Z}d�d�Z~d�S)��ProcessTestCasecCs�tjttjtjtjd�}z.|�|jtj�|�|jtj�|�|j	tj�W|j�
�|j�
�|j	�
�|��dS|j�
�|j�
�|j	�
�|��w)N��stdin�stdout�stderr)r�Popenr�PIPE�assertIsInstancer6�io�BufferedIOBaser7r8�closer#�r�prrr�test_io_buffered_by_defaultrs �



�


z+ProcessTestCase.test_io_buffered_by_defaultcCs�tjttjtjtjdd�}z.|�|jtj�|�|jtj�|�|j	tj�W|j�
�|j�
�|j	�
�|��dS|j�
�|j�
�|j	�
�|��w)Nr)r6r7r8�bufsize)rr9rr:r;r6r<�	RawIOBaser7r8r>r#r?rrr�test_io_unbuffered_works�s �



�


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�TimeoutExpiredrIrJrKrrrr�test_call_timeout�s

�z!ProcessTestCase.test_call_timeoutcCst�t�}|�|d�dS�Nr)r�
check_callrrLrMrrr�test_check_call_zero�s
z$ProcessTestCase.test_check_call_zerocC�R|�tj��}t�tjddg�Wd�n1swY|�|jjd�dSrE)	rTr�CalledProcessErrorrXrJrKrL�	exceptionr�r�crrr�test_check_call_nonzero�s
��z'ProcessTestCase.test_check_call_nonzerocCs"t�tjddg�}|�d|�dS)Nr�
print('BDFL')�BDFL�r�check_outputrJrK�assertIn�r�outputrrr�test_check_output�s
�z!ProcessTestCase.test_check_outputcCrZ)Nrzimport sys; sys.exit(5)�)	rTrr[rcrJrKrLr\rr]rrr�test_check_output_nonzero�s
��z)ProcessTestCase.test_check_output_nonzerocCs(tjtjddgtjd�}|�d|�dS)Nr�$import sys; sys.stderr.write('BDFL')�r8ra)rrcrJrK�STDOUTrdrerrr�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())�r6�PEAR)�tempfile�
TemporaryFile�
addCleanupr>�write�seekrrcrJrKrd)r�tfrfrrr�test_check_output_stdin_arg�s

��z+ProcessTestCase.test_check_output_stdin_argcCs&tjtjddgdd�}|�d|�dS)Nrrorn��inputrqrbrerrr�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 '')rysXX�rrcrJrK�assertNotInrerrr�test_check_output_input_none�s��z,ProcessTestCase.test_check_output_input_nonecC�(tjtjddgddd�}|�d|�dS)Nrr|T)rz�text�XXr}rerrr�!test_check_output_input_none_text����z1ProcessTestCase.test_check_output_input_none_textcCr�)Nrr|T)rz�universal_newlinesr�r}rerrr�/test_check_output_input_none_universal_newlines�r�z?ProcessTestCase.test_check_output_input_none_universal_newlinescCs*tjtjddgdddd�}|�d|�dS)Nrzprint('foo')�utf-8�ignore)rz�encoding�errors�foorbrerrr�,test_check_output_input_none_encoding_errors�s

�z<ProcessTestCase.test_check_output_input_none_encoding_errorscCsd|�t��}tjtjddgtjd�}|�d�Wd�n1s!wY|�d|j	j
d�dS)Nr�print('will not be run')�r7z-Expected ValueError when stdout arg supplied.r7r)rT�
ValueErrorrrcrJrKr7�failrdr\r0�rr^rfrrr�test_check_output_stdout_arg�s
��z,ProcessTestCase.test_check_output_stdout_argcCs�t��}|�|j�|�d�|�d�|�t��}tj	t
jddg|dd�}|�d�Wd�n1s5wY|�
d|jjd�|�
d	|jjd�dS)
Nrnrrr��hare�r6rz�7Expected ValueError when stdin and input args supplied.r6rz)rrrsrtr>rurvrTr�rrcrJrKr�rdr\r0�rrwr^rfrrr�&test_check_output_stdin_with_input_arg�s


��z6ProcessTestCase.test_check_output_stdin_with_input_argcCs`|�tj��}tjtjddgdd�}|�d�Wd�n1s!wY|�|jj	d�dS)Nr�Mimport sys, time
sys.stdout.write('BDFL')
sys.stdout.flush()
time.sleep(3600)�rRzExpected TimeoutExpired.ra)
rTrrUrcrJrKr�rLr\rfr�rrr�test_check_output_timeouts��	�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�copyrrIrJrKrL)r�newenvrNrrr�test_call_kwargss

��z ProcessTestCase.test_call_kwargscCs~t���)}|jttjdd�tjjjj}dg|d}|jttjg|�R�Wd�n1s0wY|�	|�
�d�dS)Nr�)Zinvalid_arg_namerr
)rZcaptured_stderrrT�	TypeErrorrr9�__init__�__code__�co_argcountrL�getvalue)r�sZargcountZ
too_many_argsrrr�test_invalid_argss
�z!ProcessTestCase.test_invalid_argscC�Rtjtjddgtjtjd�}|�|jj�|�|jj�|�	�|�
|jd�dS)Nr�print("banana")�r7r8)rr9rJrKr:rtr7r>r8r#rLr6r?rrr�test_stdin_none'��zProcessTestCase.test_stdin_nonecC�ld}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�
rr9rJrKr:rtr7r>r8�communicaterLr�rstrip�r�coder@�out�errrrr�test_stdout_none0s�z ProcessTestCase.test_stdout_nonecCr�)Nrr��r6r7)rr9rJrKr:rtr7r>r6r#rLr8r?rrr�test_stderr_noneGr�z ProcessTestCase.test_stderr_nonecKs6|dg}tj|fi|��}|��|�d|j�dS�NrFrG)rr9r#rLr)r�pre_argsr1r0r@rrr�_assert_pythonPs
zProcessTestCase._assert_pythoncCs0tj�tj�tj�d�}|j|dgtjd�dS�N�doesnotexistr�rK)r�path�join�dirnamerJrKr��rr�rrr�test_executableXs�zProcessTestCase.test_executablecCs6tj�tj�tj�d�}|j|dgt�tj�d�dSr�)rr�r�r�rJrKr��fsencoder�rrr�test_bytes_executablecs�


�z%ProcessTestCase.test_bytes_executablecCs4tj�tj�tj�d�}|j|dgttj�d�dSr�)rr�r�r�rJrKr�rr�rrr�test_pathlike_executableis�

�z(ProcessTestCase.test_pathlike_executablecCs0tjdg}|�|�|jt|j|tdd�dS)Nrrr�)rJrKr�rT�NONEXISTING_ERRORS�NONEXISTING_CMD)rr�rrr� test_executable_takes_precedenceos


�z0ProcessTestCase.test_executable_takes_precedencez"executable argument replaces shellcCs|jgtjdd�dS�NT)rK�shell)r�rJrKrrrr�test_executable_replaces_shellysz.ProcessTestCase.test_executable_replaces_shellcCs|jgt�tj�dd�dSr�)r�rr�rJrKrrrr�$test_bytes_executable_replaces_shells
�z4ProcessTestCase.test_bytes_executable_replaces_shellcCs|jgttj�dd�dSr�)r�rrJrKrrrr�'test_pathlike_executable_replaces_shell�s
�z7ProcessTestCase.test_pathlike_executable_replaces_shellcCs6t�|��t��Wd�S1swYdSr)rZ
change_cwdr�getcwd)r�cwdrrr�_normalize_cwd�s$�zProcessTestCase._normalize_cwdcCstj�tj�}tj�|�Sr)rr��realpathrJrK�split)rZpython_pathrrr�_split_python_path�sz"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)r7rG)rr9r:rtr7r>r#rLrrr��normcase�read�decode)rZexpected_cwdZ
python_argr1r@r�rrr�_assert_cwd�s���
�zProcessTestCase._assert_cwdcCs(t��}|�|�}|j|tj|d�dS�N�r�)rr�
gettempdirr�r�rJrK�r�temp_dirrrr�test_cwd�s
zProcessTestCase.test_cwdcCs.t��}|�|�}|j|tjt�|�d�dSr�)rrr�r�r�rJrKrr�r�rrr�test_cwd_with_bytes�s
z#ProcessTestCase.test_cwd_with_bytescCs,t��}|�|�}|j|tjt|�d�dSr�)rrr�r�r�rJrKrr�rrr�test_cwd_with_pathlike�s
z&ProcessTestCase.test_cwd_with_pathlikez"pending resolution of issue #15533cCs�|��\}}tj�tj|�}t���*}|�tt	j
|g�|jtt	j
|g|d�|�|�}|j|||d�Wd�dS1s?wYdSr��
r�rr�r��curdirrZtemp_cwdrT�FileNotFoundErrorrr9r�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���.}|jtt	j
|g|d�|jtt	j
|g||d�|�|�}|j||||d�Wd�dS1sEwYdS)N�somethingyoudonthaver��rKr�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�dS1s=wYdSr�)
r�rr�r�r�rr�rTr�rr9r�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�rJrK)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�rr�rJrKrrrr�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")rprnr�)rr9rJrKr:r6rur>r#rLrr?rrr�test_stdin_pipe�s
��
zProcessTestCase.test_stdin_pipecCsft��}|�|j�|��}t�|d�t�|dd�tj	t
jddg|d�}|��|�
|jd�dS�Nrnrrr�rpr�)rrrsrtr>�filenorru�lseekrr9rJrKr#rLr�rrw�dr@rrr�test_stdin_filedess
��z"ProcessTestCase.test_stdin_filedescCsXt��}|�|j�|�d�|�d�tjtj	ddg|d�}|�
�|�|jd�dSr�)
rrrsrtr>rurvrr9rJrKr#rLr�rrwr@rrr�test_stdin_fileobjs


��z"ProcessTestCase.test_stdin_fileobjcC�Ttjtjddgtjd�}|�|�|j��d�Wd�dS1s#wYdS)Nr�&import sys; sys.stdout.write("orange")r��orange�rr9rJrKr:rLr7r�r?rrr�test_stdout_pipe!�
��"�z ProcessTestCase.test_stdout_pipecC�`t��}|�|j�|��}tjtjddg|d�}|�	�t
�|dd�|�t
�
|d�d�dS)Nrr�r�r�r��rrrsrtr>r�rr9rJrKr#rr�rLr�r�rrr�test_stdout_filedes)�
��z#ProcessTestCase.test_stdout_filedescC�Pt��}|�|j�tjtjddg|d�}|��|�	d�|�
|��d�dS)Nrr�r�rr��rrrsrtr>rr9rJrKr#rvrLr�r�rrr�test_stdout_fileobj5�
��
z#ProcessTestCase.test_stdout_fileobjcCr�)Nr�*import sys; sys.stderr.write("strawberry")rk�
strawberry)rr9rJrKr:rLr8r�r?rrr�test_stderr_pipe@rz ProcessTestCase.test_stderr_pipecCr)Nrr
rkrrrrr�rrr�test_stderr_filedesHrz#ProcessTestCase.test_stderr_filedescCr)Nrr
rkrrrr�rrr�test_stderr_fileobjTr	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�rr9rJrKr:r�rLr�rr@r7r8rrr�,test_stderr_redirect_with_no_stdout_redirect_s
��	z<ProcessTestCase.test_stderr_redirect_with_no_stdout_redirectcCsXtjtjddgtjtjd�}|�|�|j��d�Wd�dS1s%wYdS)Nr�Rimport sys;sys.stdout.write("apple");sys.stdout.flush();sys.stderr.write("orange")r��appleorange)	rr9rJrKr:rlrLr7r�r?rrr�test_stdout_stderr_pipets
��"�z'ProcessTestCase.test_stdout_stderr_pipecCsRt��}|�|j�tjtjddg||d�}|��|�	d�|�
|��d�dS)Nrrr�rrrr�rrr�test_stdout_stderr_file�s
��
z'ProcessTestCase.test_stdout_stderr_filecCr�)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_stdoutcC�2tjtjddgtjd�}|��|�|jd�dS)Nrz'for i in range(10240):print("x" * 1024)r�)rr9rJrK�DEVNULLr#rLr7r?rrr�test_stdout_devnull��
��z#ProcessTestCase.test_stdout_devnullcCr)Nrz=import sys
for i in range(10240):sys.stderr.write("x" * 1024)rk)rr9rJrKrr#rLr8r?rrr�test_stderr_devnull�s
��z#ProcessTestCase.test_stderr_devnullcCr)Nrzimport sys;sys.stdin.read(1)rp)rr9rJrKrr#rLr6r?rrr�test_stdin_devnull�rz"ProcessTestCase.test_stdin_devnull�F_GETPIPE_SZz%fcntl.F_GETPIPE_SZ required for test.c	Cs�t��\}}zt�|tj�}Wt�|�t�|�nt�|�t�|�w|d}|dkr2t�d��tjt	j
ddgtjtjtj|d�}z.|j|j
|jfD]}|�t�|��tj�|�qL|j�d�|j��W|��|��dS|��|��w)N�iz+default pipesize too small to perform test.r�Qimport sys; sys.stdin.read(); sys.stdout.write("out"); sys.stderr.write("error!")�r6r7r8�pipesizern)r�pipe�fcntlrr>�unittest�SkipTestrr9rJrKr:r6r7r8rLr�ru�killr#)rZtest_pipe_rZtest_pipe_wZpipesize_defaultr"r@�fiforrr�test_pipesizes�s>

������
zProcessTestCase.test_pipesizesc	Cs�tjtjddgtjtjtjdd�}zMt��\}}z+t�|tj�}|j	|j
|jfD]}|�t�|�
�tj�|�q(Wt�|�t�|�nt�|�t�|�w|j	��W|��|��dS|��|��w)Nrr ���r!)rr9rJrKr:rr#r$rr6r7r8rLr�r>r'r#)rr@Zfp_rZfp_wZdefault_pipesizer(rrr�test_pipesize_default�s4����

��
z%ProcessTestCase.test_pipesize_defaultcCsjtj��}d|d<tjtjddgtj|d��}|��\}}|�	|d�Wd�dS1s.wYdS)N�oranger�r�2import sys,os;sys.stdout.write(os.getenv("FRUIT"))�r7r�r�)
rr�r�rr9rJrKr:r�rL�rr�r@r7r8rrr�test_env�s

��"�zProcessTestCase.test_envr	z#cannot test an empty env on WindowsZPy_ENABLE_SHAREDr�zEThe Python shared library cannot be loaded with an empty environment.T)�addressz)AddressSanitizer adds to the environment.cs�	dd��tjtjddgtjid��*}|��\}}t|���}|�|t	��fdd�|D�}|�
|g�Wd�dS1s?wYdS)NcSs,	d|vpd|vp|dkp|�d�p|dkS)NZ	VERSIONERZ__CFZ
LD_PRELOADZSANDBOX�LC_CTYPE)�
startswith)�nrrr�is_env_var_to_ignores���z<ProcessTestCase.test_empty_env.<locals>.is_env_var_to_ignorerz)import os; print(list(os.environ.keys()))r.csg|]}�|�s|�qSrr)�.0�k�r5rr�
<listcomp>s
�z2ProcessTestCase.test_empty_env.<locals>.<listcomp>)rr9rJrKr:r��eval�stripr;�listrL)rr@r7r8Zchild_env_namesrr8r�test_empty_envs
	��"�zProcessTestCase.test_empty_envcCs�tjd}|�t��t�|ddg�Wd�n1swY|�t��t�tjddg�Wd�dS1s<wYdS)N�rrzpass#)rJrKrTr�rr9)r�cmdrrr�test_invalid_cmd"s
�"�z ProcessTestCase.test_invalid_cmdcCsHtj��}d|d<|�t��tjt|d�Wd�n1s wYtj��}d|d<|�t��tjt|d�Wd�n1sEwYtj��}d|d<|�t��tjt|d�Wd�n1sjwYtj��}d|d<tjtj	d	d
gtj
|d��}|��\}}|�|d�Wd�dS1s�wYdS)
NZcabbagezFRUITVEGETABLEr�zorangeVEGETABLE=cabbager�ZlemonzFRUIT=ORANGEzorange=lemonrz3import sys, os;sys.stdout.write(os.getenv("FRUIT"))r.sorange=lemon)
rr�r�rTr�rr9rrJrKr:r�rLr/rrr�test_invalid_env,s6
�
�
�

��"�z ProcessTestCase.test_invalid_envcCs4tjtjddgtjd�}|�d�|�|jd�dS)Nrz/import sys;sys.exit(sys.stdin.read() == "pear")rprnr�rr?rrr�test_communicate_stdinJs
��
z&ProcessTestCase.test_communicate_stdincCs@tjtjddgtjd�}|��\}}|�|d�|�|d�dS)Nrz)import sys; sys.stdout.write("pineapple")r��	pineapple�rr9rJrKr:r�rLrrrr�test_communicate_stdoutR�
��z'ProcessTestCase.test_communicate_stdoutcCs@tjtjddgtjd�}|��\}}|�|d�|�|d�dS)Nrz)import sys; sys.stderr.write("pineapple")rkrCrDrrrr�test_communicate_stderrZrFz'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())r5�bananarC)rr9rJrKr:rtr7r>r8r6r�rLrrrr�test_communicatebs
��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�r6r7r8r�g333333�?rRspineapple
pear
)
rr9rJrKr:rTrUr�rL�encoderrrr�test_communicate_timeoutqs
��
�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�������?rRi)
rr9rJrKr:rTrUr�rL�len)rr@r7�_rrr�%test_communicate_timeout_large_output�s
�	�
z5ProcessTestCase.test_communicate_timeout_large_outputcCs�dD]W}dD]R}dD]M}i}|rtj|d<|rtj|d<|r#tj|d<|s&q
tjtfi|��}|��|jdur?|�|jj�|jdurK|�|jj�|j	durW|�|j	j�q
qqdS)N�FTr6r7r8)
rr:r9rr�r6�
assertTrue�closedr7r8)rZ
stdin_pipeZstdout_pipeZstderr_pipe�optionsr@rrr�test_communicate_pipe_fd_leak�s0





����z-ProcessTestCase.test_communicate_pipe_fd_leakcCs:t�tjddg�}|��\}}|�|d�|�|d�dS)NrrF)rr9rJrKr�rLrrrr�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())r5�a)rr#r>rr9rJrKr�
PIPE_MAX_SIZEr:rtr7r8r6r�rL)r�x�yr@Zstring_to_writer7r8rrr�test_communicate_pipe_buf�s$


���	
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())r5rHssplitsbananasplitr)
rr9rJrKr:rtr7r>r8r6rur�rLrrrr�test_writes_before_communicate�s
��z.ProcessTestCase.test_writes_before_communicatec
Cs�tjddtdg}dD]g}tj|fidtjdtj|di��}|�J|j�d�|j��|�	|j
��d�|j�d	�|j��|�
|j
j�|�	|j
��d
�|�	|j
�d�d	�|�	|j
��d�Wd�n1snwYqdS)
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�r�r6r7Tzline1
zline3
zline2
�zline4
line5
line6
line7
line8)rJrK�	SETBINARYrr9r:r6ru�flushrLr7�readliner>rtr�)rr0Zextra_kwargr@rrr� test_universal_newlines_and_text�s:���

������z0ProcessTestCase.test_universal_newlines_and_textcCs^tjtjddtdgtjtjdd�}|�|jj�|�|j	j�|�
�\}}|�|d�dS)Nrr[z�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�)r8r7r�z#line2
line4
line5
line6
line7
line8)rr9rJrKr]r:rtr7r>r8r�rLrrrr�#test_universal_newlines_communicates
����z3ProcessTestCase.test_universal_newlines_communicatecCsHtjtjddtt�d�gtjdd�}|�d�\}}|�	|j
d�dS)Nrr[a
                               s = sys.stdin.readline()
                               assert s == "line1\n", repr(s)
                               s = sys.stdin.read()
                               assert s == "line3\n", repr(s)
                              r�)r6r��line1
line3
r)rr9rJrKr]�textwrap�dedentr:r�rLrrrrr�)test_universal_newlines_communicate_stdins
��	z9ProcessTestCase.test_universal_newlines_communicate_stdincCs0tjttjtjdd�}|��|�|jd�dS)NT)r6r7r�r)rr9rr:r�rLrr?rrr�.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)
Nrr[a�
                               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)r6r8r7r�rbrzline1
line2
line3
line4
line5
zeline2
eline6
eline7
)rr9rJrKr]rcrdr:rtr7r>r8r�rLrrPr3rrrr�7test_universal_newlines_communicate_stdin_stdout_stderr4s
�
�zGProcessTestCase.test_universal_newlines_communicate_stdin_stdout_stderrcCsTdD]%}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)r6r7r�r
ryz1
2
3
4)rJrKrr9r:r�rL)rr�r�r0�popenr7r8rrr�-test_universal_newlines_communicate_encodingsPs���z=ProcessTestCase.test_universal_newlines_communicate_encodingscCs\dD])\}}d}tjd|g}tj|tjtjd|d�}|jdd�\}}|�|d�|��qdS)	N))r�r
)�replaceu��)�surrogateescapeu��)�backslashreplacez\x80\x80z2import sys; sys.stdout.buffer.write(b'[\x80\x80]')rr�)r6r7r�r�r
ryz[{}])rJrKrr9r:r�rL�format)rr��expectedr�r0rhr7r8rrr�test_communicate_errorsfs��z'ProcessTestCase.test_communicate_errorsc
CsBtsd}nd}g}t��}z�t|�D]3}ztj�|tj�}|�	t�
|tjtjB��Wqt
yE}z|jtjkr:�WYd}~nd}~ww|�d|�td�D]	}t�|���qQtd�D]}tjtjddgtjtjtjd�}|�d	�d
}|�|d	�q_W|D]}	t�|	�q�t�|�dS|D]}	t�|	�q�t�|�w)Niiz4failed to reach the file descriptor limit (tried %d)�
�rz-import sys;sys.stdout.write(sys.stdin.read())r5slimer)r!rr�mkdtemp�rangerr�r�rZTESTFN�append�open�O_WRONLY�O_CREAT�OSError�errnoZEMFILE�skipTestr>�poprr9rJrKr:r�rLr�rmtree)
rZmax_handles�handles�tmpdir�iZtmpfile�er@�data�hrrr�test_no_leaking{sL���
���
�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\"br^r�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�abr
zab "")rLr�list2cmdlinerrrr�test_list2cmdline�s0��������z!ProcessTestCase.test_list2cmdlinecCsbtjtjddgtjd�}|�|jj�|�|�	��t
�|j��d�|�
�|�|�	�d�dS)Nrzimport os; os.read(0, 1)rp�Ar)rr9rJrKr:rtr6r>�assertIsNone�pollrrur�r#rLr?rrr�	test_poll�s
��zProcessTestCase.test_pollcCs.t�t�}|�|��d�|�|��d�dSrW�rr9rrLr#r?rrr�	test_wait�s
zProcessTestCase.test_waitcCsvt�tjddg�}|�tj��}|jdd�Wd�n1s wY|�dt|j	��|�
|jtjd�d�dS)Nrzimport time; time.sleep(0.3)�-C��6?rRz0.0001r)
rr9rJrKrTrUr#rd�strr\rLr�
SHORT_TIMEOUT)rr@r^rrr�test_wait_timeout�s��z!ProcessTestCase.test_wait_timeoutcCs<|�t��t�td�Wd�dS1swYdS)Nr,)rTr�rr9rrrrr�test_invalid_bufsize�s"�z$ProcessTestCase.test_invalid_bufsizecCs>t�td�}|�|��d�tjtdd�}|�|��d�dS)Nr)rBr�r?rrr�test_bufsize_is_none�sz$ProcessTestCase.test_bufsize_is_nonec
Cs�tjtjddgtjtjtjd|d��C}|j�|�t�	|j�
��|j��}t
���z|j�	�Wn	ty;YnwWd�n1sFwYd|_Wd�n1sXwY|�|jd�|�||�dS)NrzDimport sys;sys.stdout.write(sys.stdin.readline());sys.stdout.flush()r�)r6r7r8rBr�r)rr9rJrKr:rr6rurr>r�r7r_r�SuppressCrashReportrxrLr)r�linernr�r@Z	read_linerrr�_test_bufsize_equal_one�s.�

����z'ProcessTestCase._test_bufsize_equal_onecCsd}|j||dd�dS)Nzline
T�r�)r��rr�rrr� test_bufsize_equal_one_text_mode�sz0ProcessTestCase.test_bufsize_equal_one_text_modecCsPdtj��}|�td��|j|ddd�Wd�dS1s!wYdS)Nslinezline bufferingrFr�)r�lineseprJZassertWarnsRegex�RuntimeWarningr�r�rrr�"test_bufsize_equal_one_binary_mode�s"�z2ProcessTestCase.test_bufsize_equal_one_binary_modec	CsPtd�D]!}|�t��tjttjtjd�Wd�n1s wYqdS)Nrr�)rsrTr�rr9r�r:)rrrrr�test_leaking_fds_on_error�s����z)ProcessTestCase.test_leaking_fds_on_errorc	Cs�z	ddl}|jWnttfy|�d�Ynwt�dt�d��}tj	d|g}t
j|t
jdd�}|�|�
�d}Wd�n1sEwY|�|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)r8r�r�r
)�msvcrtZCrtSetReportMode�AttributeError�ImportErrorrzrcrdr�rJrKrr9r:r�rLr)rr�r�r?�procr8rrr�test_nonexisting_with_pipes
s&
�
���z+ProcessTestCase.test_nonexisting_with_pipescs g��fdd�}tj|d�}|��zP|�t��tjttjtjtjd�Wd�n1s/wYW|�	�d}�D]}zt
�|�Wq=tyZ}z|}WYd}~q=d}~ww|dura|�dS|�	�d}�D]}zt
�|�Wqkty�}z|}WYd}~qkd}~ww|dur�|�w)Ncs*td�D]}��t���t�d�qdS)N�g����MbP?)rs�extendrr#�time�sleep)r��fdsrr�open_fds6s�z<ProcessTestCase.test_double_close_on_error.<locals>.open_fds)�targetr5)
�	threading�Thread�startrT�EnvironmentErrorrr9r�r:r�rr>rx)rr��t�exc�fdr�rr�r�test_double_close_on_error3sH���������z*ProcessTestCase.test_double_close_on_errorcs�	t�tjddg��|��jd�g���fdd�}t�d|�}|��t	r)d}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)rtr�r'r#rr�r��resultsrr�kill_proc_timer_threadWszDProcessTestCase.test_threadsafe_wait.<locals>.kill_proc_timer_thread皙�����?r�i����rRz*unexpected result in wait from main thread��msgz&unexpected result in second main wait.)r�Nr�r�)rr9rJrKrLrr��Timerr�r!r#rr�r�)rr�r�Zexpected_errorcoderr�r�test_threadsafe_waitPs6
�
�
���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�rrcrJrKrPr3�ascii)rr�rfrrr�test_issue8780�s
zProcessTestCase.test_issue8780cCs�t��\}}t��\}}t��\}}ztjdg|||d�Wn'tyEt�|�t�|�t�|�t�|�t�|�t�|�Ynw|�tj	�
|��|�tj	�
|��|�tj	�
|��dS)N�*r5)rr�mkstemprr9rxrr>�remover%r��exists)rZifhandleZifnameZofhandleZofnameZefhandleZefnamerrr� test_handles_closed_on_exception�s$
�




�z0ProcessTestCase.test_handles_closed_on_exceptioncCsTtjttjtjtjd�}|�|jj�|�|jj�|�|jj�|�	dd�dS)Nr5�x�)
rr9rr:rtr7r>r8r6r�r?rrr�test_communicate_epipe�s�z&ProcessTestCase.test_communicate_epipec	
Cs�t�d�}|jj}dddgdddfgd�dddf|dd	d
|�d�fg}tjj�tj	d��$|D]\}}}}tj	||d
�}||_
|�t|�|�q/Wd�dS1sSwYdS)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')>r2�r�)
�pathlib�Path�	__class__r)r%r�patch�objectrr9rrL�repr)	rZpath_cmdZpathlib_cls�casesr?r�r�Zsxr@rrr�	test_repr�s"

��	�"�zProcessTestCase.test_reprcCs8tjttjd�}|�|jj�|��|�dd�dS)Nrpr�r�)	rr9rr:rtr6r>r#r�r?rrr�!test_communicate_epipe_only_stdin�s�z1ProcessTestCase.test_communicate_epipe_only_stdin�SIGUSR1zRequires signal.SIGUSR1r'zRequires os.kill�getppidzRequires os.getppidc	Cs�dd�}t�tj|�}|�tjtj|�tjddg}dD]$}|tji}tj|fi|���}|��Wd�n1s;wYqdS)NcS�dSrr)�signum�framerrr�handler��z7ProcessTestCase.test_communicate_eintr.<locals>.handlerrz7import os, signal;os.kill(os.getppid(), signal.SIGUSR1)r�)	�signalr�rtrJrKrr:r9r�)rr��old_handlerr0�stream�kw�processrrr�test_communicate_eintr�s�

���z&ProcessTestCase.test_communicate_eintr�/proc/%d/fdzLinux specificcCsr	dt��}t�|�}|�t��tttjtjtjd�Wd�n1s'wYt�|�}|�	||�dS)Nr�r5)
r�getpid�listdirrTr,r-rrr:rL)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�n1swY|�|jjd�dS)N)�/opt/nonexistent_binary�withZsomer0r�)rTr�rrIrLr\�filenamer]rrr�%test_file_not_found_includes_filename�s�z5ProcessTestCase.test_file_not_found_includes_filenamecCsP|�t��}tjddgdd�Wd�n1swY|�|jjd�dS)N�exit�0z/some/nonexistent/directoryr�)rTr�rr9rLr\r�r]rrr� test_file_not_found_with_bad_cwd�s�z0ProcessTestCase.test_file_not_found_with_bad_cwdcCs,|�tjttj�|�tjttj�dSr)r;rr9�bytes�types�GenericAlias�CompletedProcessr�rrrr�test_class_getitemssz#ProcessTestCase.test_class_getitemsN)r)r*r+rArDrOrVrYr_rgrirmrxr{rr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r%�skipIfr!r�r�r�r�r�r�r�r�r�r�r�r�rJ�base_prefix�prefixr��	sysconfigZis_python_buildr�r�r�r�r�rrrr
rrrrrrrr�
skipUnlessr$�hasattrr)r+r0�platform�get_config_varrr=r@rArBrErGrIrKrNrSrTrYrZr`rarerfrgriror�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rr�r��isdirr�r�r�r�r�rrrrr4ps*		

		










�
�
�

	�
 �

���
'')4����




r4c@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'��Ze�e�d(�d)�d*d+��Zd,S)-�RunFuncTestCasecKs 	tjd|g}tj|fi|��S)Nr)rJrKrr)rr�r1�argvrrr�
run_pythonszRunFuncTestCase.run_pythoncCsR|�d�}|�|jd�|�tj��
|��Wd�dS1s"wYdSr�)rrLrrTrr[�check_returncode�r�cprrr�test_returncode
s


"�zRunFuncTestCase.test_returncodecCsN|�tj��}|jddd�Wd�n1swY|�|jjd�dS)NrFT��checkrG)rTrr[rrLr\rr]rrr�
test_checks�zRunFuncTestCase.test_checkcCs tjtdd�}|�|jd�dS)NTrr)rrrrLrrrrr�test_check_zeroszRunFuncTestCase.test_check_zerocCs@|�tj��|jddd�Wd�dS1swYdS)NrPr�rR)rTrrUrrrrr�test_timeouts"�zRunFuncTestCase.test_timeoutcC�"|jdtjd�}|�d|j�dS)Nr`r�ra�rrr:rdr7rrrr�test_capture_stdout#sz#RunFuncTestCase.test_capture_stdoutcCr
)Nrjrkra)rrr:rdr8rrrr�test_capture_stderr(s�z#RunFuncTestCase.test_capture_stderrcCsLt��}|�|j�|�d�|�d�|jd|tjd�}|�	d|j
�dS)Nrnrror�rq)rrrsrtr>rurvrrr:rdr7)rrwrrrrrx-s

�z+RunFuncTestCase.test_check_output_stdin_argcCs$|jddtjd�}|�d|j�dS)Nrorn)rzr7rqrrrrrr{8s
�z+RunFuncTestCase.test_check_output_input_argcCs�t��}|�|j�|�d�|�d�|jtdd��}|jd|dd�}Wd�n1s.wY|�	d|j
jd�|�	d	|j
jd�dS)
Nrnrr�r�r�r�r�r6rz)rrrsrtr>rurvrTr�rrdr\r0r�rrrr�?s

���z6RunFuncTestCase.test_check_output_stdin_with_input_argcCsb|�tj��}|jddtjd�}Wd�n1swY|�|jjd�|�|jjd�dS)Nr�r�)rSr7ra)	rTrrUrr:rLr\rfr7)rr^rrrrr�Ls��	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��!)rr�r�rrLr)rr�rrrr�test_run_kwargsZs
�zRunFuncTestCase.test_run_kwargscCs�trdnd}t�|�}|dur|�|�d��t|�}tj|tjd�}|�|j	d�|�
t��tj|tjdd�Wd�dS1sDwYdS)Nztree.comr�z required for this testr�rT)r7r�)r!rrrzrrrrrLrrTr�)r�progr��resrrr�test_run_with_pathlike_pathbs
"�z+RunFuncTestCase.test_run_with_pathlike_pathcCs2t�tj�}|ddg}t�|�}|�|jd�dS)Nrsimport sys; sys.exit(57)�9)rr�rJrKrrrLr�rr�r0rrrr�&test_run_with_bytes_path_and_argumentsps

z6RunFuncTestCase.test_run_with_bytes_path_and_argumentscCs0ttj�}|ddg}t�|�}|�|jd�dS)Nrzimport sys; sys.exit(57)r)rrJrKrrrLrrrrr�)test_run_with_pathlike_path_and_argumentsws


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_outputrasFLUFL)rrdr7r8rrrr�test_capture_output~s
�z#RunFuncTestCase.test_capture_outputcC�~t��}|�|j�|jtdd��}|jdd|d�}Wd�n1s$wY|�d|jj	d�|�d|jj	d�dS)	NzAExpected ValueError when stdout and capture_output args supplied.r�r�T)rr7r7rr�
rrrsrtr>rTr�rrdr\r0r�rrr�#test_stdout_with_capture_output_arg�����z3RunFuncTestCase.test_stdout_with_capture_output_argcCr)	NzAExpected ValueError when stderr and capture_output args supplied.r�r�T)rr8r8rrrr�rrr�#test_stderr_with_capture_output_arg�rz3RunFuncTestCase.test_stderr_with_capture_output_argz)requires posix like 'sleep' shell commandc
Cs�	t��}ztjddddd�Wntjy+}zt��}t��}WYd}~n
d}~ww|�d�|j||dd|�d�d	�dS)
Nzsleep 3TrQ)r�rSrzTimeoutExpired not raised.g�?z/TimeoutExpired was delayed! Bad traceback:
```
z```r�)	r��	monotonicrrrU�	traceback�
format_excr�Z
assertLess)rZbefore_secsr�Z
after_secsZstacksrrr�.test_run_with_shell_timeout_and_capture_output�s"

���
�
�z>RunFuncTestCase.test_run_with_shell_timeout_and_capture_outputZ
HAVE_VFORKz!vfork() not enabled by configure.cCs�|�tj�tj�tdd��|j|�d�jddd�Wd�n1s%wYGdd�d�}tj�td|���#|j|�d�jdd	d�t`|j|�d�jdd
d�Wd�dS1s`wYdS)N�
_USE_VFORKFrrzFalse _USE_VFORK failedr�c@�eZdZdd�ZdS)z4RunFuncTestCase.test__use_vfork.<locals>.RaisingBoolcSr.)Nz"force PyObject_IsTrue to return -1)�RuntimeErrorrrrr�__bool__�r3z=RunFuncTestCase.test__use_vfork.<locals>.RaisingBool.__bool__N)r)r*r+r%rrrr�RaisingBool��r&z"odd bool()-error _USE_VFORK failedz%lack of a _USE_VFORK attribute failed)	rPrr"rr�r�rLrr)rr&rrr�test__use_vfork�s ����"�zRunFuncTestCase.test__use_vforkN)r)r*r+rrrrr	rr
rxr{r�r�rrrrrrrr%r�r!r!r�r�r(rrrrr�s2


�r�c	Cs@dD]}trz
t�|�W|StyYqwqt�d��)N)ZstaffZnogroup�grp�nobodyZ	nfsnobodyz?No identified group name to use for this test on this platform.)r)�getgrnam�KeyErrorr%r&)�
name_grouprrr�_get_test_grp_name�s���
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�ep�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�rrr �s

zPOSIXProcessTestCase.setUpc
CsPzt�|j�Wnty}z	|}WYd}~|Sd}~ww|�d|j�|S)Nz,chdir to nonexistent directory %s succeeded.)r�chdirr1rxr�)rr��desired_exceptionrrr�_get_chdir_exception�s���z)POSIXProcessTestCase._get_chdir_exceptionc
C�	|��}ztjtjddg|jd�}Wn+ty>}z|�|j|j�|�|j	|j	�|�|j
|j
�WYd}~dSd}~ww|�d|�dS)Nrr
r��Expected OSError: %s�r5rr9rJrKr1rxrLry�strerrorr�r��rr4r@r�rrr�test_exception_cwd��
���z'POSIXProcessTestCase.test_exception_cwdc
Cr6)Nrr
r�r7r8r:rrr�test_exception_bad_executable�r<z2POSIXProcessTestCase.test_exception_bad_executablec
Cs�	|��}zt�|jddg�}Wn+ty;}z|�|j|j�|�|j|j�|�|j|j�WYd}~dSd}~ww|�	d|�dS)Nrr
r7)
r5rr9r1rxrLryr9r�r�r:rrr�test_exception_bad_args_0s��z.POSIXProcessTestCase.test_exception_bad_args_0c@r#)z&POSIXProcessTestCase.PopenNoDestructorcCr�rrrrrr�__del__r�z.POSIXProcessTestCase.PopenNoDestructor.__del__N)r)r*r+r?rrrr�PopenNoDestructorr'r@z%subprocess._posixsubprocess.fork_execc	Cs�	dd�}||_tjdtd��,|�t��|�dg�Wd�n1s&wYWd�dSWd�dS1s>wYdS)NcWs0|d}d�tj���}t�|d|d�dS)N�
z{:x}sOSError:�:r)rmryZEISDIRrJrru)r0�
errpipe_writeZerr_coderrr�proper_error#szHPOSIXProcessTestCase.test_exception_errpipe_normal.<locals>.proper_error�subprocess.os.waitpid��side_effect�non_existent_command)rGrr��ChildProcessErrorrT�IsADirectoryErrorr@)r�	fork_execrDrrr�test_exception_errpipe_normal s���"�z2POSIXProcessTestCase.test_exception_errpipe_normalc	s�	d��fdd�}||_tjdtd��$|�tj��}|�dg�Wd�n1s+wYWd�n1s:wY|�t	��t
|j��dS)Ns�ޭcs|d}t�|��dS)NrAr)rru)r0rC�Z
error_datarr�	bad_error6szGPOSIXProcessTestCase.test_exception_errpipe_bad_data.<locals>.bad_errorrErFrH)rGrr�rIrTr�SubprocessErrorr@rdr�r�r\)rrKrNr�rrMr�test_exception_errpipe_bad_data1s����z4POSIXProcessTestCase.test_exception_errpipe_bad_data�/proc/self/statuszneed /proc/self/statuscCs�tjddgdd�}|��D]}|�d�r|}nq
|�d�tjddgdd�}|��D]}|�d�r6|}nq+|j||dd	�dS)
N�catrQF)�restore_signalssSigIgnz&SigIgn not found in /proc/self/status.Tz=restore_signals=True should've unblocked SIGPIPE and friends.r�)rrc�
splitlinesr3rz�assertNotEqual)rZdefault_proc_statusr�Zdefault_sig_ign_maskZrestored_proc_statusZrestored_sig_ign_maskrrr�test_restore_signalsGs,�
�
�
�
�z)POSIXProcessTestCase.test_restore_signalsc
Csrz
tjtjddgdd�}Wnty'}z|jtjkr�WYd}~dSd}~wwt�d�}t	|�}|�
||�dS)Nrzimport os; print(os.getsid(0))T)�start_new_sessionr)rrcrJrKrxry�EPERMr�getsid�intrU)rrfr�Z
parent_sidZ	child_sidrrr�test_start_new_session_s

����
z+POSIXProcessTestCase.test_start_new_session�setreuidzno setreuid on platformc

Cst��}|dkr
dnd|g}tjdkrdnd}tdur3zt�|�|�|�Wnty2d}Ynw|D]i}dD]d}|j||d��Szt	j
tjdd	g||d�}Wn$tyZYn4t
yu}z|jtjtjfvrk�WYd}~nd}~wwt|t�r�t�|�j}n|}t|�}	|�|	|�Wd�n1s�wYq9q5|�t��t	jtd
d�Wd�n1s�wY|�t��t	jttjtjdd
�Wd�n1s�wYtdu�r|du�r|�t��t	jt|d�Wd�dS1s�wYdSdSdS)N���darwinr*�unknownrO)�user�	close_fdsrzimport os; print(os.getuid())r*�ra�)r�r�ra)r�geteuidrJr��pwd�getpwnamrtr,�subTestrrcrK�PermissionErrorrxryZEACCESrX�
isinstancer��pw_uidrZrLrTr�rXr�
OverflowErrorr�r�)
r�uidZ
test_usersZname_uidrarbrfr�Zuser_uidZ
child_userrrr�	test_userosf
��
����
�����
��"��zPOSIXProcessTestCase.test_userz setreuid() available on platformcC�>|�t��tjtdd�Wd�dS1swYdS)N�rc�rTr�rrXrrrrr�test_user_error��"�z$POSIXProcessTestCase.test_user_error�setregidzno setregid() on platformc	Cs�t��}|dkr
dndg}t�}tdur|�|�||gD]N}dD]I}|j||d��8ztjtj	ddg||d�}Wn	t
yCYnwt|t�rPt�
|�j}n|}t|�}|�||�Wd�n1sfwYq"q|�t��tjtdd�Wd�n1s�wY|�t��tjttjtjd	d
�Wd�n1s�wYtdur�|�t��tjt|d�Wd�dS1s�wYdSdS)Nr]r^rO)�grouprbrzimport os; print(os.getgid())r*�rurd)r�r�ru)r�getegidr.r)rtrhrrcrJrKrirjr�r+�gr_gidrZrLrTr�rXrrlr�r�)	r�gid�
group_listr-rurbrfZ	group_gidZchild_grouprrr�
test_group�sR
�
��
�����
��"��zPOSIXProcessTestCase.test_groupz setregid() available on platformcCro)Nrprvrqrrrr�test_group_error�rsz%POSIXProcessTestCase.test_group_error�	setgroupszno setgroups() on platformc

Cs�t��}|dkr
dndg}t�}d}tdur|�|�z
tjtjddg|d�}Wnt	yC}z|j
t
jkr7�d}WYd}~n3d}~wwt��}t
�|�}tdurYdd	�|D�}	n|}	|rh|�t|�t|��n
|�t|	�t|��|�t��tjtd
gd�Wd�n1s�wY|�t��tjttjtjdgd�Wd�n1s�wYtdur�|�t��tjt|gd�Wd�dS1s�wYdSdS)
Nr]r^Frz;import os, sys, json; json.dump(os.getgroups(), sys.stdout)��extra_groupsTcSs&g|]}t|t�rt�|�jn|�qSr)rjr�r)r+rx)r6�grrrr9�s�z:POSIXProcessTestCase.test_extra_groups.<locals>.<listcomp>r*rd)r�r�r)rrwr.r)rtrrcrJrKrxryrX�	getgroups�json�loadsrL�setrTr�rXrr�r�)
rryrzr-Z
perm_errorrf�exZ
parent_groupsZchild_groupsZdesired_gidsrrr�test_extra_groups�sZ
�
���
�����"��z&POSIXProcessTestCase.test_extra_groupsz!setgroups() available on platformcCs>|�t��tjtgd�Wd�dS1swYdS)Nr~rqrrrr�test_extra_groups_errorrsz,POSIXProcessTestCase.test_extra_groups_error�umaskzPOSIX umask() is not available.c	Cs�d}zCt��}tj�|d�}tjtjdd|�d�gdd�t�	|�j
d@}d}|j||t|��d	t|���d
�W|durDt
�|�dSdS|durPt
�|�ww)NZbeansrzopen(z, 'w').close()�+)r�i�i�z != r�)rrrrrr�r�rrXrJrK�stat�st_moderL�octrr|)rr~�namer�Z
expected_moderrr�
test_umask
s$����zPOSIXProcessTestCase.test_umaskcCsXt���t�tjddg�}|��Wd�n1swY|�|jt	j
�dS)Nrzimport os; os.abort())rr�rr9rJrKr#rLrr��SIGABRTr?rrr�test_run_abort#s

�
�z#POSIXProcessTestCase.test_run_abortcCsNt�ttj�d�}t|�}|�d|���|�d|�|�ttj�|�dS)N�fake cmdr�ZSIG)rr[rZr�r�r�rd�lower�rr�Zerror_stringrrr�"test_CalledProcessError_str_signal+s
z7POSIXProcessTestCase.test_CalledProcessError_str_signalcC�$t�dd�}t|�}|�d|�dS)Ni�Ki�r�zunknown signal 9876543.�rr[r�rdr�rrr�*test_CalledProcessError_str_unknown_signal6�z?POSIXProcessTestCase.test_CalledProcessError_str_unknown_signalcCr�)Nrr�znon-zero exit status 2.r�r�rrr�$test_CalledProcessError_str_non_zero;r�z9POSIXProcessTestCase.test_CalledProcessError_str_non_zerocCsZtjtjddgtjdd�d�}|�|�|j��d�Wd�dS1s&wYdS)Nrr-cSst�dd�S)Nr��apple)r�putenvrrrr�<lambda>G�z3POSIXProcessTestCase.test_preexec.<locals>.<lambda>)r7�
preexec_fn�appler�r?rrr�test_preexec@s
��"�z!POSIXProcessTestCase.test_preexecc
Cs�dd�}z
tjtjddg|d�}Wn6tjy,}z|�tjd�WYd}~dSd}~wtyG}z|�d|j	d�WYd}~dSd}~ww|�
d	�dS)
NcSr.)Nz'What if two swallows carried a coconut?�r�rrrr�raise_itLr3z=POSIXProcessTestCase.test_preexec_exception.<locals>.raise_itrr
�r�z)Expected a ValueError from the preexec_fnZcoconutrzEException raised by preexec_fn did not make it to the parent process.)rr9rJrKrOrP�_posixsubprocessr�rdr0r�)rr�r@r�rrr�test_preexec_exceptionKs 
��� ��z+POSIXProcessTestCase.test_preexec_exceptionc@seZdZ	dd�Zdd�ZdS)z+POSIXProcessTestCase._TestExecuteChildPopencOs$||_tjj|g|�Ri|��dSr)�	_testcaserr9r�)rZtestcaser0r1rrrr�]sz4POSIXProcessTestCase._TestExecuteChildPopen.__init__cOszJtjj|g|�Ri|��Wdd�td�D�}z'|D]}|jj||j��|j��|j	��fdd�qW|D]}t
�|�q6dS|D]}t
�|�qBwdd�td�D�}z&|D]}|jj||j��|j��|j	��fdd�qWW|D]}t
�|�qrw|D]}t
�|�q}w)NcSsg|]	}t�dtj��qS)�	/dev/zero)rru�O_RDONLY�r6rMrrrr9hs�zNPOSIXProcessTestCase._TestExecuteChildPopen._execute_child.<locals>.<listcomp>�z!At least one fd was closed early.r�)rr9r2rsr�r~r6r�r7r8rr>)rr0r1Zdevzero_fdsr�rrrr2asH�����������
��z:POSIXProcessTestCase._TestExecuteChildPopen._execute_childN)r)r*r+r�r2rrrr�_TestExecuteChildPopen[sr�r�z/dev/zero required.c	CsX	dd�}|�tj��|j|ttjtjtj|d�Wd�dS1s%wYdS)NcSs
t�d��)Nz-force the _execute_child() errpipe_data path.)rrOrrrrr�xs�zWPOSIXProcessTestCase.test_preexec_errpipe_does_not_double_close_pipes.<locals>.raise_it)r6r7r8r�)rTrrOr�rr:)rr�rrr�0test_preexec_errpipe_does_not_double_close_pipests�"�zEPOSIXProcessTestCase.test_preexec_errpipe_does_not_double_close_pipescCs�t��}zKt��|�t���tjtjddgdd�d�|�t��d�t��|�	t���tjtjddgdd�d�|�	t��d�W|sNt��dSdS|sWt��ww)	Nrr
cSr�rrrrrrr���zEPOSIXProcessTestCase.test_preexec_gc_module_failure.<locals>.<lambda>r�z#Popen enabled gc when it shouldn't.cSr�rrrrrrr��r�zPopen left gc disabled.)
�gc�	isenabled�disabler%rrIrJrK�enablerP)rZenabledrrr�test_preexec_gc_module_failure�s*�
���
�z3POSIXProcessTestCase.test_preexec_gc_module_failurer_z!setrlimit() seems to fail on OS Xc
Cs�zddlm}m}m}Wnty#}z|�|�WYd}~nd}~ww||�}|\}}||d|f�|�|||�ztjt	j
ddgdd�d�Wn
tySYdSw|�d�dS)	Nr)�	getrlimit�	setrlimit�RLIMIT_NPROCrr
cSr�rrrrrrr��r�z@POSIXProcessTestCase.test_preexec_fork_failure.<locals>.<lambda>r�z.RLIMIT_NPROC had no effect; probably superuser)Zresourcer�r�r�r�rzrtrrIrJrK�BlockingIOError)rr�r�r�r�ZlimitsrMZhardrrr�test_preexec_fork_failure�s$��
��z.POSIXProcessTestCase.test_preexec_fork_failurecCs�t��\}}t|ddd��}|�dtj�|�dtj�Wd�n1s(wYt�	|d�t
�|�}|��t�
|�|�|jd�dS�N�wrk)r�z#!%s
z(exec '%s' -c 'import sys; sys.exit(47)'
i�rG)rrr�rurur�
unix_shellrJrKr�chmodrr9r#r�rLr)rr��fname�fobjr@rrr�test_args_string�s��

z%POSIXProcessTestCase.test_args_stringcCs<|jttjtjddgdd�|jttjtjddgdd�dS)NrrFrG��startupinfo��
creationflags�rTr�rrIrJrKrrrrr��s
��
�
�z&POSIXProcessTestCase.test_invalid_argscCsjtj��}d|d<tjdgdtj|d�}|�|�|j���	d�d�Wd�dS1s.wYdS�Nr�r�zecho $FRUITr��r�r7r�s 	
r��
rr�r�rr9r:rLr7r�r;�rr�r@rrr�test_shell_sequence�s

�"�z(POSIXProcessTestCase.test_shell_sequencecCshtj��}d|d<tjddtj|d�}|�|�|j���	d�d�Wd�dS1s-wYdSr�r�r�rrr�test_shell_string�s
�"�z&POSIXProcessTestCase.test_shell_stringcCs�t��\}}t|ddd��}|�dtj�|�dtj�Wd�n1s(wYt�	|d�t
�|�}t�|�|�
|d�dSr�)rrr�rururr�rJrKrr�rrIr�rL)rr�r�r�rNrrr�test_call_string�s��

z%POSIXProcessTestCase.test_call_stringc	Cs�g}dD]}dD]}tj�||�}tj�|�r|�|�qq|s%|�d�d}tj�|�r8tj�|�s8|�|�|D],}tjd|dtj	d�}|�|�
|j���
�t|d��Wd�n1sawYq:dS)	N)z/binz	/usr/bin/z/usr/local/bin)ZbashZkshz"bash or ksh required for this testz/bin/shzecho $0T)rKr�r7r�)rr�r��isfilertrz�islinkrr9r:rLr7r�r;r�)rZshellsr�r��shr@rrr�test_specific_shell�s,
��


����z(POSIXProcessTestCase.test_specific_shellc
Gstt�tjtj�}ztjtjddgdtjtjtjd�}Wt�tj|�nt�tj|�w|j�	d�t
||�|�|S)Nrz�if 1:
                                 import sys, time
                                 sys.stdout.write('x\n')
                                 sys.stdout.flush()
                                 time.sleep(30)
                                 T�rbr6r7r8r�)r��SIGINT�default_int_handlerrr9rJrKr:r7r��getattr)r�methodr0r�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�)rr9rJrKr:r7r�r�r�r�r�)rr�r0r@rrr�_kill_dead_process	s�
z'POSIXProcessTestCase._kill_dead_processcCs:|�dtj�}|��\}}|�d|�|�|��d�dS)N�send_signalsKeyboardInterruptr)r�r�r�r�rdrUr#�rr@rMr8rrr�test_send_signal+	sz%POSIXProcessTestCase.test_send_signalcC�:|�d�}|��\}}|�|d�|�|��tj�dS)Nr'r)r�r�rLr#r��SIGKILLr�rrr�	test_kill1	�
zPOSIXProcessTestCase.test_killcCr�)N�	terminater)r�r�rLr#r��SIGTERMr�rrr�test_terminate7	r�z#POSIXProcessTestCase.test_terminatecC�|�dtj�dS�Nr�)r�r�r�rrrr�test_send_signal_dead=	sz*POSIXProcessTestCase.test_send_signal_deadcC�|�d�dS�Nr'�r�rrrr�test_kill_deadA	�z#POSIXProcessTestCase.test_kill_deadcCr��Nr�r�rrrr�test_terminate_deadE	r�z(POSIXProcessTestCase.test_terminate_deadcCs6g}|D]}t�|�}t�|�}|�|||f�q|Sr)r�get_inheritable�duprt)rZsave_fdsr�r��inheritable�savedrrr�	_save_fdsI	s

zPOSIXProcessTestCase._save_fdscCs.|D]\}}}tj|||d�t�|�qdS)N)r�)r�dup2r>)rr�r�r�r�rrr�_restore_fdsQ	s�z!POSIXProcessTestCase._restore_fdsc		Cs�d}|�|�}|D]
\}}}|dkr|}nq	z1|D]}t�|�qtjtjddg|tjtjd���\}}|�	|d�|�	|d�W|�
|�dS|�
|�w)Nrrrr5r�r�)r�rr>rr9rJrKr:r�rLr�)	rr�r6�	saved_fdsr�r�r�r�r�rrr�check_close_std_fdsV	s,
�
���z(POSIXProcessTestCase.check_close_std_fdscC�|�dg�dSrW�r�rrrr�test_close_fd_0o	�z$POSIXProcessTestCase.test_close_fd_0cCr��Nr�r�rrrr�test_close_fd_1r	r�z$POSIXProcessTestCase.test_close_fd_1cCr�)Nrr�rrrr�test_close_fd_2u	r�z$POSIXProcessTestCase.test_close_fd_2cC�|�ddg�dS)Nrr�r�rrrr�test_close_fds_0_1x	�z'POSIXProcessTestCase.test_close_fds_0_1cCr�)Nrrr�rrrr�test_close_fds_0_2{	r�z'POSIXProcessTestCase.test_close_fds_0_2cCr�)Nr�rr�rrrr�test_close_fds_1_2~	r�z'POSIXProcessTestCase.test_close_fds_1_2cCs|�gd��dS)N)rr�rr�rrrr�test_close_fds_0_1_2�	sz)POSIXProcessTestCase.test_close_fds_0_1_2c
Cs�	t�d�}t�d�}z.t�d�t�d�t�tjddg���Wt�|d�t�|d�t�|�t�|�dSt�|d�t�|d�t�|�t�|�w)Nrr�rz*print('AssertionError:0:CLOEXEC failure.'))	rr�r>rr9rJrKr#r�)rZ	new_stdinZ
new_stdoutrrr�test_small_errpipe_write_fd�	s&



�
�
z0POSIXProcessTestCase.test_small_errpipe_write_fdc

CsVdd�td�D�}z�dd�|D�}|D]	\}}t�|�qt�|dd�t�|ddd�|�td��}z-t|�D]
\}}t�||�q:tj	t
jdd	g|d|d
|dd�}|��W|�
|�n|�
|�w|D]	}t�|dd�qkt�|d
d�}t�|dd���}	|�|d
�|�|	d�W|D]}t�|�q�dS|D]}t�|�q�w)NcS�g|]}t���qSr�rrr��r6rrrrr9�	�z?POSIXProcessTestCase.test_remapping_std_fds.<locals>.<listcomp>r�cS�g|]\}}|�qSrr�r6r�r�rrrr9�	rr��STDINrr�Zimport sys; got = sys.stdin.read();sys.stdout.write("got %s"%got); sys.stderr.write("err")rr5r�	got STDIN�err)rsr�unlinkrur�r��	enumerater�rr9rJrKr#r�r�r;rLr>)
r�temps�temp_fdsr�r�r��temp_fdr@r�r�rrr�test_remapping_std_fds�	s>
��
��z+POSIXProcessTestCase.test_remapping_std_fdsc

Cs:dd�td�D�}dd�|D�}z�|D]	\}}t�|�q|�td��}zNt|�D]
\}}	t�|	|�q)t�|d�t�|dd�tj	t
jddg|||d	�}
|
��|D]	}t�|dd�qTt�
|d
�}t�
|d
���}W|�|�n|�|�w|�|d�|�|d�W|D]}t�|�q�dS|D]}t�|�q�w)
NcSrrrrrrrr9�	rz7POSIXProcessTestCase.check_swap_fds.<locals>.<listcomp>r�cSrrrrrrrr9�	rrrrrr5rr	r
)rsrrr�rr�rur�rr9rJrKr#r�r;r�rLr>)
rZstdin_noZ	stdout_noZ	stderr_nor
rr�r�r�rr@r�r�rrr�check_swap_fds�	s>
����z#POSIXProcessTestCase.check_swap_fdscCsX|�ddd�|�ddd�|�ddd�|�ddd�|�ddd�|�ddd�dS)Nrr�r)rrrrr�
test_swap_fds�	sz"POSIXProcessTestCase.test_swap_fdscCs�|�td��}z�|D]}t���}t�|��|�Wd�n1s#wYq
ttd��t|���}t�	|�gd�}i}t
||�D]
\}}	||||	<qEt�d�}
ttd��t|���}t
jtjd|
t|�gfi|��}|�|d�t
||�D]9\}}	t�|dtj�t�|d�}
ttt|
�d���}t�d|�d	|�d
|�d|�d|	�d
��}|�|	g||�q|W|�|�dS|�|�w)Nr�r5z�
                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'))
            rrrr�z"
                    When testing z to z4 redirection,
                    parent descriptor z5 got redirected
                    to descriptor(s) z instead of descriptor z.
                )r�rsrrrsrr�r�r�r{r>�ziprcrdrrIrJrKr�rLr��SEEK_SETr�r<�maprZr�r�)r�from_fds�to_fdsr�Zfrom_fd�fZfd_to_closeZ	arg_namesr1Zto_fdr�Z
skipped_fdrN�
read_bytesZread_fdsr�rrr�#_check_swap_std_fds_with_one_closed
sJ
��

�����
��z8POSIXProcessTestCase._check_swap_std_fds_with_one_closedcCs<t�td�d�D]}t�td�d�D]}|�||�qqdS)Nr�r)�	itertools�combinationsrs�permutationsr)rrrrrr�!test_swap_std_fds_with_one_closed,
s
��z6POSIXProcessTestCase.test_swap_std_fds_with_one_closedc
Cs�dd�}z	tjt|d�WnBty.}z|�tj�|�t|�d�WYd}~dSd}~wtjyO}z|�	tj�|�t|�d�WYd}~dSd}~ww|�
d�dS)NcSr.)N�
surrogate:�r�rrrr�prepare2
r3zCPOSIXProcessTestCase.test_surrogates_error_message.<locals>.preparer�rz!Exception occurred in preexec_fn.z1Expected ValueError or subprocess.SubprocessError)rrIrr�r�r�rLr�rO�assertIsNotNoner�)rr r�rrr�test_surrogates_error_message1
s 
����z2POSIXProcessTestCase.test_surrogates_error_messagecCs�dD]j\}}|�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�rkz&import os; print(ascii(os.getenv(%s)))�C�LC_ALLrr�s

z'import os; print(ascii(os.getenvb(%s))))
rJr�rr�r�rrcrJrKr�rLr�r�)r�key�valueZ
encoded_value�scriptr�Z
decoded_valuer7rrr�test_undecodable_envD
s0

�


�
�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� r�Tr��PATHr�sPATH)rr�rr<r�r�rrIrLr�rJr�r��environb)	rZabs_programr0r�Zprogram�exitcoder?r�Zenvbrrr�test_bytes_programa
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)r6r7r8rbr�r7rb�,z5Expected no fds from %r to be open in child, found %r)r�findfilerr9rJrKr:rtr�r�rrZr�r6r�r7r8r%)	rZsleeper�	fd_status�p1�p2rf�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/r0zqgrep.pyszxcvbn��
F)r6r7rbc�$z���WdStyYdSwr�r��ProcessLookupErrorr)r6rr�kill_p1�
�
�zBPOSIXProcessTestCase.test_pipe_cloexec_real_tools.<locals>.kill_p1cr<rr=r)r7rr�kill_p2�
r@zBPOSIXProcessTestCase.test_pipe_cloexec_real_tools.<locals>.kill_p2rpzThe child hung)rr4rr9rJrKr:r7rtr#r6rur>�selectrPrLr�)
rZqcatZqgrepZsubdatar�r?rAZ	readfilesZignored1Zignored2r)r6r7r�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|���|�q%�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)Nr1r/r0rr��	TFr2r3zSome fds were closedzSome fds were left openzSubprocess failedc3s�|]}���VqdSr)r{r��r�rr�	<genexpr>�
s�z6POSIXProcessTestCase.test_close_fds.<locals>.<genexpr>r��r7rb�pass_fdsz'Some fds not in pass_fds were left open)rr4rr#rtr>r�rsru�devnullr��add�set_inheritablerr9rJrKr:r�rrZr�rLr%rd)
rr5r�rMr�r@rf�ignored�
remaining_fds�fds_to_keeprrEr�test_close_fds�
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)Nr1r/r0raK
        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�rz(expected exactly two lines of output:
%rr�rr3r�zSome fds were left open.)rr4rr9rJrKrcrdr:r�rTrLrLr�rrZr;r�r%)rr5r@rfZ
unused_stderrZoutput_linesZ
opened_fdsrMrrr�%test_close_fds_when_max_fd_is_lowered�
s&
/��0�2�

�z:POSIXProcessTestCase.test_close_fds_when_max_fd_is_loweredrprhc	Cs>tjddd�}t�}td�D]-}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�n1s�wY|�dt|j��q>dS)Nr1r/r0rhrr�TrGr3zfd to be passed not passedzfd to be closed passedF)rbrHzoverriding close_fds)rr4r�rsrr#rtr>rK�updaterr9rJrKr:r�rrZr�rdr%ZassertWarnsr�rIrr��warning)rr5r�rWr�r�r@rfrLrMZto_be_closed�contextrrr�
test_pass_fds?s:�

�
���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)	Nr1r/r0TFrGr3z	output=%a)rr4rr#rtr>rKrJrKr<rr�rr9r:r�r�rZr�rLr�)
rr(r�Znon_inheritablerHr0r@rfrLr�rrr�test_pass_fds_inheritableas$
�z.POSIXProcessTestCase.test_pass_fds_inheritablec	Cs
	tjddd�}g}td�D]}t�tjtj�}|�tj|�|�	|�qt�
�\}}|�tj|�|�tj|�|�d|�tj
tj|g|d|d|dd|d��t�|d	�}Wd�n1sdwYd
d�|�d�D�}|�|hd
�t|�Bd|���dS)Nr1r/r0rr�rT)r6r7r8rbrHrcSsh|]}t|��qSr)rZ)r6�numrrr�	<setcomp>�rz@POSIXProcessTestCase.test_pass_fds_redirected.<locals>.<setcomp>r3>rr�rzoutput=)rr4rsrrurI�O_RDWRrtr>rtr#�insertrr9rJrKr�r�rL�	frozenset)	rr5rHrMr�Zstdout_rZstdout_wrfr�rrr�test_pass_fds_redirecteds,��$z-POSIXProcessTestCase.test_pass_fds_redirectedcC�Lt�tjd��}tjt||d�}|��Wd�dS1swYdS)N�r+)r7r6�r<rurrIrr9rr#�rZinoutr@rrr�%test_stdout_stdin_are_single_inout_fd���
"�z:POSIXProcessTestCase.test_stdout_stdin_are_single_inout_fdcCr\)Nr]r�r^r_rrr�&test_stdout_stderr_are_single_inout_fd�raz;POSIXProcessTestCase.test_stdout_stderr_are_single_inout_fdcCr\)Nr])r8r6r^r_rrr�%test_stderr_stdin_are_single_inout_fd�raz:POSIXProcessTestCase.test_stderr_stdin_are_single_inout_fdcCsRtjddd�}tjtj|gtjtjd�}|��\}}|�d|j	d|�
d��dS)Nzsigchild_ignore.pyr/r0r�rz6sigchild_ignore.py exited non-zero with this error:
%sr�)rr4rr9rJrKr:r�rLrr�)rZsigchild_ignorer@r7r8rrr�test_wait_when_sigchild_ignored�s��
�z4POSIXProcessTestCase.test_wait_when_sigchild_ignoredc	Cs�t�d�}tjtjddgtjdd�}|j}|�|j	�z|�
|�d�d�|�||�
|gggd�d�W|��dS|��w)	NrBrz$import sys;sys.stdout.write("apple")r)r7rBr:sapplg)r�
import_modulerr9rJrKr:r7rtr>rLr�rdrBr#)rrBr@rrrr�test_select_unbuffered�s

�� z+POSIXProcessTestCase.test_select_unbufferedcCs�tjtjddgtjtjd�}|�|jj�|�|jj�t	|�}|j
}t�dt
f��
d}Wd�n1s7wYtrF|�tj�dS|�|dd�tjD��dS)Nrz import sys, time;time.sleep(0.2)r�r
cS�g|]}t|��qSr��id�r6�orrrr9�rzEPOSIXProcessTestCase.test_zombie_fast_process_del.<locals>.<listcomp>)rr9rJrKr:rtr7r>r8ri�pidr�check_warnings�ResourceWarningr!r�r"rd)rr@�identrlrrr�test_zombie_fast_process_del�s 
���z1POSIXProcessTestCase.test_zombie_fast_process_delc	Csbtjtjddgtjtjd�}|�|jj�|�|jj�t	|�}|j
}t�dt
f��d}t��Wd�n1s;wYt�|tj�trP|�tj�n|�|dd�tjD��t�d�|�t��"tjttjtjd��}Wd�n1s|wYWd�n1s�wY|�ttj|d�tr�|�tj�dS|�|d	d�tjD��dS)
Nrzimport time;time.sleep(3)r�r
cSrgrrhrjrrrr9�rzJPOSIXProcessTestCase.test_leak_fast_process_del_killed.<locals>.<listcomp>r�rcSrgrrhrjrrrr9r)rr9rJrKr:rtr7r>r8rirlrrmrnr�
gc_collectrr'r�r�r!r�r"rdr�r�rTrxr��waitpidr~)rr@rorlr�rrr�!test_leak_fast_process_del_killed�sB
��
�
����z6POSIXProcessTestCase.test_leak_fast_process_del_killedcsxtjddd�}t�d��|�tj��tjtj	|gtj
d�fdd�d�}|��\}}tt
t|�d	���}|��|�dS)
Nr1r/r0r�Tcst�d��Sr�)rr�r�r�rrr�r�zCPOSIXProcessTestCase.test_close_fds_after_preexec.<locals>.<lambda>)r7rbr�r3)rr4rr�rtr>rr9rJrKr:r�r�rrZr�r~)rr5r@rfrLrMrrtr�test_close_fds_after_preexec
s

�z1POSIXProcessTestCase.test_close_fds_after_preexecc	Csddl}t��}zpdd�}t��ddgddgfdgdddgfdgdgddgfdgdgddffD]=\}}}}|�t��!}|�||dd	||d
d
d
d
ddd
ddddgdd
|�Wd�n1s_wY|�dt|j	��q0W|swt�
�dSdS|s�t�
�ww)NrcSr�rrrrrrr�%r�z5POSIXProcessTestCase.test_fork_exec.<locals>.<lambda>r�sexe�envsargTrr*r�rr�r:Fz
takes exactly)r�r�r�r�rTr�rKr~r�r\r�)	rr��
gc_enabled�funcr0Zexe_listr��env_listr�rrr�test_fork_execs8�����
�z#POSIXProcessTestCase.test_fork_execcCs�ddl}Gdd�d�}t��}z_t��ddddd|d	�|d
�ffD]A}|jtd�|�d��$}|�d
gd
gd|ddgddddd	d
ddddddddd�Wd�n1sUwY|�dt	|j
��q"W|smt��dSdS|svt��ww)Nrc@s eZdZdZdd�Zdd�ZdS)zJPOSIXProcessTestCase.test_fork_exec_sorted_fd_sanity_check.<locals>.BadIntTcSs
||_dSr)r')rr'rrrr�Is
zSPOSIXProcessTestCase.test_fork_exec_sorted_fd_sanity_check.<locals>.BadInt.__init__cSs|jr	d|_|jSt�)NF)�firstr'r�rrrr�__int__KszRPOSIXProcessTestCase.test_fork_exec_sorted_fd_sanity_check.<locals>.BadInt.__int__N)r)r*r+r{r�r|rrrr�BadIntGsr})r*rr�r:rh)r�r:)���*l)rhr:)r\r�r�r�r�rzfds_to_keep={}r�sfalseTrvr*r�r:rN)r�r�r�r�rTr�rmrKrdr�r\r�)rr�r}rwrNr^rrr�%test_fork_exec_sorted_fd_sanity_checkCsD
��
����
�z:POSIXProcessTestCase.test_fork_exec_sorted_fd_sanity_checkc	Cs�t�t�}|�5tj�|d��}t|j_|�	�|j�
�Wd�n1s'wYWd�dSWd�dS1s?wYdS)Nr6)rr9rrr�r��BrokenPipeErrorr>rGr�Zassert_called_with�rr��mock_proc_stdinrrr�,test_communicate_BrokenPipeError_stdin_closems
P�zAPOSIXProcessTestCase.test_communicate_BrokenPipeError_stdin_closec	Cs�t�t�}|�<tj�|d��}t|j_|�	d�|j�
d�|j�
�Wd�n1s.wYWd�dSWd�dS1sFwYdS)Nr6�stuff)rr9rrr�r�r�rurGr��assert_called_once_withr>r�rrr�,test_communicate_BrokenPipeError_stdin_writevs

P�zAPOSIXProcessTestCase.test_communicate_BrokenPipeError_stdin_writec
Cs�tjtjdgtjtjd�}|�Rtj�|d��2}tt	j
d��}t|j_
|��|j_|�d�|j��Wd�n1s=wYWd�n1sLwYWd�dSWd�dS1sdwYdS)N�-hr�r6�wbr�)rr9rJrKr:rr�r�rurrIr�r^rGr��return_valuer�r�)rr�r�Zdev_nullrrr�,test_communicate_BrokenPipeError_stdin_flush�s�
�
��PzAPOSIXProcessTestCase.test_communicate_BrokenPipeError_stdin_flushc	Cs�tjtjdgtjtjd�}|�7tj�|d��}t|j	_
|jdd�|j	��Wd�n1s1wYWd�dSWd�dS1sIwYdS)Nr�r�r6i�rR)
rr9rJrKr:rr�r�r�r>rGr�r�r�rrr�9test_communicate_BrokenPipeError_stdin_close_with_timeout�s�P�zNPOSIXProcessTestCase.test_communicate_BrokenPipeError_stdin_close_with_timeoutN�
W_STOPCODEzneed _testcapi.W_STOPCODEcCsv	t}t�|�}tj|jdd�t�d�}tj	d|j|fd��|�
�}Wd�n1s.wY|�|d�dS)Nr�r-r�rE)r����)rrr9r�wait_processrl�	_testcapir�rr�r#rL)rr0r��statusrrrr�test_stopped�s


�z!POSIXProcessTestCase.test_stoppedcCstt�t�}tj|jdd�|�|j�t�	d��}|�
tj�Wd�n1s)wY|�
�|�|j�dS)Nrr�zos.kill)rr9rrr�rlr�rrr�r�r�r�Zassert_not_calledr!)rr�Z	mock_killrrr�test_send_signal_race�s
�z*POSIXProcessTestCase.test_send_signal_racecCstt�tjddg�}|js|��|jrtjj|ddd�d��d|_|�	t
j�Wd�dS1s3wYdS)Nrzexit(1)r�cSr�rrrrrrr��r�z=POSIXProcessTestCase.test_send_signal_race2.<locals>.<lambda>)�new)rr9rJrKrr�rr�r�r�r�r�r?rrr�test_send_signal_race2�s�"�z+POSIXProcessTestCase.test_send_signal_race2cCsDtjtjddgtjd�}	z	|jdd�WdStjy Ynwq
)Nrz+import os, time; os.close(1), time.sleep(2)r�TrQrR)rr9rJrKr:r�rU�rr�rrr�1test_communicate_repeated_call_after_stdout_close�s
����zFPOSIXProcessTestCase.test_communicate_repeated_call_after_stdout_close)nr)r*r+r r5r;r=r>rr9r@rr�rLrPr%r�rr�r�rVr[r�r�rnrrr{r|r�r�r!r�r�r�r�r�r�r�r�r�r�rJr�r�r�r�r�r�r�r�r�r3r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrrrr"r)r.r9rCrOr��st_devrPrZrequires_mac_verrTrUr[r`rbrcrdrfrprsru�cpython_onlyrzr�r�r�r�r�r�r�r�r�r��
__classcell__rrr2rr/�s�


�

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�wShowWindowrIr)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�rIr)rZSTARTF_USERSHOWWINDOWr�r�rrr�test_startupinfo_keywords�s�
�z.Win32ProcessTestCase.test_startupinfo_keywordsc
Cs�t��}tj|_tj|_td�D]l}t}tt	j
d��/}tj||tj|d�}|�|�
�Wd�n1s6wY|�|jd�Wd�n1sLwY|�|jtj�|�|j�|�|j�|�|j�|�|jtj�|�|jdgi�qdS)Nrr�)r7r8r�r�handle_list)rr�r�r�ZSW_HIDEr�rsrrurrIr9rlr�rLrr�Z	hStdInputZ
hStdOutputZ	hStdError�lpAttributeList)rr�rMr?Znullr�rrr�test_startupinfo_copy�s2�
��	��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�)rJr8rurrIrK)rZCREATE_NEW_CONSOLErrr�test_creationflags
s�
�z'Win32ProcessTestCase.test_creationflagscCs$|jttjtjddgdd�d�dS)NrrFcSsdSr�rrrrrr�%
r�z8Win32ProcessTestCase.test_invalid_args.<locals>.<lambda>r�r�rrrrr� 
s
�
�z&Win32ProcessTestCase.test_invalid_argscCsPGdd�dt�}|�t��tjt|�d�Wd�dS1s!wYdS)Nc@seZdZdZdS)z4Win32ProcessTestCase.test_issue31471.<locals>.BadEnvN)r)r*r+�keysrrrr�BadEnv+
sr�r�)�dictrTr�rr9r)rr�rrr�test_issue31471'
s"�z$Win32ProcessTestCase.test_issue31471cCs&tjtjddgdd�}|�|d�dS)NrrFT)rbrGrHrMrrrrO0
s
��z#Win32ProcessTestCase.test_close_fdsc
Cs�ddl}t��}|�tj|d�|�tj|d�g}|D]}t�|d�|�|�|��qtj	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��8t��}	d
|dd�i|	_tj	t
jdd�|d�gtj
tj
|	dd�}|��\}}|�|jd�Wd�dS1s�wYdS)
Nrr�Trz2import msvcrt; print(msvcrt.open_osfhandle({}, 0))Fr2)r7r8rbsOSErrorr�)r7r8r�rbz.*overriding close_fds)r�rr#rtr>rKrtZ
get_osfhandlerr9rJrKrmr:r�rLrrZr;rdr�r�rrmr�)
rr�r�r}r�r@r7r8r�r�rrr�test_close_fds_with_stdio7
sd
��
�
�

���
��"�z.Win32ProcessTestCase.test_close_fds_with_stdiocCs t��}i|_tjt|d�dS)Nr��rr�r�rIr�rr�rrr�test_empty_attribute_listi
s

�z.Win32ProcessTestCase.test_empty_attribute_listcCs$t��}dgi|_tjt|d�dS)Nr�r�r�r�rrr�test_empty_handle_listo
s


�z+Win32ProcessTestCase.test_empty_handle_listcCsdtj��}d|d<tjdgdtj|d�}|�|�d|j���Wd�dS1s+wYdS�N�physalisr�r�r�r�sphysalis�	rr�r�rr9r:rdr7r�r�rrrr�u
s

�"�z(Win32ProcessTestCase.test_shell_sequencecCsbtj��}d|d<tjddtj|d�}|�|�d|j���Wd�dS1s*wYdSr�r�r�rrrr�
s
�"�z&Win32ProcessTestCase.test_shell_stringc	CsndD]2}tj��}d|d<tjddtj||d�}|�|�d|j��|�Wd�n1s/wYqdS)N)�ansiZoemr�r�r�r�)r�r7r�r�r�)r�encr�r@rrr�test_shell_encodings�
s
����z)Win32ProcessTestCase.test_shell_encodingscCs t�tjd�}|�|d�dS)Nz -c "import sys; sys.exit(47)"rGrHrMrrrr��
s�z%Win32ProcessTestCase.test_call_stringcGs�tjtjddgtjtjtjd�}|�%|j�d�t||�|�|��\}}|�	|d�|�
�}Wd�n1s:wY|�|d�dS)Nrz�if 1:
                             import sys, time
                             sys.stdout.write('x\n')
                             sys.stdout.flush()
                             time.sleep(30)
                             r5r�rr)rr9rJrKr:r7r�r�r�rLr#rU)rr�r0r@rMr8rrrrr��
s�	
�z"Win32ProcessTestCase._kill_processcGs�tjtjddgtjtjtjd�}|�*|j�d�t�d�t	||�|�|�
�\}}|�|d�|��}Wd�n1s?wY|�|d�dS)Nrz�if 1:
                             import sys, time
                             sys.stdout.write('x\n')
                             sys.stdout.flush()
                             sys.exit(42)
                             r5r�rr�)
rr9rJrKr:r7r�r�r�r�r�rLr#)rr�r0r@rMr8rNrrrr��
s�	

�z'Win32ProcessTestCase._kill_dead_processcCr�r�)r�r�r�rrrrr��
r�z%Win32ProcessTestCase.test_send_signalcCr�r��r�rrrrr��
�zWin32ProcessTestCase.test_killcCr�r�r�rrrrr��
r�z#Win32ProcessTestCase.test_terminatecCr�r�)r�r�r�rrrrr��
r�z*Win32ProcessTestCase.test_send_signal_deadcCr�r�r�rrrrr��
r�z#Win32ProcessTestCase.test_kill_deadcCr�r�r�rrrrr��
r�z(Win32ProcessTestCase.test_terminate_deadN)r)r*r+r�r�r�r�r�rr�r�rOr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrr��s0
2

r�c@s^eZdZGdd�dej�Zej�ejd�dd��Z	dd�Z
dd	�Zd
d�Zdd
�Z
dd�ZdS)�	MiscTestscs"eZdZ	gZ�fdd�Z�ZS)zMiscTests.RecordingPopencs"t�j|i|��|j�|�dSr)r0r��instances_createdrtr/r2rrr��
sz!MiscTests.RecordingPopen.__init__)r)r*r+r�r�r�rrr2r�RecordingPopen�
sr��_communicatec
Ks�	t|_z�tj�tjd���}t|_tj�td|j��,|�t��|t	j
ddgfdtji|��Wd�n1s:wYWd�n1sIwY|jdd�D]}|�
|tjdd�d|j���qUg}|jD]}|tjd	d�kr{|�|�ql|j|jd
|jd�|jt|�d|jd�Wd�n!1s�wYW|jj��}|��|��|�g|jj�dSW|jj��}|��|��|�g|jj�dS|jj��}|��|��|�g|jj�w)N�_waitr9rzLimport time
time.sleep(9)
import sys
sys.stderr.write('\n!runaway child!\n')r7r�rRz.no open-ended wait() after the first allowed: g�?rr�)�KeyboardInterruptrGrr�r�rr9r�rTrJrKrZcall_args_listrUrIrtZassertLessEqualZ
call_countrLrLr�r{r'r#)rZpopenerZmock__communicater1Z
mock__waitrIZsigint_callsr�rrr�_test_keyboardinterrupt_no_kill�
sf
���������

�
�����z)MiscTests._test_keyboardinterrupt_no_killcC�|jtjdd�dS�NgT㥛� @rR)r�rrIrrrr�#test_call_keyboardinterrupt_no_kill�z-MiscTests.test_call_keyboardinterrupt_no_killcCr�r�)r�rrrrrr�"test_run_keyboardinterrupt_no_killr�z,MiscTests.test_run_keyboardinterrupt_no_killcCsdd�}|�|�dS)Nc_s*tj|i|���}t�1swYdSr)rr9r�)r0r1Zunused_processrrr�popen_via_context_managers�z[MiscTests.test_context_manager_keyboardinterrupt_no_kill.<locals>.popen_via_context_manager)r�)rr�rrr�.test_context_manager_keyboardinterrupt_no_killsz8MiscTests.test_context_manager_keyboardinterrupt_no_killc	Cs�|�t�d�d�|�t�d�d�d}z,t��}tj�|d�}t�t	r&dnd|�\}}|�
|d�W|dur?t�|�dSdS|durKt�|�ww)Nz
echo xyzzy�xyzzy)rr�r�ztype zcat r)rLr�	getoutput�getstatusoutputrrrrrr�r�r!rU�rmdir)r�dirr�r�rfrrr�test_getoutputs"����zMiscTests.test_getoutputcCsp	hd�}ttj�}t�}ddl}tj��D]\}}|�d�r qt||jf�r(q|�	|�q|�
|||�dS)N>�Handler�rfr)r$rrM)r�r�__all__r��__dict__�itemsr3rj�
ModuleTyperJrL)rZintentionally_excludedZexportedZpossible_exportsr�r�r'rrr�test__all__.s

zMiscTests.test__all__N)r)r*r+rr9r�rr�r�r�r�r�r�r�r�rrrrr��
s
+r��PollSelectorz!Test needs selectors.PollSelectorc@r)�ProcessTestCaseNoPollcCstj|_tjt_t�|�dSr)r�_PopenSelector�
orig_selector�	selectors�SelectSelectorr4r rrrrr @szProcessTestCaseNoPoll.setUpcCs|jt_t�|�dSr)r�rr�r4r'rrrrr'EszProcessTestCaseNoPoll.tearDownNr(rrrrr�=sr�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])))	r0r rrr�r�r�rrur>)rrr�r2rrr Ms


zCommandsWithSpaces.setUpcst�|j�t���dSr)rr�r�r0r'rr2rrr'VszCommandsWithSpaces.tearDowncOsbtj|d<tj|i|��}|�|�|j���d�d|j�Wd�dS1s*wYdS)Nr7�mbcsz2 [%r, 'ab cd'])rr:r9rLr7r�r�r�)rr0r1r@rrr�with_spacesZs
�"�zCommandsWithSpaces.with_spacescCs |jdtj|jdfdd�dS)N�"%s" "%s" "%s"�ab cdr�r��r�rJrKr�rrrr�test_shell_string_with_spacescs
�
�z0CommandsWithSpaces.test_shell_string_with_spacescCs|jtj|jdgdd�dS)Nr�r�r�r�rrrr�test_shell_sequence_with_spaceshsz2CommandsWithSpaces.test_shell_sequence_with_spacescCs|�dtj|jdf�dS)Nr�r�r�rrrr�test_noshell_string_with_spacesls�z2CommandsWithSpaces.test_noshell_string_with_spacescCs|�tj|jdg�dS)Nr�r�rrrr�!test_noshell_sequence_with_spacesqsz4CommandsWithSpaces.test_noshell_sequence_with_spaces)r)r*r+r r'r�r�r�r�r�r�rrr2rr�Js		r�c@s4eZdZdd�Zdd�Zdd�Zdd�Zd	d
�ZdS)�ContextManagerTestscCs�tjtjddgtjtjd��}|�|j��d�|�|j��d�Wd�n1s+wY|�	|jj
�|�	|jj
�dS)NrzAimport sys;sys.stdout.write('stdout');sys.stderr.write('stderr');r�sstdoutsstderr)rr9rJrKr:rLr7r�r8rPrQr�rrr�	test_pipexs
���	zContextManagerTests.test_pipecCsDt�tjddg��}Wd�n1swY|�|jd�dS)Nrzimport sys; sys.exit(100)�d)rr9rJrKrLrr�rrrr�s
��z#ContextManagerTests.test_returncodecCsVtjtjddgtjd��}|�d�|�|jd�Wd�dS1s$wYdS)Nrz2import sys;sys.exit(sys.stdin.read() == 'context')rpscontextr�rr�rrrrB�s
��
"�z*ContextManagerTests.test_communicate_stdinc	Cst|�t��+tjttjtjd��}Wd�n1swYWd�dSWd�dS1s3wYdS)Nr�)rTr�rr9r�r:r�rrrr��s���"�z%ContextManagerTests.test_invalid_argscCst	tjttjtjdd�}|��}|j�dtj�|�	|j
�|�t|j
ddd�|�|j
d�|�|jj�dS)Nr)r6rBr�r)rr9rr:rrV�	__enter__r6rur�rrTrx�__exit__rLrPrQr�rrr�test_broken_pipe_cleanup�s�z,ContextManagerTests.test_broken_pipe_cleanupN)r)r*r+r�rrBr�r�rrrrr�vs
r��__main__)Er%rr#rZtest.supportrrrrrrJr�r<rrryrrr�rr�r�r�rBrr�r�rcr�r�Ztest.support.os_helperrr�r�rfr)r$ZPGOr&r�r!r]r�r��NotADirectoryErrorrir�rKrrZTestCaser�	Exceptionr,r9r-r4r�r.r�r/r�r�r�r�r�r�r�r)�mainrrrr�<module>s����



F

~d�
+
6�