403Webshell
Server IP : 118.27.122.248  /  Your IP : 216.73.217.130
Web Server : Apache
System : Linux web0264.sh.tyo1 4.18.0-553.79.1.lve.el7h.x86_64 #1 SMP Wed Oct 15 16:34:46 UTC 2025 x86_64
User : c9415830 ( 11735)
PHP Version : 8.4.17
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : OFF  |  Pkexec : OFF
Directory :  /opt/alt/python36/lib64/python3.6/test/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /opt/alt/python36/lib64/python3.6/test/__pycache__/test_subprocess.cpython-36.opt-2.pyc
3

�
�^E�@sdddlZddlmZddlmZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlZddlZddlZddlZddlmZyddlZWnek
r�dZYn
XddlZyddlZWnek
r�dZYnXyddlZWnek
�r&dZYnXej�r:ejd��ejdkZe�rPdZndZd&Ze e!e"fZ#Gd
d�dej$�Z%Gdd
�d
e&�Z'Gdd�dej(�Z)Gdd�de%�Z*Gdd�de%�Z+ej,ed�Gdd�de%��Z-ej.ed�Gdd�de%��Z/Gdd�dej$�Z0ej.e1ed�d�Gdd�de*��Z2ej.ed �Gd!d"�d"e%��Z3Gd#d$�d$e%�Z4e5d%k�r`ej6�dS)'�N)�mock)�support)�FakePathztest is not helpful for PGO�win32z@import msvcrt; msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY);��nonexisting_i_hopec@s&eZdZdd�Zdd�Zddd�ZdS)	�BaseTestCasecCstj�dS)N)r�
reap_children)�self�r�4/opt/alt/python36/lib64/python3.6/test_subprocess.py�setUp<szBaseTestCase.setUpcCsBxtjD]}|j�qWtj�|jtjd�|j�tj�dS)Nzsubprocess._active not empty)�
subprocess�_active�waitZ_cleanup�assertFalseZ
doCleanupsrr	)r
Zinstrrr�tearDownAszBaseTestCase.tearDownNcCs$tj|�}|j�}|j|||�dS)N)r�strip_python_stderr�strip�assertEqual)r
�stderr�expected�msg�actualrrr�assertStderrEqualIs
zBaseTestCase.assertStderrEqual)N)�__name__�
__module__�__qualname__r
rrrrrrr;src@seZdZdS)�PopenTestExceptionN)rrrrrrrrSsrc@seZdZdd�ZdS)�PopenExecuteChildRaisescOstd��dS)NzForced Exception for Test)r)r
�args�kwargsrrr�_execute_child[sz&PopenExecuteChildRaises._execute_childN)rrrr"rrrrrWsrc@seZdZdd�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,�Zejed-�d.d/��Zd0d1�Zd2d3�Zd4d5�Zd6d7�Z d8d9�Z!ejed:�d;d<��Z"ejed:�d=d>��Z#d?d@�Z$eje%j&e%j'kdA�dBdC��Z(eje%j&e%j'kdA�eje)j*�dD�dEdF���Z+dGdH�Z,dIdJ�Z-dKdL�Z.dMdN�Z/dOdP�Z0dQdR�Z1dSdT�Z2dUdV�Z3dWdX�Z4dYdZ�Z5d[d\�Z6d]d^�Z7d_d`�Z8dadb�Z9dcdd�Z:dedf�Z;dgdh�Z<eje%j=dikdj�eje)j>dk�dlk	dm�dndo���Z?dpdq�Z@drds�ZAdtdu�ZBdvdw�ZCdxdy�ZDdzd{�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��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_eje`dlkd��d�d���Zaeje`dlkd��d�d���Zbd�d��Zcd�d��Zdd�d��Zed�d��Zfejgeheid��d��ejgehejd��d��ejgehejd��dƒd�dĄ���Zkejgejjljmd�ejjn��dƃd�dȄ�ZoejedɃd�d˄�ZpejedɃd�d̈́�ZqdlS)��ProcessTestCasec
Cs�tjtjddgtjtjtjd�}z4|j|jtj�|j|j	tj�|j|j
tj�Wd|jj�|j	j�|j
j�|j�XdS)Nz-czimport sys; sys.exit(0))�stdin�stdoutr)
r�Popen�sys�
executable�PIPE�assertIsInstancer$�io�BufferedIOBaser%r�closer)r
�prrr�test_io_buffered_by_defaultas



z+ProcessTestCase.test_io_buffered_by_defaultc
Cs�tjtjddgtjtjtjdd�}z4|j|jtj�|j|j	tj�|j|j
tj�Wd|jj�|j	j�|j
j�|j�XdS)Nz-czimport sys; sys.exit(0)r)r$r%r�bufsize)
rr&r'r(r)r*r$r+�	RawIOBaser%rr-r)r
r.rrr�test_io_unbuffered_worksos


z(ProcessTestCase.test_io_unbuffered_workscCs"tjtjddg�}|j|d�dS)Nz-czimport sys; sys.exit(47)�/)r�callr'r(r)r
�rcrrr�
test_call_seq}s
zProcessTestCase.test_call_seqcCs"|jtjtjtjddgdd�dS)Nz-czwhile True: passg�������?)�timeout)�assertRaisesr�TimeoutExpiredr4r'r()r
rrr�test_call_timeout�s
z!ProcessTestCase.test_call_timeoutcCs"tjtjddg�}|j|d�dS)Nz-czimport sys; sys.exit(0)r)r�
check_callr'r(r)r
r5rrr�test_check_call_zero�s
z$ProcessTestCase.test_check_call_zerocCs>|jtj��}tjtjddg�WdQRX|j|jjd�dS)Nz-czimport sys; sys.exit(47)r3)	r8r�CalledProcessErrorr;r'r(r�	exception�
returncode)r
�crrr�test_check_call_nonzero�s
z'ProcessTestCase.test_check_call_nonzerocCs"tjtjddg�}|jd|�dS)Nz-cz
print('BDFL')sBDFL)r�check_outputr'r(�assertIn)r
�outputrrr�test_check_output�sz!ProcessTestCase.test_check_outputcCs>|jtj��}tjtjddg�WdQRX|j|jjd�dS)Nz-czimport sys; sys.exit(5)�)	r8rr=rBr'r(rr>r?)r
r@rrr�test_check_output_nonzero�sz)ProcessTestCase.test_check_output_nonzerocCs(tjtjddgtjd�}|jd|�dS)Nz-cz$import sys; sys.stderr.write('BDFL'))rsBDFL)rrBr'r(�STDOUTrC)r
rDrrr�test_check_output_stderr�s

z(ProcessTestCase.test_check_output_stderrcCsNtj�}|j|j�|jd�|jd�tjtj	ddg|d�}|j
d|�dS)Nspearrz-cz6import sys; sys.stdout.write(sys.stdin.read().upper()))r$sPEAR)�tempfile�
TemporaryFile�
addCleanupr-�write�seekrrBr'r(rC)r
�tfrDrrr�test_check_output_stdin_arg�s

z+ProcessTestCase.test_check_output_stdin_argcCs&tjtjddgdd�}|jd|�dS)Nz-cz6import sys; sys.stdout.write(sys.stdin.read().upper())spear)�inputsPEAR)rrBr'r(rC)r
rDrrr�test_check_output_input_arg�s
z+ProcessTestCase.test_check_output_input_argcCsP|jt��(}tjtjddgtjd�}|jd�WdQRX|jd|j	j
d�dS)Nz-czprint('will not be run'))r%z-Expected ValueError when stdout arg supplied.r%r)r8�
ValueErrorrrBr'r(r%�failrCr>r )r
r@rDrrr�test_check_output_stdout_arg�s

z,ProcessTestCase.test_check_output_stdout_argcCs�tj�}|j|j�|jd�|jd�|jt��(}tj	t
jddg|dd�}|jd�WdQRX|j
d|jjd�|j
d	|jjd�dS)
Nspearrz-czprint('will not be run')share)r$rQz7Expected ValueError when stdin and input args supplied.r$rQ)rJrKrLr-rMrNr8rSrrBr'r(rTrCr>r )r
rOr@rDrrr�&test_check_output_stdin_with_input_arg�s



z6ProcessTestCase.test_check_output_stdin_with_input_argcCsL|jtj��&}tjtjddgdd�}|jd�WdQRX|j|jj	d�dS)Nz-czMimport sys, time
sys.stdout.write('BDFL')
sys.stdout.flush()
time.sleep(3600)�)r7zExpected TimeoutExpired.sBDFL)
r8rr9rBr'r(rTrr>rD)r
r@rDrrr�test_check_output_timeout�sz)ProcessTestCase.test_check_output_timeoutcCs8tjj�}d|d<tjtjddg|d�}|j|d�dS)N�banana�FRUITz-cz5import sys, os;sys.exit(os.getenv("FRUIT")=="banana"))�env�)�os�environ�copyrr4r'r(r)r
�newenvr5rrr�test_call_kwargs�s

z ProcessTestCase.test_call_kwargscCshtj��F}|jttjdd�tjjjj}dg|d}|jttjf|��WdQRX|j	|j
�d�dS)Nr\)Zinvalid_arg_namerr)rZcaptured_stderrr8�	TypeErrorrr&�__init__�__code__�co_argcountr�getvalue)r
�sZargcountZ
too_many_argsrrr�test_invalid_args�s
z!ProcessTestCase.test_invalid_argscCsRtjtjddgtjtjd�}|j|jj�|j|jj�|j	�|j
|jd�dS)Nz-czprint("banana"))r%r)rr&r'r(r)rLr%r-rrrr$)r
r.rrr�test_stdin_none�szProcessTestCase.test_stdin_nonecCsld}tjtjd|gtjtjd�}|j|jj�|j|jj�|j	�\}}|j
|jd|�|j
|j�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;z-c)r%rrstest_stdout_none)
rr&r'r(r)rLr%r-r�communicaterr?�rstrip)r
�coder.�out�errrrr�test_stdout_nonesz ProcessTestCase.test_stdout_nonecCsRtjtjddgtjtjd�}|j|jj�|j|jj�|j	�|j
|jd�dS)Nz-czprint("banana"))r$r%)rr&r'r(r)rLr%r-r$rrr)r
r.rrr�test_stderr_nonesz ProcessTestCase.test_stderr_nonecKs2|dg}tj|f|�}|j�|jd|j�dS)Nzimport sys; sys.exit(47)r3)rr&rrr?)r
�pre_argsr!r r.rrr�_assert_python$s
zProcessTestCase._assert_pythoncCs0tjjtjjtj�d�}|j|dgtjd�dS)N�doesnotexistz-c)r()r]�path�join�dirnamer'r(rr)r
rsrrr�test_executable,szProcessTestCase.test_executablecCs0tjdg}|j|�|jt|j|tdd�dS)Nz-cr)r()r'r(rrr8�NONEXISTING_ERRORS�NONEXISTING_CMD)r
rqrrr� test_executable_takes_precedence7s


z0ProcessTestCase.test_executable_takes_precedencez"executable argument replaces shellcCs|jgtjdd�dS)NT)r(�shell)rrr'r()r
rrr�test_executable_replaces_shellAsz.ProcessTestCase.test_executable_replaces_shellc	Cstj|��
tj�SQRXdS)N)rZ
change_cwdr]�getcwd)r
�cwdrrr�_normalize_cwdHszProcessTestCase._normalize_cwdcCstjjtj�}tjj|�S)N)r]rt�realpathr'r(�split)r
Zpython_pathrrr�_split_python_pathPsz"ProcessTestCase._split_python_pathcKsntj|ddgfdtji|��}|j|jj�|j�|jd|j�t	j
j}|j||�||jj�j
d���dS)Nz-cz;import os, sys; sys.stdout.write(os.getcwd()); sys.exit(47)r%r3zutf-8)rr&r)rLr%r-rrr?r]rt�normcase�read�decode)r
Zexpected_cwdZ
python_argr!r.r�rrr�_assert_cwdVs
zProcessTestCase._assert_cwdcCs(tj�}|j|�}|j|tj|d�dS)N)r~)rJ�
gettempdirrr�r'r()r
�temp_dirrrr�test_cwdgs
zProcessTestCase.test_cwdcCs,tj�}|j|�}|j|tjt|�d�dS)N)r~)rJr�rr�r'r(r)r
r�rrr�test_cwd_with_pathlikems
z&ProcessTestCase.test_cwd_with_pathlikez"pending resolution of issue #15533c
Csv|j�\}}tjjtj|�}tj��H}|jtt	j
|g�|jtt	j
|g|d�|j|�}|j|||d�WdQRXdS)N)r~)
r�r]rtru�curdirr�temp_cwdr8�FileNotFoundErrorrr&rr�)r
�
python_dir�python_base�
rel_python�	wrong_dirrrr�test_cwd_with_relative_argrs



z*ProcessTestCase.test_cwd_with_relative_argcCs�|j�\}}tjjtj|�}d}tj��P}|jtt	j
|g|d�|jtt	j
|g||d�|j|�}|j||||d�WdQRXdS)N�somethingyoudonthave)r()r(r~)
r�r]rtrur�rr�r8r�rr&rr�)r
r�r�r�Zdoesntexistr�rrr�!test_cwd_with_relative_executable�s




z1ProcessTestCase.test_cwd_with_relative_executablec
Csr|j�\}}tjj||�}tjjtj|�}tj��6}|jtt	j
|g|d�|j|�}|j|||d�WdQRXdS)N)r~)
r�r]rtrur�rr�r8r�rr&rr�)r
r�r�Z
abs_pythonr�r�rrr�test_cwd_with_absolute_arg�s


z*ProcessTestCase.test_cwd_with_absolute_argzTest is not venv-compatiblecCs.|j�\}}|j|�}|j|dtj|d�dS)Nr�)r(r~)r�rr�r'r()r
r�r�rrr�test_executable_with_cwd�s
z(ProcessTestCase.test_executable_with_cwdz#need an installed Python. See #7774cCs|jtj�dtjd�dS)Nr�)r()r�r]r}r'r()r
rrr�test_executable_without_cwd�sz+ProcessTestCase.test_executable_without_cwdcCsHtjtjddgtjd�}|jjd�|jj�|j�|j	|j
d�dS)Nz-cz0import sys; sys.exit(sys.stdin.read() == "pear"))r$spearr\)rr&r'r(r)r$rMr-rrr?)r
r.rrr�test_stdin_pipe�s


zProcessTestCase.test_stdin_pipecCsftj�}|j|j�|j�}tj|d�tj|dd�tj	t
jddg|d�}|j�|j
|jd�dS)Nspearrz-cz0import sys; sys.exit(sys.stdin.read() == "pear"))r$r\)rJrKrLr-�filenor]rM�lseekrr&r'r(rrr?)r
rO�dr.rrr�test_stdin_filedes�s
z"ProcessTestCase.test_stdin_filedescCsXtj�}|j|j�|jd�|jd�tjtj	ddg|d�}|j
�|j|jd�dS)Nspearrz-cz0import sys; sys.exit(sys.stdin.read() == "pear"))r$r\)
rJrKrLr-rMrNrr&r'r(rrr?)r
rOr.rrr�test_stdin_fileobj�s


z"ProcessTestCase.test_stdin_fileobjc
Cs>tjtjddgtjd�}|�|j|jj�d�WdQRXdS)Nz-cz&import sys; sys.stdout.write("orange"))r%sorange)rr&r'r(r)rr%r�)r
r.rrr�test_stdout_pipe�s


z ProcessTestCase.test_stdout_pipecCs`tj�}|j|j�|j�}tjtjddg|d�}|j	�t
j|dd�|jt
j
|d�d�dS)Nz-cz&import sys; sys.stdout.write("orange"))r%risorange)rJrKrLr-r�rr&r'r(rr]r�rr�)r
rOr�r.rrr�test_stdout_filedes�s
z#ProcessTestCase.test_stdout_filedescCsPtj�}|j|j�tjtjddg|d�}|j�|j	d�|j
|j�d�dS)Nz-cz&import sys; sys.stdout.write("orange"))r%rsorange)rJrKrLr-rr&r'r(rrNrr�)r
rOr.rrr�test_stdout_fileobj�s

z#ProcessTestCase.test_stdout_fileobjc
Cs>tjtjddgtjd�}|�|j|jj�d�WdQRXdS)Nz-cz*import sys; sys.stderr.write("strawberry"))rs
strawberry)rr&r'r(r)rrr�)r
r.rrr�test_stderr_pipe�s


z ProcessTestCase.test_stderr_pipecCs`tj�}|j|j�|j�}tjtjddg|d�}|j	�t
j|dd�|jt
j
|d�d�dS)Nz-cz*import sys; sys.stderr.write("strawberry"))rris
strawberry)rJrKrLr-r�rr&r'r(rr]r�rr�)r
rOr�r.rrr�test_stderr_filedes�s
z#ProcessTestCase.test_stderr_filedescCsPtj�}|j|j�tjtjddg|d�}|j�|j	d�|j
|j�d�dS)Nz-cz*import sys; sys.stderr.write("strawberry"))rrs
strawberry)rJrKrLr-rr&r'r(rrNrr�)r
rOr.rrr�test_stderr_fileobjs

z#ProcessTestCase.test_stderr_fileobjcCsRtjtjddgtjtjd�}|j�\}}|j|d�|j|d�|j|jd�dS)Nz-cz�import sys, subprocess;rc = subprocess.call([sys.executable, "-c",    "import sys;"    "sys.stderr.write('42')"],    stderr=subprocess.STDOUT);sys.exit(rc))r%rs42�r)	rr&r'r(r)rjrrr?)r
r.r%rrrr�,test_stderr_redirect_with_no_stdout_redirects

z<ProcessTestCase.test_stderr_redirect_with_no_stdout_redirectc
CsBtjtjddgtjtjd�}|�|j|jj�d�WdQRXdS)Nz-czRimport sys;sys.stdout.write("apple");sys.stdout.flush();sys.stderr.write("orange"))r%rsappleorange)	rr&r'r(r)rHrr%r�)r
r.rrr�test_stdout_stderr_pipe+s

z'ProcessTestCase.test_stdout_stderr_pipecCsRtj�}|j|j�tjtjddg||d�}|j�|j	d�|j
|j�d�dS)Nz-czRimport sys;sys.stdout.write("apple");sys.stdout.flush();sys.stderr.write("orange"))r%rrsappleorange)rJrKrLr-rr&r'r(rrNrr�)r
rOr.rrr�test_stdout_stderr_file7s

z'ProcessTestCase.test_stdout_stderr_filecCsld}tjtjd|gtjtjd�}|j|jj�|j|jj�|j	�\}}|j
|jd|�|j
|j�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 == 18z-c)r%rrstest with stdout=1)
rr&r'r(r)rLr%r-rrjrr?rk)r
rlr.rmrnrrr�test_stdout_filedes_of_stdoutFsz-ProcessTestCase.test_stdout_filedes_of_stdoutcCs2tjtjddgtjd�}|j�|j|jd�dS)Nz-cz'for i in range(10240):print("x" * 1024))r%)rr&r'r(�DEVNULLrrr%)r
r.rrr�test_stdout_devnullYs


z#ProcessTestCase.test_stdout_devnullcCs2tjtjddgtjd�}|j�|j|jd�dS)Nz-cz=import sys
for i in range(10240):sys.stderr.write("x" * 1024))r)rr&r'r(r�rrr)r
r.rrr�test_stderr_devnullas


z#ProcessTestCase.test_stderr_devnullcCs2tjtjddgtjd�}|j�|j|jd�dS)Nz-czimport sys;sys.stdin.read(1))r$)rr&r'r(r�rrr$)r
r.rrr�test_stdin_devnulljs


z"ProcessTestCase.test_stdin_devnullc
CsTtjj�}d|d<tjtjddgtj|d��}|j�\}}|j	|d�WdQRXdS)N�orangerZz-cz2import sys,os;sys.stdout.write(os.getenv("FRUIT")))r%r[sorange)
r]r^r_rr&r'r(r)rjr)r
r`r.r%rrrr�test_envrs


zProcessTestCase.test_envrz#cannot test an empty env on Windows�Py_ENABLE_SHAREDNz=the python library cannot be loaded with an empty environmentcCsFtjtjddgtjid��"}|j�\}}|j|j�d�WdQRXdS)Nz-cz)import os; print(list(os.environ.keys())))r%r[�[]�['__CF_USER_TEXT_ENCODING'])r�r�)rr&r'r(r)rjrCr)r
r.r%rrrr�test_empty_envs


zProcessTestCase.test_empty_envcCs\tjd}|jt��tj|ddg�WdQRX|jt��tjtjddg�WdQRXdS)N�z-c�passzpass#)r'r(r8rSrr&)r
�cmdrrr�test_invalid_cmd�s

z ProcessTestCase.test_invalid_cmdcCstjj�}d|d<|jt��tjtjddg|d�WdQRXtjj�}d|d<|jt��tjtjddg|d�WdQRXtjj�}d|d	<|jt��tjtjddg|d�WdQRXtjj�}d
|d<tjtjddgtj	|d��}|j
�\}}|j|d
�WdQRXdS)NZcabbagezFRUITVEGETABLEz-cr�)r[zorangeVEGETABLE=cabbagerZZlemonzFRUIT=ORANGEzorange=lemonz3import sys, os;sys.stdout.write(os.getenv("FRUIT")))r%r[sorange=lemon)r]r^r_r8rSrr&r'r(r)rjr)r
r`r.r%rrrr�test_invalid_env�s(
 
 
 


z ProcessTestCase.test_invalid_envcCs4tjtjddgtjd�}|jd�|j|jd�dS)Nz-cz/import sys;sys.exit(sys.stdin.read() == "pear"))r$spearr\)rr&r'r(r)rjrr?)r
r.rrr�test_communicate_stdin�s



z&ProcessTestCase.test_communicate_stdincCs@tjtjddgtjd�}|j�\}}|j|d�|j|d�dS)Nz-cz)import sys; sys.stdout.write("pineapple"))r%s	pineapple)rr&r'r(r)rjr)r
r.r%rrrr�test_communicate_stdout�s

z'ProcessTestCase.test_communicate_stdoutcCs@tjtjddgtjd�}|j�\}}|j|d�|j|d�dS)Nz-cz)import sys; sys.stderr.write("pineapple"))rs	pineapple)rr&r'r(r)rjrr)r
r.r%rrrr�test_communicate_stderr�s

z'ProcessTestCase.test_communicate_stderrcCsttjtjddgtjtjtjd�}|j|jj�|j|jj�|j|j	j�|j
d�\}}|j|d�|j|d�dS)Nz-czNimport sys,os;sys.stderr.write("pineapple");sys.stdout.write(sys.stdin.read()))r$r%rsbananas	pineapple)
rr&r'r(r)rLr%r-rr$rjrr)r
r.r%rrrr�test_communicate�s

z ProcessTestCase.test_communicatecCsdtjtjddgdtjtjtjd�}|jtj|jddd�|j�\}}|j|d�|j	|j
�d�dS)	Nz-cz~import sys,os,time;sys.stderr.write("pineapple\n");time.sleep(1);sys.stderr.write("pear\n");sys.stdout.write(sys.stdin.read())T)�universal_newlinesr$r%rrYg333333�?)r7spineapple
pear
)rr&r'r(r)r8r9rjrr�encode)r
r.r%rrrr�test_communicate_timeout�s

z(ProcessTestCase.test_communicate_timeoutcCsLtjtjddgtjd�}|jtj|jdd�|j�\}}|jt	|�d
�dS)Nz-cz�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�������?)r7��@i�i)
rr&r'r(r)r8r9rjr�len)r
r.r%�_rrr�%test_communicate_timeout_large_output�s

z5ProcessTestCase.test_communicate_timeout_large_outputcCs�x�dD]�}x�d	D]�}x�d
D]�}i}|r0tj|d<|r>tj|d<|rLtj|d<|sRqtjtjddff|�}|j�|jdk	r�|j|jj�|j	dk	r�|j|j	j�|j
dk	r|j|j
j�qWqWqWdS)NFTr$r%rz-cr�)FT)FT)FT)rr)r&r'r(rjr$�
assertTrue�closedr%r)r
Z
stdin_pipeZstdout_pipeZstderr_pipe�optionsr.rrr�test_communicate_pipe_fd_leaks(








z-ProcessTestCase.test_communicate_pipe_fd_leakcCs:tjtjddg�}|j�\}}|j|d�|j|d�dS)Nz-czimport sys; sys.exit(47))rr&r'r(rjr)r
r.r%rrrr�test_communicate_returnss

z(ProcessTestCase.test_communicate_returnscCs�tj�\}}tj|�tj|�tjtjddtjgtj	tj	tj	d�}|j
|jj�|j
|jj�|j
|j
j�dtj}|j|�\}}|j||�dS)Nz-czpimport sys,os;sys.stdout.write(sys.stdin.read(47));sys.stderr.write("x" * %d);sys.stdout.write(sys.stdin.read()))r$r%r�a)r]�piper-rr&r'r(r�
PIPE_MAX_SIZEr)rLr%rr$rjr)r
�x�yr.Zstring_to_writer%rrrr�test_communicate_pipe_buf"s




z)ProcessTestCase.test_communicate_pipe_bufcCs�tjtjddgtjtjtjd�}|j|jj�|j|jj�|j|j	j�|j	j
d�|jd�\}}|j|d�|j
|d�dS)Nz-cz0import sys,os;sys.stdout.write(sys.stdin.read()))r$r%rsbananassplitsbananasplitr�)rr&r'r(r)rLr%r-rr$rMrjrr)r
r.r%rrrr�test_writes_before_communicate9s

z.ProcessTestCase.test_writes_before_communicatec
Cs�tjtjddtdgtjtjdd�}|��|jjd�|jj�|j	|j
j�d�|jjd�|jj�|j
|j
j�|j	|j
j�d�|j	|j
jd	�d�|j	|j
j�d
�WdQRXdS)Nz-czimport 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$r%r�zline1
zline3
zline2
�zline4
line5
line6
line7
line8)rr&r'r(�	SETBINARYr)r$rM�flushrr%�readliner-rLr�)r
r.rrr�test_universal_newlinesIs&


z'ProcessTestCase.test_universal_newlinescCs^tjtjddtdgtjtjdd�}|j|jj�|j|j	j�|j
�\}}|j|d�dS)Nz-czimport sys,os;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\)rr%r�z#line2
line4
line5
line6
line7
line8)rr&r'r(r�r)rLr%r-rrjr)r
r.r%rrrr�#test_universal_newlines_communicatems
z3ProcessTestCase.test_universal_newlines_communicatecCsHtjtjddttjd�gtjdd�}|jd�\}}|j	|j
d�dS)Nz-czimport sys,os;a
                               s = sys.stdin.readline()
                               assert s == "line1\n", repr(s)
                               s = sys.stdin.read()
                               assert s == "line3\n", repr(s)
                              r\)r$r�zline1
line3
r)rr&r'r(r��textwrap�dedentr)rjrr?)r
r.r%rrrr�)test_universal_newlines_communicate_stdin�s

z9ProcessTestCase.test_universal_newlines_communicate_stdincCs8tjtjddgtjtjdd�}|j�|j|jd�dS)Nz-cr�T)r$r%r�r)rr&r'r(r)rjrr?)r
r.rrr�.test_universal_newlines_communicate_input_none�sz>ProcessTestCase.test_universal_newlines_communicate_input_nonecCs�tjtjddttjd�gtjtjtjdd�}|j|j	j
�|j|jj
�|jd�\}}|j
|jd�|j
d|�|j|jd	��dS)
Nz-czimport sys,os;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)r$rr%r�zline1
line3
rzline1
line2
line3
line4
line5
zeline2
eline6
eline7
)rr&r'r(r�r�r�r)rLr%r-rrjrr?r��
startswith)r
r.r%rrrr�7test_universal_newlines_communicate_stdin_stdout_stderr�s

zGProcessTestCase.test_universal_newlines_communicate_stdin_stdout_stderrcCsXxRd	D]J}d|}tjd|g}tj|tjtj|d�}|jdd�\}}|j|d�qWdS)
N�utf-16�	utf-32-bez@import sys; sys.stdout.buffer.write('1\r\n2\r3\n4'.encode('%s'))z-c)r$r%�encodingr)rQz1
2
3
4)r�r�)r'r(rr&r)rjr)r
r�rlr �popenr%rrrr�-test_universal_newlines_communicate_encodings�s
z=ProcessTestCase.test_universal_newlines_communicate_encodingscCs`xZdD]R\}}d	}tjd
|g}tj|tjtjd|d�}|jdd
�\}}|j|dj|��qWdS)N�ignorer�replace����surrogateescape����backslashreplace�\x80\x80z2import sys; sys.stdout.buffer.write(b'[\x80\x80]')z-czutf-8)r$r%r��errors)rQz[{}]�r�r�r�r��r�r��r�r�)r�r�r�r�)r'r(rr&r)rjr�format)r
r�rrlr r�r%rrrr�test_communicate_errors�sz'ProcessTestCase.test_communicate_errorsc
Cs8ts
d}nd}g}tj�}z�x�t|�D]f}y.tjj|tj�}|j	tj
|tjtjB��Wq&t
k
r�}z|jtjkrx�PWYdd}~Xq&Xq&W|jd|�xtd�D]}tj|j��q�WxJtd�D]>}tjtjddgtjtjtjd�}|jd	�d
}|j|d	�q�WWdx|D]}	tj|	��qWtj|�XdS)Niiz4failed to reach the file descriptor limit (tried %d)�
�z-cz-import sys;sys.stdout.write(sys.stdin.read()))r$r%rslimer)�	mswindowsrJ�mkdtemp�ranger]rtrurZTESTFN�append�open�O_WRONLY�O_CREAT�OSError�errnoZEMFILE�skipTestr-�poprr&r'r(r)rjr�shutilZrmtree)
r
Zmax_handlesZhandlesZtmpdir�iZtmpfile�er.�data�hrrr�test_no_leaking�s:


zProcessTestCase.test_no_leakingcCs�|jtjdddg�d�|jtjdddg�d�|jtjdddg�d	�|jtjd
ddg�d
�|jtjdddg�d�|jtjdddg�d�|jtjdddg�d�|jtjddg�d�dS)Nza b cr�r�z"a b c" d ezab"c�\z	ab\"c \ dz \z
ab\"c " \\" dza\\\bzde fgrza\\\b "de fg" hza\"br@z
a\\\"b c dza\\b cz"a\\b c" d eza\\b\ cz
"a\\b\ c" d eZabrzab "")rr�list2cmdline)r
rrr�test_list2cmdlines z!ProcessTestCase.test_list2cmdlinecCsbtjtjddgtjd�}|j|jj�|j|j	��t
j|jj�d�|j
�|j|j	�d�dS)Nz-czimport os; os.read(0, 1))r$�Ar)rr&r'r(r)rLr$r-�assertIsNone�pollr]rMr�rr)r
r.rrr�	test_poll!s

zProcessTestCase.test_pollcCs6tjtjddg�}|j|j�d�|j|j�d�dS)Nz-cr�r)rr&r'r(rr)r
r.rrr�	test_wait,szProcessTestCase.test_waitcCs`tjtjddg�}|jtj��}|jdd�WdQRX|jdt|j	��|j
|jdd�d�dS)Nz-czimport time; time.sleep(0.3)g-C��6?)r7z0.0001rWr)rr&r'r(r8r9rrC�strr>r)r
r.r@rrr�test_wait_timeout2s
z!ProcessTestCase.test_wait_timeoutcCs�tjtjddg�}zJy.|jt��}|jtj�dd�WdQRXWntjk
rXYnXWd|j	�X|j
d|j�|j
dt|j
��dS)Nz-cr�g{�G�z�?)�endtimeztest_subprocess.pyr
)rr&r'r(�assertWarns�DeprecationWarningr�timer9�killrC�filenamer�warning)r
r.Zwarn_cmrrr�test_wait_endtime<s"

z!ProcessTestCase.test_wait_endtimecCs.|jt��tjtjddgd�WdQRXdS)Nz-cr�r�)r8rbrr&r'r()r
rrr�test_invalid_bufsizeIsz$ProcessTestCase.test_invalid_bufsizecCsNtjtjddgd�}|j|j�d�tjtjddgdd�}|j|j�d�dS)Nz-cr�r)r0)rr&r'r(rr)r
r.rrr�test_bufsize_is_noneOsz$ProcessTestCase.test_bufsize_is_nonecCs�tjtjddgtjtjtjd|d��j}|jj|�tj	|jj
��|jj�}t
j��*y|jj	�Wntk
rxYnXWdQRXd|_WdQRX|j|jd�|j||�dS)Nz-czDimport sys;sys.stdout.write(sys.stdin.readline());sys.stdout.flush()r\)r$r%rr0r�r)rr&r'r(r)r�r$rMr]r-r�r%r�r�SuppressCrashReportr�rr?)r
�linerr�r.Z	read_linerrr�_test_bufsize_equal_oneWs"


z'ProcessTestCase._test_bufsize_equal_onecCsd}|j||dd�dS)Nzline
T)r�)r)r
rrrr� test_bufsize_equal_one_text_modemsz0ProcessTestCase.test_bufsize_equal_one_text_modecCs"dtjj�}|j|ddd�dS)Nsliner�F)r�)r]�linesepr�r)r
rrrr�"test_bufsize_equal_one_binary_modessz2ProcessTestCase.test_bufsize_equal_one_binary_modec
Cs@x:td�D].}|jt��tjttjtjd�WdQRXq
WdS)Ni)r%r)r�r8rxrr&ryr))r
r�rrr�test_leaking_fds_on_errorys
z)ProcessTestCase.test_leaking_fds_on_errorcCs�yddl}|jWn"ttfk
r4|jd�YnXtjdt�d��}tj	d|g}t
j|t
jdd�}|�|j
�d}WdQRX|j|d	�|j|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
        z-cT)rr�r\r)�msvcrtZCrtSetReportMode�AttributeError�ImportErrorr�r�r�ryr'r(rr&r)rjrr?)r
rrlr��procrrrr�test_nonexisting_with_pipes�s
z+ProcessTestCase.test_nonexisting_with_pipeszthreading requiredcs�g��fdd�}tj|d�}|j�z2|jt��tjttjtjtjd�WdQRXWd|j	�d}xB�D]:}yt
j|�Wqjtk
r�}z
|}WYdd}~XqjXqjW|dk	r�|�XdS)Ncs.x(td�D]}�jtj��tjd�q
WdS)N�g����MbP?)r��extendr]r�r�sleep)r�)�fdsrr�open_fds�sz<ProcessTestCase.test_double_close_on_error.<locals>.open_fds)�target)r$r%r)
�	threadingZThread�startr8�EnvironmentErrorrr&ryr)rur]r-r�)r
r'�t�exc�fdr�r)r&r�test_double_close_on_error�s&
z*ProcessTestCase.test_double_close_on_errorcs�tjtjddg��|j�jd�g���fdd�}tjd|�}|j�t	rPd}nd}�j
dd	�|j�j|d
d��j
�|j�j|dd�|j�|jdd|fd|fg��dS)Nz-czimport time; time.sleep(12)csN�jd�j�f��j��j��jd�jf��j��jd�jf�dS)Nzthread-start-poll-resultzthread-after-kill-and-waitzthread-after-second-wait)r�rrrr?r)r!�resultsrr�kill_proc_timer_thread�szDProcessTestCase.test_threadsafe_wait.<locals>.kill_proc_timer_threadg�������?r\�	r#)r7z*unexpected result in wait from main thread)rz&unexpected result in second main wait.�thread-start-poll-resultzthread-after-kill-and-waitzthread-after-second-waiti����)r3N)rr&r'r(rr?r)ZTimerr*r�rru)r
r1r,Zexpected_errorcoder)r!r0r�test_threadsafe_wait�s*


z$ProcessTestCase.test_threadsafe_waitcCs6djd�}tjtjd|g�}|j|jd�t|��dS)N�;�import subprocess, sys�Jretcode = subprocess.call([sys.executable, '-c', 'print("Hello World!")'])�assert retcode == 0z-csHello World!)r6r7r8)rurrBr'r(r�r��ascii)r
rlrDrrr�test_issue8780szProcessTestCase.test_issue8780cCs�tj�\}}tj�\}}tj�\}}ytjdg|||d�WnPtk
r�tj|�tj|�tj|�tj|�tj|�tj|�YnX|jtj	j
|��|jtj	j
|��|jtj	j
|��dS)N�*)r$r%r)rJ�mkstemprr&r�r]r-�removerrt�exists)r
ZifhandleZifnameZofhandleZofnameZefhandleZefnamerrr� test_handles_closed_on_exceptions 




z0ProcessTestCase.test_handles_closed_on_exceptioncCs\tjtjddgtjtjtjd�}|j|jj�|j|jj�|j|j	j�|j
dd�dS)Nz-cr�)r$r%r�x�r#i)rr&r'r(r)rLr%r-rr$rj)r
r.rrr�test_communicate_epipe s
z&ProcessTestCase.test_communicate_epipecCs@tjtjddgtjd�}|j|jj�|j�|j	dd�dS)Nz-cr�)r$r@rAr#i)
rr&r'r(r)rLr$r-rrj)r
r.rrr�!test_communicate_epipe_only_stdin+s

z1ProcessTestCase.test_communicate_epipe_only_stdin�SIGUSR1zRequires signal.SIGUSR1rzRequires os.kill�getppidzRequires os.getppidc
Csrdd�}tjtj|�}|jtjtj|�tjddg}x8dD]0}|tji}tj|f|��}|j�WdQRXq:WdS)NcSsdS)Nr)Zsignum�framerrr�handler;sz7ProcessTestCase.test_communicate_eintr.<locals>.handlerz-cz7import os, signal;os.kill(os.getppid(), signal.SIGUSR1)r%r)r%r)	�signalrDrLr'r(rr)r&rj)r
rG�old_handlerr �stream�kwZprocessrrr�test_communicate_eintr3s

z&ProcessTestCase.test_communicate_eintrz/proc/%d/fdzLinux specificc
Csddtj�}tj|�}|jt��$ttjddgtj	tj	tj	d�WdQRXtj|�}|j
||�dS)Nz/proc/%d/fdz-cr�)r$r%r)r]�getpid�listdirr8rrr'r(rr)r)r
Zfd_directoryZfds_before_popenZfds_after_exceptionrrr�!test_failed_child_execute_fd_leakLs

z1ProcessTestCase.test_failed_child_execute_fd_leakz+behavior currently not supported on WindowscCs<|jt��}tjddddg�WdQRX|j|jjd�dS)Nz/opt/nonexistent_binary�withZsomer )r8r�rr4rr>r)r
r@rrr�%test_file_not_found_includes_filename^sz5ProcessTestCase.test_file_not_found_includes_filenamecCs<|jt��}tjddgdd�WdQRX|j|jjd�dS)N�exit�0z/some/nonexistent/directory)r~)r8r�rr&rr>r)r
r@rrr� test_file_not_found_with_bad_cwddsz0ProcessTestCase.test_file_not_found_with_bad_cwd)rrrrr/r2r6r:r<rArErGrIrPrRrUrVrXrarhrirorprrrwrz�unittest�skipIfr�r|rr�r�r�r�r�r�r�r'�base_prefix�prefixr��	sysconfig�is_python_buildr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r��platform�get_config_varr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrr	r
rrrrrrrrr"r)r/r4r:r?rBrC�
skipUnless�hasattrrHr]rLrt�isdirrMrOrQrTrrrrr#_s�		

		



	

$'

)5r#c@sleZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�ZdS)�RunFuncTestCasecKstjd|g}tj|f|�S)Nz-c)r'r(rZrun)r
rlr!�argvrrr�
run_pythonlszRunFuncTestCase.run_pythonc	Cs<|jd�}|j|jd�|jtj��|j�WdQRXdS)Nzimport sys; sys.exit(47)r3)rbrr?r8rr=Zcheck_returncode)r
�cprrr�test_returncodeqs
zRunFuncTestCase.test_returncodecCs:|jtj��}|jddd�WdQRX|j|jjd�dS)Nzimport sys; sys.exit(47)T)�checkr3)r8rr=rbrr>r?)r
r@rrr�
test_checkxszRunFuncTestCase.test_checkcCs |jddd�}|j|jd�dS)Nzimport sys; sys.exit(0)T)rer)rbrr?)r
rcrrr�test_check_zero}szRunFuncTestCase.test_check_zerocCs*|jtj��|jddd�WdQRXdS)Nzwhile True: passg-C��6?)r7)r8rr9rb)r
rrr�test_timeout�szRunFuncTestCase.test_timeoutcCs"|jdtjd�}|jd|j�dS)Nz
print('BDFL'))r%sBDFL)rbrr)rCr%)r
rcrrr�test_capture_stdout�sz#RunFuncTestCase.test_capture_stdoutcCs"|jdtjd�}|jd|j�dS)Nz$import sys; sys.stderr.write('BDFL'))rsBDFL)rbrr)rCr)r
rcrrr�test_capture_stderr�s
z#RunFuncTestCase.test_capture_stderrcCsLtj�}|j|j�|jd�|jd�|jd|tjd�}|j	d|j
�dS)Nspearrz6import sys; sys.stdout.write(sys.stdin.read().upper()))r$r%sPEAR)rJrKrLr-rMrNrbrr)rCr%)r
rOrcrrrrP�s

z+RunFuncTestCase.test_check_output_stdin_argcCs$|jddtjd�}|jd|j�dS)Nz6import sys; sys.stdout.write(sys.stdin.read().upper())spear)rQr%sPEAR)rbrr)rCr%)r
rcrrrrR�sz+RunFuncTestCase.test_check_output_input_argcCs~tj�}|j|j�|jd�|jd�|jtdd��}|jd|dd�}WdQRX|j	d|j
jd�|j	d	|j
jd�dS)
Nspearrz7Expected ValueError when stdin and input args supplied.)rzprint('will not be run')share)r$rQr$rQ)rJrKrLr-rMrNr8rSrbrCr>r )r
rOr@rDrrrrV�s


z6RunFuncTestCase.test_check_output_stdin_with_input_argcCsN|jtj��}|jddtjd�}WdQRX|j|jjd�|j|jjd�dS)NzMimport sys, time
sys.stdout.write('BDFL')
sys.stdout.flush()
time.sleep(3600)rW)r7r%sBDFL)	r8rr9rbr)rr>rDr%)r
r@rcrrrrX�sz)RunFuncTestCase.test_check_output_timeoutcCs2tjj�}d|d<|jd|d�}|j|jd�dS)NrYrZzCimport sys, os;sys.exit(33 if os.getenv("FRUIT")=="banana" else 31))r[�!)r]r^r_rbrr?)r
r`rcrrr�test_run_kwargs�s

zRunFuncTestCase.test_run_kwargsN)rrrrbrdrfrgrhrirjrPrRrVrXrlrrrrr`ks
r`zPOSIX specific testscs2eZdZ�fdd�Zdd�Zdd�Zdd�Zd	d
�ZGdd�dej	�Z
ejd
�dd��Z
ejd
�dd��Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd d!�ZGd"d#�d#ej	�Zejejjd$�d%�d&d'��Zd(d)�Zejej d*kd+�d,d-��Z!d.d/�Z"d0d1�Z#d2d3�Z$d4d5�Z%d6d7�Z&d8d9�Z'd:d;�Z(ejej j)d��d>�d?d@��Z*dAdB�Z+dCdD�Z,dEdF�Z-dGdH�Z.dIdJ�Z/dKdL�Z0dMdN�Z1dOdP�Z2dQdR�Z3dSdT�Z4dUdV�Z5dWdX�Z6dYdZ�Z7d[d\�Z8d]d^�Z9d_d`�Z:dadb�Z;dcdd�Z<dedf�Z=dgdh�Z>didj�Z?dkdl�Z@dmdn�ZAdodp�ZBdqdr�ZCdsdt�ZDdudv�ZEdwdx�ZFejej j)dy��oXejGdz�jHejGd{�jHkd|�d}d~��ZIeJjKdd��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]ej^e_d�k	�oe`e_d��d��d�d���Za�ZbS)��POSIXProcessTestCasecst�j�d|_dS)Nz/_this/pa.th/does/not/exist)�superr
�_nonexistent_dir)r
)�	__class__rrr
�s
zPOSIXProcessTestCase.setUpcCsdytj|j�Wn>tk
rN}z"|}|jdt|j�7_WYdd}~XnX|jd|j�|S)Nz: z,chdir to nonexistent directory %s succeeded.)r]�chdirror��strerror�reprrT)r
r��desired_exceptionrrr�_get_chdir_exception�s*
z)POSIXProcessTestCase._get_chdir_exceptioncCsz|j�}ytjtjddg|jd�}WnBtk
rf}z&|j|j|j�|j|j	|j	�WYdd}~XnX|j
d|�dS)Nz-cr)r~zExpected OSError: %s)rurr&r'r(ror�rr�rrrT)r
rtr.r�rrr�test_exception_cwd�s"z'POSIXProcessTestCase.test_exception_cwdcCsz|j�}ytjtjddg|jd�}WnBtk
rf}z&|j|j|j�|j|j	|j	�WYdd}~XnX|j
d|�dS)Nz-cr)r(zExpected OSError: %s)rurr&r'r(ror�rr�rrrT)r
rtr.r�rrr�test_exception_bad_executable�s"z2POSIXProcessTestCase.test_exception_bad_executablecCst|j�}ytj|jddg�}WnBtk
r`}z&|j|j|j�|j|j|j�WYdd}~XnX|jd|�dS)Nz-crzExpected OSError: %s)	rurr&ror�rr�rrrT)r
rtr.r�rrr�test_exception_bad_args_0�s"z.POSIXProcessTestCase.test_exception_bad_args_0c@seZdZdd�ZdS)z&POSIXProcessTestCase.PopenNoDestructorcCsdS)Nr)r
rrr�__del__sz.POSIXProcessTestCase.PopenNoDestructor.__del__N)rrrryrrrr�PopenNoDestructorsrzz%subprocess._posixsubprocess.fork_execcCsNdd�}||_tjdtd��(|jt��|jdg�WdQRXWdQRXdS)NcWs0|d}djtj�j�}tj|d|d�dS)N�
z{:x}sOSError:�:r)r�r�ZEISDIRr�r]rM)r �
errpipe_writeZerr_coderrr�proper_errorszHPOSIXProcessTestCase.test_exception_errpipe_normal.<locals>.proper_errorzsubprocess.os.waitpid)�side_effect�non_existent_command)rr�patch�ChildProcessErrorr8�IsADirectoryErrorrz)r
�	fork_execr~rrr�test_exception_errpipe_normals
z2POSIXProcessTestCase.test_exception_errpipe_normalcsnd��fdd�}||_tjdtd��*|jtj��}|jdg�WdQRXWdQRX|jt	��t
|j��dS)Ns�ޭcs|d}tj|��dS)Nr{r)r]rM)r r})�
error_datarr�	bad_error)szGPOSIXProcessTestCase.test_exception_errpipe_bad_data.<locals>.bad_errorzsubprocess.os.waitpid)rr�)rrr�r�r8r�SubprocessErrorrzrCrsrr>)r
r�r�r�r)r�r�test_exception_errpipe_bad_data$s
 z4POSIXProcessTestCase.test_exception_errpipe_bad_datacCs0tjtjddgdd�tjtjddgdd�dS)Nz-crT)Zrestore_signalsF)rr4r'r()r
rrr�test_restore_signals;sz)POSIXProcessTestCase.test_restore_signalscCsrytjtjddgdd�}Wn0tk
rJ}z|jtjkr:�WYdd}~Xn$Xtjtj	��}t
|�}|j||�dS)Nz-cz)import os; print(os.getpgid(os.getpid()))T)Zstart_new_session)rrBr'r(r�r�ZEPERMr]�getpgidrM�int�assertNotEqual)r
rDr�Zparent_pgidZ
child_pgidrrr�test_start_new_sessionBsz+POSIXProcessTestCase.test_start_new_sessioncCsDtj�� tjtjddg�}|j�WdQRX|j|jt	j
�dS)Nz-czimport os; os.abort())rrrr&r'r(rrr?rH�SIGABRT)r
r.rrr�test_run_abortSs


z#POSIXProcessTestCase.test_run_abortcCsNtjttj�d�}t|�}|jd|j��|jd|�|jttj�|�dS)Nzfake cmdrHZSIG)rr=r�rHr�rrC�lower)r
rn�error_stringrrr�"test_CalledProcessError_str_signal[s
z7POSIXProcessTestCase.test_CalledProcessError_str_signalcCs$tjdd�}t|�}|jd|�dS)Ni?��zfake cmdzunknown signal 9876543.i�Ki�)rr=rrC)r
rnr�rrr�*test_CalledProcessError_str_unknown_signalfsz?POSIXProcessTestCase.test_CalledProcessError_str_unknown_signalcCs$tjdd�}t|�}|jd|�dS)NrAzfake cmdznon-zero exit status 2.)rr=rrC)r
rnr�rrr�$test_CalledProcessError_str_non_zeroksz9POSIXProcessTestCase.test_CalledProcessError_str_non_zeroc
CsDtjtjddgtjdd�d�}|�|j|jj�d�WdQRXdS)Nz-cz2import sys,os;sys.stdout.write(os.getenv("FRUIT"))cSstjdd�S)NrZ�apple)r]�putenvrrrr�<lambda>wsz3POSIXProcessTestCase.test_preexec.<locals>.<lambda>)r%�
preexec_fnsapple)rr&r'r(r)rr%r�)r
r.rrr�test_preexecps
z!POSIXProcessTestCase.test_preexeccCs�dd�}ytjtjddg|d�}Wndtjk
rT}z|jtjd�WYdd}~Xn>tk
r�}z|jd|j	d�WYdd}~XnX|j
d	�dS)
NcSstd��dS)Nz'What if two swallows carried a coconut?)rSrrrr�raise_it|sz=POSIXProcessTestCase.test_preexec_exception.<locals>.raise_itz-cr)r�z)Expected a ValueError from the preexec_fnZcoconutrzEException raised by preexec_fn did not make it to the parent process.)rr&r'r(r�r��_posixsubprocessrSrCr rT)r
r�r.r�rrr�test_preexec_exception{s$z+POSIXProcessTestCase.test_preexec_exceptionc@seZdZdd�Zdd�ZdS)z+POSIXProcessTestCase._TestExecuteChildPopencOs||_tjj|f|�|�dS)N)�	_testcaserr&rc)r
Ztestcaser r!rrrrc�sz4POSIXProcessTestCase._TestExecuteChildPopen.__init__cOs�ztjj|f|�|�Wddd�td�D�}z<x6|D].}|jj||jj�|jj�|j	j�fdd�q4WWdx|D]}t
j|�qpWXXdS)NcSsg|]}tjdtj��qS)z	/dev/zero)r]r��O_RDONLY)�.0r�rrr�
<listcomp>�szNPOSIXProcessTestCase._TestExecuteChildPopen._execute_child.<locals>.<listcomp>�z!At least one fd was closed early.)r)rr&r"r�r��assertNotInr$r�r%rr]r-)r
r r!Zdevzero_fdsr.rrrr"�s


z:POSIXProcessTestCase._TestExecuteChildPopen._execute_childN)rrrrcr"rrrr�_TestExecuteChildPopen�sr�z	/dev/zeroz/dev/zero required.cCsHdd�}|jtj��*|j|tjddgtjtjtj|d�WdQRXdS)NcSstjd��dS)Nz-force the _execute_child() errpipe_data path.)rr�rrrrr��szWPOSIXProcessTestCase.test_preexec_errpipe_does_not_double_close_pipes.<locals>.raise_itz-cr�)r$r%rr�)r8rr�r�r'r(r))r
r�rrr�0test_preexec_errpipe_does_not_double_close_pipes�szEPOSIXProcessTestCase.test_preexec_errpipe_does_not_double_close_pipescCsdd�}tj�}tj}tj}z�tj�|jtj��tjtjddgdd�d�|jtj�d�tj�|j	tj��tjtjddgd	d�d�|j	tj�d
�|t_|j
ttjtjddgdd�d�t`|j
t
tjtjddgdd�d�Wd|t_|t_|�stj�XdS)
NcSstd��dS)Nzthis shouldn't escape)�RuntimeErrorrrrr�raise_runtime_error�szPPOSIXProcessTestCase.test_preexec_gc_module_failure.<locals>.raise_runtime_errorz-crcSsdS)Nrrrrrr��szEPOSIXProcessTestCase.test_preexec_gc_module_failure.<locals>.<lambda>)r�z#Popen enabled gc when it shouldn't.cSsdS)Nrrrrrr��szPopen left gc disabled.cSsdS)Nrrrrrr��scSsdS)Nrrrrrr��s)�gc�	isenabled�disablerrr4r'r(�enabler�r8r�r&r)r
r�ZenabledZorig_gc_disableZorig_gc_isenabledrrr�test_preexec_gc_module_failure�s8




z3POSIXProcessTestCase.test_preexec_gc_module_failure�darwinz!setrlimit() seems to fail on OS XcCs�yddlm}m}m}Wn,tk
rD}z|j|�WYdd}~XnX||�}|\}}||d|f�|j|||�ytjt	j
ddgdd�d�Wntk
r�YnX|jd�dS)	Nr)�	getrlimit�	setrlimit�RLIMIT_NPROCz-crcSsdS)Nrrrrrr��sz@POSIXProcessTestCase.test_preexec_fork_failure.<locals>.<lambda>)r�z.RLIMIT_NPROC had no effect; probably superuser)Zresourcer�r�r�r r�rLrr4r'r(�BlockingIOError)r
r�r�r�rnZlimitsr�Zhardrrr�test_preexec_fork_failure�sz.POSIXProcessTestCase.test_preexec_fork_failurec
Cs�tj�\}}t|ddd��&}|jdtj�|jdtj�WdQRXtj	|d�t
j|�}|j�tj
|�|j|jd�dS)N�wr�)r�z#!%s
z(exec '%s' -c 'import sys; sys.exit(47)'
i�r3)rJr<r�rMr�
unix_shellr'r(r]�chmodrr&rr=rr?)r
r.�fname�fobjr.rrr�test_args_string�s

z%POSIXProcessTestCase.test_args_stringcCs<|jttjtjddgdd�|jttjtjddgdd�dS)Nz-czimport sys; sys.exit(47)r3)�startupinfo)�
creationflags)r8rSrr4r'r()r
rrrrh�s

z&POSIXProcessTestCase.test_invalid_argsc
CsTtjj�}d|d<tjdgdtj|d�}|�|j|jj�j	d�d�WdQRXdS)Nr�rZzecho $FRUITr\)r{r%r[s 	
sapple)
r]r^r_rr&r)rr%r�r)r
r`r.rrr�test_shell_sequences

z(POSIXProcessTestCase.test_shell_sequencec
CsRtjj�}d|d<tjddtj|d�}|�|j|jj�j	d�d�WdQRXdS)Nr�rZzecho $FRUITr\)r{r%r[s 	
sapple)
r]r^r_rr&r)rr%r�r)r
r`r.rrr�test_shell_strings
z&POSIXProcessTestCase.test_shell_stringc
Csvtj�\}}t|ddd��&}|jdtj�|jdtj�WdQRXtj	|d�t
j|�}tj|�|j
|d�dS)Nr�r�)r�z#!%s
z(exec '%s' -c 'import sys; sys.exit(47)'
i�r3)rJr<r�rMrr�r'r(r]r�rr4r=r)r
r.r�r�r5rrr�test_call_strings

z%POSIXProcessTestCase.test_call_stringc
Cs�g}x>dD]6}x0d
D](}tjj||�}tjj|�r|j|�qWq
W|sR|jd�d}tjj|�rztjj|�rz|j|�xL|D]D}tjd|d	tj	d
�}|�"|j
|jj�j
�t|d��WdQRXq�WdS)N�/bin�	/usr/bin/�/usr/local/bin�bash�kshz"bash or ksh required for this testz/bin/shzecho $0T)r(r{r%r9)r�r�r�)r�r�)r]rtru�isfiler�r��islinkrr&r)rr%r�r�bytes)r
ZshellsrX�name�shr.rrr�test_specific_shell's 






z(POSIXProcessTestCase.test_specific_shellc
Gsftjtjtj�}z&tjtjddgdtjtjtjd�}Wdtjtj|�X|jj	d�t
||�|�|S)Nz-cz�if 1:
                                 import sys, time
                                 sys.stdout.write('x\n')
                                 sys.stdout.flush()
                                 time.sleep(30)
                                 T)�	close_fdsr$r%rr\)rH�SIGINT�default_int_handlerrr&r'r(r)r%r��getattr)r
�methodr rIr.rrr�
_kill_process;s
z"POSIXProcessTestCase._kill_process�netbsd�openbsdz"Due to known OS bug (issue #16762)cGsRtjtjddgdtjtjtjd�}|jjd�tjd�t	||�|�|j
�dS)Nz-cz�if 1:
                             import sys, time
                             sys.stdout.write('x\n')
                             sys.stdout.flush()
                             T)r�r$r%rr\)rr&r'r(r)r%r�rr%r�rj)r
r�r r.rrr�_kill_dead_processTs


z'POSIXProcessTestCase._kill_dead_processcCs:|jdtj�}|j�\}}|jd|�|j|j�d�dS)N�send_signalsKeyboardInterruptr)r�rHr�rjrCr�r)r
r.r�rrrr�test_send_signalksz%POSIXProcessTestCase.test_send_signalcCs:|jd�}|j�\}}|j|d�|j|j�tj�dS)Nrr�)r�rjrrrrH�SIGKILL)r
r.r�rrrr�	test_killqs
zPOSIXProcessTestCase.test_killcCs:|jd�}|j�\}}|j|d�|j|j�tj�dS)N�	terminater�)r�rjrrrrH�SIGTERM)r
r.r�rrrr�test_terminatews
z#POSIXProcessTestCase.test_terminatecCs|jdtj�dS)Nr�)r�rHr�)r
rrr�test_send_signal_dead}sz*POSIXProcessTestCase.test_send_signal_deadcCs|jd�dS)Nr)r�)r
rrr�test_kill_dead�sz#POSIXProcessTestCase.test_kill_deadcCs|jd�dS)Nr�)r�)r
rrr�test_terminate_dead�sz(POSIXProcessTestCase.test_terminate_deadcCs:g}x0|D](}tj|�}tj|�}|j|||f�q
W|S)N)r]�get_inheritable�dupr�)r
Zsave_fdsr&r.�inheritable�savedrrr�	_save_fds�s


zPOSIXProcessTestCase._save_fdscCs2x,|D]$\}}}tj|||d�tj|�qWdS)N)r�)r]�dup2r-)r
r&r.r�r�rrr�_restore_fds�sz!POSIXProcessTestCase._restore_fdsc	Cs�d}|j|�}x |D]\}}}|dkr|}PqWz\x|D]}tj|�q8Wtjtjddg|tjtjd�j�\}}t	j
|�}|j||fd�Wd|j|�XdS)Nrz-czRimport sys;sys.stdout.write("apple");sys.stdout.flush();sys.stderr.write("orange"))r$r%r�apple�orange)r�r�)
r�r]r-rr&r'r(r)rjrrrr�)	r
r&r$�	saved_fdsr.r�r�rmrnrrr�check_close_std_fds�s"



z(POSIXProcessTestCase.check_close_std_fdscCs|jdg�dS)Nr)r�)r
rrr�test_close_fd_0�sz$POSIXProcessTestCase.test_close_fd_0cCs|jdg�dS)Nr\)r�)r
rrr�test_close_fd_1�sz$POSIXProcessTestCase.test_close_fd_1cCs|jdg�dS)NrA)r�)r
rrr�test_close_fd_2�sz$POSIXProcessTestCase.test_close_fd_2cCs|jddg�dS)Nrr\)r�)r
rrr�test_close_fds_0_1�sz'POSIXProcessTestCase.test_close_fds_0_1cCs|jddg�dS)NrrA)r�)r
rrr�test_close_fds_0_2�sz'POSIXProcessTestCase.test_close_fds_0_2cCs|jddg�dS)Nr\rA)r�)r
rrr�test_close_fds_1_2�sz'POSIXProcessTestCase.test_close_fds_1_2cCs|jdddg�dS)Nrr\rA)r�)r
rrr�test_close_fds_0_1_2�sz)POSIXProcessTestCase.test_close_fds_0_1_2cCsvtjd�}tjd�}z.tjd�tjd�tjtjddg�j�Wdtj|d�tj|d�tj|�tj|�XdS)Nrr\z-cz*print('AssertionError:0:CLOEXEC failure.'))	r]r�r-rr&r'r(rr�)r
Z	new_stdinZ
new_stdoutrrr�test_small_errpipe_write_fd�s




z0POSIXProcessTestCase.test_small_errpipe_write_fdc
CsPdd�td�D�}�zdd�|D�}x|D]\}}tj|�q*Wtj|dd�tj|ddd�|jtd��}zTx t|�D]\}}tj||�q|Wtj	t
jdd	g|d|d
|dd�}|j�Wd|j
|�Xx|D]}tj|dd�q�Wtj|d
d�}tjtj|dd��}	|j|d
�|j|	d�Wdx|D]}tj|��q6WXdS)NcSsg|]}tj��qSr)rJr<)r�r�rrrr��sz?POSIXProcessTestCase.test_remapping_std_fds.<locals>.<listcomp>rWcSsg|]\}}|�qSrr)r�r.r�rrrr��sr\sSTDINrz-czZimport sys; got = sys.stdin.read();sys.stdout.write("got %s"%got); sys.stderr.write("err")rA)r$r%ris	got STDINserr)r�r]�unlinkrMr�r��	enumerater�rr&r'r(rr�r�rrrr-)
r
�temps�temp_fdsr.r�r��temp_fdr.rmrnrrr�test_remapping_std_fds�s4


z+POSIXProcessTestCase.test_remapping_std_fdsc
Cs2dd�td�D�}dd�|D�}z�x|D]\}}tj|�q(W|jtd��}z�x t|�D]\}}	tj|	|�qXWtj|d�tj|dd�tj	t
jddg|||d	�}
|
j�x|D]}tj|dd�q�Wtj
|d
�}tjtj
|d
��}Wd|j|�X|j|d�|j|d�Wdx|D]}tj|��qWXdS)
NcSsg|]}tj��qSr)rJr<)r�r�rrrr�
sz7POSIXProcessTestCase.check_swap_fds.<locals>.<listcomp>rWcSsg|]\}}|�qSrr)r�r.r�rrrr�ssSTDINrz-czZimport sys; got = sys.stdin.read();sys.stdout.write("got %s"%got); sys.stderr.write("err"))r$r%ris	got STDINserr)r�r]r�r�r�r�rMr�rr&r'r(rr�rrr�rr-)
r
Zstdin_noZ	stdout_noZ	stderr_nor�r�r.r�r�r�r.rmrnrrr�check_swap_fdss4


z#POSIXProcessTestCase.check_swap_fdscCsX|jddd�|jddd�|jddd�|jddd�|jddd�|jddd�dS)Nrr\rA)r�)r
rrr�
test_swap_fds8sz"POSIXProcessTestCase.test_swap_fdscCsx|jtd��}�zVx0|D](}tj��}tj|j�|�WdQRXqWttd��t|�j�}tj	|�dddg}i}x"t
||�D]\}}	||||	<q�Wtjd�}
ttd��t|�j�}t
jtjd|
t|�gf|�}|j|d�x�t
||�D]r\}}	tj|dtj�tj|d�}
ttt|
jd	���}tjd
|�d|�d|�d
|�d|	�d��}|j|	g||�q�WWd|j|�XdS)NrWr$r%rz�
                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'))
            z-crir9z"
                    When testing z to z4 redirection,
                    parent descriptor z5 got redirected
                    to descriptor(s) z instead of descriptor z.
                )r�r�rJrKr]r�r��setr�r-�zipr�r�rr4r'r(rrr��SEEK_SETr��list�mapr�r�r�)r
�from_fds�to_fdsr�Zfrom_fd�fZfd_to_closeZ	arg_namesr!Zto_fdrlZ
skipped_fdr5Z
read_bytesZread_fdsrrrr�#_check_swap_std_fds_with_one_closed@s2



&z8POSIXProcessTestCase._check_swap_std_fds_with_one_closedcCsDx>tjtd�d�D]*}x$tjtd�d�D]}|j||�q(WqWdS)NrWrA)�	itertools�combinationsr��permutationsr�)r
r�r�rrr�!test_swap_std_fds_with_one_closedlsz6POSIXProcessTestCase.test_swap_std_fds_with_one_closedcCs�dd�}ytjtjddg|d�Wn|tk
r`}z"|jtj�|jt|�d�WYdd}~XnJtj	k
r�}z"|j
tj�|jt|�d�WYdd}~XnX|jd�dS)	NcSstd��dS)Nu
surrogate:�)rSrrrr�preparerszCPOSIXProcessTestCase.test_surrogates_error_message.<locals>.preparez-cr�)r�u
surrogate:�z!Exception occurred in preexec_fn.z1Expected ValueError or subprocess.SubprocessError)rr4r'r(rSrr�rrr�ZassertIsNotNonerT)r
rrnrrr�test_surrogates_error_messageqs
 "z2POSIXProcessTestCase.test_surrogates_error_messagecCs��x�dD]�\}}|jdd�}dt|�}tjj�}|||<d|d	<tjjd
�r\|jdd�}n|}t	j
tjd|g|d
�}|jd�}|j
|jd�t|��|jdd�}dt|�}tjj�}|||<t	j
tjd|g|d
�}|jd�}|j
|jd�t|��qWdS)N�test�abc��test��42r9r�z&import os; print(ascii(os.getenv(%s)))�C�LC_ALL�aix�latin1z-c)r[s

z'import os; print(ascii(os.getenvb(%s)))�rr�rr	)rr)r�rsr]r^r_r'r[r�r�rrBr(rkrr9)r
�key�valueZ
encoded_value�scriptr[Z
decoded_valuer%rrr�test_undecodable_env�s.





z)POSIXProcessTestCase.test_undecodable_envcCs�tjtj�}tjjtj�\}}tj|�}tj|ddg�}|j|d�d|d}tj|dd�}|j|d�tj	j
�}||d<tj|ddg|d	�}|j|d�tjj
�}tj|�|d
<tj|ddg|d	�}|j|d�dS)Nz-cr�r�'s	' -c passT)r{�PATH)r[sPATH)r]�fsencoder'r(rtr�rr4rr^r_�environb)r
Zabs_programrtZprogramZexitcoder�r[Zenvbrrr�test_bytes_program�s 


z'POSIXProcessTestCase.test_bytes_programc	Cs�tjddd�}tjddd�}tjtj|gtjtjtjdd�}|j|jd�tjtj|gtjdd�}|j�\}}t	t
t|jd	���}t	|j
j�|jj�|jj�g�}|j||@d
|||@f�dS)Nzinput_reader.py�subprocessdata)�subdirzfd_status.pyF)r$r%rr�r�)r%r��,z5Expected no fds from %r to be open in child, found %r)r�findfilerr&r'r(r)rLrjr�r�r�r�r$r�r%rr)	r
Zsleeper�	fd_status�p1�p2rD�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	�|j�j	��fd
d�}�fdd
�}|j|�|j|��j
j|��j
j�t
j
�jgggd�\}}}	|j|d�|j�jj�|��jj��jj�dS)Nzqcat.pyr)rzqgrep.pyszxcvbnr��
F)r$r%r�cs&y�j�Wntk
r YnXdS)N)r��ProcessLookupErrorr)rrr�kill_p1�szBPOSIXProcessTestCase.test_pipe_cloexec_real_tools.<locals>.kill_p1cs&y�j�Wntk
r YnXdS)N)r�r#r)rrr�kill_p2�szBPOSIXProcessTestCase.test_pipe_cloexec_real_tools.<locals>.kill_p2r�zThe child hung)rrrr&r'r(r)r%rLrr$rMr-�selectr�rr�)
r
ZqcatZqgrepZsubdatarr$r%Z	readfilesZignored1Zignored2r)rrr�test_pipe_cloexec_real_tools�s.



z1POSIXProcessTestCase.test_pipe_cloexec_real_toolsc
s�tjddd�}tj�}|jtj|d�|jtj|d�t|��x8td�D],}tjtj	tj
�}|jtj|��j|�qLWx�D]}tj|d�q�Wt
jtj|gt
jdd	�}|j�\}}ttt|jd
���}|j|�@�d�t
jtj|gt
jdd	�}|j�\}}ttt|jd
���}|j|�@d�|jd|d
�t�fdd�td�D��}	t
jtj|gt
jdfd�}|j�\}}ttt|jd
���}|j||	@�@d�|jd|d
�dS)Nzfd_status.pyr)rrr\r2TF)r%r�rzSome fds were closedzSome fds were left openzSubprocess failedc3s|]}�j�VqdS)N)r�)r�r�)r'rr�	<genexpr>%	sz6POSIXProcessTestCase.test_close_fds.<locals>.<genexpr>r�)r%r��pass_fdsz'Some fds not in pass_fds were left open)rrr]r�rLr-r�r�r��devnullr��add�set_inheritablerr&r'r(r)rjr�r�r�rrrC)
r
rr&r�r.r.rD�ignored�
remaining_fds�fds_to_keepr)r'r�test_close_fds	sB

z#POSIXProcessTestCase.test_close_fdsZfreebsdz/devz/dev/fdz/Requires fdescfs mounted on /dev/fd on FreeBSD.cCs�tjddd�}tjtjdtjd|�gtjd�}|j	�\}}|j
�}|jt|�dd|d	�t
tt|d
j�jd���}t
tt|dj�jd���}|j||@d
d	�dS)Nzfd_status.pyr)rz-caK
        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%rAz(expected exactly two lines of output:
%r)rrrr\zSome fds were left open.)rrrr&r'r(r�r�r)rj�
splitlinesrr�r�r�r�rr�r)r
rr.rDZ
unused_stderrZoutput_linesZ
opened_fdsr.rrr�%test_close_fds_when_max_fd_is_lowered1	s
0
z:POSIXProcessTestCase.test_close_fds_when_max_fd_is_loweredr�rFcCs:tjddd�}t�}xftd�D]Z}tj�}|jtj|d�|jtj|d�tj|dd�tj|dd�|j	|�qWx�|D]�}t
jtj
|gt
jd|fd�}|j�\}}ttt|jd	���}	||h}
|j||	d
�|j|	|
@d�|jt��&}|jt
jtj
dd
gd|fd��WdQRX|jdt|j��q�WdS)Nzfd_status.pyr)rrFrr\T)r%r�r)rzfd to be passed not passedzfd to be closed passedz-czimport sys; sys.exit(0)F)r�r)zoverriding close_fds)rrr�r�r]r�rLr-r,�updaterr&r'r(r)rjr�r�r�rCrr�RuntimeWarningr4rr)r
rr'r�r&r.r.rDr-r.Zto_be_closed�contextrrr�
test_pass_fds�	s0




z"POSIXProcessTestCase.test_pass_fdsc
Cs�tjddd�}tj�\}}|jtj|�|jtj|�tj|d�tj|d�||f}tj|g}|t	t
t|��7}tj
|tjd|d�}|j�\}}tt
t|jd���}	|j|	t|�d|�|jtj|�d�|jtj|�d�dS)	Nzfd_status.pyr)rTF)r%r�r)rz	output=%a)rrr]r�rLr-r,r'r(r�r�rrr&r)rjr�r�r�rr�)
r
rr�Znon_inheritabler)r r.rDr-r&rrr�test_pass_fds_inheritable�	s"
z.POSIXProcessTestCase.test_pass_fds_inheritablec	Cs�tjddd�}g}x8td�D],}tjtjtj�}|jtj|�|j	|�qWtj
�\}}|jtj|�|jtj|�|jd|�tj
tj|g|d|d|dd|d��tj|d	�}WdQRXd
d�|jd�D�}|j|dddht|�Bd
|���dS)Nzfd_status.pyr)rrAr\rT)r$r%rr�r)icSsh|]}t|��qSr)r�)r�Znumrrr�	<setcomp>�	sz@POSIXProcessTestCase.test_pass_fds_redirected.<locals>.<setcomp>rzoutput=)rrr�r]r�r*�O_RDWRrLr-r�r��insertrr&r'r(r�r�r�	frozenset)	r
rr)r�r.Zstdout_rZstdout_wrDr&rrr�test_pass_fds_redirected�	s&
z-POSIXProcessTestCase.test_pass_fds_redirectedcCs>tjtjd��&}tjtjddg||d�}|j�WdQRXdS)Nzr+z-czimport sys; sys.exit(0))r%r$)	r+r�r]r*rr&r'r(r)r
�inoutr.rrr�%test_stdout_stdin_are_single_inout_fd�	s
z:POSIXProcessTestCase.test_stdout_stdin_are_single_inout_fdcCs>tjtjd��&}tjtjddg||d�}|j�WdQRXdS)Nzr+z-czimport sys; sys.exit(0))r%r)	r+r�r]r*rr&r'r(r)r
r=r.rrr�&test_stdout_stderr_are_single_inout_fd�	s
z;POSIXProcessTestCase.test_stdout_stderr_are_single_inout_fdcCs>tjtjd��&}tjtjddg||d�}|j�WdQRXdS)Nzr+z-czimport sys; sys.exit(0))rr$)	r+r�r]r*rr&r'r(r)r
r=r.rrr�%test_stderr_stdin_are_single_inout_fd�	s
z:POSIXProcessTestCase.test_stderr_stdin_are_single_inout_fdcCsRtjddd�}tjtj|gtjtjd�}|j�\}}|jd|j	d|j
d��dS)Nzsigchild_ignore.pyr)r)r%rrz6sigchild_ignore.py exited non-zero with this error:
%szutf-8)rrrr&r'r(r)rjrr?r�)r
Zsigchild_ignorer.r%rrrr�test_wait_when_sigchild_ignored�	sz4POSIXProcessTestCase.test_wait_when_sigchild_ignoredc
Csztjd�}tjtjddgtjdd�}|j}|j|j	�z4|j
|jd�d�|j||j
|gggd�d�Wd|j�XdS)	Nr&z-cz$import sys;sys.stdout.write("apple")r)r%r0r�sapplg)r�
import_modulerr&r'r(r)r%rLr-rr�rCr&r)r
r&r.r�rrr�test_select_unbuffered�	s

"z+POSIXProcessTestCase.test_select_unbufferedcCs�tjtjddgtjtjd�}|j|jj�|j|jj�t	|�}|j
}tjdt
f��
d}WdQRX|j|dd�tjD��dS)Nz-cz import sys, time;time.sleep(0.2))r%rrcSsg|]}t|��qSr)�id)r��orrrr�
szEPOSIXProcessTestCase.test_zombie_fast_process_del.<locals>.<listcomp>)rr&r'r(r)rLr%r-rrD�pidr�check_warnings�ResourceWarningrCr)r
r.�identrFrrr�test_zombie_fast_process_del
s

z1POSIXProcessTestCase.test_zombie_fast_process_delcCs�tjtjddgtjtjd�}|j|jj�|j|jj�t	|�}|j
}tjdt
f��
d}WdQRXtj|tj�|j|dd�tjD��tjd�|jt��&tjttjtjd��}WdQRXWdQRX|jttj|d�|j|d	d�tjD��dS)
Nz-czimport time;time.sleep(3))r%rrcSsg|]}t|��qSr)rD)r�rErrrr�4
szJPOSIXProcessTestCase.test_leak_fast_process_del_killed.<locals>.<listcomp>g�������?rcSsg|]}t|��qSr)rD)r�rErrrr�@
s)rr&r'r(r)rLr%r-rrDrFrrGrHr]rrHr�rCrrr%r8r�ry�waitpidr�)r
r.rIrFr!rrr�!test_leak_fast_process_del_killed 
s(


z6POSIXProcessTestCase.test_leak_fast_process_del_killedcsxtjddd�}tjd��|jtj��tjtj	|gtj
d�fdd�d�}|j�\}}tt
t|jd	���}|j�|�dS)
Nzfd_status.pyr)rr\Tcstjd��S)Nr\)r]r�r)r.rrr�L
szCPOSIXProcessTestCase.test_close_fds_after_preexec.<locals>.<lambda>)r%r�r�r)rrr]r�rLr-rr&r'r(r)rjr�r�r�r�r�)r
rr.rDr-r.r)r.r�test_close_fds_after_preexecB
s
z1POSIXProcessTestCase.test_close_fds_after_preexecc Cs�ddl}tj�}z�dd�}tj�x�ddgddgfdgdddgfdgdgddgfdgdgddffD]L\}}}}|jt��0|j||df||d
dddd	d
dddd|�WdQRXqbWWd|s�tj�XdS)NrcSsdS)Nrrrrrr�]
sz5POSIXProcessTestCase.test_fork_exec.<locals>.<lambda>�{sexesenvsargTr\rArWr����rOrOrO)r�r�r�r�r8rbr�r�)r
r��
gc_enabled�funcr Zexe_listr~Zenv_listrrr�test_fork_execS
s&z#POSIXProcessTestCase.test_fork_execc Cs�ddl}Gdd�d�}tj�}z�tj�x�ddddd|d�|d�ffD]f}|jtdj|�d��6}|jdgdgd|ddgddd d!ddddddd�WdQRX|jdt	|j
��qFWWd|s�tj�XdS)"Nrc@s eZdZdZdd�Zdd�ZdS)zJPOSIXProcessTestCase.test_fork_exec_sorted_fd_sanity_check.<locals>.BadIntTcSs
||_dS)N)r)r
rrrrrcw
szSPOSIXProcessTestCase.test_fork_exec_sorted_fd_sanity_check.<locals>.BadInt.__init__cSs|jrd|_|jSt�dS)NF)�firstrrS)r
rrr�__int__y
szRPOSIXProcessTestCase.test_fork_exec_sorted_fd_sanity_check.<locals>.BadInt.__int__N)rrrrSrcrTrrrr�BadIntu
srUr\rArWr�rFr���*�?r��r�zfds_to_keep={})rsfalseTsenvr/rO)rOrArWr�rF)rr��)rVrWrXr[)rFr�)r�rZrZr�rOrOrOrO)r�r�r�r�r8rSr�r�rCrr>r�)r
r�rUrPr/r@rrr�%test_fork_exec_sorted_fd_sanity_checkq
s0

z:POSIXProcessTestCase.test_fork_exec_sorted_fd_sanity_checkcCsZtjtjddg�}|�:tjj|d�� }t|j_	|j
�|jj�WdQRXWdQRXdS)Nz-cr�r$)rr&r'r(rr��object�BrokenPipeErrorr-rrjZassert_called_with)r
r!�mock_proc_stdinrrr�,test_communicate_BrokenPipeError_stdin_close�
s
zAPOSIXProcessTestCase.test_communicate_BrokenPipeError_stdin_closecCshtjtjddg�}|�Htjj|d��.}t|j_	|j
d�|jjd�|jj�WdQRXWdQRXdS)Nz-cr�r$sstuff)
rr&r'r(rr�r]r^rMrrj�assert_called_once_withr-)r
r!r_rrr�,test_communicate_BrokenPipeError_stdin_write�
s
zAPOSIXProcessTestCase.test_communicate_BrokenPipeError_stdin_writecCs�tjtjdgtjtjd�}|�`tjj|d��F}tt	j
d��.}t|j_
|j�|j_|jd�|jj�WdQRXWdQRXWdQRXdS)Nz-h)r$r%r$�wbsstuff)rr&r'r(r)rr�r]r�r]r*r^r�rr��return_valuerjra)r
r!r_Zdev_nullrrr�,test_communicate_BrokenPipeError_stdin_flush�
s

zAPOSIXProcessTestCase.test_communicate_BrokenPipeError_stdin_flushcCsftjtjdgtjtjd�}|�>tjj|d��$}t|j	_
|jdd�|j	j�WdQRXWdQRXdS)Nz-h)r$r%r$i�)r7)
rr&r'r(r)rr�r]r^r-rrjra)r
r!r_rrr�9test_communicate_BrokenPipeError_stdin_close_with_timeout�
s
zNPOSIXProcessTestCase.test_communicate_BrokenPipeError_stdin_close_with_timeoutN�
W_STOPCODEzneed _testcapi.W_STOPCODEc
Csxtjddg}tj|�}|j}tj|d�\}}|j|d�tj	d�}t
jd||fd��|j�}WdQRX|j|d�dS)Nz-cr�rrWzsubprocess.os.waitpid)rd���)
r'r(rr&rFr]rKr�	_testcapirgrr�r)r
r r!rF�statusr?rrr�test_stopped�
s

z!POSIXProcessTestCase.test_stopped)r�r�)crrrr
rurvrwrxrr&rzrr�r�r�r�r�r�r�r�r�r�r�r�rUrVr]rtr>r�r�r'r[r�r�rhr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrr!r'r0�stat�st_devr2rZrequires_mac_verr6r7r<r>r?r@rArCrJrLrM�cpython_onlyrRr\r`rbrerfr]rir^rk�
__classcell__rr)rprrm�s�%


-0,#)0Q""(	
rmzWindows specific testsc@s�eZdZdd�Zdd�Zdd�Zejdd��Zd	d
�Z	dd�Z
d
d�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#S)$�Win32ProcessTestCasecCs6d}d}tj�}||_||_tjtjddg|d�dS)Nr\rWz-czimport sys; sys.exit(0))r�)rZSTARTUPINFOZdwFlagsZwShowWindowr4r'r()r
ZSTARTF_USESHOWWINDOWZSW_MAXIMIZEr�rrr�test_startupinfo�
sz%Win32ProcessTestCase.test_startupinfocCs(d}tjjd�tjtjd|d�dS)N�z'    a DOS box should flash briefly ...
z# -c "import time; time.sleep(0.25)")r�)r'rrMrr4r()r
ZCREATE_NEW_CONSOLErrr�test_creationflags�
s
z'Win32ProcessTestCase.test_creationflagscCsD|jttjtjddgdd�d�|jttjtjddgtjdd�dS)Nz-czimport sys; sys.exit(47)cSsdS)Nr\rrrrrr��
sz8Win32ProcessTestCase.test_invalid_args.<locals>.<lambda>)r�T)r%r�)r8rSrr4r'r(r))r
rrrrh�
s

z&Win32ProcessTestCase.test_invalid_argscCsBGdd�dt�}|jt��tjtjddg|�d�WdQRXdS)Nc@seZdZdZdS)z4Win32ProcessTestCase.test_issue31471.<locals>.BadEnvN)rrr�keysrrrr�BadEnvsruz-cr�)r[)�dictr8rbrr&r'r()r
rurrr�test_issue31471sz$Win32ProcessTestCase.test_issue31471cCs&tjtjddgdd�}|j|d�dS)Nz-czimport sys; sys.exit(47)T)r�r3)rr4r'r(r)r
r5rrrr0
s
z#Win32ProcessTestCase.test_close_fdsc
CsNtjj�}d|d<tjdgdtj|d�}|�|jd|jj��WdQRXdS)N�physalisrZr�r\)r{r%r[sphysalis)	r]r^r_rr&r)rCr%r�)r
r`r.rrrr�s

z(Win32ProcessTestCase.test_shell_sequencec
CsLtjj�}d|d<tjddtj|d�}|�|jd|jj��WdQRXdS)NrxrZr�r\)r{r%r[sphysalis)	r]r^r_rr&r)rCr%r�)r
r`r.rrrr�s
z&Win32ProcessTestCase.test_shell_stringcCs^xXdD]P}tjj�}d|d<tjddtj||d�}|�|jd|jj�|�WdQRXqWdS)	N�ansi�oemrxrZr�r\)r{r%r[r�)ryrz)	r]r^r_rr&r)rCr%r�)r
�encr`r.rrr�test_shell_encodings%s

z)Win32ProcessTestCase.test_shell_encodingscCs tjtjd�}|j|d�dS)Nz -c "import sys; sys.exit(47)"r3)rr4r'r(r)r
r5rrrr�1sz%Win32ProcessTestCase.test_call_stringc
Gsztjtjddgtjtjtjd�}|�@|jjd�t||�|�|j�\}}|j	|d�|j
�}WdQRX|j|d�dS)Nz-cz�if 1:
                             import sys, time
                             sys.stdout.write('x\n')
                             sys.stdout.flush()
                             time.sleep(30)
                             )r$r%rr\r�r)rr&r'r(r)r%r�r�rjrrr�)r
r�r r.r�rr?rrrr�7s

z"Win32ProcessTestCase._kill_processc
Gs�tjtjddgtjtjtjd�}|�J|jjd�tjd�t	||�|�|j
�\}}|j|d�|j�}WdQRX|j
|d�dS)Nz-cz�if 1:
                             import sys, time
                             sys.stdout.write('x\n')
                             sys.stdout.flush()
                             sys.exit(42)
                             )r$r%rr\r�rX)rr&r'r(r)r%r�rr%r�rjrrr)r
r�r r.r�rr5rrrr�Ls


z'Win32ProcessTestCase._kill_dead_processcCs|jdtj�dS)Nr�)r�rHr�)r
rrrr�csz%Win32ProcessTestCase.test_send_signalcCs|jd�dS)Nr)r�)r
rrrr�fszWin32ProcessTestCase.test_killcCs|jd�dS)Nr�)r�)r
rrrr�isz#Win32ProcessTestCase.test_terminatecCs|jdtj�dS)Nr�)r�rHr�)r
rrrr�lsz*Win32ProcessTestCase.test_send_signal_deadcCs|jd�dS)Nr)r�)r
rrrr�osz#Win32ProcessTestCase.test_kill_deadcCs|jd�dS)Nr�)r�)r
rrrr�rsz(Win32ProcessTestCase.test_terminate_deadN)rrrrqrsrhrrnrwr0r�r�r|r�r�r�r�r�r�r�r�r�rrrrrp�
s"	

rpc@seZdZdd�Zdd�ZdS)�	MiscTestscCs�|jtjd�d�|jtjd�d�d}z@tj�}tjj|d�}tjt	rLdnd|�\}}|j
|d�Wd|dk	r|tj|�XdS)Nz
echo xyzzy�xyzzyrZfooztype zcat )rr~)rrZ	getoutputZgetstatusoutputrJr�r]rtrur�r��rmdir)r
�dirr�rjrDrrr�test_getoutputvszMiscTests.test_getoutputcCsrddh}ttj�}t�}ddl}x<tjj�D].\}}|jd�r@q,t||jf�rPq,|j	|�q,W|j
|||�dS)NrZHandlerr�)r�r�__all__�types�__dict__�itemsr��
isinstance�
ModuleTyper+r)r
Zintentionally_excludedZexportedZpossible_exportsr�r�rrrr�test__all__�s

zMiscTests.test__all__N)rrrr�r�rrrrr}usr}ZPollSelectorz!Test needs selectors.PollSelectorc@seZdZdd�Zdd�ZdS)�ProcessTestCaseNoPollcCstj|_tjt_tj|�dS)N)r�_PopenSelector�
orig_selector�	selectorsZSelectSelectorr#r
)r
rrrr
�szProcessTestCaseNoPoll.setUpcCs|jt_tj|�dS)N)r�rr�r#r)r
rrrr�szProcessTestCaseNoPoll.tearDownN)rrrr
rrrrrr��sr�zWindows-specific testscsPeZdZ�fdd�Z�fdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	�Z
S)�CommandsWithSpacescs>t�j�tjdd�\}}|j�|_tj|d�tj|�dS)Nz.pyzte stsVimport sys;sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv])))	rnr
rJr<r�r�r]rMr-)r
r�r�)rprrr
�s


zCommandsWithSpaces.setUpcstj|j�t�j�dS)N)r]r=r�rnr)r
)rprrr�szCommandsWithSpaces.tearDowncOsHtj|d<tj||�}|�$|j|jj�jd�d|j�WdQRXdS)Nr%�mbcsz2 [%r, 'ab cd'])rr)r&rr%r�r�r�)r
r r!r.rrr�with_spaces�s
zCommandsWithSpaces.with_spacescCs |jdtj|jdfdd�dS)Nz"%s" "%s" "%s"zab cdr\)r{)r�r'r(r�)r
rrr�test_shell_string_with_spaces�sz0CommandsWithSpaces.test_shell_string_with_spacescCs|jtj|jdgdd�dS)Nzab cdr\)r{)r�r'r(r�)r
rrr�test_shell_sequence_with_spaces�sz2CommandsWithSpaces.test_shell_sequence_with_spacescCs|jdtj|jdf�dS)Nz"%s" "%s" "%s"zab cd)r�r'r(r�)r
rrr�test_noshell_string_with_spaces�sz2CommandsWithSpaces.test_noshell_string_with_spacescCs|jtj|jdg�dS)Nzab cd)r�r'r(r�)r
rrr�!test_noshell_sequence_with_spaces�sz4CommandsWithSpaces.test_noshell_sequence_with_spaces)rrrr
rr�r�r�r�r�rorr)rprr��s		r�c@s4eZdZdd�Zdd�Zdd�Zdd�Zd	d
�ZdS)�ContextManagerTestsc
Csltjtjddgtjtjd��*}|j|jj�d�|j|j	j�d�WdQRX|j
|jj�|j
|j	j�dS)Nz-czAimport sys;sys.stdout.write('stdout');sys.stderr.write('stderr');)r%rsstdoutsstderr)rr&r'r(r)rr%r�rrr�r�)r
r!rrr�	test_pipe�s
zContextManagerTests.test_pipec
Cs0tjtjddg��}WdQRX|j|jd�dS)Nz-czimport sys; sys.exit(100)�d)rr&r'r(rr?)r
r!rrrrd�s


z#ContextManagerTests.test_returncodec
Cs@tjtjddgtjd��}|jd�|j|jd�WdQRXdS)Nz-cz2import sys;sys.exit(sys.stdin.read() == 'context'))r$scontextr\)rr&r'r(r)rjrr?)r
r!rrrr��s


z*ContextManagerTests.test_communicate_stdincCs:|jt��&tjttjtjd��}WdQRXWdQRXdS)N)r%r)r8rxrr&ryr))r
r!rrrrh�s
z%ContextManagerTests.test_invalid_argscCsztjtjddgtjtjdd�}|j�}|jj	dtj�|j
|j�|jt
|jddd�|j|jd�|j|jj�dS)Nz-cr�rA)r$r0r@r)rr&r'r(r)rr��	__enter__r$rMrr?r8r��__exit__rr�r�)r
r!rrr�test_broken_pipe_cleanup�sz,ContextManagerTests.test_broken_pipe_cleanupN)rrrr�rdr�rhr�rrrrr��s

r��__main__)r)7rUrrrrr'r[rHr+rr]r�rJrr�rYr&r�r�r�Ztest.supportrZctypesr Zctypes.utilr)riZPGOZSkipTestr�r�ryr��NotADirectoryError�PermissionErrorrxZTestCaser�	Exceptionrr&rr#r`rVrmr]rpr}r^r�r�r�r�mainrrrr�<module>s�





_#+6


Youez - 2016 - github.com/yon3zu
LinuXploit