Server : LiteSpeed
System : Linux server51.dnsbootclub.com 4.18.0-553.62.1.lve.el8.x86_64 #1 SMP Mon Jul 21 17:50:35 UTC 2025 x86_64
User : nandedex ( 1060)
PHP Version : 8.1.33
Disable Function : NONE
Directory :  /opt/cppython/lib/python3.8/test/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]


Current File : //opt/cppython/lib/python3.8/test/__pycache__/test_subprocess.cpython-38.opt-2.pyc
U

>��g�:�@s&ddlZddlmZddlmZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlZddlZddlZddlZddlZddlmZzddlZWnek
r�dZYnXejr�e�d��ejdkZer�dZndZd	Zee e!fZ"ej#d
dfa$dd
�Z%Gdd�dej&�Z'Gdd�de(�Z)Gdd�dej*�Z+Gdd�de'�Z,Gdd�de'�Z-e�.ed�Gdd�de'��Z/e�0ed�Gdd�de'��Z1Gdd�dej&�Z2e�0e3ed �d!�Gd"d#�d#e,��Z4e�0ed$�Gd%d&�d&e'��Z5Gd'd(�d(e'�Z6e7d)k�r"e�8�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�}|dkrdSt�|tj�r<t�|g�jdkr<|fadS)N�truer)	�shutil�which�os�access�X_OK�
subprocess�run�
returncode�ZERO_RETURN_CMD)Z
shell_true�r�3/opt/cppython/lib/python3.8/test/test_subprocess.py�setUpModule3s
�rc@s&eZdZdd�Zdd�Zddd�ZdS)	�BaseTestCasecCst��dS�N)r�
reap_children��selfrrr�setUp>szBaseTestCase.setUpcCsBts.tjD]}|��q
t��|�tjd�|��t��dS)Nzsubprocess._active not empty)	�	mswindowsr�_active�wait�_cleanup�assertFalseZ
doCleanupsrr)r�instrrr�tearDownCs

�zBaseTestCase.tearDownNcCs$t�|�}|��}|�|||�dSr)r�strip_python_stderr�strip�assertEqual)r�stderr�expected�msg�actualrrr�assertStderrEqualOs
zBaseTestCase.assertStderrEqual)N)�__name__�
__module__�__qualname__rr"r*rrrrr=src@seZdZdS)�PopenTestExceptionN)r+r,r-rrrrr.Ysr.c@seZdZdd�ZdS)�PopenExecuteChildRaisescOstd��dS)NzForced Exception for Test)r.�r�args�kwargsrrr�_execute_childasz&PopenExecuteChildRaises._execute_childN)r+r,r-r3rrrrr/]sr/c@s2eZdZdd�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ɄZle�meneodʃdˡe�menepd̃d͡e�menepd΃dϡd�dф���Zqe�mepjr�sd�ep�t��dӡd�dՄ�Zue�e d֡d�d؄�Zve�e d֡d�dڄ�Zwd�S)��ProcessTestCasecCsztjttjtjtjd�}z4|�	|jt
j�|�	|jt
j�|�	|jt
j�W5|j��|j��|j��|��XdS)N��stdin�stdoutr&)r�Popenr�PIPEr6�closer7r&r�assertIsInstance�io�BufferedIOBase�r�prrr�test_io_buffered_by_defaultgs�


z+ProcessTestCase.test_io_buffered_by_defaultcCs|tjttjtjtjdd�}z4|�	|jt
j�|�	|jt
j�|�	|jt
j�W5|j��|j��|j��|��XdS)Nr)r6r7r&�bufsize)rr8rr9r6r:r7r&rr;r<�	RawIOBaser>rrr�test_io_unbuffered_worksus�


z(ProcessTestCase.test_io_unbuffered_workscCs"t�tjddg�}|�|d�dS�Nr�import sys; sys.exit(47)�/�r�call�sys�
executabler%�r�rcrrr�
test_call_seq�s
�zProcessTestCase.test_call_seqcCs"|jtjtjtjddgdd�dS)Nr�while True: pass皙�����?��timeout)�assertRaisesr�TimeoutExpiredrHrIrJrrrr�test_call_timeout�s
�z!ProcessTestCase.test_call_timeoutcCst�t�}|�|d�dS�Nr)r�
check_callrr%rKrrr�test_check_call_zero�s
z$ProcessTestCase.test_check_call_zeroc	Cs>|�tj��}t�tjddg�W5QRX|�|jjd�dSrD)	rRr�CalledProcessErrorrVrIrJr%�	exceptionr�r�crrr�test_check_call_nonzero�s

�z'ProcessTestCase.test_check_call_nonzerocCs"t�tjddg�}|�d|�dS)Nr�
print('BDFL')�BDFL�r�check_outputrIrJ�assertIn�r�outputrrr�test_check_output�s
�z!ProcessTestCase.test_check_outputc	Cs>|�tj��}t�tjddg�W5QRX|�|jjd�dS)Nrzimport sys; sys.exit(5)�)	rRrrXr`rIrJr%rYrrZrrr�test_check_output_nonzero�s

�z)ProcessTestCase.test_check_output_nonzerocCs(tjtjddgtjd�}|�d|�dS)Nr�$import sys; sys.stderr.write('BDFL')�r&r^)rr`rIrJ�STDOUTrarbrrr�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�seekrr`rIrJra)r�tfrcrrr�test_check_output_stdin_arg�s

��z+ProcessTestCase.test_check_output_stdin_argcCs&tjtjddgdd�}|�d|�dS)Nrrlrk��inputrnr_rbrrr�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 '')rvsXX�rr`rIrJ�assertNotInrbrrr�test_check_output_input_none�s��z,ProcessTestCase.test_check_output_input_nonecCs(tjtjddgddd�}|�d|�dS)NrryT)rw�text�XXrzrbrrr�!test_check_output_input_none_text�s��z1ProcessTestCase.test_check_output_input_none_textcCs(tjtjddgddd�}|�d|�dS)NrryT)rw�universal_newlinesr~rzrbrrr�/test_check_output_input_none_universal_newlines�s��z?ProcessTestCase.test_check_output_input_none_universal_newlinesc	CsP|�t��(}tjtjddgtjd�}|�d�W5QRX|�d|j	j
d�dS)Nr�print('will not be run')�r7z-Expected ValueError when stdout arg supplied.r7r)rR�
ValueErrorrr`rIrJr7�failrarYr1�rr[rcrrr�test_check_output_stdout_arg�s
�z,ProcessTestCase.test_check_output_stdout_argc	Cs�t��}|�|j�|�d�|�d�|�t��(}tj	t
jddg|dd�}|�d�W5QRX|�
d|jjd�|�
d	|jjd�dS)
Nrkrrr��hare�r6rw�7Expected ValueError when stdin and input args supplied.r6rw)rorprqr:rrrsrRr�rr`rIrJr�rarYr1�rrtr[rcrrr�&test_check_output_stdin_with_input_arg�s


�z6ProcessTestCase.test_check_output_stdin_with_input_argc	CsL|�tj��&}tjtjddgdd�}|�d�W5QRX|�|jj	d�dS)Nr�Mimport sys, time
sys.stdout.write('BDFL')
sys.stdout.flush()
time.sleep(3600)�rPzExpected TimeoutExpired.r^)
rRrrSr`rIrJr�r%rYrcr�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�copyrrHrIrJr%)r�newenvrLrrr�test_call_kwargss

��z ProcessTestCase.test_call_kwargsc	Csht���F}|jttjdd�tjjjj}dg|d}|jttjf|��W5QRX|�	|�
�d�dS)Nr�)Zinvalid_arg_namerr)rZcaptured_stderrrR�	TypeErrorrr8�__init__�__code__�co_argcountr%�getvalue)r�sZargcountZ
too_many_argsrrr�test_invalid_argss
z!ProcessTestCase.test_invalid_argscCsRtjtjddgtjtjd�}|�|jj�|�|jj�|�	�|�
|jd�dS)Nr�print("banana")�r7r&)rr8rIrJr9rqr7r:r&rr%r6r>rrr�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�
rr8rIrJr9rqr7r:r&�communicater%r�rstrip�r�coder?�out�errrrr�test_stdout_nones�z ProcessTestCase.test_stdout_nonecCsRtjtjddgtjtjd�}|�|jj�|�|jj�|�	�|�
|jd�dS)Nrr��r6r7)rr8rIrJr9rqr7r:r6rr%r&r>rrr�test_stderr_none6s�z ProcessTestCase.test_stderr_nonecKs2|dg}tj|f|�}|��|�d|j�dS�NrErF)rr8rr%r)r�pre_argsr2r1r?rrr�_assert_python?s
zProcessTestCase._assert_pythoncCs0tj�tj�tj�d�}|j|dgtjd�dS�N�doesnotexistr�rJ)r�path�join�dirnamerIrJr��rr�rrr�test_executableGs�zProcessTestCase.test_executablecCs6tj�tj�tj�d�}|j|dgt�tj�d�dSr�)rr�r�r�rIrJr��fsencoder�rrr�test_bytes_executableRs�

�z%ProcessTestCase.test_bytes_executablecCs4tj�tj�tj�d�}|j|dgttj�d�dSr�)rr�r�r�rIrJr�rr�rrr�test_pathlike_executableXs�
�z(ProcessTestCase.test_pathlike_executablecCs0tjdg}|�|�|jt|j|tdd�dS)Nrrr�)rIrJr�rR�NONEXISTING_ERRORS�NONEXISTING_CMD)rr�rrr� test_executable_takes_precedence^s

�z0ProcessTestCase.test_executable_takes_precedencez"executable argument replaces shellcCs|jgtjdd�dS�NT)rJ�shell)r�rIrJrrrr�test_executable_replaces_shellhsz.ProcessTestCase.test_executable_replaces_shellcCs|jgt�tj�dd�dSr�)r�rr�rIrJrrrr�$test_bytes_executable_replaces_shellns�z4ProcessTestCase.test_bytes_executable_replaces_shellcCs|jgttj�dd�dSr�)r�rrIrJrrrr�'test_pathlike_executable_replaces_shellss�z7ProcessTestCase.test_pathlike_executable_replaces_shellc
Cs*t�|��t��W5QR�SQRXdSr)rZ
change_cwdr�getcwd)r�cwdrrr�_normalize_cwdyszProcessTestCase._normalize_cwdcCstj�tj�}tj�|�Sr)rr��realpathrIrJ�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)r7rF)rr8r9rqr7r:rr%rrr��normcase�read�decode)rZexpected_cwdZ
python_argr2r?r�rrr�_assert_cwd�s���
�zProcessTestCase._assert_cwdcCs(t��}|�|�}|j|tj|d�dS�N�r�)ro�
gettempdirr�r�rIrJ�r�temp_dirrrr�test_cwd�s
zProcessTestCase.test_cwdcCs.t��}|�|�}|j|tjt�|�d�dSr�)ror�r�r�rIrJrr�r�rrr�test_cwd_with_bytes�s
z#ProcessTestCase.test_cwd_with_bytescCs,t��}|�|�}|j|tjt|�d�dSr�)ror�r�r�rIrJrr�rrr�test_cwd_with_pathlike�s
z&ProcessTestCase.test_cwd_with_pathlikez"pending resolution of issue #15533c	Csv|��\}}tj�tj|�}t���H}|�tt	j
|g�|jtt	j
|g|d�|�|�}|j|||d�W5QRXdSr��
r�rr�r��curdirrZtemp_cwdrR�FileNotFoundErrorrr8r�r�)r�
python_dir�python_base�
rel_python�	wrong_dirrrr�test_cwd_with_relative_arg�s

�
�
z*ProcessTestCase.test_cwd_with_relative_argc	Cs�|��\}}tj�tj|�}d}t���P}|jtt	j
|g|d�|jtt	j
|g||d�|�|�}|j||||d�W5QRXdS)N�somethingyoudonthaver��rJr�r�)rr�r�r�Zdoesntexistr�rrr�!test_cwd_with_relative_executable�s"

�
�

�z1ProcessTestCase.test_cwd_with_relative_executablec	Csr|��\}}tj�||�}tj�tj|�}t���6}|jtt	j
|g|d�|�|�}|j|||d�W5QRXdSr�)
r�rr�r�r�rr�rRr�rr8r�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�rIrJ)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�rIrJrrrr�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")rmrkr�)rr8rIrJr9r6rrr:rr%rr>rrr�test_stdin_pipe�s
��
zProcessTestCase.test_stdin_pipecCsft��}|�|j�|��}t�|d�t�|dd�tj	t
jddg|d�}|��|�
|jd�dS�Nrkrrr�rmr�)rorprqr:�filenorrr�lseekrr8rIrJrr%r�rrt�dr?rrr�test_stdin_filedes�s
��z"ProcessTestCase.test_stdin_filedescCsXt��}|�|j�|�d�|�d�tjtj	ddg|d�}|�
�|�|jd�dSr�)
rorprqr:rrrsrr8rIrJrr%r�rrtr?rrr�test_stdin_fileobjs


��z"ProcessTestCase.test_stdin_fileobjc	Cs>tjtjddgtjd�}|�|�|j��d�W5QRXdS)Nr�&import sys; sys.stdout.write("orange")r��orange�rr8rIrJr9r%r7r�r>rrr�test_stdout_pipes
��z ProcessTestCase.test_stdout_pipecCs`t��}|�|j�|��}tjtjddg|d�}|�	�t
�|dd�|�t
�
|d�d�dS)Nrr�r�r�r�)rorprqr:r�rr8rIrJrrr�r%r�r�rrr�test_stdout_filedess
��z#ProcessTestCase.test_stdout_filedescCsPt��}|�|j�tjtjddg|d�}|��|�	d�|�
|��d�dS)Nrr�r�rr�)rorprqr:rr8rIrJrrsr%r�r�rrr�test_stdout_fileobj$s
��
z#ProcessTestCase.test_stdout_fileobjc	Cs>tjtjddgtjd�}|�|�|j��d�W5QRXdS)Nr�*import sys; sys.stderr.write("strawberry")rh�
strawberry)rr8rIrJr9r*r&r�r>rrr�test_stderr_pipe/s
��z ProcessTestCase.test_stderr_pipecCs`t��}|�|j�|��}tjtjddg|d�}|�	�t
�|dd�|�t
�
|d�d�dS)Nrr�rhrr�r�)rorprqr:r�rr8rIrJrrr�r*r�r�rrr�test_stderr_filedes7s
��z#ProcessTestCase.test_stderr_filedescCsPt��}|�|j�tjtjddg|d�}|��|�	d�|�
|��d�dS)Nrr�rhrr��rorprqr:rr8rIrJrrsr*r�r�rrr�test_stderr_fileobjCs
��
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)	rr8rIrJr9r�r*r%r�rr?r7r&rrr�,test_stderr_redirect_with_no_stdout_redirectNs
��	z<ProcessTestCase.test_stderr_redirect_with_no_stdout_redirectc	CsBtjtjddgtjtjd�}|�|�|j��d�W5QRXdS)Nr�Rimport sys;sys.stdout.write("apple");sys.stdout.flush();sys.stderr.write("orange")r��appleorange)	rr8rIrJr9rir*r7r�r>rrr�test_stdout_stderr_pipecs
��z'ProcessTestCase.test_stdout_stderr_pipecCsRt��}|�|j�tjtjddg||d�}|��|�	d�|�
|��d�dS)Nrr�r�rr�r�r�rrr�test_stdout_stderr_fileos
��
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�)rr8rIrJ�DEVNULLrr%r7r>rrr�test_stdout_devnull�s
��z#ProcessTestCase.test_stdout_devnullcCs2tjtjddgtjd�}|��|�|jd�dS)Nrz=import sys
for i in range(10240):sys.stderr.write("x" * 1024)rh)rr8rIrJrrr%r&r>rrr�test_stderr_devnull�s
��z#ProcessTestCase.test_stderr_devnullcCs2tjtjddgtjd�}|��|�|jd�dS)Nrzimport sys;sys.stdin.read(1)rm)rr8rIrJrrr%r6r>rrr�test_stdin_devnull�s
��z"ProcessTestCase.test_stdin_devnullc	CsTtj��}d|d<tjtjddgtj|d��}|��\}}|�	|d�W5QRXdS)N�oranger�r�2import sys,os;sys.stdout.write(os.getenv("FRUIT"))�r7r�r�)
rr�r�rr8rIrJr9r�r%�rr�r?r7r&rrr�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.c	stdd��tjtjddgtjid��H}|��\}}t|���}|�|t	��fdd�|D�}|�
|g�W5QRXdS)NcSs*d|kp(d|kp(|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>)rr8rIrJr9r��evalr$r;�listr%)rr?r7r&Zchild_env_namesrrr�test_empty_env�s	
��zProcessTestCase.test_empty_envc	Cs\tjd}|�t��t�|ddg�W5QRX|�t��t�tjddg�W5QRXdS)N�rrzpass#)rIrJrRr�rr8)r�cmdrrr�test_invalid_cmd�s

z ProcessTestCase.test_invalid_cmdc	Cs�tj��}d|d<|�t��tjt|d�W5QRXtj��}d|d<|�t��tjt|d�W5QRXtj��}d|d<|�t��tjt|d�W5QRXtj��}d|d<tjtj	d	d
gtj
|d��}|��\}}|�|d�W5QRXdS)
NZcabbagezFRUITVEGETABLEr�zorangeVEGETABLE=cabbager�ZlemonzFRUIT=ORANGEzorange=lemonrz3import sys, os;sys.stdout.write(os.getenv("FRUIT"))rsorange=lemon)
rr�r�rRr�rr8rrIrJr9r�r%r	rrr�test_invalid_env�s.




��z ProcessTestCase.test_invalid_envcCs4tjtjddgtjd�}|�d�|�|jd�dS)Nrz/import sys;sys.exit(sys.stdin.read() == "pear")rmrkr��rr8rIrJr9r�r%rr>rrr�test_communicate_stdin�s
��
z&ProcessTestCase.test_communicate_stdincCs@tjtjddgtjd�}|��\}}|�|d�|�|d�dS)Nrz)import sys; sys.stdout.write("pineapple")r��	pineapple)rr8rIrJr9r�r%r�rrr�test_communicate_stdouts
��z'ProcessTestCase.test_communicate_stdoutcCs@tjtjddgtjd�}|��\}}|�|d�|�|d�dS)Nrz)import sys; sys.stderr.write("pineapple")rhr)rr8rIrJr9r�r%r*r�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())r5�bananar)
rr8rIrJr9rqr7r:r&r6r�r%r*r�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�r6r7r&r�g333333�?rPspineapple
pear
)rr8rIrJr9rRrSr�r%r*�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�������?rPi)
rr8rIrJr9rRrSr�r%�len)rr?r7�_rrr�%test_communicate_timeout_large_output4s
�	�
z5ProcessTestCase.test_communicate_timeout_large_outputcCs�dD]�}dD]�}dD]�}i}|r*tj|d<|r8tj|d<|rFtj|d<|sLqtjtf|�}|��|jdk	rz|�|jj�|jdk	r�|�|jj�|j	dk	r|�|j	j�qqqdS)N)FTr6r7r&)
rr9r8rr�r6�
assertTrue�closedr7r&)rZ
stdin_pipeZstdout_pipeZstderr_pipe�optionsr?rrr�test_communicate_pipe_fd_leakEs(





z-ProcessTestCase.test_communicate_pipe_fd_leakcCs:t�tjddg�}|��\}}|�|d�|�|d�dS)NrrE)rr8rIrJr�r%r�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())r5�a)r�piper:rr8rIrJr�
PIPE_MAX_SIZEr9rqr7r&r6r�r%)r�x�yr?Zstring_to_writer7r&rrr�test_communicate_pipe_bufcs$


���	
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())r5rssplitsbananasplitr�)rr8rIrJr9rqr7r:r&r6rrr�r%r*r�rrr�test_writes_before_communicatezs
��z.ProcessTestCase.test_writes_before_communicatec
Cs�tjddtdg}dD]�}tj|fdtjdtj|di�}|��|j�d�|j��|�	|j
��d�|j�d	�|j��|�
|j
j�|�	|j
��d
�|�	|j
�d�d	�|�	|j
��d�W5QRXqdS)
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)rIrJ�	SETBINARYrr8r9r6rr�flushr%r7�readliner:rqr�)rr1Zextra_kwargr?rrr� test_universal_newlines_and_text�s:���

���z0ProcessTestCase.test_universal_newlines_and_textcCs^tjtjddtdgtjtjdd�}|�|jj�|�|j	j�|�
�\}}|�|d�dS)Nrr2z�buf = sys.stdout.buffer;buf.write(b"line2\n");buf.flush();buf.write(b"line4\n");buf.flush();buf.write(b"line5\r\n");buf.flush();buf.write(b"line6\r");buf.flush();buf.write(b"\nline7");buf.flush();buf.write(b"\nline8");r�)r&r7r�z#line2
line4
line5
line6
line7
line8)rr8rIrJr4r9rqr7r:r&r�r%r�rrr�#test_universal_newlines_communicate�s
����z3ProcessTestCase.test_universal_newlines_communicatecCsHtjtjddtt�d�gtjdd�}|�d�\}}|�	|j
d�dS)Nrr2a
                               s = sys.stdin.readline()
                               assert s == "line1\n", repr(s)
                               s = sys.stdin.read()
                               assert s == "line3\n", repr(s)
                              r�)r6r��line1
line3
r)rr8rIrJr4�textwrap�dedentr9r�r%rr�rrr�)test_universal_newlines_communicate_stdin�s
��	z9ProcessTestCase.test_universal_newlines_communicate_stdincCs0tjttjtjdd�}|��|�|jd�dS)NT)r6r7r�r)rr8rr9r�r%rr>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)
Nrr2a�
                               s = sys.stdin.buffer.readline()
                               sys.stdout.buffer.write(s)
                               sys.stdout.buffer.write(b"line2\r")
                               sys.stderr.buffer.write(b"eline2\n")
                               s = sys.stdin.buffer.read()
                               sys.stdout.buffer.write(s)
                               sys.stdout.buffer.write(b"line4\n")
                               sys.stdout.buffer.write(b"line5\r\n")
                               sys.stderr.buffer.write(b"eline6\r")
                               sys.stderr.buffer.write(b"eline7\r\nz")
                              T)r6r&r7r�r9rzline1
line2
line3
line4
line5
zeline2
eline6
eline7
)rr8rIrJr4r:r;r9rqr7r:r&r�r%rr&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)r6r7�encodingrrvz1
2
3
4)rIrJrr8r9r�r%)rr?r�r1�popenr7r&rrr�-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)r6r7r?�errorsrrvz[{}])rIrJrr8r9r�r%�format)rrGr'r�r1r@r7r&rrr�test_communicate_errorss�z'ProcessTestCase.test_communicate_errorsc
Cs0ts
d}nd}g}t��}z�t|�D]n}z.tj�	|t
j�}|�t�
|tjtjB��Wq$tk
r�}z|jtjkrv�WY�q�W5d}~XYq$Xq$|�d|�td�D]}t�|���q�td�D]>}tjtjddgtjtjtjd�}|�d	�d
}	|�|	d	�q�W5|D]}t�|��qt�|�XdS)Niiz4failed to reach the file descriptor limit (tried %d)�
�rz-import sys;sys.stdout.write(sys.stdin.read())r5slimer)rro�mkdtemprr:r
�rmtree�ranger�r�rZTESTFN�append�open�O_WRONLY�O_CREAT�OSError�errnoZEMFILE�skipTest�poprr8rIrJr9r�r%)
rZmax_handles�handlesZtmpdir�h�iZtmpfile�er?�datarrr�test_no_leaking,s@�
��zProcessTestCase.test_no_leakingcCs�|�t�dddg�d�|�t�dddg�d�|�t�dddg�d	�|�t�d
ddg�d
�|�t�dddg�d�|�t�dddg�d�|�t�dddg�d�|�t�ddg�d�dS)Nza b cr�rZz"a b c" d ezab"c�\z	ab\"c \ dz \z
ab\"c " \\" dza\\\bzde fgrXza\\\b "de fg" hza\"br[z
a\\\"b c dza\\b cz"a\\b c" d eza\\b\ cz
"a\\b\ c" d e�abrzab "")r%r�list2cmdlinerrrr�test_list2cmdlineSs0��������z!ProcessTestCase.test_list2cmdlinecCsbtjtjddgtjd�}|�|jj�|�|�	��t
�|j��d�|�
�|�|�	�d�dS)Nrzimport os; os.read(0, 1)rm�Ar)rr8rIrJr9rqr6r:�assertIsNone�pollrrrr�rr%r>rrr�	test_polles
��zProcessTestCase.test_pollcCs.t�t�}|�|��d�|�|��d�dSrU�rr8rr%rr>rrr�	test_waitps
zProcessTestCase.test_waitc	Cs`t�tjddg�}|�tj��}|jdd�W5QRX|�dt|j	��|�
|jdd�d�dS)Nrzimport time; time.sleep(0.3)�-C��6?rPz0.0001r�r)rr8rIrJrRrSrra�strrYr%)rr?r[rrr�test_wait_timeoutvs�z!ProcessTestCase.test_wait_timeoutc	Cs&|�t��t�td�W5QRXdS)Nr)rRr�rr8rrrrr�test_invalid_bufsize�sz$ProcessTestCase.test_invalid_bufsizecCs>t�td�}|�|��d�tjtdd�}|�|��d�dS)Nr)rArer>rrr�test_bufsize_is_none�sz$ProcessTestCase.test_bufsize_is_nonec
Cs�tjtjddgtjtjtjd|d��j}|j�|�t�	|j�
��|j��}t
���*z|j�	�Wntk
rxYnXW5QRXd|_W5QRX|�|jd�|�||�dS)NrzDimport sys;sys.stdout.write(sys.stdin.readline());sys.stdout.flush()r�)r6r7r&rAr�r)rr8rIrJr9rr6rrrr:r�r7r6r�SuppressCrashReportrSr%r)r�liner'r�r?Z	read_linerrr�_test_bufsize_equal_one�s&�

z'ProcessTestCase._test_bufsize_equal_onecCsd}|j||dd�dS)Nzline
T�r�)rn�rrmrrr� test_bufsize_equal_one_text_mode�sz0ProcessTestCase.test_bufsize_equal_one_text_modec	Cs:dtj��}|�td��|j|ddd�W5QRXdS)Nslinezline bufferingr�Fro)r�linesepr!ZassertWarnsRegex�RuntimeWarningrnrprrr�"test_bufsize_equal_one_binary_mode�sz2ProcessTestCase.test_bufsize_equal_one_binary_modec
Cs<td�D].}|�t��tjttjtjd�W5QRXqdS)Nr�r�)rNrRr�rr8r�r9)rrYrrr�test_leaking_fds_on_error�s�z)ProcessTestCase.test_leaking_fds_on_errorc	Cs�zddl}|jWn"ttfk
r4|�d�YnXt�dt�d��}tj	d|g}t
j|t
jdd�}|�|�
�d}W5QRX|�|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)r&r�r�r)�msvcrtZCrtSetReportMode�AttributeError�ImportErrorrUr:r;r�rIrJrr8r9r�r%r)rrvr�r�procr&rrr�test_nonexisting_with_pipes�s
�z+ProcessTestCase.test_nonexisting_with_pipescs�g��fdd�}tj|d�}|��z2|�t��t	j
tt	jt	jt	jd�W5QRXW5|��d}�D]:}zt�|�Wqhtk
r�}z|}W5d}~XYqhXqh|dk	r�|�XdS)Ncs*td�D]}��t���t�d�qdS)N�g����MbP?)rN�extendrr,�time�sleep)rY��fdsrr�open_fds�sz<ProcessTestCase.test_double_close_on_error.<locals>.open_fds)�targetr5)
�	threading�Thread�startr�rr:rSrR�EnvironmentErrorrr8r�r9)rr��t�exc�fdrZrrr�test_double_close_on_error�s(�z*ProcessTestCase.test_double_close_on_errorcs�t�tjddg��|��jd�g���fdd�}t�d|�}|��t	rPd}nd}�j
dd	�|j�j|d
d���
�|j�j|dd�|��|�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)rOrc�killrrr�ry�resultsrr�kill_proc_timer_thread
szDProcessTestCase.test_threadsafe_wait.<locals>.kill_proc_timer_thread皙�����?r�i����r{rPz*unexpected result in wait from main thread�r(z&unexpected result in second main wait.)r�Nr�r�)rr8rIrJr%rr��Timerr�rrr�)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`rIrJr&r�ascii)rr�rcrrr�test_issue87807s
zProcessTestCase.test_issue8780cCs�t��\}}t��\}}t��\}}ztjdg|||d�WnPtk
r�t�|�t�|�t�|�t�|�t�|�t�|�YnX|�tj	�
|��|�tj	�
|��|�tj	�
|��dS)N�*r5)ro�mkstemprr8rSrr:�remover r��exists)rZifhandleZifnameZofhandleZofnameZefhandleZefnamerrr� test_handles_closed_on_exceptionBs"�





z0ProcessTestCase.test_handles_closed_on_exceptioncCsTtjttjtjtjd�}|�|jj�|�|jj�|�|jj�|�	dd�dS)Nr5�x�)
rr8rr9rqr7r:r&r6r�r>rrr�test_communicate_epipeVs�z&ProcessTestCase.test_communicate_epipecCs8tjttjd�}|�|jj�|��|�dd�dS)Nrmr�r�)	rr8rr9rqr6r:rr�r>rrr�!test_communicate_epipe_only_stdinas�z1ProcessTestCase.test_communicate_epipe_only_stdin�SIGUSR1zRequires signal.SIGUSR1r�zRequires os.kill�getppidzRequires os.getppidc
Csndd�}t�tj|�}|�tjtj|�tjddg}dD]0}|tji}tj|f|��}|��W5QRXq8dS)NcSsdSrr)�signum�framerrr�handlerqsz7ProcessTestCase.test_communicate_eintr.<locals>.handlerrz7import os, signal;os.kill(os.getppid(), signal.SIGUSR1)r�)	�signalr�rqrIrJrr9r8r�)rr��old_handlerr1�stream�kw�processrrr�test_communicate_eintris�
z&ProcessTestCase.test_communicate_eintr�/proc/%d/fdzLinux specificc	Cs\dt��}t�|�}|�t��tttjtjtjd�W5QRXt�|�}|�	||�dS)Nr�r5)
r�getpid�listdirrRr.r/rrr9r%)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 Windowsc	Cs<|�t��}t�ddddg�W5QRX|�|jjd�dS)Nz/opt/nonexistent_binary�withZsomer1)rRr�rrHr%rY�filenamerZrrr�%test_file_not_found_includes_filename�sz5ProcessTestCase.test_file_not_found_includes_filenamec	Cs<|�t��}tjddgdd�W5QRX|�|jjd�dS)N�exit�0z/some/nonexistent/directoryr�)rRr�rr8r%rYr�rZrrr� test_file_not_found_with_bad_cwd�sz0ProcessTestCase.test_file_not_found_with_bad_cwdN)xr+r,r-r@rCrMrTrWr\rdrfrjrurxr|rr�r�r�r�r�r�r�r�r�r�r�r�r�r��unittest�skipIfrr�r�r�r�r�r�r�r�r�r�r�r�rI�base_prefix�prefixr��	sysconfigZis_python_buildr�r�r�r�r�r�r�r�r�r�r�r�rrrrrr
�platformZget_config_varrrrrrrr r"r%r)r*r0r1r7r8r<r=r>rArIr\r`rdrfrirjrkrnrqrtrurzr�r�r�r�r�r��
skipUnless�hasattrr�rr�r��isdirr�r�r�r�rrrrr4es		

		










�
�
�

	
��
''
)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'��Zd(S))�RunFuncTestCasecKstjd|g}tj|f|�S)Nr)rIrJrr)rr�r2�argvrrr�
run_python�szRunFuncTestCase.run_pythonc	Cs<|�d�}|�|jd�|�tj��|��W5QRXdSr�)r�r%rrRrrX�check_returncode�r�cprrr�test_returncode�s
zRunFuncTestCase.test_returncodec	Cs:|�tj��}|jddd�W5QRX|�|jjd�dS)NrET��checkrF)rRrrXr�r%rYrrZrrr�
test_check�szRunFuncTestCase.test_checkcCs tjtdd�}|�|jd�dS)NTr�r)rrrr%rr�rrr�test_check_zero�szRunFuncTestCase.test_check_zeroc	Cs*|�tj��|jddd�W5QRXdS)NrNrgrP)rRrrSr�rrrr�test_timeout�szRunFuncTestCase.test_timeoutcCs"|jdtjd�}|�d|j�dS)Nr]r�r^�r�rr9rar7r�rrr�test_capture_stdout�sz#RunFuncTestCase.test_capture_stdoutcCs"|jdtjd�}|�d|j�dS)Nrgrhr^)r�rr9rar&r�rrr�test_capture_stderr�s�z#RunFuncTestCase.test_capture_stderrcCsLt��}|�|j�|�d�|�d�|jd|tjd�}|�	d|j
�dS)Nrkrrlr�rn)rorprqr:rrrsr�rr9rar7)rrtr�rrrru�s

�z+RunFuncTestCase.test_check_output_stdin_argcCs$|jddtjd�}|�d|j�dS)Nrlrk)rwr7rnr�r�rrrrx�s�z+RunFuncTestCase.test_check_output_input_argc	Cs~t��}|�|j�|�d�|�d�|jtdd��}|jd|dd�}W5QRX|�	d|j
jd�|�	d	|j
jd�dS)
Nrkrr�r�r�r�r�r6rw)rorprqr:rrrsrRr�r�rarYr1r�rrrr��s

��z6RunFuncTestCase.test_check_output_stdin_with_input_argc	CsN|�tj��}|jddtjd�}W5QRX|�|jjd�|�|jjd�dS)Nr�r�)rQr7r^)	rRrrSr�r9r%rYrcr7)rr[r�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�r%r)rr�r�rrr�test_run_kwargs�s
�zRunFuncTestCase.test_run_kwargsc	Cs�trdnd}t�|�}|dkr.|�|�d��t|�}tj|tjd�}|�|j	d�|�
t��tj|tjdd�W5QRXdS)Nztree.comZlsz required for this testr�rT)r7r�)rr
rrUrrrrr%rrRr�)r�progr��resrrr�test_run_with_pathlike_path�s
z+RunFuncTestCase.test_run_with_pathlike_pathcCs2t�tj�}|ddg}t�|�}|�|jd�dS)Nrsimport sys; sys.exit(57)�9)rr�rIrJrrr%r�rr�r1r�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�)rrIrJrrr%rr�rrr�)test_run_with_pathlike_path_and_argumentss


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_outputr^sFLUFL)r�rar7r&r�rrr�test_capture_outputs
�z#RunFuncTestCase.test_capture_outputc	Csjt��}|�|j�|jtdd��}|jdd|d�}W5QRX|�d|jj	d�|�d|jj	d�dS)	NzAExpected ValueError when stdout and capture_output args supplied.r�r�T)r�r7r7rr��
rorprqr:rRr�r�rarYr1r�rrr�#test_stdout_with_capture_output_arg#s��z3RunFuncTestCase.test_stdout_with_capture_output_argc	Csjt��}|�|j�|jtdd��}|jdd|d�}W5QRX|�d|jj	d�|�d|jj	d�dS)	NzAExpected ValueError when stderr and capture_output args supplied.r�r�T)r�r&r&rr�r�r�rrr�#test_stderr_with_capture_output_arg/s��z3RunFuncTestCase.test_stderr_with_capture_output_argz)requires posix like 'sleep' shell commandc
Cs~t��}ztjddddd�Wn4tjk
rR}zt��}t��}W5d}~XYnX|�d�|j||dd|�d�d	�dS)
Nzsleep 3TrO)r�rQr�zTimeoutExpired not raised.g�?z/TimeoutExpired was delayed! Bad traceback:
```
z```r�)	r}�	monotonicrrrS�	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_outputN)r+r,r-r�r�r�r�r�r�r�rurxr�r�r�r�r�r�r�r�r�r�r�rr�rrrrr��s(

r�zPOSIX specific testscsPeZdZ�fdd�Zdd�Zdd�Zdd�Zd	d
�ZGdd�dej	�Z
e�d
�dd��Z
e�d
�dd��Ze�ej�d�d�dd��Zdd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�ZGd$d%�d%ej	�Ze�ej�d&�d'�d(d)��Zd*d+�Ze�ej d,kd-�d.d/��Z!d0d1�Z"d2d3�Z#d4d5�Z$d6d7�Z%d8d9�Z&d:d;�Z'd<d=�Z(e�ej �)d>�d?�d@dA��Z*dBdC�Z+dDdE�Z,dFdG�Z-dHdI�Z.dJdK�Z/dLdM�Z0dNdO�Z1dPdQ�Z2dRdS�Z3dTdU�Z4dVdW�Z5dXdY�Z6dZd[�Z7d\d]�Z8d^d_�Z9d`da�Z:dbdc�Z;ddde�Z<dfdg�Z=dhdi�Z>djdk�Z?dldm�Z@dndo�ZAdpdq�ZBdrds�ZCdtdu�ZDdvdw�ZEdxdy�ZFe�ej �)dz��one�Gd{�jHe�Gd|�jHkd}�d~d��ZIeJ�Kd�d��d�d���ZLd�d��ZMd�d��ZNd�d��ZOd�d��ZPd�d��ZQd�d��ZRd�d��ZSd�d��ZTd�d��ZUd�d��ZVeJjWd�d���ZXeJjWd�d���ZYd�d��ZZd�d��Z[d�d��Z\d�d��Z]e�^e_d�k	�o2e`e_d��d��d�d���Zad�d��Zb�ZcS)��POSIXProcessTestCasecst���d|_dS)Nz/_this/pa.th/does/not/exist)�superr�_nonexistent_dirr��	__class__rrrSs
zPOSIXProcessTestCase.setUpc
CsLzt�|j�Wn&tk
r6}z|}W5d}~XYnX|�d|j�|S)Nz,chdir to nonexistent directory %s succeeded.)r�chdirr�rSr�)rrZ�desired_exceptionrrr�_get_chdir_exceptionWs�z)POSIXProcessTestCase._get_chdir_exceptionc
Cs�|��}ztjtjddg|jd�}WnRtk
rv}z4|�|j|j�|�|j	|j	�|�|j
|j
�W5d}~XYnX|�d|�dS)Nrrr��Expected OSError: %s�r�rr8rIrJr�rSr%rT�strerrorr�r��rr�r?rZrrr�test_exception_cwdds�
"z'POSIXProcessTestCase.test_exception_cwdc
Cs�|��}ztjtjddg|jd�}WnRtk
rv}z4|�|j|j�|�|j	|j	�|�|j
|j
�W5d}~XYnX|�d|�dS)Nrrr�r�r�r�rrr�test_exception_bad_executabless�
"z2POSIXProcessTestCase.test_exception_bad_executablec
Cs�|��}zt�|jddg�}WnRtk
rp}z4|�|j|j�|�|j|j�|�|j|j�W5d}~XYnX|�	d|�dS)Nrrr�)
r�rr8r�rSr%rTr�r�r�r�rrr�test_exception_bad_args_0�s"z.POSIXProcessTestCase.test_exception_bad_args_0c@seZdZdd�ZdS)z&POSIXProcessTestCase.PopenNoDestructorcCsdSrrrrrr�__del__�sz.POSIXProcessTestCase.PopenNoDestructor.__del__N)r+r,r-r�rrrr�PopenNoDestructor�sr�z%subprocess._posixsubprocess.fork_execc
CsNdd�}||_tjdtd��(|�t��|�dg�W5QRXW5QRXdS)NcWs0|d}d�tj���}t�|d|d�dS)N�
z{:x}sOSError:�:r)rHrTZEISDIRr!rrr)r1�
errpipe_writeZerr_coderrr�proper_error�szHPOSIXProcessTestCase.test_exception_errpipe_normal.<locals>.proper_error�subprocess.os.waitpid��side_effect�non_existent_command)r�r�patch�ChildProcessErrorrR�IsADirectoryErrorr�)r�	fork_execr�rrr�test_exception_errpipe_normal�s�z2POSIXProcessTestCase.test_exception_errpipe_normalc
snd��fdd�}||_tjdtd��*|�tj��}|�dg�W5QRXW5QRX|�t	��t
|j��dS)Ns�ޭcs|d}t�|��dS)Nr�r)rrr)r1r��Z
error_datarr�	bad_error�szGPOSIXProcessTestCase.test_exception_errpipe_bad_data.<locals>.bad_errorr�r�r�)r�rr�r�rRr�SubprocessErrorr�ra�reprrhrY)rr�rrZrr�r�test_exception_errpipe_bad_data�s� z4POSIXProcessTestCase.test_exception_errpipe_bad_data�/proc/self/statuszneed /proc/self/statuscCs�tjddgdd�}|��D]}|�d�r|}q<q|�d�tjddgdd�}|��D]}|�d�rV|}qnqV|j||dd	�dS)
N�catrF)�restore_signalssSigIgnz&SigIgn not found in /proc/self/status.Tz=restore_signals=True should've unblocked SIGPIPE and friends.r�)rr`�
splitlinesrrU�assertNotEqual)rZdefault_proc_statusrmZdefault_sig_ign_maskZrestored_proc_statusZrestored_sig_ign_maskrrr�test_restore_signals�s(�

�
�z)POSIXProcessTestCase.test_restore_signalsc
Csnztjtjddgdd�}Wn0tk
rJ}z|jtjkr:�W5d}~XYn Xt�d�}t	|�}|�
||�dS)Nrzimport os; print(os.getsid(0))T)�start_new_sessionr)rr`rIrJrSrT�EPERMr�getsid�intr)rrcrZZ
parent_sidZ	child_sidrrr�test_start_new_session�s
�

z+POSIXProcessTestCase.test_start_new_sessionc	CsDt��� t�tjddg�}|��W5QRX|�|jt	j
�dS)Nrzimport os; os.abort())rrlrr8rIrJrr%rr��SIGABRTr>rrr�test_run_abort�s

�z#POSIXProcessTestCase.test_run_abortcCsNt�ttj�d�}t|�}|�d|���|�d|�|�ttj�|�dS)N�fake cmdr�ZSIG)rrXr
r�rrhra�lower�rr�Zerror_stringrrr�"test_CalledProcessError_str_signal�s
z7POSIXProcessTestCase.test_CalledProcessError_str_signalcCs$t�dd�}t|�}|�d|�dS)Ni�Ki�rzunknown signal 9876543.�rrXrhrarrrr�*test_CalledProcessError_str_unknown_signal�sz?POSIXProcessTestCase.test_CalledProcessError_str_unknown_signalcCs$t�dd�}t|�}|�d|�dS)N�rznon-zero exit status 2.rrrrr�$test_CalledProcessError_str_non_zerosz9POSIXProcessTestCase.test_CalledProcessError_str_non_zeroc	CsDtjtjddgtjdd�d�}|�|�|j��d�W5QRXdS)NrrcSst�dd�S)Nr��apple)r�putenvrrrr�<lambda>r�z3POSIXProcessTestCase.test_preexec.<locals>.<lambda>)r7�
preexec_fn�appler�r>rrr�test_preexecs
��z!POSIXProcessTestCase.test_preexecc
Cs�dd�}ztjtjddg|d�}Wndtjk
rT}z|�tjd�W5d}~XYn>tk
r�}z|�d|j	d�W5d}~XYnX|�
d	�dS)
NcSstd��dS)Nz'What if two swallows carried a coconut?�r�rrrr�raise_itsz=POSIXProcessTestCase.test_preexec_exception.<locals>.raise_itrr�rz)Expected a ValueError from the preexec_fnZcoconutrzEException raised by preexec_fn did not make it to the parent process.)rr8rIrJrr&�_posixsubprocessr�rar1r�)rr r?rZrrr�test_preexec_exceptions�
�$z+POSIXProcessTestCase.test_preexec_exceptionc@seZdZdd�Zdd�ZdS)z+POSIXProcessTestCase._TestExecuteChildPopencOs||_tjj|f|�|�dSr)�	_testcaserr8r�)rZtestcaser1r2rrrr�$sz4POSIXProcessTestCase._TestExecuteChildPopen.__init__cOs�zt	j
j|f|�|�W5dd�td�D�}z8|D].}|jj||j��|j��|j��fdd�q2W5|D]}t�|�qjXXdS)NcSsg|]}t�dtj��qS)�	/dev/zero)rrP�O_RDONLY�rr$rrrr/s�zNPOSIXProcessTestCase._TestExecuteChildPopen._execute_child.<locals>.<listcomp>�z!At least one fd was closed early.r�)rNrr:r$r{r6r�r7r&rr8r3)rr1r2Zdevzero_fdsr�rrrr3(s ���z:POSIXProcessTestCase._TestExecuteChildPopen._execute_childN)r+r,r-r�r3rrrr�_TestExecuteChildPopen"sr)r%z/dev/zero required.c	Cs@dd�}|�tj��"|j|ttjtjtj|d�W5QRXdS)NcSst�d��dS)Nz-force the _execute_child() errpipe_data path.)rrrrrrr ?s�zWPOSIXProcessTestCase.test_preexec_errpipe_does_not_double_close_pipes.<locals>.raise_it)r6r7r&r)rRrrr)rr9)rr rrr�0test_preexec_errpipe_does_not_double_close_pipes;s�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�W5|t_|t_|�st��XdS)
NcSstd��dS)Nzthis shouldn't escape)�RuntimeErrorrrrr�raise_runtime_errorLszPPOSIXProcessTestCase.test_preexec_gc_module_failure.<locals>.raise_runtime_errorrrcSsdSrrrrrrrUr�zEPOSIXProcessTestCase.test_preexec_gc_module_failure.<locals>.<lambda>r!z#Popen enabled gc when it shouldn't.cSsdSrrrrrrr\r�zPopen left gc disabled.cSsdSrrrrrrrbr�cSsdSrrrrrrrgr�)�gc�	isenabled�disabler rrHrIrJ�enabler&rRr+r8rw)rr,ZenabledZorig_gc_disableZorig_gc_isenabledrrr�test_preexec_gc_module_failureIsB�
��

�

�
z3POSIXProcessTestCase.test_preexec_gc_module_failure�darwinz!setrlimit() seems to fail on OS Xc
Cs�zddlm}m}m}Wn,tk
rD}z|�|�W5d}~XYnX||�}|\}}||d|f�|�|||�ztjt	j
ddgdd�d�Wntk
r�YnX|�d�dS)	Nr)�	getrlimit�	setrlimit�RLIMIT_NPROCrrcSsdSrrrrrrr}r�z@POSIXProcessTestCase.test_preexec_fork_failure.<locals>.<lambda>r!z.RLIMIT_NPROC had no effect; probably superuser)�resourcer3r4r5rxrUrqrrHrIrJ�BlockingIOError)rr3r4r5r�Zlimitsr$Zhardrrr�test_preexec_fork_failurens�
z.POSIXProcessTestCase.test_preexec_fork_failurec	Cs�t��\}}t|ddd��&}|�dtj�|�dtj�W5QRXt�	|d�t
�|�}|��t�
|�|�|jd�dS�N�wrD)rGz#!%s
z(exec '%s' -c 'import sys; sys.exit(47)'
i�rF)ror�rPrrr�
unix_shellrIrJr�chmodrr8rr�r%r)rr��fname�fobjr?rrr�test_args_string�s�

z%POSIXProcessTestCase.test_args_stringcCs<|jttjtjddgdd�|jttjtjddgdd�dS)NrrErF��startupinfo��
creationflags�rRr�rrHrIrJrrrrr��s
��
��z&POSIXProcessTestCase.test_invalid_argsc	CsTtj��}d|d<tjdgdtj|d�}|�|�|j���	d�d�W5QRXdS�Nrr�zecho $FRUITr��r�r7r�s 	
r�
rr�r�rr8r9r%r7r�r$�rr�r?rrr�test_shell_sequence�s

�z(POSIXProcessTestCase.test_shell_sequencec	CsRtj��}d|d<tjddtj|d�}|�|�|j���	d�d�W5QRXdSrErGrHrrr�test_shell_string�s
�z&POSIXProcessTestCase.test_shell_stringc	Csvt��\}}t|ddd��&}|�dtj�|�dtj�W5QRXt�	|d�t
�|�}t�|�|�
|d�dSr9)ror�rPrrrr;rIrJrr<rrHr�r%)rr�r=r>rLrrr�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]D}tjd|dtj	d�}|�"|�
|j���
�t|d��W5QRXqtdS)	N)z/binz	/usr/bin/z/usr/local/bin)ZbashZkshz"bash or ksh required for this testz/bin/shzecho $0T)rJr�r7r�)rr�r��isfilerOrU�islinkrr8r9r%r7r�r$�bytes)rZshellsr��name�shr?rrr�test_specific_shell�s"


�z(POSIXProcessTestCase.test_specific_shellc
Gsft�tjtj�}z&tjtjddgdtjtjtjd�}W5t�tj|�X|j�	d�t
||�|�|S)Nrz�if 1:
                                 import sys, time
                                 sys.stdout.write('x\n')
                                 sys.stdout.flush()
                                 time.sleep(30)
                                 T��	close_fdsr6r7r&r�)r��SIGINT�default_int_handlerrr8rIrJr9r7r��getattr)r�methodr1r�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()
                             TrRr�)rr8rIrJr9r7r�r}r~rVr�)rrWr1r?rrr�_kill_dead_process�s�
z'POSIXProcessTestCase._kill_dead_processcCs:|�dtj�}|��\}}|�d|�|�|��d�dS)N�send_signalsKeyboardInterruptr)rXr�rTr�rarr�rr?r$r&rrr�test_send_signalsz%POSIXProcessTestCase.test_send_signalcCs:|�d�}|��\}}|�|d�|�|��tj�dS)Nr�r�)rXr�r*r%rr��SIGKILLr[rrr�	test_kills
zPOSIXProcessTestCase.test_killcCs:|�d�}|��\}}|�|d�|�|��tj�dS)N�	terminater�)rXr�r*r%rr��SIGTERMr[rrr�test_terminates
z#POSIXProcessTestCase.test_terminatecCs|�dtj�dS�NrZ)rYr�rTrrrr�test_send_signal_deadsz*POSIXProcessTestCase.test_send_signal_deadcCs|�d�dS�Nr��rYrrrr�test_kill_deadsz#POSIXProcessTestCase.test_kill_deadcCs|�d�dS�Nr_rerrrr�test_terminate_deadsz(POSIXProcessTestCase.test_terminate_deadcCs6g}|D](}t�|�}t�|�}|�|||f�q|Sr)r�get_inheritable�duprO)rZsave_fdsr�r��inheritable�savedrrr�	_save_fds s

zPOSIXProcessTestCase._save_fdscCs.|D]$\}}}tj|||d�t�|�qdS)N)rk)r�dup2r:)rr�r�rlrkrrr�_restore_fds(sz!POSIXProcessTestCase._restore_fdsc		Cs�d}|�|�}|D]\}}}|dkr|}q.qzX|D]}t�|�q4tjtjddg|tjtjd��	�\}}t
�|�}|�||fd�W5|�|�XdS)Nrrr�r5)rr�)
rmrorr:rr8rIrJr9r�rr#r%)	rr�r6�	saved_fdsr�rlrkr�r�rrr�check_close_std_fds-s&

��
z(POSIXProcessTestCase.check_close_std_fdscCs|�dg�dSrU�rqrrrr�test_close_fd_0Fsz$POSIXProcessTestCase.test_close_fd_0cCs|�dg�dS�Nr�rrrrrr�test_close_fd_1Isz$POSIXProcessTestCase.test_close_fd_1cCs|�dg�dS)Nrrrrrrr�test_close_fd_2Lsz$POSIXProcessTestCase.test_close_fd_2cCs|�ddg�dS)Nrr�rrrrrr�test_close_fds_0_1Osz'POSIXProcessTestCase.test_close_fds_0_1cCs|�ddg�dS)Nrrrrrrrr�test_close_fds_0_2Rsz'POSIXProcessTestCase.test_close_fds_0_2cCs|�ddg�dS)Nr�rrrrrrr�test_close_fds_1_2Usz'POSIXProcessTestCase.test_close_fds_1_2cCs|�dddg�dS�Nrr�rrrrrrr�test_close_fds_0_1_2Xsz)POSIXProcessTestCase.test_close_fds_0_1_2c
Csvt�d�}t�d�}z.t�d�t�d�t�tjddg���W5t�|d�t�|d�t�|�t�|�XdS)Nrr�rz*print('AssertionError:0:CLOEXEC failure.'))	rrjrnr:rr8rIrJr)rZ	new_stdinZ
new_stdoutrrr�test_small_errpipe_write_fd]s



�
z0POSIXProcessTestCase.test_small_errpipe_write_fdc

Cs@dd�td�D�}�zdd�|D�}|D]\}}t�|�q(t�|dd�t�|ddd�|�td��}zPt|�D]\}}t�	||�qvt
jtj
dd	g|d|d
|dd�}|��W5|�|�X|D]}t�|dd�q�t�|d
d�}t�t�|dd��}	|�|d
�|�|	d�W5|D]}t�|��q(XdS)NcSsg|]}t���qSr�ror��rrYrrrrtsz?POSIXProcessTestCase.test_remapping_std_fds.<locals>.<listcomp>r�cSsg|]\}}|�qSrr�rr�r=rrrrvsr��STDINrr�Zimport sys; got = sys.stdin.read();sys.stdout.write("got %s"%got); sys.stderr.write("err")rr5r��	got STDIN�err)rNrr:�unlinkrrr�rmro�	enumeraternrr8rIrJrr�rr#r%)
r�temps�temp_fdsr�r=rp�temp_fdr?r�r�rrr�test_remapping_std_fdsrs8
��z+POSIXProcessTestCase.test_remapping_std_fdsc

Cs"dd�td�D�}dd�|D�}z�|D]\}}t�|�q&|�td��}z�t|�D]\}}	t�|	|�qRt�|d�t�	|dd�t
jtj
ddg|||d	�}
|
��|D]}t�	|dd�q�t�|d
�}t�t�|d
��}W5|�|�X|�|d�|�|d�W5|D]}t�|��q
XdS)
NcSsg|]}t���qSrr}r~rrrr�sz7POSIXProcessTestCase.check_swap_fds.<locals>.<listcomp>r�cSsg|]\}}|�qSrrrrrrr�sr�rrr�r5r�r�r�)rNrr:r�rmror�rnrrr�rr8rIrJrr�rr#r%)
rZstdin_noZ	stdout_noZ	stderr_nor�r�r�r=rpr�r?r�r�rrr�check_swap_fds�s8
��z#POSIXProcessTestCase.check_swap_fdscCsX|�ddd�|�ddd�|�ddd�|�ddd�|�ddd�|�ddd�dSrz)r�rrrr�
test_swap_fds�sz"POSIXProcessTestCase.test_swap_fdscCsl|�td��}�zJ|D](}t���}t�|��|�W5QRXqttd��t|��	�}t�
|�dddg}i}t||�D]\}}	||||	<qzt�
d�}
ttd��t|��	�}tjtjd|
t|�gf|�}|�|d�t||�D]r\}}	t�|dtj�t�|d�}
ttt|
�d	���}t�
d
|�d|�d|�d
|�d|	�d��}|�|	g||�q�W5|�|�XdS)Nr�r6r7r&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.
                )rmrNrororprrnr��setrVr:�zipr:r;rrHrIrJrhr%r��SEEK_SETr�r�mapr
r�)r�from_fds�to_fdsrpZfrom_fd�fZfd_to_closeZ	arg_namesr2Zto_fdr�Z
skipped_fdrLZ
read_bytesZread_fdsr(rrr�#_check_swap_std_fds_with_one_closed�sD



������
z8POSIXProcessTestCase._check_swap_std_fds_with_one_closedcCs<t�td�d�D]&}t�td�d�D]}|�||�q$qdS)Nr�r)�	itertools�combinationsrN�permutationsr�)rr�r�rrr�!test_swap_std_fds_with_one_closed	sz6POSIXProcessTestCase.test_swap_std_fds_with_one_closedc
Cs�dd�}ztjt|d�Wn|tk
rX}z |�tj�|�t|�d�W5d}~XYnJtjk
r�}z |�	tj�|�t|�d�W5d}~XYnX|�
d�dS)NcSstd��dS)N�
surrogate:�rrrrr�prepare		szCPOSIXProcessTestCase.test_surrogates_error_message.<locals>.preparer!r�z!Exception occurred in preexec_fn.z1Expected ValueError or subprocess.SubprocessError)rrHrr�rbr"r%rhrZassertIsNotNoner�)rr�r�rrr�test_surrogates_error_message	s�
 "z2POSIXProcessTestCase.test_surrogates_error_messagecCs�dD]�\}}|�dd�}dt|�}tj��}|||<d|d<|}tjtjd|g|d�}|�	d	�}|�
|�d�t|��|�dd�}d
t|�}tj��}|||<tjtjd|g|d�}|�	d	�}|�
|�d�t|��qdS)N))�testuabc�)utest�Z42r�rDz&import os; print(ascii(os.getenv(%s)))�C�LC_ALLrr�s

z'import os; print(ascii(os.getenvb(%s))))
r!rrr�r�rr`rIrJr�r%r�r�)r�key�valueZ
encoded_value�scriptr�Z
decoded_valuer7rrr�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� rFT�r��PATHr�sPATH)rr�rrr�r�rrHr%r�r!r�r��environb)	rZabs_programr1r�Zprogram�exitcoderr�Zenvbrrr�test_bytes_program8	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)r6r7r&rSr��r7rS�,z5Expected no fds from %r to be open in child, found %r)r�findfilerr8rIrJr9rqr�r�r�r
r�r6r�r7r&r )	rZsleeper�	fd_status�p1�p2rc�errorZ
result_fdsZunwanted_fdsrrr�test_pipe_cloexecS	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)r6r7rScs&z���Wntk
r YnXdSr�r_�ProcessLookupErrorr)r�rr�kill_p1{	szBPOSIXProcessTestCase.test_pipe_cloexec_real_tools.<locals>.kill_p1cs&z���Wntk
r YnXdSrr�r)r�rr�kill_p2�	szBPOSIXProcessTestCase.test_pipe_cloexec_real_tools.<locals>.kill_p2rJzThe child hung)rr�rr8rIrJr9r7rqrr6rrr:�selectr&r%r�)
rZqcatZqgrepZsubdatar[r�r�Z	readfilesZignored1Zignored2r)r�r�r�test_pipe_cloexec_real_toolsj	s6��



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)rVr'�r�rr�	<genexpr>�	sz6POSIXProcessTestCase.test_close_fds.<locals>.<genexpr>r(�r7rS�pass_fdsz'Some fds not in pass_fds were left open)rr�rr,rqr:r�rNrP�devnullr&�add�set_inheritablerr8rIrJr9r�r�r
r�r%r ra)
rr�r�r$r�r?rc�ignored�
remaining_fds�fds_to_keeprr�r�test_close_fds�	sT���
���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�rz(expected exactly two lines of output:
%rr�rr�r�zSome fds were left open.)rr�rr8rIrJr:r;r9r�rr%r#r�r�r
r$r�r )rr�r?rcZ
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_loweredrJrec
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
��W5QRX|�dt|j��q|dS)Nr�r�r�rerr�Tr�r�zfd to be passed not passedzfd to be closed passedF)rSr�zoverriding close_fds)rr�r�rNrr,rqr:r��updaterr8rIrJr9r�r�r
r�rar ZassertWarnsrsrHrrh�warning)rr�r�r.r�r�r?rcr�r�Z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)	Nr�r�r�TFr�r�z	output=%a)rr�rr,rqr:r�rIrJrr�rhrr8r9r�r�r
r�r%ri)
rr�rkZnon_inheritabler�r1r?rcr�r�rrr�test_pass_fds_inheritable8
s&
�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	�}W5QRXd
d�|�d�D�}|�|dddht|�Bd
|���dS)Nr�r�r�rr�rT)r6r7r&rSr�r�cSsh|]}t|��qSr)r
)r�numrrr�	<setcomp>k
sz@POSIXProcessTestCase.test_pass_fds_redirected.<locals>.<setcomp>r�zoutput=)rr�rNrrPr��O_RDWRrqr:rOr,�insertrr8rIrJr�r�r%�	frozenset)	rr�r�r$r�Zstdout_rZstdout_wrcr�rrr�test_pass_fds_redirectedV
s(�z-POSIXProcessTestCase.test_pass_fds_redirectedc	Cs6t�tjd��}tjt||d�}|��W5QRXdS)N�r+)r7r6�r<rPrr�rr8rr�rZinoutr?rrr�%test_stdout_stdin_are_single_inout_fdp
s�z:POSIXProcessTestCase.test_stdout_stdin_are_single_inout_fdc	Cs6t�tjd��}tjt||d�}|��W5QRXdS)Nr�r�r�r�rrr�&test_stdout_stderr_are_single_inout_fdv
s�z;POSIXProcessTestCase.test_stdout_stderr_are_single_inout_fdc	Cs6t�tjd��}tjt||d�}|��W5QRXdS)Nr�)r&r6r�r�rrr�%test_stderr_stdin_are_single_inout_fd|
s�z:POSIXProcessTestCase.test_stderr_stdin_are_single_inout_fdcCsRtjddd�}tjtj|gtjtjd�}|��\}}|�d|j	d|�
d��dS)Nzsigchild_ignore.pyr�r�r�rz6sigchild_ignore.py exited non-zero with this error:
%srF)rr�rr8rIrJr9r�r%rr�)rZsigchild_ignorer?r7r&rrr�test_wait_when_sigchild_ignored�
s���z4POSIXProcessTestCase.test_wait_when_sigchild_ignoredc	Cszt�d�}tjtjddgtjdd�}|j}|�|j	�z4|�|�d�d�|�
||�|gggd�d�W5|�
�XdS)	Nr�rz$import sys;sys.stdout.write("apple")r)r7rAr�sapplg)r�
import_modulerr8rIrJr9r7rqr:rr%r�rar�)rr�r?r�rrr�test_select_unbuffered�
s

��"z+POSIXProcessTestCase.test_select_unbufferedc	Cs�tjtjddgtjtjd�}|�|jj�|�|jj�t	|�}|j
}t�dt
f��
d}W5QRXtrv|�tj�n|�|dd�tjD��dS)Nrz import sys, time;time.sleep(0.2)r�rcSsg|]}t|��qSr��id�r�orrrr�
szEPOSIXProcessTestCase.test_zombie_fast_process_del.<locals>.<listcomp>)rr8rIrJr9rqr7r:r&r��pidr�check_warnings�ResourceWarningrrbrra)rr?�identr�rrr�test_zombie_fast_process_del�
s
��z1POSIXProcessTestCase.test_zombie_fast_process_delc
Cstjtjddgtjtjd�}|�|jj�|�|jj�t	|�}|j
}t�dt
f��
d}W5QRXt�|tj�tr�|�tj�n|�|dd�tjD��t�d�|�t��&tjttjtjd��}W5QRXW5QRX|�ttj|d�t�r|�tj�n|�|d	d�tjD��dS)
Nrzimport time;time.sleep(3)r�rcSsg|]}t|��qSrr�r�rrrr�
szJPOSIXProcessTestCase.test_leak_fast_process_del_killed.<locals>.<listcomp>r�rcSsg|]}t|��qSrr�r�rrrr�
s)rr8rIrJr9rqr7r:r&r�r�rr�r�rr�r�r]rrbrrar}r~rRrSr��waitpidr{)rr?r�r�ryrrr�!test_leak_fast_process_del_killed�
s8
��
�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��Srt)rrnr�r�rrr�
r�zCPOSIXProcessTestCase.test_close_fds_after_preexec.<locals>.<lambda>)r7rSrr�)rr�rrjrqr:rr8rIrJr9r�r�r�r
r�r{)rr�r?rcr�r�rr�r�test_close_fds_after_preexec�
s

�z1POSIXProcessTestCase.test_close_fds_after_preexeccCs�ddl}t��}z�dd�}t��ddgddgfdgdddgfdgdgddgfdgdgddffD]L\}}}}|�t��0|�||dd	||d
d
d
d
ddd
ddd|�W5QRXq`W5|s�t��XdS)NrcSsdSrrrrrrr�
r�z5POSIXProcessTestCase.test_fork_exec.<locals>.<lambda>�{sexe�envsargTr���r�rr�r�)r"r-r.r/r0rRr�r�)rr"�
gc_enabled�funcr1Zexe_listr��env_listrrr�test_fork_exec�
s@��z#POSIXProcessTestCase.test_fork_execcCs�ddl}Gdd�d�}t��}z�t��ddddd|d	�|d
�ffD]f}|jtd�|�d��6}|�d
gd
gd|ddgddddd	d
ddddd�W5QRX|�	dt
|j��qDW5|s�t��XdS)Nrc@s eZdZdZdd�Zdd�ZdS)zJPOSIXProcessTestCase.test_fork_exec_sorted_fd_sanity_check.<locals>.BadIntTcSs
||_dSr)r�)rr�rrrr�szSPOSIXProcessTestCase.test_fork_exec_sorted_fd_sanity_check.<locals>.BadInt.__init__cSs|jrd|_|jSt�dS)NF)�firstr�r�rrrr�__int__szRPOSIXProcessTestCase.test_fork_exec_sorted_fd_sanity_check.<locals>.BadInt.__int__N)r+r,r-r�r�r�rrrr�BadIntsr�)r�rr�r�re)rhr�)���*l)rer�)r3�r�r(r�rzfds_to_keep={}r�sfalseTr�r�r�r�r�)r"r-r.r/r0rRr�rHr�rarhrY)rr"r�r�r�r[rrr�%test_fork_exec_sorted_fd_sanity_checksN
���z:POSIXProcessTestCase.test_fork_exec_sorted_fd_sanity_checkc
CsRt�t�}|�:tj�|d�� }t|j_|�	�|j�
�W5QRXW5QRXdS)Nr6)rr8rrr��object�BrokenPipeErrorr:r�r�Zassert_called_with�rry�mock_proc_stdinrrr�,test_communicate_BrokenPipeError_stdin_close7s

zAPOSIXProcessTestCase.test_communicate_BrokenPipeError_stdin_closec
Cs`t�t�}|�Htj�|d��.}t|j_|�	d�|j�
d�|j�
�W5QRXW5QRXdS)Nr6�stuff)rr8rrr�r�r�rrr�r��assert_called_once_withr:r�rrr�,test_communicate_BrokenPipeError_stdin_write@s

zAPOSIXProcessTestCase.test_communicate_BrokenPipeError_stdin_writecCs�tjtjdgtjtjd�}|�`tj�|d��F}tt	j
d��.}t|j_
|��|j_|�d�|j��W5QRXW5QRXW5QRXdS)N�-hr�r6�wbr�)rr8rIrJr9rr�r�rPrr�r�r5r�r��return_valuer�r�)rryr�Zdev_nullrrr�,test_communicate_BrokenPipeError_stdin_flushJs�
�
zAPOSIXProcessTestCase.test_communicate_BrokenPipeError_stdin_flushc
Csftjtjdgtjtjd�}|�>tj�|d��$}t|j	_
|jdd�|j	��W5QRXW5QRXdS)Nrr�r6i�rP)
rr8rIrJr9rr�r�r�r:r�r�r�r�rrr�9test_communicate_BrokenPipeError_stdin_close_with_timeoutYs�zNPOSIXProcessTestCase.test_communicate_BrokenPipeError_stdin_close_with_timeoutN�
W_STOPCODEzneed _testcapi.W_STOPCODEc	Cspt}t�|�}|j}t�|d�\}}|�|d�t�d�}t	j
d||fd��|��}W5QRX|�|d�dS)Nrr�r�)r���)rrr8r�rr�r%�	_testcapirrr�r)rr1ryr��statusrrrr�test_stoppedes

z!POSIXProcessTestCase.test_stoppedcCsHtjtjddgtjd�}z|jdd�WdStjk
r@YqXqdS)Nrz+import os, time; os.close(1), time.sleep(2)r�rOrP)rr8rIrJr9r�rS�rryrrr�1test_communicate_repeated_call_after_stdout_closexs
��zFPOSIXProcessTestCase.test_communicate_repeated_call_after_stdout_close)dr+r,r-rr�r�r�r�rr8r�rr�r�rr�r�rr�r�r	rrrrrrr#r)r*r1rIr�r8r?r�rIrJrKrQrXrrYr\r^rarcrfrhrmrorqrsrurvrwrxryr{r|r�r�r�r�r�r�r�r�r�r�r��stat�st_devr�rZrequires_mac_verr�r�r�r�r�r�r�r�r�r�r��cpython_onlyr�r�r�r�rrr�rr�r	r�
__classcell__rrr�rr�Ps�


�


%�



�
-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�wShowWindowrHr)r�STARTF_USESHOWWINDOW�SW_MAXIMIZErArrr�test_startupinfo�s�z%Win32ProcessTestCase.test_startupinfocCs(d}d}tj||d�}tjt|d�dS)Nr�r�)rrr@)rrrHr)rZSTARTF_USERSHOWWINDOWrrArrr�test_startupinfo_keywords�s��z.Win32ProcessTestCase.test_startupinfo_keywordscCs�t��}tj|_tj|_td�D]�}t}tt	j
d��@}tj||tj|d�}|�|�
�W5QRX|�|jd�W5QRX|�|jtj�|�|j�|�|j�|�|j�|�|jtj�|�|jdgi�q dS)Nrr:)r7r&rAr�handle_list)rrrrZSW_HIDErrNrrPrr�r8rir�r%rrbZ	hStdInputZ
hStdOutputZ	hStdError�lpAttributeList)rrAr$rZnullryrrr�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)"rB)rIr&rrrrHrJ)rZCREATE_NEW_CONSOLErrr�test_creationflags�s��z'Win32ProcessTestCase.test_creationflagscCs$|jttjtjddgdd�d�dS)NrrEcSsdSrtrrrrrr�r�z8Win32ProcessTestCase.test_invalid_args.<locals>.<lambda>r!rDrrrrr��s
��z&Win32ProcessTestCase.test_invalid_argsc	Cs:Gdd�dt�}|�t��tjt|�d�W5QRXdS)Nc@seZdZdZdS)z4Win32ProcessTestCase.test_issue31471.<locals>.BadEnvN)r+r,r-�keysrrrr�BadEnv�srr�)�dictrRr�rr8r)rrrrr�test_issue31471�sz$Win32ProcessTestCase.test_issue31471cCs&tjtjddgdd�}|�|d�dS)NrrET)rSrFrGrKrrrr��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��dt��}	d
|dd�i|	_tj	t
jdd�|d�gtj
tj
|	dd�}|��\}}|�|jd�W5QRXdS)
Nrr�Trz2import msvcrt; print(msvcrt.open_osfhandle({}, 0))Fr�)r7r&rSsOSErrorr)r7r&rArSz.*overriding close_fds)rvrr,rqr:r�rOZ
get_osfhandlerr8rIrJrHr9r�r%rr
r$rarrrr�rs)
rrvr�rWr�r?r7r&rrArrr�test_close_fds_with_stdio�sl
��
��

��
��z.Win32ProcessTestCase.test_close_fds_with_stdiocCs t��}i|_tjt|d�dS)Nr@�rrrrHr�rrArrr�test_empty_attribute_lists
�z.Win32ProcessTestCase.test_empty_attribute_listcCs$t��}dgi|_tjt|d�dS)Nrr@r"r#rrr�test_empty_handle_lists

�z+Win32ProcessTestCase.test_empty_handle_listc	CsNtj��}d|d<tjdgdtj|d�}|�|�d|j���W5QRXdS�N�physalisr�r�r�rFsphysalis�	rr�r�rr8r9rar7r�rHrrrrIs

�z(Win32ProcessTestCase.test_shell_sequencec	CsLtj��}d|d<tjddtj|d�}|�|�d|j���W5QRXdSr&r(rHrrrrJ's
�z&Win32ProcessTestCase.test_shell_stringc
CsZdD]P}tj��}d|d<tjddtj||d�}|�|�d|j��|�W5QRXqdS)N)�ansiZoemr'r�r�r�)r�r7r�r?r()r�encr�r?rrr�test_shell_encodings1s
�z)Win32ProcessTestCase.test_shell_encodingscCs t�tjd�}|�|d�dS)Nz -c "import sys; sys.exit(47)"rFrGrKrrrrK=s�z%Win32ProcessTestCase.test_call_stringc	Gsztjtjddgtjtjtjd�}|�@|j�d�t||�|�|��\}}|�	|d�|�
�}W5QRX|�|d�dS)Nrz�if 1:
                             import sys, time
                             sys.stdout.write('x\n')
                             sys.stdout.flush()
                             time.sleep(30)
                             r5r�r�r)rr8rIrJr9r7r�rVr�r*rr)rrWr1r?r$r&rrrrrXCs�	z"Win32ProcessTestCase._kill_processc	Gs�tjtjddgtjtjtjd�}|�J|j�d�t�d�t	||�|�|�
�\}}|�|d�|��}W5QRX|�
|d�dS)Nrz�if 1:
                             import sys, time
                             sys.stdout.write('x\n')
                             sys.stdout.flush()
                             sys.exit(42)
                             r5r�r�r�)rr8rIrJr9r7r�r}r~rVr�r*rr%)rrWr1r?r$r&rLrrrrYXs�	
z'Win32ProcessTestCase._kill_dead_processcCs|�dtj�dSrb)rXr�r`rrrrr\osz%Win32ProcessTestCase.test_send_signalcCs|�d�dSrd�rXrrrrr^rszWin32ProcessTestCase.test_killcCs|�d�dSrgr,rrrrrausz#Win32ProcessTestCase.test_terminatecCs|�dtj�dSrb)rYr�r`rrrrrcxsz*Win32ProcessTestCase.test_send_signal_deadcCs|�d�dSrdrerrrrrf{sz#Win32ProcessTestCase.test_kill_deadcCs|�d�dSrgrerrrrrh~sz(Win32ProcessTestCase.test_terminate_deadN)r+r,r-rrrrr�rrr r�r!r$r%rIrJr+rKrXrYr\r^rarcrfrhrrrrr�s.
1

rc@s^eZdZGdd�dej�Zej�ejd�dd��Z	dd�Z
dd	�Zd
d�Zdd
�Z
dd�ZdS)�	MiscTestscs eZdZgZ�fdd�Z�ZS)zMiscTests.RecordingPopencst�j||�|j�|�dSr)r�r��instances_createdrOr0r�rrr��sz!MiscTests.RecordingPopen.__init__)r+r,r-r.r�rrrr�r�RecordingPopen�sr/�_communicatecKs8t|_z�tj	�
tjd���}t|_tj	�
td|j��:|�
t��$|tjddgfdtji|��W5QRXW5QRX|jdd�D]"}|�|tjdd�d|j���q�g}|jD]}|tjd	d�kr�|�|�q�|j|jd
|jd�|jt|�d|jd�W5QRXW5|jj��}|��|��|�g|jj�XdS)N�_waitr8rzLimport time
time.sleep(9)
import sys
sys.stderr.write('\n!runaway child!\n')r7r�rPz.no open-ended wait() after the first allowed: g�?rr�)�KeyboardInterruptr�r/r.rVr�rr%rr�r�rr8rRrIrJrZcall_args_listrrHrOZassertLessEqualZ
call_countr#)rZpopenerZmock__communicater2r�Z
mock__waitrHZsigint_callsrrr�_test_keyboardinterrupt_no_kill�sF
����

�

��z)MiscTests._test_keyboardinterrupt_no_killcCs|jtjdd�dS�NgT㥛� @rP)r3rrHrrrr�#test_call_keyboardinterrupt_no_kill�sz-MiscTests.test_call_keyboardinterrupt_no_killcCs|jtjdd�dSr4)r3rrrrrr�"test_run_keyboardinterrupt_no_kill�sz,MiscTests.test_run_keyboardinterrupt_no_killcCsdd�}|�|�dS)Nc	_s tj||��
}t�W5QRXdSr)rr8r2)r1r2Zunused_processrrr�popen_via_context_manager�sz[MiscTests.test_context_manager_keyboardinterrupt_no_kill.<locals>.popen_via_context_manager)r3)rr7rrr�.test_context_manager_keyboardinterrupt_no_kill�sz8MiscTests.test_context_manager_keyboardinterrupt_no_killc	Cs�|�t�d�d�|�t�d�d�d}z@t��}tj�	|d�}t�t
rLdnd|�\}}|�|d�W5|dk	r|t�|�XdS)Nz
echo xyzzy�xyzzy)rr9Zfooztype zcat r)r%r�	getoutput�getstatusoutputr�rmdirrorLr�r�rr)r�dirrOrrcrrr�test_getoutput�s��zMiscTests.test_getoutputcCsnddh}ttj�}t�}ddl}tj��D].\}}|�d�r>q*t||jf�rNq*|�	|�q*|�
|||�dS)Nr_�Handlerr$)r�r�__all__�types�__dict__�itemsr�
isinstance�
ModuleTyper�r%)rZintentionally_excludedZexportedZpossible_exportsrArOr�rrr�test__all__�s

zMiscTests.test__all__N)r+r,r-rr8r/rr�r�r3r5r6r8r>rFrrrrr-�s
+r-�PollSelectorz!Test needs selectors.PollSelectorc@seZdZdd�Zdd�ZdS)�ProcessTestCaseNoPollcCstj|_tjt_t�|�dSr)r�_PopenSelector�
orig_selector�	selectors�SelectSelectorr4rrrrrr�szProcessTestCaseNoPoll.setUpcCs|jt_t�|�dSr)rJrrIr4r"rrrrr"�szProcessTestCaseNoPoll.tearDownN)r+r,r-rr"rrrrrH�srHzWindows-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�rror�rr=rrrr:)rr�r=r�rrr�s


zCommandsWithSpaces.setUpcst�|j�t���dSr)rr�r=r�r"rr�rrr"�szCommandsWithSpaces.tearDownc	OsHtj|d<tj||�}|�$|�|j���d�d|j�W5QRXdS)Nr7�mbcsz2 [%r, 'ab cd'])rr9r8r%r7r�r�r=)rr1r2r?rrr�with_spaces
s
�zCommandsWithSpaces.with_spacescCs |jdtj|jdfdd�dS)N�"%s" "%s" "%s"�ab cdr�r��rOrIrJr=rrrr�test_shell_string_with_spaces
s
��z0CommandsWithSpaces.test_shell_string_with_spacescCs|jtj|jdgdd�dS)NrQr�r�rRrrrr�test_shell_sequence_with_spaces
sz2CommandsWithSpaces.test_shell_sequence_with_spacescCs|�dtj|jdf�dS)NrPrQrRrrrr�test_noshell_string_with_spaces
s�z2CommandsWithSpaces.test_noshell_string_with_spacescCs|�tj|jdg�dS)NrQrRrrrr�!test_noshell_sequence_with_spaces
sz4CommandsWithSpaces.test_noshell_sequence_with_spaces)r+r,r-rr"rOrSrTrUrVrrrr�rrM�s		rMc@s4eZdZdd�Zdd�Zdd�Zdd�Zd	d
�ZdS)�ContextManagerTestsc	Csltjtjddgtjtjd��*}|�|j��d�|�|j	��d�W5QRX|�
|jj�|�
|j	j�dS)NrzAimport sys;sys.stdout.write('stdout');sys.stderr.write('stderr');r�sstdoutsstderr)rr8rIrJr9r%r7r�r*r&r&r'r
rrr�	test_pipe 
s
��zContextManagerTests.test_pipec	Cs0t�tjddg��}W5QRX|�|jd�dS)Nrzimport sys; sys.exit(100)�d)rr8rIrJr%rr
rrrr�-
s
�
z#ContextManagerTests.test_returncodec	Cs@tjtjddgtjd��}|�d�|�|jd�W5QRXdS)Nrz2import sys;sys.exit(sys.stdin.read() == 'context')rmscontextr�rr
rrrr4
s
��
z*ContextManagerTests.test_communicate_stdinc
Cs:|�t��&tjttjtjd��}W5QRXW5QRXdS)Nr�)rRr�rr8r�r9r
rrrr�<
s�z%ContextManagerTests.test_invalid_argscCsrtjttjtjdd�}|��}|j�dtj�|�	|j
�|�t|j
ddd�|�|j
d�|�|jj�dS)Nr)r6rAr�r)rr8rr9rr-�	__enter__r6rrrbrrRrS�__exit__r%r&r'r
rrr�test_broken_pipe_cleanupC
s�z,ContextManagerTests.test_broken_pipe_cleanupN)r+r,r-rXr�rr�r\rrrrrW
s

rW�__main__)9r�rr�rrrIr�r<r�rrTror}r�rKr�r�r
r�r-r:Ztest.supportrrrxZPGOZSkipTestr�rr4r�r��NotADirectoryError�PermissionErrorr�rJrrZTestCaser�	Exceptionr.r8r/r4r�r�r�r�rr-r�rHrMrWr+�mainrrrr�<module>s�




F0
=
}d�
+6


F1le Man4ger