B
    u9aÃ% ã               @   sF  d dl Z d dl mZ d dlmZ d dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlmZ yd dlZW n ek
rØ   dZY n
X d dlZyd dlZW n ek
r   dZY nX ejre  d¡‚ejdkZer2dZndZd	Ze e!e"fZ#G d
d„ de j$ƒZ%G dd„ de&ƒZ'G dd„ dej(ƒZ)G dd„ de%ƒZ*G dd„ de%ƒZ+e  ,ed¡G dd„ de%ƒƒZ-e  .ed¡G dd„ de%ƒƒZ/G dd„ de j$ƒZ0e  .e1edƒd¡G dd„ de*ƒƒZ2e  .ed ¡G d!d"„ d"e%ƒƒZ3G d#d$„ d$e%ƒZ4e5d%krBe  6¡  dS )&é    N)Úmock)Úsupport)ÚFakePathztest is not helpful for PGOÚwin32z@import msvcrt; msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY);Ú )Znonexisting_i_hopec               @   s&   e Zd Zdd„ Zdd„ Zddd„ZdS )	ÚBaseTestCasec             C   s   t  ¡  d S )N)r   Úreap_children)Úself© r
   ú%/usr/lib/python3.7/test_subprocess.pyÚsetUp8   s    zBaseTestCase.setUpc             C   sB   xt jD ]}| ¡  qW t  ¡  |  t jd¡ |  ¡  t ¡  d S )Nzsubprocess._active not empty)Ú
subprocessÚ_activeÚwaitZ_cleanupÚassertFalseZ
doCleanupsr   r   )r	   Úinstr
   r
   r   ÚtearDown=   s    zBaseTestCase.tearDownNc             C   s$   t  |¡}| ¡ }|  |||¡ d S )N)r   Ústrip_python_stderrÚstripÚassertEqual)r	   ÚstderrÚexpectedÚmsgÚactualr
   r
   r   ÚassertStderrEqualE   s    
zBaseTestCase.assertStderrEqual)N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r
   r
   r
   r   r   7   s   r   c               @   s   e Zd ZdS )ÚPopenTestExceptionN)r   r   r   r
   r
   r
   r   r   O   s   r   c               @   s   e Zd ZdZdd„ ZdS )ÚPopenExecuteChildRaiseszePopen subclass for testing cleanup of subprocess.PIPE filehandles when
    _execute_child fails.
    c             O   s   t dƒ‚d S )NzForced Exception for Test)r   )r	   ÚargsÚkwargsr
   r
   r   Ú_execute_childW   s    z&PopenExecuteChildRaises._execute_childN)r   r   r   Ú__doc__r"   r
   r
   r
   r   r   S   s   r   c               @   sÚ  e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Ze ed-¡d.d/„ ƒZd0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Z d8d9„ Z!e ed:¡d;d<„ ƒZ"e ed:¡d=d>„ ƒZ#d?d@„ Z$e e%j&e%j'kdA¡dBdC„ ƒZ(e e%j&e%j'kdA¡e e) *¡ 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<e e%j=dikdj¡e e) >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„ ZOdd‘„ 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¹„ Zde eefegdºƒd»¡e eefehd¼ƒd½¡e eefehd¾ƒd¿¡dÀdÁ„ ƒƒƒZie eehjj kdÂeh l¡  ¡dÃ¡dÄdÅ„ ƒZme edÆ¡dÇdÈ„ ƒZne edÆ¡dÉdÊ„ ƒZodËS )ÌÚProcessTestCasec             C   s‚   t jtjddgt jt jt jd}z4|  |jtj¡ |  |j	tj¡ |  |j
tj¡ W d |j ¡  |j	 ¡  |j
 ¡  | ¡  X d S )Nz-czimport sys; sys.exit(0))ÚstdinÚstdoutr   )r   ÚPopenÚsysÚ
executableÚPIPEÚassertIsInstancer%   ÚioÚBufferedIOBaser&   r   Úcloser   )r	   Úpr
   r
   r   Útest_io_buffered_by_default]   s    



z+ProcessTestCase.test_io_buffered_by_defaultc             C   s„   t jtjddgt jt jt jdd}z4|  |jtj¡ |  |j	tj¡ |  |j
tj¡ W d |j ¡  |j	 ¡  |j
 ¡  | ¡  X d S )Nz-czimport sys; sys.exit(0)r   )r%   r&   r   Úbufsize)r   r'   r(   r)   r*   r+   r%   r,   Ú	RawIOBaser&   r   r.   r   )r	   r/   r
   r
   r   Útest_io_unbuffered_worksk   s    


z(ProcessTestCase.test_io_unbuffered_worksc             C   s"   t  tjddg¡}|  |d¡ d S )Nz-czimport sys; sys.exit(47)é/   )r   Úcallr(   r)   r   )r	   Úrcr
   r
   r   Útest_call_seqy   s    
zProcessTestCase.test_call_seqc             C   s"   | j tjtjtjddgdd d S )Nz-czwhile True: passgš™™™™™¹?)Útimeout)ÚassertRaisesr   ÚTimeoutExpiredr5   r(   r)   )r	   r
   r
   r   Útest_call_timeout   s    
z!ProcessTestCase.test_call_timeoutc             C   s"   t  tjddg¡}|  |d¡ d S )Nz-czimport sys; sys.exit(0)r   )r   Ú
check_callr(   r)   r   )r	   r6   r
   r
   r   Útest_check_call_zeroˆ   s    
z$ProcessTestCase.test_check_call_zeroc          	   C   s>   |   tj¡}t tjddg¡ W d Q R X |  |jjd¡ d S )Nz-czimport sys; sys.exit(47)r4   )	r9   r   ÚCalledProcessErrorr<   r(   r)   r   Ú	exceptionÚ
returncode)r	   Úcr
   r
   r   Útest_check_call_nonzeroŽ   s    
z'ProcessTestCase.test_check_call_nonzeroc             C   s"   t  tjddg¡}|  d|¡ d S )Nz-czprint('BDFL')s   BDFL)r   Úcheck_outputr(   r)   ÚassertIn)r	   Úoutputr
   r
   r   Útest_check_output•   s    z!ProcessTestCase.test_check_outputc          	   C   s>   |   tj¡}t tjddg¡ W d Q R X |  |jjd¡ d S )Nz-czimport sys; sys.exit(5)é   )	r9   r   r>   rC   r(   r)   r   r?   r@   )r	   rA   r
   r
   r   Útest_check_output_nonzero›   s    z)ProcessTestCase.test_check_output_nonzeroc             C   s(   t jtjddgt jd}|  d|¡ d S )Nz-cz$import sys; sys.stderr.write('BDFL'))r   s   BDFL)r   rC   r(   r)   ÚSTDOUTrD   )r	   rE   r
   r
   r   Útest_check_output_stderr¢   s    

z(ProcessTestCase.test_check_output_stderrc             C   sN   t  ¡ }|  |j¡ | d¡ | d¡ tjtj	ddg|d}|  
d|¡ d S )Ns   pearr   z-cz6import sys; sys.stdout.write(sys.stdin.read().upper()))r%   s   PEAR)ÚtempfileÚTemporaryFileÚ
addCleanupr.   ÚwriteÚseekr   rC   r(   r)   rD   )r	   ÚtfrE   r
   r
   r   Útest_check_output_stdin_arg©   s    

z+ProcessTestCase.test_check_output_stdin_argc             C   s&   t jtjddgdd}|  d|¡ d S )Nz-cz6import sys; sys.stdout.write(sys.stdin.read().upper())s   pear)Úinputs   PEAR)r   rC   r(   r)   rD   )r	   rE   r
   r
   r   Útest_check_output_input_argµ   s
    z+ProcessTestCase.test_check_output_input_argc          	   C   sP   |   t¡(}tjtjddgtjd}|  d¡ W d Q R X |  d|j	j
d ¡ d S )Nz-czprint('will not be run'))r&   z-Expected ValueError when stdout arg supplied.r&   r   )r9   Ú
ValueErrorr   rC   r(   r)   r&   ÚfailrD   r?   r    )r	   rA   rE   r
   r
   r   Útest_check_output_stdout_arg½   s    

z,ProcessTestCase.test_check_output_stdout_argc          	   C   sŒ   t  ¡ }|  |j¡ | d¡ | d¡ |  t¡(}tj	t
jddg|dd}|  d¡ W d Q R X |  d|jjd ¡ |  d	|jjd ¡ d S )
Ns   pearr   z-czprint('will not be run')s   hare)r%   rR   z7Expected ValueError when stdin and input args supplied.r%   rR   )rK   rL   rM   r.   rN   rO   r9   rT   r   rC   r(   r)   rU   rD   r?   r    )r	   rP   rA   rE   r
   r
   r   Ú&test_check_output_stdin_with_input_argÆ   s    



z6ProcessTestCase.test_check_output_stdin_with_input_argc          	   C   sL   |   tj¡&}tjtjddgdd}|  d¡ W d Q R X |  |jj	d¡ d S )Nz-czMimport sys, time
sys.stdout.write('BDFL')
sys.stdout.flush()
time.sleep(3600)é   )r8   zExpected TimeoutExpired.s   BDFL)
r9   r   r:   rC   r(   r)   rU   r   r?   rE   )r	   rA   rE   r
   r
   r   Útest_check_output_timeoutÔ   s    z)ProcessTestCase.test_check_output_timeoutc             C   s8   t j ¡ }d|d< tjtjddg|d}|  |d¡ d S )NÚbananaÚFRUITz-cz5import sys, os;sys.exit(os.getenv("FRUIT")=="banana"))Úenvé   )ÚosÚenvironÚcopyr   r5   r(   r)   r   )r	   Únewenvr6   r
   r
   r   Útest_call_kwargsã   s    

z ProcessTestCase.test_call_kwargsc          	   C   sh   t  ¡ F}| jttjdd tjjjj}dg|d  }| jttjf|žŽ  W d Q R X |  	| 
¡ d¡ d S )Nr]   )Zinvalid_arg_namer   r   )r   Zcaptured_stderrr9   Ú	TypeErrorr   r'   Ú__init__Ú__code__Úco_argcountr   Úgetvalue)r	   ÚsZargcountZtoo_many_argsr
   r
   r   Útest_invalid_argsí   s    
z!ProcessTestCase.test_invalid_argsc             C   sR   t jtjddgt jt jd}|  |jj¡ |  |jj¡ | 	¡  |  
|jd ¡ d S )Nz-czprint("banana"))r&   r   )r   r'   r(   r)   r*   rM   r&   r.   r   r   r   r%   )r	   r/   r
   r
   r   Útest_stdin_none÷   s    zProcessTestCase.test_stdin_nonec             C   sl   d}t jtjd|gt jt jd}|  |jj¡ |  |jj¡ | 	¡ \}}|  
|jd|¡ |  
| ¡ d¡ d S )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&   r   r   s   test_stdout_none)r   r'   r(   r)   r*   rM   r&   r.   r   Úcommunicater   r@   Úrstrip)r	   Úcoder/   ÚoutÚerrr
   r
   r   Útest_stdout_none   s    z ProcessTestCase.test_stdout_nonec             C   sR   t jtjddgt jt jd}|  |jj¡ |  |jj¡ | 	¡  |  
|jd ¡ d S )Nz-czprint("banana"))r%   r&   )r   r'   r(   r)   r*   rM   r&   r.   r%   r   r   r   )r	   r/   r
   r
   r   Útest_stderr_none  s    z ProcessTestCase.test_stderr_nonec             K   s2   |dg }t j|f|Ž}| ¡  |  d|j¡ d S )Nzimport sys; sys.exit(47)r4   )r   r'   r   r   r@   )r	   Úpre_argsr!   r    r/   r
   r
   r   Ú_assert_python   s    
zProcessTestCase._assert_pythonc             C   s0   t j t j tj¡d¡}| j|dgtjd d S )NÚdoesnotexistz-c)r)   )r^   ÚpathÚjoinÚdirnamer(   r)   rs   )r	   rt   r
   r
   r   Útest_executable(  s    zProcessTestCase.test_executablec             C   s0   t jdg}|  |¡ | jt| j|td d d S )Nz-cr   )r)   )r(   r)   rs   r9   ÚNONEXISTING_ERRORSÚNONEXISTING_CMD)r	   rr   r
   r
   r   Ú test_executable_takes_precedence3  s
    

z0ProcessTestCase.test_executable_takes_precedencez"executable argument replaces shellc             C   s   | j g tjdd d S )NT)r)   Úshell)rs   r(   r)   )r	   r
   r
   r   Útest_executable_replaces_shell=  s    z.ProcessTestCase.test_executable_replaces_shellc          	   C   s   t  |¡
 t ¡ S Q R X d S )N)r   Z
change_cwdr^   Úgetcwd)r	   Úcwdr
   r
   r   Ú_normalize_cwdD  s    zProcessTestCase._normalize_cwdc             C   s   t j tj¡}t j |¡S )N)r^   ru   Úrealpathr(   r)   Úsplit)r	   Zpython_pathr
   r
   r   Ú_split_python_pathL  s    z"ProcessTestCase._split_python_pathc             K   sn   t j|ddgfdt ji|—Ž}|  |jj¡ | ¡  |  d|j¡ t	j
j}|  ||ƒ||j ¡  d¡ƒ¡ d S )Nz-cz;import os, sys; sys.stdout.write(os.getcwd()); sys.exit(47)r&   r4   zutf-8)r   r'   r*   rM   r&   r.   r   r   r@   r^   ru   ÚnormcaseÚreadÚdecode)r	   Zexpected_cwdZ
python_argr!   r/   r„   r
   r
   r   Ú_assert_cwdR  s    
zProcessTestCase._assert_cwdc             C   s(   t  ¡ }|  |¡}| j|tj|d d S )N)r   )rK   Ú
gettempdirr€   r‡   r(   r)   )r	   Útemp_dirr
   r
   r   Útest_cwdc  s    
zProcessTestCase.test_cwdc             C   s,   t  ¡ }|  |¡}| j|tjt|ƒd d S )N)r   )rK   rˆ   r€   r‡   r(   r)   r   )r	   r‰   r
   r
   r   Útest_cwd_with_pathlikei  s    
z&ProcessTestCase.test_cwd_with_pathlikez"pending resolution of issue #15533c          	   C   sv   |   ¡ \}}tj tj|¡}t ¡ H}|  tt	j
|g¡ | jtt	j
|g|d |  |¡}| j|||d W d Q R X d S )N)r   )rƒ   r^   ru   rv   Úcurdirr   Útemp_cwdr9   ÚFileNotFoundErrorr   r'   r€   r‡   )r	   Ú
python_dirÚpython_baseÚ
rel_pythonÚ	wrong_dirr
   r
   r   Útest_cwd_with_relative_argn  s    



z*ProcessTestCase.test_cwd_with_relative_argc          	   C   s‚   |   ¡ \}}tj tj|¡}d}t ¡ P}| jtt	j
|g|d | jtt	j
|g||d |  |¡}| j||||d W d Q R X d S )NÚsomethingyoudonthave)r)   )r)   r   )rƒ   r^   ru   rv   rŒ   r   r   r9   rŽ   r   r'   r€   r‡   )r	   r   r   r‘   Zdoesntexistr’   r
   r
   r   Ú!test_cwd_with_relative_executable~  s    




z1ProcessTestCase.test_cwd_with_relative_executablec          	   C   sr   |   ¡ \}}tj ||¡}tj tj|¡}t ¡ 6}| jtt	j
|g|d |  |¡}| j|||d W d Q R X d S )N)r   )rƒ   r^   ru   rv   rŒ   r   r‰   r9   rŽ   r   r'   r€   r‡   )r	   r   r   Z
abs_pythonr‘   r’   r
   r
   r   Útest_cwd_with_absolute_arg‘  s    


z*ProcessTestCase.test_cwd_with_absolute_argzTest is not venv-compatiblec             C   s.   |   ¡ \}}|  |¡}| j|dtj|d d S )Nr”   )r)   r   )rƒ   r€   r‡   r(   r)   )r	   r   r   r
   r
   r   Útest_executable_with_cwdŸ  s    
z(ProcessTestCase.test_executable_with_cwdz#need an installed Python. See #7774c             C   s   | j t ¡ dtjd d S )Nr”   )r)   )r‡   r^   r~   r(   r)   )r	   r
   r
   r   Útest_executable_without_cwd§  s    z+ProcessTestCase.test_executable_without_cwdc             C   sH   t jtjddgt jd}|j d¡ |j ¡  | ¡  |  	|j
d¡ d S )Nz-cz0import sys; sys.exit(sys.stdin.read() == "pear"))r%   s   pearr]   )r   r'   r(   r)   r*   r%   rN   r.   r   r   r@   )r	   r/   r
   r
   r   Útest_stdin_pipe±  s    


zProcessTestCase.test_stdin_pipec             C   sf   t  ¡ }|  |j¡ | ¡ }t |d¡ t |dd¡ tj	t
jddg|d}| ¡  |  |jd¡ d S )Ns   pearr   z-cz0import sys; sys.exit(sys.stdin.read() == "pear"))r%   r]   )rK   rL   rM   r.   Úfilenor^   rN   Úlseekr   r'   r(   r)   r   r   r@   )r	   rP   Údr/   r
   r
   r   Útest_stdin_filedes»  s    
z"ProcessTestCase.test_stdin_filedesc             C   sX   t  ¡ }|  |j¡ | d¡ | d¡ tjtj	ddg|d}| 
¡  |  |jd¡ d S )Ns   pearr   z-cz0import sys; sys.exit(sys.stdin.read() == "pear"))r%   r]   )rK   rL   rM   r.   rN   rO   r   r'   r(   r)   r   r   r@   )r	   rP   r/   r
   r
   r   Útest_stdin_fileobjÈ  s    


z"ProcessTestCase.test_stdin_fileobjc          	   C   s>   t jtjddgt jd}| |  |j ¡ d¡ W d Q R X d S )Nz-cz&import sys; sys.stdout.write("orange"))r&   s   orange)r   r'   r(   r)   r*   r   r&   r…   )r	   r/   r
   r
   r   Útest_stdout_pipeÔ  s
    

z ProcessTestCase.test_stdout_pipec             C   s`   t  ¡ }|  |j¡ | ¡ }tjtjddg|d}| 	¡  t
 |dd¡ |  t
 |d¡d¡ d S )Nz-cz&import sys; sys.stdout.write("orange"))r&   r   i   s   orange)rK   rL   rM   r.   rš   r   r'   r(   r)   r   r^   r›   r   r…   )r	   rP   rœ   r/   r
   r
   r   Útest_stdout_filedesÜ  s    
z#ProcessTestCase.test_stdout_filedesc             C   sP   t  ¡ }|  |j¡ tjtjddg|d}| ¡  | 	d¡ |  
| ¡ d¡ d S )Nz-cz&import sys; sys.stdout.write("orange"))r&   r   s   orange)rK   rL   rM   r.   r   r'   r(   r)   r   rO   r   r…   )r	   rP   r/   r
   r
   r   Útest_stdout_fileobjè  s    

z#ProcessTestCase.test_stdout_fileobjc          	   C   s>   t jtjddgt jd}| |  |j ¡ d¡ W d Q R X d S )Nz-cz*import sys; sys.stderr.write("strawberry"))r   s
   strawberry)r   r'   r(   r)   r*   r   r   r…   )r	   r/   r
   r
   r   Útest_stderr_pipeó  s
    

z ProcessTestCase.test_stderr_pipec             C   s`   t  ¡ }|  |j¡ | ¡ }tjtjddg|d}| 	¡  t
 |dd¡ |  t
 |d¡d¡ d S )Nz-cz*import sys; sys.stderr.write("strawberry"))r   r   i   s
   strawberry)rK   rL   rM   r.   rš   r   r'   r(   r)   r   r^   r›   r   r…   )r	   rP   rœ   r/   r
   r
   r   Útest_stderr_filedesû  s    
z#ProcessTestCase.test_stderr_filedesc             C   sP   t  ¡ }|  |j¡ tjtjddg|d}| ¡  | 	d¡ |  
| ¡ d¡ d S )Nz-cz*import sys; sys.stderr.write("strawberry"))r   r   s
   strawberry)rK   rL   rM   r.   r   r'   r(   r)   r   rO   r   r…   )r	   rP   r/   r
   r
   r   Útest_stderr_fileobj  s    

z#ProcessTestCase.test_stderr_fileobjc             C   sR   t jtjddgt jt jd}| ¡ \}}|  |d¡ |  |d¡ |  |jd¡ d S )Nz-cz›import sys, subprocess;rc = subprocess.call([sys.executable, "-c",    "import sys;"    "sys.stderr.write('42')"],    stderr=subprocess.STDOUT);sys.exit(rc))r&   r   s   42ó    r   )	r   r'   r(   r)   r*   rk   r   r   r@   )r	   r/   r&   r   r
   r
   r   Ú,test_stderr_redirect_with_no_stdout_redirect  s    

z<ProcessTestCase.test_stderr_redirect_with_no_stdout_redirectc          	   C   sB   t jtjddgt jt jd}| |  |j ¡ d¡ W d Q R X d S )Nz-czRimport sys;sys.stdout.write("apple");sys.stdout.flush();sys.stderr.write("orange"))r&   r   s   appleorange)	r   r'   r(   r)   r*   rI   r   r&   r…   )r	   r/   r
   r
   r   Útest_stdout_stderr_pipe'  s    

z'ProcessTestCase.test_stdout_stderr_pipec             C   sR   t  ¡ }|  |j¡ tjtjddg||d}| ¡  | 	d¡ |  
| ¡ d¡ d S )Nz-czRimport sys;sys.stdout.write("apple");sys.stdout.flush();sys.stderr.write("orange"))r&   r   r   s   appleorange)rK   rL   rM   r.   r   r'   r(   r)   r   rO   r   r…   )r	   rP   r/   r
   r
   r   Útest_stdout_stderr_file3  s    

z'ProcessTestCase.test_stdout_stderr_filec             C   sl   d}t jtjd|gt jt jd}|  |jj¡ |  |jj¡ | 	¡ \}}|  
|jd|¡ |  
| ¡ d¡ d S )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&   r   r   s   test with stdout=1)r   r'   r(   r)   r*   rM   r&   r.   r   rk   r   r@   rl   )r	   rm   r/   rn   ro   r
   r
   r   Útest_stdout_filedes_of_stdoutB  s    z-ProcessTestCase.test_stdout_filedes_of_stdoutc             C   s2   t jtjddgt jd}| ¡  |  |jd ¡ d S )Nz-cz'for i in range(10240):print("x" * 1024))r&   )r   r'   r(   r)   ÚDEVNULLr   r   r&   )r	   r/   r
   r
   r   Útest_stdout_devnullU  s
    

z#ProcessTestCase.test_stdout_devnullc             C   s2   t jtjddgt jd}| ¡  |  |jd ¡ d S )Nz-cz=import sys
for i in range(10240):sys.stderr.write("x" * 1024))r   )r   r'   r(   r)   rª   r   r   r   )r	   r/   r
   r
   r   Útest_stderr_devnull]  s
    

z#ProcessTestCase.test_stderr_devnullc             C   s2   t jtjddgt jd}| ¡  |  |jd ¡ d S )Nz-czimport sys;sys.stdin.read(1))r%   )r   r'   r(   r)   rª   r   r   r%   )r	   r/   r
   r
   r   Útest_stdin_devnullf  s
    

z"ProcessTestCase.test_stdin_devnullc          	   C   sT   t j ¡ }d|d< tjtjddgtj|d}| ¡ \}}|  	|d¡ W d Q R X d S )NÚoranger[   z-cz2import sys,os;sys.stdout.write(os.getenv("FRUIT")))r&   r\   s   orange)
r^   r_   r`   r   r'   r(   r)   r*   rk   r   )r	   ra   r/   r&   r   r
   r
   r   Útest_envn  s    


zProcessTestCase.test_envr   z#cannot test an empty env on WindowsZPy_ENABLE_SHAREDr]   zEThe Python shared library cannot be loaded with an empty environment.c          	      st   dd„ ‰ t jtjddgt ji dH}| ¡ \}}t| ¡ ƒ}|  |t	¡ ‡ fdd„|D ƒ}|  
|g ¡ W dQ R X dS )	z+Verify that env={} is as empty as possible.c             S   s2   d| kp0d| kp0d| kp0| dkp0|   d¡p0| dkS )z:Determine if an environment variable is under our control.Z	VERSIONERZ__CFÚ__PYVENV_LAUNCHER__Z
LD_PRELOADZSANDBOXÚLC_CTYPE)Ú
startswith)Únr
   r
   r   Úis_env_var_to_ignoreƒ  s    z<ProcessTestCase.test_empty_env.<locals>.is_env_var_to_ignorez-cz)import os; print(list(os.environ.keys())))r&   r\   c                s   g | ]}ˆ |ƒs|‘qS r
   r
   )Ú.0Úk)r´   r
   r   ú
<listcomp>“  s    z2ProcessTestCase.test_empty_env.<locals>.<listcomp>N)r   r'   r(   r)   r*   rk   Úevalr   r+   Úlistr   )r	   r/   r&   r   Zchild_env_namesr
   )r´   r   Útest_empty_env{  s    

zProcessTestCase.test_empty_envc          	   C   s\   t jd }|  t¡ t |ddg¡ W d Q R X |  t¡ t t jddg¡ W d Q R X d S )Nú z-cÚpasszpass# )r(   r)   r9   rT   r   r'   )r	   Úcmdr
   r
   r   Útest_invalid_cmd—  s
    
z ProcessTestCase.test_invalid_cmdc          	   C   s  t j ¡ }d|d< |  t¡ tjtjddg|d W d Q R X t j ¡ }d|d< |  t¡ tjtjddg|d W d Q R X t j ¡ }d|d	< |  t¡ tjtjddg|d W d Q R X t j ¡ }d
|d< tjtjddgtj	|d}| 
¡ \}}|  |d¡ W d Q R X d S )NZcabbagezFRUIT VEGETABLEz-cr¼   )r\   zorange VEGETABLE=cabbager[   ZlemonzFRUIT=ORANGEzorange=lemonz3import sys, os;sys.stdout.write(os.getenv("FRUIT")))r&   r\   s   orange=lemon)r^   r_   r`   r9   rT   r   r'   r(   r)   r*   rk   r   )r	   ra   r/   r&   r   r
   r
   r   Útest_invalid_env¡  s(    
 
 
 


z ProcessTestCase.test_invalid_envc             C   s4   t jtjddgt jd}| d¡ |  |jd¡ d S )Nz-cz/import sys;sys.exit(sys.stdin.read() == "pear"))r%   s   pearr]   )r   r'   r(   r)   r*   rk   r   r@   )r	   r/   r
   r
   r   Útest_communicate_stdin¿  s
    


z&ProcessTestCase.test_communicate_stdinc             C   s@   t jtjddgt jd}| ¡ \}}|  |d¡ |  |d ¡ d S )Nz-cz)import sys; sys.stdout.write("pineapple"))r&   s	   pineapple)r   r'   r(   r)   r*   rk   r   )r	   r/   r&   r   r
   r
   r   Útest_communicate_stdoutÇ  s    

z'ProcessTestCase.test_communicate_stdoutc             C   s@   t jtjddgt jd}| ¡ \}}|  |d ¡ |  |d¡ d S )Nz-cz)import sys; sys.stderr.write("pineapple"))r   s	   pineapple)r   r'   r(   r)   r*   rk   r   r   )r	   r/   r&   r   r
   r
   r   Útest_communicate_stderrÏ  s    

z'ProcessTestCase.test_communicate_stderrc             C   st   t jtjddgt jt jt jd}|  |jj¡ |  |jj¡ |  |j	j¡ | 
d¡\}}|  |d¡ |  |d¡ d S )Nz-czNimport sys,os;sys.stderr.write("pineapple");sys.stdout.write(sys.stdin.read()))r%   r&   r   s   bananas	   pineapple)r   r'   r(   r)   r*   rM   r&   r.   r   r%   rk   r   r   )r	   r/   r&   r   r
   r
   r   Útest_communicate×  s    

z ProcessTestCase.test_communicatec             C   sd   t jtjddgdt jt jt jd}| jt j|jddd | ¡ \}}|  |d¡ |  	| 
¡ d¡ d S )	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&   r   rZ   g333333Ó?)r8   s   pineapple
pear
)r   r'   r(   r)   r*   r9   r:   rk   r   r   Úencode)r	   r/   r&   r   r
   r
   r   Útest_communicate_timeoutæ  s    

z(ProcessTestCase.test_communicate_timeoutc             C   sL   t jtjddgt jd}| jt j|jdd | ¡ \}}|  t	|ƒd¡ d S )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š™™™™™Ù?)r8   i   )
r   r'   r(   r)   r*   r9   r:   rk   r   Úlen)r	   r/   r&   Ú_r
   r
   r   Ú%test_communicate_timeout_large_outputù  s    

z5ProcessTestCase.test_communicate_timeout_large_outputc             C   sÈ   xÂdD ]º}x´dD ]¬}x¦dD ]ž}i }|r0t j|d< |r>t j|d< |rLt j|d< |sRqt jtjddff|Ž}| ¡  |jd k	rˆ|  |jj¡ |j	d k	r |  |j	j¡ |j
d k	r|  |j
j¡ qW qW qW d S )N)FTr%   r&   r   z-cr¼   )r   r*   r'   r(   r)   rk   r%   Ú
assertTrueÚclosedr&   r   )r	   Z
stdin_pipeZstdout_pipeZstderr_pipeÚoptionsr/   r
   r
   r   Útest_communicate_pipe_fd_leak
  s(    








z-ProcessTestCase.test_communicate_pipe_fd_leakc             C   s:   t  tjddg¡}| ¡ \}}|  |d ¡ |  |d ¡ d S )Nz-czimport sys; sys.exit(47))r   r'   r(   r)   rk   r   )r	   r/   r&   r   r
   r
   r   Útest_communicate_returns   s
    
z(ProcessTestCase.test_communicate_returnsc             C   s˜   t  ¡ \}}t  |¡ t  |¡ tjtjddtj gtj	tj	tj	d}|  
|jj¡ |  
|jj¡ |  
|jj¡ dtj }| |¡\}}|  ||¡ d S )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.   r   r'   r(   r)   r   ÚPIPE_MAX_SIZEr*   rM   r&   r   r%   rk   r   )r	   ÚxÚyr/   Zstring_to_writer&   r   r
   r
   r   Útest_communicate_pipe_buf(  s    




z)ProcessTestCase.test_communicate_pipe_bufc             C   s€   t jtjddgt jt jt jd}|  |jj¡ |  |jj¡ |  |j	j¡ |j	 
d¡ | d¡\}}|  |d¡ |  |d¡ d S )Nz-cz0import sys,os;sys.stdout.write(sys.stdin.read()))r%   r&   r   s   bananas   splits   bananasplitr¥   )r   r'   r(   r)   r*   rM   r&   r.   r   r%   rN   rk   r   r   )r	   r/   r&   r   r
   r
   r   Útest_writes_before_communicate?  s    

z.ProcessTestCase.test_writes_before_communicatec          
   C   sØ   t jddt d g}x¾dD ]¶}tj|fdtjdtj|diŽ}|Š |j d¡ |j ¡  |  	|j
 ¡ d¡ |j d	¡ |j ¡  |  |j
j¡ |  	|j
 ¡ d
¡ |  	|j
 d¡d	¡ |  	|j
 ¡ d¡ W d Q R X qW d S )Nz-cz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Ä   Útextr%   r&   Tzline1
zline3
zline2
é   zline4
line5
line6
line7
line8)r(   r)   Ú	SETBINARYr   r'   r*   r%   rN   Úflushr   r&   Úreadliner.   rM   r…   )r	   r    Zextra_kwargr/   r
   r
   r   Ú test_universal_newlines_and_textO  s(    



z0ProcessTestCase.test_universal_newlines_and_textc             C   s^   t jtjddt d gt jt jdd}|  |jj¡ |  |j	j¡ | 
¡ \}}|  |d¡ d S )Nz-czimport 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]   )r   r&   rÄ   z#line2
line4
line5
line6
line7
line8)r   r'   r(   r)   rØ   r*   rM   r&   r.   r   rk   r   )r	   r/   r&   r   r
   r
   r   Ú#test_universal_newlines_communicatev  s    
z3ProcessTestCase.test_universal_newlines_communicatec             C   sH   t jtjddt t d¡ gt jdd}| d¡\}}|  	|j
d¡ d S )Nz-czimport 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   )r   r'   r(   r)   rØ   ÚtextwrapÚdedentr*   rk   r   r@   )r	   r/   r&   r   r
   r
   r   Ú)test_universal_newlines_communicate_stdin  s    

z9ProcessTestCase.test_universal_newlines_communicate_stdinc             C   s8   t jtjddgt jt jdd}| ¡  |  |jd¡ d S )Nz-cr¼   T)r%   r&   rÄ   r   )r   r'   r(   r)   r*   rk   r   r@   )r	   r/   r
   r
   r   Ú.test_universal_newlines_communicate_input_none  s    z>ProcessTestCase.test_universal_newlines_communicate_input_nonec             C   sˆ   t jtjddt t d¡ gt jt jt jdd}|  |j	j
¡ |  |jj
¡ | d¡\}}|  |jd¡ |  d|¡ |  | d	¡¡ d S )
Nz-czimport 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%   r   r&   rÄ   zline1
line3
r   zline1
line2
line3
line4
line5
zeline2
eline6
eline7
)r   r'   r(   r)   rØ   rÝ   rÞ   r*   rM   r&   r.   r   rk   r   r@   rÊ   r²   )r	   r/   r&   r   r
   r
   r   Ú7test_universal_newlines_communicate_stdin_stdout_stderr©  s    

zGProcessTestCase.test_universal_newlines_communicate_stdin_stdout_stderrc             C   sX   xRdD ]J}d| }t jd|g}tj|tjtj|d}|jdd\}}|  |d¡ qW d S )N)zutf-16z	utf-32-bez@import sys; sys.stdout.buffer.write('1\r\n2\r3\n4'.encode('%s'))z-c)r%   r&   Úencodingr   )rR   z1
2
3
4)r(   r)   r   r'   r*   rk   r   )r	   râ   rm   r    Úpopenr&   r   r
   r
   r   Ú-test_universal_newlines_communicate_encodingsÆ  s    
z=ProcessTestCase.test_universal_newlines_communicate_encodingsc             C   s`   xZdD ]R\}}d}t jd|g}tj|tjtjd|d}|jdd\}}|  |d |¡¡ qW d S )	N))Úignorer   )Úreplaceu   ï¿½ï¿½)Úsurrogateescapeu   í²€í²€)Úbackslashreplacez\x80\x80z2import sys; sys.stdout.buffer.write(b'[\x80\x80]')z-czutf-8)r%   r&   râ   Úerrorsr   )rR   z[{}])r(   r)   r   r'   r*   rk   r   Úformat)r	   ré   r   rm   r    rã   r&   r   r
   r
   r   Útest_communicate_errorsÜ  s    z'ProcessTestCase.test_communicate_errorsc       
      C   s8  t s
d}nd}g }t ¡ }zòx€t|ƒD ]f}y.tj |tj¡}| 	t 
|tjtjB ¡¡ W q& tk
rŠ } z|jtjkrx‚ P W d d }~X Y q&X q&W |  d| ¡ xtdƒD ]}t | ¡ ¡ q¨W xJtdƒD ]>}tjtjddgtjtjtjd}| d	¡d
 }|  |d	¡ qÈW W d x|D ]}	t |	¡ qW t |¡ X d S )Ni  i  z4failed to reach the file descriptor limit (tried %d)é
   é   z-cz-import sys;sys.stdout.write(sys.stdin.read()))r%   r&   r   s   limer   )Ú	mswindowsrK   ÚmkdtempÚranger^   ru   rv   r   ZTESTFNÚappendÚopenÚO_WRONLYÚO_CREATÚOSErrorÚerrnoZEMFILEÚskipTestr.   Úpopr   r'   r(   r)   r*   rk   r   ÚshutilZrmtree)
r	   Zmax_handlesÚhandlesZtmpdirÚiZtmpfileÚer/   ÚdataÚhr
   r
   r   Útest_no_leakingñ  s:    


zProcessTestCase.test_no_leakingc             C   sÂ   |   t dddg¡d¡ |   t dddg¡d¡ |   t dddg¡d	¡ |   t d
ddg¡d¡ |   t dddg¡d¡ |   t dddg¡d¡ |   t dddg¡d¡ |   t ddg¡d¡ d S )Nza b crœ   rü   z"a b c" d ezab"cú\z	ab\"c \ dz \zab\"c " \\" dza\\\bzde fgrþ   za\\\b "de fg" hza\"brA   z
a\\\"b c dza\\b cz"a\\b c" d eza\\b\ cz"a\\b\ c" d eZabr   zab "")r   r   Úlist2cmdline)r	   r
   r
   r   Útest_list2cmdline  s     z!ProcessTestCase.test_list2cmdlinec             C   sb   t jtjddgt jd}|  |jj¡ |  | 	¡ ¡ t
 |j ¡ d¡ | ¡  |  | 	¡ d¡ d S )Nz-czimport os; os.read(0, 1))r%   ó   Ar   )r   r'   r(   r)   r*   rM   r%   r.   ÚassertIsNoneÚpollr^   rN   rš   r   r   )r	   r/   r
   r
   r   Ú	test_poll*  s    

zProcessTestCase.test_pollc             C   s6   t  tjddg¡}|  | ¡ d¡ |  | ¡ d¡ d S )Nz-cr¼   r   )r   r'   r(   r)   r   r   )r	   r/   r
   r
   r   Ú	test_wait5  s    zProcessTestCase.test_waitc          	   C   s`   t  tjddg¡}|  t j¡}|jdd W d Q R X |  dt|j	ƒ¡ |  
|jddd¡ d S )Nz-czimport time; time.sleep(0.3)g-Cëâ6?)r8   z0.0001rX   r   )r   r'   r(   r)   r9   r:   r   rD   Ústrr?   r   )r	   r/   rA   r
   r
   r   Útest_wait_timeout;  s    
z!ProcessTestCase.test_wait_timeoutc          	   C   s.   |   t¡ t tjddgd¡ W d Q R X d S )Nz-cr¼   r®   )r9   rc   r   r'   r(   r)   )r	   r
   r
   r   Útest_invalid_bufsizeE  s    z$ProcessTestCase.test_invalid_bufsizec             C   sN   t  tjddgd ¡}|  | ¡ d¡ t jtjddgd d}|  | ¡ d¡ d S )Nz-cr¼   r   )r1   )r   r'   r(   r)   r   r   )r	   r/   r
   r
   r   Útest_bufsize_is_noneK  s    z$ProcessTestCase.test_bufsize_is_nonec          
   C   s²   t jtjddgt jt jt jd|dj}|j |¡ t 	|j 
¡ ¡ |j ¡ }t ¡ * y|j 	¡  W n tk
rx   Y nX W d Q R X d |_W d Q R X |  |jd¡ |  ||¡ d S )Nz-czDimport sys;sys.stdout.write(sys.stdin.readline());sys.stdout.flush()r]   )r%   r&   r   r1   rÄ   r   )r   r'   r(   r)   r*   rª   r%   rN   r^   r.   rš   r&   rÚ   r   ÚSuppressCrashReportrõ   r   r@   )r	   Úliner   rÄ   r/   Z	read_liner
   r
   r   Ú_test_bufsize_equal_oneS  s"    


z'ProcessTestCase._test_bufsize_equal_onec             C   s   d}| j ||dd d S )Nzline
T)rÄ   )r  )r	   r  r
   r
   r   Ú test_bufsize_equal_one_text_modei  s    z0ProcessTestCase.test_bufsize_equal_one_text_modec             C   s"   dt j ¡  }| j|ddd d S )Ns   liner¥   F)rÄ   )r^   ÚlineseprÅ   r  )r	   r  r
   r
   r   Ú"test_bufsize_equal_one_binary_modeo  s    z2ProcessTestCase.test_bufsize_equal_one_binary_modec          
   C   s@   x:t dƒD ].}|  t¡ tjttjtjd W d Q R X q
W d S )Ni   )r&   r   )rð   r9   ry   r   r'   rz   r*   )r	   rû   r
   r
   r   Útest_leaking_fds_on_erroru  s
    z)ProcessTestCase.test_leaking_fds_on_errorc          	   C   s    ydd l }|j W n" ttfk
r4   |  d¡ Y nX t dt›d¡}tj	d|g}t
j|t
jdd}| | ¡ d }W d Q R X |  |d	¡ |  |jd¡ d S )
Nr   zneed 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)r   rÄ   r]   r   )ÚmsvcrtZCrtSetReportModeÚAttributeErrorÚImportErrorr÷   rÝ   rÞ   rz   r(   r)   r   r'   r*   rk   r   r@   )r	   r  rm   r½   Úprocr   r
   r
   r   Útest_nonexisting_with_pipes  s    
z+ProcessTestCase.test_nonexisting_with_pipesc                sº   g ‰ ‡ fdd„}t j|d}| ¡  z2|  t¡ tjttjtjtjd W d Q R X W d | 	¡  d }xBˆ D ]:}yt
 |¡ W qj tk
r¢ } z|}W d d }~X Y qjX qjW |d k	r´|‚X d S )Nc                 s.   x(t dƒD ]} ˆ  t ¡ ¡ t d¡ q
W d S )Né   gü©ñÒMbP?)rð   Úextendr^   rÐ   ÚtimeÚsleep)rû   )Úfdsr
   r   Úopen_fds­  s    z<ProcessTestCase.test_double_close_on_error.<locals>.open_fds)Útarget)r%   r&   r   )Ú	threadingZThreadÚstartr9   ÚEnvironmentErrorr   r'   rz   r*   rv   r^   r.   rõ   )r	   r  ÚtÚexcÚfdrü   r
   )r  r   Útest_double_close_on_errorª  s&    
z*ProcessTestCase.test_double_close_on_errorc                s²   t  tjddg¡‰ |  ˆ jd¡ g ‰‡ ‡fdd„}t d|¡}| ¡  t	rPd}nd}ˆ j
d	d
 | jˆ j|dd ˆ  
¡  | jˆ j|dd | ¡  |  dd|fd|fgˆ¡ dS )z?Issue21291: Popen.wait() needs to be threadsafe for returncode.z-czimport time; time.sleep(12)Nc                  sN   ˆ  dˆ  ¡ f¡ ˆ  ¡  ˆ  ¡  ˆ  dˆ jf¡ ˆ  ¡  ˆ  dˆ jf¡ d S )Nzthread-start-poll-resultzthread-after-kill-and-waitzthread-after-second-wait)rñ   r  Úkillr   r@   r
   )r  Úresultsr
   r   Úkill_proc_timer_threadÎ  s    zDProcessTestCase.test_threadsafe_wait.<locals>.kill_proc_timer_threadgš™™™™™É?r]   i÷ÿÿÿr  )r8   z*unexpected result in wait from main thread)r   z&unexpected result in second main wait.)zthread-start-poll-resultNzthread-after-kill-and-waitzthread-after-second-wait)r   r'   r(   r)   r   r@   r  ZTimerr   rî   r   rv   )r	   r(  r"  Zexpected_errorcoder
   )r  r'  r   Útest_threadsafe_waitÇ  s*    


z$ProcessTestCase.test_threadsafe_waitc             C   s6   d  d¡}t tjd|g¡}|  | d¡t|ƒ¡ d S )Nú;)zimport subprocess, syszJretcode = subprocess.call([sys.executable, '-c', 'print("Hello World!")'])zassert retcode == 0z-cs   Hello World!)rv   r   rC   r(   r)   rÊ   r²   Úascii)r	   rm   rE   r
   r
   r   Útest_issue8780û  s    zProcessTestCase.test_issue8780c             C   sÈ   t  ¡ \}}t  ¡ \}}t  ¡ \}}ytjdg|||d W nP tk
rŒ   t |¡ t |¡ t |¡ t |¡ t |¡ t |¡ Y nX |  tj	 
|¡¡ |  tj	 
|¡¡ |  tj	 
|¡¡ d S )NÚ*)r%   r&   r   )rK   Úmkstempr   r'   rõ   r^   r.   Úremover   ru   Úexists)r	   ZifhandleZifnameZofhandleZofnameZefhandleZefnamer
   r
   r   Ú test_handles_closed_on_exception  s     




z0ProcessTestCase.test_handles_closed_on_exceptionc             C   s\   t jtjddgt jt jt jd}|  |jj¡ |  |jj¡ |  |j	j¡ | 
dd ¡ d S )Nz-cr¼   )r%   r&   r   ó   xi   )r   r'   r(   r)   r*   rM   r&   r.   r   r%   rk   )r	   r/   r
   r
   r   Útest_communicate_epipe  s    
z&ProcessTestCase.test_communicate_epipec             C   s@   t jtjddgt jd}|  |jj¡ | ¡  | 	dd ¡ d S )Nz-cr¼   )r%   r2  i   )
r   r'   r(   r)   r*   rM   r%   r.   r   rk   )r	   r/   r
   r
   r   Ú!test_communicate_epipe_only_stdin%  s
    
z1ProcessTestCase.test_communicate_epipe_only_stdinÚSIGUSR1zRequires signal.SIGUSR1r&  zRequires os.killÚgetppidzRequires os.getppidc          
   C   sr   dd„ }t   t j|¡}|  t j t j|¡ tjddg}x8dD ]0}|tji}tj|f|Ž}| ¡  W d Q R X q:W d S )Nc             S   s   d S )Nr
   )ZsignumÚframer
   r
   r   Úhandler5  s    z7ProcessTestCase.test_communicate_eintr.<locals>.handlerz-cz7import os, signal;os.kill(os.getppid(), signal.SIGUSR1))r&   r   )	Úsignalr5  rM   r(   r)   r   r*   r'   rk   )r	   r8  Úold_handlerr    ÚstreamÚkwÚprocessr
   r
   r   Útest_communicate_eintr-  s    

z&ProcessTestCase.test_communicate_eintrz/proc/%d/fdzLinux specificc          	   C   sd   dt  ¡  }t  |¡}|  t¡$ ttjddgtj	tj	tj	d W dQ R X t  |¡}|  
||¡ dS )z;Test for the fork() failure fd leak reported in issue16327.z/proc/%d/fdz-cr¼   )r%   r&   r   N)r^   ÚgetpidÚlistdirr9   r   r   r(   r)   r   r*   r   )r	   Zfd_directoryZfds_before_popenZfds_after_exceptionr
   r
   r   Ú!test_failed_child_execute_fd_leakF  s    

z1ProcessTestCase.test_failed_child_execute_fd_leakz+behavior currently not supported on Windowsc          	   C   s<   |   t¡}t ddddg¡ W d Q R X |  |jjd¡ d S )Nz/opt/nonexistent_binaryÚwithZsomer    )r9   rŽ   r   r5   r   r?   Úfilename)r	   rA   r
   r
   r   Ú%test_file_not_found_includes_filenameX  s    z5ProcessTestCase.test_file_not_found_includes_filenamec          	   C   s<   |   t¡}tjddgdd W d Q R X |  |jjd¡ d S )NÚexitÚ0z/some/nonexistent/directory)r   )r9   rŽ   r   r'   r   r?   rC  )r	   rA   r
   r
   r   Ú test_file_not_found_with_bad_cwd^  s    z0ProcessTestCase.test_file_not_found_with_bad_cwdN)pr   r   r   r0   r3   r7   r;   r=   rB   rF   rH   rJ   rQ   rS   rV   rW   rY   rb   ri   rj   rp   rq   rs   rx   r{   ÚunittestÚskipIfrî   r}   r€   rƒ   r‡   rŠ   r‹   r“   r•   r–   r(   Úbase_prefixÚprefixr—   Ú	sysconfigZis_python_buildr˜   r™   r   rž   rŸ   r    r¡   r¢   r£   r¤   r¦   r§   r¨   r©   r«   r¬   r­   r¯   ÚplatformZget_config_varrº   r¾   r¿   rÀ   rÁ   rÂ   rÃ   rÆ   rÉ   rÍ   rÎ   rÔ   rÕ   rÛ   rÜ   rß   rà   rá   rä   rë   rÿ   r  r  r  r	  r
  r  r  r  r  r  r  r%  r)  r,  r1  r3  r4  Ú
skipUnlessÚhasattrr9  r^   r>  ru   Úisdirr?  rA  rD  rG  r
   r
   r
   r   r$   [   sÒ   		

		


	
''
)4r$   c               @   s„   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS ) ÚRunFuncTestCasec             K   s   t jd|g}tj|f|ŽS )z4Run Python code in a subprocess using subprocess.runz-c)r(   r)   r   Úrun)r	   rm   r!   Úargvr
   r
   r   Ú
run_pythonf  s    zRunFuncTestCase.run_pythonc          	   C   s<   |   d¡}|  |jd¡ |  tj¡ | ¡  W d Q R X d S )Nzimport sys; sys.exit(47)r4   )rT  r   r@   r9   r   r>   Zcheck_returncode)r	   Úcpr
   r
   r   Útest_returncodek  s    
zRunFuncTestCase.test_returncodec          	   C   s:   |   tj¡}| jddd W d Q R X |  |jjd¡ d S )Nzimport sys; sys.exit(47)T)Úcheckr4   )r9   r   r>   rT  r   r?   r@   )r	   rA   r
   r
   r   Ú
test_checkr  s    zRunFuncTestCase.test_checkc             C   s    | j ddd}|  |jd¡ d S )Nzimport sys; sys.exit(0)T)rW  r   )rT  r   r@   )r	   rU  r
   r
   r   Útest_check_zerow  s    zRunFuncTestCase.test_check_zeroc          	   C   s*   |   tj¡ | jddd W d Q R X d S )Nzwhile True: passg-Cëâ6?)r8   )r9   r   r:   rT  )r	   r
   r
   r   Útest_timeout|  s    zRunFuncTestCase.test_timeoutc             C   s"   | j dtjd}|  d|j¡ d S )Nzprint('BDFL'))r&   s   BDFL)rT  r   r*   rD   r&   )r	   rU  r
   r
   r   Útest_capture_stdout„  s    z#RunFuncTestCase.test_capture_stdoutc             C   s"   | j dtjd}|  d|j¡ d S )Nz$import sys; sys.stderr.write('BDFL'))r   s   BDFL)rT  r   r*   rD   r   )r	   rU  r
   r
   r   Útest_capture_stderr‰  s    
z#RunFuncTestCase.test_capture_stderrc             C   sL   t  ¡ }|  |j¡ | d¡ | d¡ | jd|tjd}|  	d|j
¡ d S )Ns   pearr   z6import sys; sys.stdout.write(sys.stdin.read().upper()))r%   r&   s   PEAR)rK   rL   rM   r.   rN   rO   rT  r   r*   rD   r&   )r	   rP   rU  r
   r
   r   rQ   Ž  s    

z+RunFuncTestCase.test_check_output_stdin_argc             C   s$   | j ddtjd}|  d|j¡ d S )Nz6import sys; sys.stdout.write(sys.stdin.read().upper())s   pear)rR   r&   s   PEAR)rT  r   r*   rD   r&   )r	   rU  r
   r
   r   rS   ™  s    z+RunFuncTestCase.test_check_output_input_argc          	   C   s~   t  ¡ }|  |j¡ | d¡ | d¡ | jtdd}| jd|dd}W d Q R X |  	d|j
jd ¡ |  	d	|j
jd ¡ d S )
Ns   pearr   z7Expected ValueError when stdin and input args supplied.)r   zprint('will not be run')s   hare)r%   rR   r%   rR   )rK   rL   rM   r.   rN   rO   r9   rT   rT  rD   r?   r    )r	   rP   rA   rE   r
   r
   r   rW      s    


z6RunFuncTestCase.test_check_output_stdin_with_input_argc          	   C   sN   |   tj¡}| jddtjd}W d Q R X |  |jjd¡ |  |jjd¡ d S )NzMimport sys, time
sys.stdout.write('BDFL')
sys.stdout.flush()
time.sleep(3600)rX   )r8   r&   s   BDFL)	r9   r   r:   rT  r*   r   r?   rE   r&   )r	   rA   rU  r
   r
   r   rY   ­  s    z)RunFuncTestCase.test_check_output_timeoutc             C   s2   t j ¡ }d|d< | jd|d}|  |jd¡ d S )NrZ   r[   zCimport sys, os;sys.exit(33 if os.getenv("FRUIT")=="banana" else 31))r\   é!   )r^   r_   r`   rT  r   r@   )r	   ra   rU  r
   r
   r   Útest_run_kwargs»  s
    
zRunFuncTestCase.test_run_kwargsc             C   s.   | j ddd}|  d|j¡ |  d|j¡ d S )Nz>import sys;sys.stdout.write('BDFL'); sys.stderr.write('FLUFL')T)Úcapture_outputs   BDFLs   FLUFL)rT  rD   r&   r   )r	   rU  r
   r
   r   Útest_capture_outputÃ  s    z#RunFuncTestCase.test_capture_outputc          	   C   sj   t  ¡ }|  |j¡ | jtdd}| jdd|d}W d Q R X |  d|jj	d ¡ |  d|jj	d ¡ d S )	NzAExpected ValueError when stdout and capture_output args supplied.)r   zprint('will not be run')T)r_  r&   r&   r   r_  )
rK   rL   rM   r.   r9   rT   rT  rD   r?   r    )r	   rP   rA   rE   r
   r
   r   Ú#test_stdout_with_capture_output_argË  s    z3RunFuncTestCase.test_stdout_with_capture_output_argc          	   C   sj   t  ¡ }|  |j¡ | jtdd}| jdd|d}W d Q R X |  d|jj	d ¡ |  d|jj	d ¡ d S )	NzAExpected ValueError when stderr and capture_output args supplied.)r   zprint('will not be run')T)r_  r   r   r   r_  )
rK   rL   rM   r.   r9   rT   rT  rD   r?   r    )r	   rP   rA   rE   r
   r
   r   Ú#test_stderr_with_capture_output_arg×  s    z3RunFuncTestCase.test_stderr_with_capture_output_argN)r   r   r   rT  rV  rX  rY  rZ  r[  r\  rQ   rS   rW   rY   r^  r`  ra  rb  r
   r
   r
   r   rQ  e  s   rQ  zPOSIX specific testsc                   sH  e Zd Z‡ fdd„Zdd„ Zdd„ Zdd„ Zd	d
„ ZG dd„ dej	ƒZ
e d¡dd„ ƒZe d¡dd„ ƒZe ej d¡ d¡dd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ ZG d$d%„ d%ej	ƒZe ej d&¡ d'¡d(d)„ ƒZd*d+„ Ze ej d,kd-¡d.d/„ ƒZ!d0d1„ Z"d2d3„ Z#d4d5„ Z$d6d7„ Z%d8d9„ Z&d:d;„ Z'd<d=„ Z(e ej  )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 ej  )dz¡one Gd{¡jHe Gd|¡jHkd}¡d~d„ ƒZIeJ Kd€d¡d‚dƒ„ ƒZLd„d…„ ZMd†d‡„ ZNdˆd‰„ ZOdŠd‹„ ZPdŒd„ ZQdŽd„ ZRdd‘„ 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¨„ ƒZa‡  ZbS )©ÚPOSIXProcessTestCasec                s   t ƒ  ¡  d| _d S )Nz/_this/pa.th/does/not/exist)Úsuperr   Ú_nonexistent_dir)r	   )Ú	__class__r
   r   r   ç  s    
zPOSIXProcessTestCase.setUpc          
   C   sd   yt  | j¡ W n> tk
rN } z |}| jdt| jƒ 7  _W d d }~X Y nX |  d| j ¡ |S )Nz: z,chdir to nonexistent directory %s succeeded.)r^   Úchdirre  rõ   ÚstrerrorÚreprrU   )r	   rü   Údesired_exceptionr
   r
   r   Ú_get_chdir_exceptionë  s    *
z)POSIXProcessTestCase._get_chdir_exceptionc          
   C   sz   |   ¡ }ytjtjddg| jd}W nB tk
rf } z$|  |j|j¡ |  |j	|j	¡ W dd}~X Y nX |  
d| ¡ dS )z;Test error in the child raised in the parent for a bad cwd.z-cr   )r   NzExpected OSError: %s)rk  r   r'   r(   r)   re  rõ   r   rö   rh  rU   )r	   rj  r/   rü   r
   r
   r   Útest_exception_cwdù  s    "z'POSIXProcessTestCase.test_exception_cwdc          
   C   sz   |   ¡ }ytjtjddg| jd}W nB tk
rf } z$|  |j|j¡ |  |j	|j	¡ W dd}~X Y nX |  
d| ¡ dS )zBTest error in the child raised in the parent for a bad executable.z-cr   )r)   NzExpected OSError: %s)rk  r   r'   r(   r)   re  rõ   r   rö   rh  rU   )r	   rj  r/   rü   r
   r
   r   Útest_exception_bad_executable  s    "z2POSIXProcessTestCase.test_exception_bad_executablec          
   C   st   |   ¡ }yt | jddg¡}W nB tk
r` } z$|  |j|j¡ |  |j|j¡ W dd}~X Y nX |  d| ¡ dS )z?Test error in the child raised in the parent for a bad args[0].z-cr   NzExpected OSError: %s)	rk  r   r'   re  rõ   r   rö   rh  rU   )r	   rj  r/   rü   r
   r
   r   Útest_exception_bad_args_0  s    "z.POSIXProcessTestCase.test_exception_bad_args_0c               @   s   e Zd Zdd„ ZdS )z&POSIXProcessTestCase.PopenNoDestructorc             C   s   d S )Nr
   )r	   r
   r
   r   Ú__del__*  s    z.POSIXProcessTestCase.PopenNoDestructor.__del__N)r   r   r   ro  r
   r
   r
   r   ÚPopenNoDestructor)  s   rp  z%subprocess._posixsubprocess.fork_execc          
   C   sN   dd„ }||_ tjdtd( |  t¡ |  dg¡ W dQ R X W dQ R X dS )z>Test error passing done through errpipe_write in the good casec              W   s0   | d }d  tj¡ ¡ }t |d| d ¡ dS )Né   z{:x}s   OSError:ó   :r   )rê   rö   ZEISDIRrÅ   r^   rN   )r    Úerrpipe_writeZerr_coder
   r
   r   Úproper_error0  s    zHPOSIXProcessTestCase.test_exception_errpipe_normal.<locals>.proper_errorzsubprocess.os.waitpid)Úside_effectÚnon_existent_commandN)ru  r   ÚpatchÚChildProcessErrorr9   ÚIsADirectoryErrorrp  )r	   Ú	fork_execrt  r
   r
   r   Útest_exception_errpipe_normal-  s    
z2POSIXProcessTestCase.test_exception_errpipe_normalc          
      sn   d‰ ‡ fdd„}||_ tjdtd* |  tj¡}|  dg¡ W dQ R X W dQ R X |  t	ˆ ƒt
|jƒ¡ dS )zZTest error passing done through errpipe_write where its not
        in the expected formats   ÿ Þ­c                 s   | d }t  |ˆ ¡ dS )Nrq  r   )r^   rN   )r    rs  )Ú
error_datar
   r   Ú	bad_errorC  s    zGPOSIXProcessTestCase.test_exception_errpipe_bad_data.<locals>.bad_errorzsubprocess.os.waitpid)ru  rv  N)ru  r   rw  rx  r9   r   ÚSubprocessErrorrp  rD   ri  r  r?   )r	   rz  r}  rü   r
   )r|  r   Útest_exception_errpipe_bad_data>  s    
 z4POSIXProcessTestCase.test_exception_errpipe_bad_dataz/proc/self/statuszneed /proc/self/statusc             C   s†   t jddgdd}x*| ¡ D ]}| d¡r|}P qW |  d¡ t jddgdd}x | ¡ D ]}| d¡rZ|}P qZW | j||dd	 d S )
NÚcatz/proc/self/statusF)Zrestore_signalss   SigIgnz&SigIgn not found in /proc/self/status.Tz=restore_signals=True should've unblocked SIGPIPE and friends.)r   )r   rC   Ú
splitlinesr²   r÷   ÚassertNotEqual)r	   Zdefault_proc_statusr  Zdefault_sig_ign_maskZrestored_proc_statusZrestored_sig_ign_maskr
   r
   r   Útest_restore_signalsT  s"    


z)POSIXProcessTestCase.test_restore_signalsc          
   C   sr   yt jtjddgdd}W n0 tk
rJ } z|jtjkr:‚ W d d }~X Y n$X t t 	¡ ¡}t
|ƒ}|  ||¡ d S )Nz-cz)import os; print(os.getpgid(os.getpid()))T)Zstart_new_session)r   rC   r(   r)   rõ   rö   ZEPERMr^   Úgetpgidr?  Úintr‚  )r	   rE   rü   Zparent_pgidZ
child_pgidr
   r
   r   Útest_start_new_sessionl  s    z+POSIXProcessTestCase.test_start_new_sessionc          	   C   sD   t  ¡   t tjddg¡}| ¡  W d Q R X |  |j t	j
¡ d S )Nz-czimport os; os.abort())r   r  r   r'   r(   r)   r   r   r@   r9  ÚSIGABRT)r	   r/   r
   r
   r   Útest_run_abort}  s
    

z#POSIXProcessTestCase.test_run_abortc             C   sN   t  ttjƒ d¡}t|ƒ}|  d| ¡ ¡ |  d|¡ |  ttjƒ|¡ d S )Nzfake cmdr9  ZSIG)r   r>   r…  r9  r‡  r  rD   Úlower)r	   ro   Úerror_stringr
   r
   r   Ú"test_CalledProcessError_str_signal…  s
    z7POSIXProcessTestCase.test_CalledProcessError_str_signalc             C   s$   t  dd¡}t|ƒ}|  d|¡ d S )NiÁKiÿzfake cmdzunknown signal 9876543.)r   r>   r  rD   )r	   ro   rŠ  r
   r
   r   Ú*test_CalledProcessError_str_unknown_signal  s    z?POSIXProcessTestCase.test_CalledProcessError_str_unknown_signalc             C   s$   t  dd¡}t|ƒ}|  d|¡ d S )Né   zfake cmdznon-zero exit status 2.)r   r>   r  rD   )r	   ro   rŠ  r
   r
   r   Ú$test_CalledProcessError_str_non_zero•  s    z9POSIXProcessTestCase.test_CalledProcessError_str_non_zeroc          	   C   sD   t jtjddgt jdd„ d}| |  |j ¡ d¡ W d Q R X d S )Nz-cz2import sys,os;sys.stdout.write(os.getenv("FRUIT"))c               S   s   t  dd¡S )Nr[   Úapple)r^   Úputenvr
   r
   r
   r   Ú<lambda>¡  r¥   z3POSIXProcessTestCase.test_preexec.<locals>.<lambda>)r&   Ú
preexec_fns   apple)r   r'   r(   r)   r*   r   r&   r…   )r	   r/   r
   r
   r   Útest_preexecš  s    
z!POSIXProcessTestCase.test_preexecc          
   C   s–   dd„ }yt jtjddg|d}W nd t jk
rT } z|  t jd¡ W d d }~X Y n> tk
r† } z|  d|j	d ¡ W d d }~X Y nX |  
d	¡ d S )
Nc               S   s   t dƒ‚d S )Nz'What if two swallows carried a coconut?)rT   r
   r
   r
   r   Úraise_it¦  s    z=POSIXProcessTestCase.test_preexec_exception.<locals>.raise_itz-cr   )r’  z)Expected a ValueError from the preexec_fnZcoconutr   zEException raised by preexec_fn did not make it to the parent process.)r   r'   r(   r)   r~  rÊ   Ú_posixsubprocessrT   rD   r    rU   )r	   r”  r/   rü   r
   r
   r   Útest_preexec_exception¥  s    $z+POSIXProcessTestCase.test_preexec_exceptionc               @   s    e Zd ZdZdd„ Zdd„ ZdS )z+POSIXProcessTestCase._TestExecuteChildPopenz3Used to test behavior at the end of _execute_child.c             O   s   || _ tjj| f|ž|Ž d S )N)Ú	_testcaser   r'   rd   )r	   Ztestcaser    r!   r
   r
   r   rd   ·  s    z4POSIXProcessTestCase._TestExecuteChildPopen.__init__c             O   sŠ   zt jj| f|ž|Ž W d dd„ tdƒD ƒ}z<x6|D ].}| jj|| j ¡ | j ¡ | j	 ¡ fdd q4W W d x|D ]}t
 |¡ qpW X X d S )Nc             S   s   g | ]}t  d t j¡‘qS )z	/dev/zero)r^   rò   ÚO_RDONLY)rµ   rÈ   r
   r
   r   r·   Â  s   zNPOSIXProcessTestCase._TestExecuteChildPopen._execute_child.<locals>.<listcomp>é   z!At least one fd was closed early.)r   )r   r'   r"   rð   r—  ÚassertNotInr%   rš   r&   r   r^   r.   )r	   r    r!   Zdevzero_fdsr$  r
   r
   r   r"   »  s    


z:POSIXProcessTestCase._TestExecuteChildPopen._execute_childN)r   r   r   r#   rd   r"   r
   r
   r
   r   Ú_TestExecuteChildPopenµ  s   r›  z	/dev/zeroz/dev/zero required.c          	   C   sH   dd„ }|   tj¡* | j| tjddgtjtjtj|d W dQ R X dS )z6Issue16140: Don't double close pipes on preexec error.c               S   s   t  d¡‚d S )Nz-force the _execute_child() errpipe_data path.)r   r~  r
   r
   r
   r   r”  Ò  s    zWPOSIXProcessTestCase.test_preexec_errpipe_does_not_double_close_pipes.<locals>.raise_itz-cr¼   )r%   r&   r   r’  N)r9   r   r~  r›  r(   r)   r*   )r	   r”  r
   r
   r   Ú0test_preexec_errpipe_does_not_double_close_pipesÎ  s    zEPOSIXProcessTestCase.test_preexec_errpipe_does_not_double_close_pipesc             C   s  dd„ }t  ¡ }t j}t j}zÎt  ¡  |  t  ¡ ¡ tjtjddgdd„ d |  t  ¡ d¡ t  ¡  |  	t  ¡ ¡ tjtjddgd	d„ d |  	t  ¡ d
¡ |t _| j
ttjtjddgdd„ d t `| j
ttjtjddgdd„ d W d |t _|t _|st  ¡  X d S )Nc               S   s   t dƒ‚d S )Nzthis shouldn't escape)ÚRuntimeErrorr
   r
   r
   r   Úraise_runtime_errorß  s    zPPOSIXProcessTestCase.test_preexec_gc_module_failure.<locals>.raise_runtime_errorz-cr   c               S   s   d S )Nr
   r
   r
   r
   r   r‘  è  r¥   zEPOSIXProcessTestCase.test_preexec_gc_module_failure.<locals>.<lambda>)r’  z#Popen enabled gc when it shouldn't.c               S   s   d S )Nr
   r
   r
   r
   r   r‘  ï  r¥   zPopen left gc disabled.c               S   s   d S )Nr
   r
   r
   r
   r   r‘  õ  r¥   c               S   s   d S )Nr
   r
   r
   r
   r   r‘  ú  r¥   )ÚgcÚ	isenabledÚdisabler   r   r5   r(   r)   ÚenablerÊ   r9   r  r'   r  )r	   rž  ZenabledZorig_gc_disableZorig_gc_isenabledr
   r
   r   Útest_preexec_gc_module_failureÜ  s8    




z3POSIXProcessTestCase.test_preexec_gc_module_failureÚdarwinz!setrlimit() seems to fail on OS Xc          
   C   s´   yddl m}m}m} W n, tk
rD } z|  |¡ W d d }~X Y nX ||ƒ}|\}}||d|fƒ |  |||¡ ytjt	j
ddgdd„ d W n tk
r¤   Y nX |  d¡ d S )	Nr   )Ú	getrlimitÚ	setrlimitÚRLIMIT_NPROCz-cr   c               S   s   d S )Nr
   r
   r
   r
   r   r‘    r¥   z@POSIXProcessTestCase.test_preexec_fork_failure.<locals>.<lambda>)r’  z.RLIMIT_NPROC had no effect; probably superuser)Úresourcer¥  r¦  r§  r  r÷   rM   r   r5   r(   r)   ÚBlockingIOError)r	   r¥  r¦  r§  ro   ZlimitsrÈ   Zhardr
   r
   r   Útest_preexec_fork_failure  s    z.POSIXProcessTestCase.test_preexec_fork_failurec          	   C   s€   t  ¡ \}}t|ddd&}| dtj ¡ | dtj ¡ W d Q R X t 	|d¡ t
 |¡}| ¡  t |¡ |  |jd¡ d S )NÚwrç   )ré   z#!%s
z(exec '%s' -c 'import sys; sys.exit(47)'
iÀ  r4   )rK   r.  rò   rN   r   Ú
unix_shellr(   r)   r^   Úchmodr   r'   r   r/  r   r@   )r	   r$  ÚfnameÚfobjr/   r
   r
   r   Útest_args_string  s    

z%POSIXProcessTestCase.test_args_stringc             C   s<   | j ttjtjddgdd | j ttjtjddgdd d S )Nz-czimport sys; sys.exit(47)r4   )Ústartupinfo)Úcreationflags)r9   rT   r   r5   r(   r)   )r	   r
   r
   r   ri   %  s    

z&POSIXProcessTestCase.test_invalid_argsc          	   C   sT   t j ¡ }d|d< tjdgdtj|d}| |  |j ¡  	d¡d¡ W d Q R X d S )Nr  r[   zecho $FRUITr]   )r|   r&   r\   s    	
s   apple)
r^   r_   r`   r   r'   r*   r   r&   r…   r   )r	   ra   r/   r
   r
   r   Útest_shell_sequence0  s    

z(POSIXProcessTestCase.test_shell_sequencec          	   C   sR   t j ¡ }d|d< tjddtj|d}| |  |j ¡  	d¡d¡ W d Q R X d S )Nr  r[   zecho $FRUITr]   )r|   r&   r\   s    	
s   apple)
r^   r_   r`   r   r'   r*   r   r&   r…   r   )r	   ra   r/   r
   r
   r   Útest_shell_string:  s    
z&POSIXProcessTestCase.test_shell_stringc          	   C   sv   t  ¡ \}}t|ddd&}| dtj ¡ | dtj ¡ W d Q R X t 	|d¡ t
 |¡}t |¡ |  |d¡ d S )Nr«  rç   )ré   z#!%s
z(exec '%s' -c 'import sys; sys.exit(47)'
iÀ  r4   )rK   r.  rò   rN   r   r¬  r(   r)   r^   r­  r   r5   r/  r   )r	   r$  r®  r¯  r6   r
   r
   r   Útest_call_stringD  s    

z%POSIXProcessTestCase.test_call_stringc          
   C   sÊ   g }x>dD ]6}x0dD ](}t j ||¡}t j |¡r| |¡ qW q
W |sR|  d¡ d}t j |¡rxt j |¡sx| |¡ xL|D ]D}tjd|dtj	d}|" |  
|j ¡  ¡ t|dƒ¡ W d Q R X q~W d S )	N)z/binz	/usr/bin/z/usr/local/bin)ZbashZkshz"bash or ksh required for this testz/bin/shzecho $0T)r)   r|   r&   r+  )r^   ru   rv   Úisfilerñ   r÷   Úislinkr   r'   r*   r   r&   r…   r   Úbytes)r	   ZshellsrK  ÚnameÚshr/   r
   r
   r   Útest_specific_shellQ  s     






z(POSIXProcessTestCase.test_specific_shellc          
   G   sf   t   t jt j¡}z&tjtjddgdtjtjtjd}W d t   t j|¡ X |j 	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&   r   r]   )r9  ÚSIGINTÚdefault_int_handlerr   r'   r(   r)   r*   r&   r…   Úgetattr)r	   Úmethodr    r:  r/   r
   r
   r   Ú_kill_processe  s    
z"POSIXProcessTestCase._kill_process)ZnetbsdZopenbsdz"Due to known OS bug (issue #16762)c             G   sR   t jtjddgdt jt jt jd}|j d¡ t d¡ t	||ƒ|Ž  | 
¡  d S )Nz-cz¶if 1:
                             import sys, time
                             sys.stdout.write('x\n')
                             sys.stdout.flush()
                             T)r¼  r%   r&   r   r]   )r   r'   r(   r)   r*   r&   r…   r  r  r¿  rk   )r	   rÀ  r    r/   r
   r
   r   Ú_kill_dead_process~  s    


z'POSIXProcessTestCase._kill_dead_processc             C   s:   |   dtj¡}| ¡ \}}|  d|¡ |  | ¡ d¡ d S )NÚsend_signals   KeyboardInterruptr   )rÁ  r9  r½  rk   rD   r‚  r   )r	   r/   rÈ   r   r
   r
   r   Útest_send_signal•  s    z%POSIXProcessTestCase.test_send_signalc             C   s:   |   d¡}| ¡ \}}|  |d¡ |  | ¡ tj ¡ d S )Nr&  r¥   )rÁ  rk   r   r   r   r9  ÚSIGKILL)r	   r/   rÈ   r   r
   r
   r   Ú	test_kill›  s    
zPOSIXProcessTestCase.test_killc             C   s:   |   d¡}| ¡ \}}|  |d¡ |  | ¡ tj ¡ d S )NÚ	terminater¥   )rÁ  rk   r   r   r   r9  ÚSIGTERM)r	   r/   rÈ   r   r
   r
   r   Útest_terminate¡  s    
z#POSIXProcessTestCase.test_terminatec             C   s   |   dtj¡ d S )NrÃ  )rÂ  r9  r½  )r	   r
   r
   r   Útest_send_signal_dead§  s    z*POSIXProcessTestCase.test_send_signal_deadc             C   s   |   d¡ d S )Nr&  )rÂ  )r	   r
   r
   r   Útest_kill_dead«  s    z#POSIXProcessTestCase.test_kill_deadc             C   s   |   d¡ d S )NrÇ  )rÂ  )r	   r
   r
   r   Útest_terminate_dead¯  s    z(POSIXProcessTestCase.test_terminate_deadc             C   s:   g }x0|D ](}t  |¡}t  |¡}| |||f¡ q
W |S )N)r^   Úget_inheritableÚduprñ   )r	   Zsave_fdsr  r$  ÚinheritableÚsavedr
   r
   r   Ú	_save_fds³  s    


zPOSIXProcessTestCase._save_fdsc             C   s2   x,|D ]$\}}}t j|||d t  |¡ qW d S )N)rÏ  )r^   Údup2r.   )r	   r  r$  rÐ  rÏ  r
   r
   r   Ú_restore_fds»  s    z!POSIXProcessTestCase._restore_fdsc       	   	   C   sž   d}|   |¡}x |D ]\}}}|dkr|}P qW z\x|D ]}t |¡ q8W tjtjddg|tjtjd ¡ \}}t	 
|¡}|  ||fd¡ W d |  |¡ X d S )Nr   z-czRimport sys;sys.stdout.write("apple");sys.stdout.flush();sys.stderr.write("orange"))r%   r&   r   )s   apples   orange)rÑ  r^   r.   r   r'   r(   r)   r*   rk   r   r   r   rÓ  )	r	   r  r%   Ú	saved_fdsr$  rÐ  rÏ  rn   ro   r
   r
   r   Úcheck_close_std_fdsÀ  s"    



z(POSIXProcessTestCase.check_close_std_fdsc             C   s   |   dg¡ d S )Nr   )rÕ  )r	   r
   r
   r   Útest_close_fd_0Ù  s    z$POSIXProcessTestCase.test_close_fd_0c             C   s   |   dg¡ d S )Nr]   )rÕ  )r	   r
   r
   r   Útest_close_fd_1Ü  s    z$POSIXProcessTestCase.test_close_fd_1c             C   s   |   dg¡ d S )Nr  )rÕ  )r	   r
   r
   r   Útest_close_fd_2ß  s    z$POSIXProcessTestCase.test_close_fd_2c             C   s   |   ddg¡ d S )Nr   r]   )rÕ  )r	   r
   r
   r   Útest_close_fds_0_1â  s    z'POSIXProcessTestCase.test_close_fds_0_1c             C   s   |   ddg¡ d S )Nr   r  )rÕ  )r	   r
   r
   r   Útest_close_fds_0_2å  s    z'POSIXProcessTestCase.test_close_fds_0_2c             C   s   |   ddg¡ d S )Nr]   r  )rÕ  )r	   r
   r
   r   Útest_close_fds_1_2è  s    z'POSIXProcessTestCase.test_close_fds_1_2c             C   s   |   dddg¡ d S )Nr   r]   r  )rÕ  )r	   r
   r
   r   Útest_close_fds_0_1_2ë  s    z)POSIXProcessTestCase.test_close_fds_0_1_2c          
   C   sv   t  d¡}t  d¡}z.t  d¡ t  d¡ t tjddg¡ ¡  W dt  |d¡ t  |d¡ t  |¡ t  |¡ X dS )z=Issue #15798: Popen should work when stdio fds are available.r   r]   z-cz*print('AssertionError:0:CLOEXEC failure.')N)	r^   rÎ  r.   r   r'   r(   r)   r   rÒ  )r	   Z	new_stdinZ
new_stdoutr
   r
   r   Útest_small_errpipe_write_fdð  s    




z0POSIXProcessTestCase.test_small_errpipe_write_fdc       
   
   C   sP  dd„ t dƒD ƒ}zdd„ |D ƒ}x|D ]\}}t |¡ q*W t |d d¡ t |d dd¡ |  t dƒ¡}zTx t|ƒD ]\}}t ||¡ q|W tj	t
jdd	g|d |d
 |d d}| ¡  W d |  |¡ X x|D ]}t |dd¡ qØW t |d
 d¡}t t |d d¡¡}	|  |d¡ |  |	d¡ W d x|D ]}t |¡ q6W X d S )Nc             S   s   g | ]}t  ¡ ‘qS r
   )rK   r.  )rµ   rû   r
   r
   r   r·     s    z?POSIXProcessTestCase.test_remapping_std_fds.<locals>.<listcomp>rX   c             S   s   g | ]\}}|‘qS r
   r
   )rµ   r$  r®  r
   r
   r   r·   	  s    r]   s   STDINr   z-czZimport sys; got = sys.stdin.read();sys.stdout.write("got %s"%got); sys.stderr.write("err")r  )r%   r&   r   i   s	   got STDINs   err)rð   r^   ÚunlinkrN   r›   rÑ  Ú	enumeraterÒ  r   r'   r(   r)   r   rÓ  r…   r   r   r   r.   )
r	   ÚtempsÚtemp_fdsr$  r®  rÔ  Útemp_fdr/   rn   ro   r
   r
   r   Útest_remapping_std_fds  s4    


z+POSIXProcessTestCase.test_remapping_std_fdsc          
   C   s2  dd„ t dƒD ƒ}dd„ |D ƒ}zðx|D ]\}}t |¡ q(W |  t dƒ¡}zœx t|ƒD ]\}}	t |	|¡ qXW t |d¡ t |dd¡ tj	t
jddg|||d	}
|
 ¡  x|D ]}t |dd¡ q²W t |d
¡}t t |d
¡¡}W d |  |¡ X |  |d¡ |  |d¡ W d x|D ]}t |¡ qW X d S )Nc             S   s   g | ]}t  ¡ ‘qS r
   )rK   r.  )rµ   rû   r
   r
   r   r·   4  s    z7POSIXProcessTestCase.check_swap_fds.<locals>.<listcomp>rX   c             S   s   g | ]\}}|‘qS r
   r
   )rµ   r$  r®  r
   r
   r   r·   5  s    s   STDINr   z-czZimport sys; got = sys.stdin.read();sys.stdout.write("got %s"%got); sys.stderr.write("err"))r%   r&   r   i   s	   got STDINs   err)rð   r^   rÞ  rÑ  rß  rÒ  rN   r›   r   r'   r(   r)   r   r…   r   r   rÓ  r   r.   )r	   Zstdin_noZ	stdout_noZ	stderr_norà  rá  r$  r®  rÔ  râ  r/   rn   ro   r
   r
   r   Úcheck_swap_fds2  s4    


z#POSIXProcessTestCase.check_swap_fdsc             C   sX   |   ddd¡ |   ddd¡ |   ddd¡ |   ddd¡ |   ddd¡ |   ddd¡ d S )Nr   r]   r  )rä  )r	   r
   r
   r   Útest_swap_fdsb  s    z"POSIXProcessTestCase.test_swap_fdsc             C   sx  |   tdƒ¡}zVx0|D ](}t ¡ }t | ¡ |¡ W d Q R X qW ttdƒƒt|ƒ  ¡ }t 	|¡ dddg}i }x"t
||ƒD ]\}}	||||	 < q€W t d¡}
ttdƒƒt|ƒ  ¡ }tjtjd|
t|ƒgf|Ž}|  |d¡ x€t
||ƒD ]r\}}	t |dtj¡ t |d¡}ttt| d	¡ƒƒ}t d
|› d|› d|› d|› d|	› d¡}|  |	g||¡ qîW W d |  |¡ X d S )NrX   r%   r&   r   zå
                import os, sys
                skipped_fd = int(sys.argv[1])
                for fd in range(3):
                    if fd != skipped_fd:
                        os.write(fd, str(fd).encode('ascii'))
            z-cr   i   r+  z"
                    When testing z to z4 redirection,
                    parent descriptor z5 got redirected
                    to descriptor(s) z instead of descriptor z.
                )rÑ  rð   rK   rL   r^   rÒ  rš   Úsetrø   r.   ÚziprÝ   rÞ   r   r5   r(   r)   r  r   r›   ÚSEEK_SETr…   r¹   Úmapr…  r†   rÓ  )r	   Úfrom_fdsÚto_fdsrÔ  Zfrom_fdÚfZfd_to_closeZ	arg_namesr!   Zto_fdrm   Z
skipped_fdr6   Z
read_bytesZread_fdsr   r
   r
   r   Ú#_check_swap_std_fds_with_one_closedj  s2    



&z8POSIXProcessTestCase._check_swap_std_fds_with_one_closedc             C   sD   x>t  tdƒd¡D ]*}x$t  tdƒd¡D ]}|  ||¡ q(W qW d S )NrX   r  )Ú	itertoolsÚcombinationsrð   Úpermutationsrí  )r	   rê  rë  r
   r
   r   Ú!test_swap_std_fds_with_one_closed–  s    z6POSIXProcessTestCase.test_swap_std_fds_with_one_closedc          
   C   s®   dd„ }yt jtjddg|d W n| tk
r` } z |  t j¡ |  t|ƒd¡ W d d }~X Y nJ t j	k
rž } z |  
t j¡ |  t|ƒd¡ W d d }~X Y nX |  d¡ d S )	Nc               S   s   t dƒ‚d S )Nu   surrogate:í³¿)rT   r
   r
   r
   r   Úprepareœ  s    zCPOSIXProcessTestCase.test_surrogates_error_message.<locals>.preparez-cr¼   )r’  u   surrogate:í³¿z!Exception occurred in preexec_fn.z1Expected ValueError or subprocess.SubprocessError)r   r5   r(   r)   rT   r  r•  r   r  r~  ZassertIsNotNonerU   )r	   rò  ro   r
   r
   r   Útest_surrogates_error_message›  s    
 "z2POSIXProcessTestCase.test_surrogates_error_messagec             C   sâ   xÜdD ]Ô\}}|  dd¡}dt|ƒ }tj ¡ }|||< d|d< |}tjtjd|g|d}| 	d	¡}|  
| d¡t|ƒ¡ |  dd¡}d
t|ƒ }tj ¡ }|||< tjtjd|g|d}| 	d	¡}|  
| d¡t|ƒ¡ qW d S )N))Útestu   abcí³¿)u   testí³¿Z42r+  rç   z&import os; print(ascii(os.getenv(%s)))ÚCÚLC_ALLz-c)r\   s   
z'import os; print(ascii(os.getenvb(%s))))rÅ   ri  r^   r_   r`   r   rC   r(   r)   rl   r   r†   r+  )r	   ÚkeyÚvalueZencoded_valueÚscriptr\   Zdecoded_valuer&   r
   r
   r   Útest_undecodable_env®  s*    





z)POSIXProcessTestCase.test_undecodable_envc             C   sØ   t  tj¡}t j tj¡\}}t  |¡}t |ddg¡}|  |d¡ d| d }tj|dd}|  |d¡ t j	 
¡ }||d< tj|ddg|d	}|  |d¡ t j 
¡ }t  |¡|d
< tj|ddg|d	}|  |d¡ d S )Nz-cr¼   r   ó   's	   ' -c passT)r|   ÚPATH)r\   s   PATH)r^   Úfsencoder(   r)   ru   r‚   r   r5   r   r_   r`   Úenvironb)r	   Zabs_programru   ZprogramZexitcoder½   r\   Zenvbr
   r
   r   Útest_bytes_programË  s     


z'POSIXProcessTestCase.test_bytes_programc       	      C   sÂ   t jddd}t jddd}tjtj|gtjtjtjdd}|  |jd¡ tjtj|gtjdd}| ¡ \}}t	t
t| d	¡ƒƒ}t	|j ¡ |j ¡ |j ¡ gƒ}|  ||@ d
|||@ f ¡ d S )Nzinput_reader.pyÚsubprocessdata)Úsubdirzfd_status.pyF)r%   r&   r   r¼  r¥   )r&   r¼  ó   ,z5Expected no fds from %r to be open in child, found %r)r   Úfindfiler   r'   r(   r)   r*   rM   rk   ræ  ré  r…  r‚   r%   rš   r&   r   r   )	r	   ZsleeperÚ	fd_statusÚp1Úp2rE   ÚerrorZ
result_fdsZunwanted_fdsr
   r
   r   Útest_pipe_cloexecå  s    
z&POSIXProcessTestCase.test_pipe_cloexecc       
         s  t jddd}t jddd}d}|d d }tjtj|gtjtjdd	‰ tjtj||gˆ jtjdd	‰|  ˆ j	¡ |  ˆj	¡ ‡ fd
d„}‡fdd„}|  |¡ |  |¡ ˆ j
 |¡ ˆ j
 ¡  t ˆjgg g d¡\}}}	|  |d¡ |  ˆj ¡ |¡ ˆ j ¡  ˆj ¡  d S )Nzqcat.pyr   )r  zqgrep.pys   zxcvbné   ó   
F)r%   r&   r¼  c                  s&   yˆ   ¡  W n tk
r    Y nX d S )N)rÇ  ÚProcessLookupErrorr
   )r  r
   r   Úkill_p1	  s    zBPOSIXProcessTestCase.test_pipe_cloexec_real_tools.<locals>.kill_p1c                  s&   yˆ   ¡  W n tk
r    Y nX d S )N)rÇ  r  r
   )r  r
   r   Úkill_p2	  s    zBPOSIXProcessTestCase.test_pipe_cloexec_real_tools.<locals>.kill_p2rì   zThe child hung)r   r  r   r'   r(   r)   r*   r&   rM   r   r%   rN   r.   ÚselectrÊ   r   r…   )
r	   ZqcatZqgrepZsubdatarý   r  r  Z	readfilesZignored1Zignored2r
   )r  r  r   Útest_pipe_cloexec_real_toolsü  s.    



z1POSIXProcessTestCase.test_pipe_cloexec_real_toolsc       
         s°  t jddd}t ¡ }|  tj|d ¡ |  tj|d ¡ t|ƒ‰ x8tdƒD ],}t tj	tj
¡}|  tj|¡ ˆ  |¡ qLW xˆ D ]}t |d¡ q‚W tjtj|gtjdd	}| ¡ \}}ttt| d
¡ƒƒ}|  |ˆ @ ˆ d¡ tjtj|gtjdd	}| ¡ \}}ttt| d
¡ƒƒ}|  |ˆ @ d¡ |  d|d¡ t‡ fdd„tdƒD ƒƒ}	tjtj|gtjd|	d}| ¡ \}}ttt| d
¡ƒƒ}|  ||	 ˆ @ d¡ |  d|d¡ d S )Nzfd_status.pyr   )r  r   r]   é	   TF)r&   r¼  r  zSome fds were closedzSome fds were left openzSubprocess failedc             3   s   | ]}ˆ   ¡ V  qd S )N)rø   )rµ   rÈ   )r  r
   r   ú	<genexpr>I	  s    z6POSIXProcessTestCase.test_close_fds.<locals>.<genexpr>r™  )r&   r¼  Úpass_fdsz'Some fds not in pass_fds were left open)r   r  r^   rÐ   rM   r.   ræ  rð   rò   Údevnullr˜  ÚaddÚset_inheritabler   r'   r(   r)   r*   rk   ré  r…  r‚   r   r   rD   )
r	   r  r  rÈ   r$  r/   rE   ÚignoredÚremaining_fdsÚfds_to_keepr
   )r  r   Útest_close_fds%	  sB    

z#POSIXProcessTestCase.test_close_fdsZfreebsdz/devz/dev/fdz/Requires fdescfs mounted on /dev/fd on FreeBSD.c             C   sª   t jddd}tjtjdt d| ¡gtjd}| 	¡ \}}| 
¡ }| jt|ƒdd| d	 ttt|d
  ¡  d¡ƒƒ}ttt|d  ¡  d¡ƒƒ}| j||@ dd	 dS )z;Confirm that issue21618 is fixed (may fail under valgrind).zfd_status.pyr   )r  z-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&   r  z(expected exactly two lines of output:
%r)r   r   r  r]   zSome fds were left open.N)r   r  r   r'   r(   r)   rÝ   rÞ   r*   rk   r  r   rÇ   ræ  ré  r…  r   r‚   r   )r	   r  r/   rE   Zunused_stderrZoutput_linesZ
opened_fdsr  r
   r
   r   Ú%test_close_fds_when_max_fd_is_loweredU	  s    0
z:POSIXProcessTestCase.test_close_fds_when_max_fd_is_loweredrì   rG   c          
   C   s:  t jddd}tƒ }xftdƒD ]Z}t ¡ }|  tj|d ¡ |  tj|d ¡ t |d d¡ t |d d¡ | 	|¡ qW x¸|D ]°}t
jtj|gt
jd|fd}| ¡ \}}ttt| d	¡ƒƒ}	||h }
|  ||	d
¡ |  |	|
@ d¡ |  t¡&}|  t
jtjddgd|fd¡ W d Q R X |  dt|jƒ¡ q‚W d S )Nzfd_status.pyr   )r  rG   r   r]   T)r&   r¼  r  r  zfd to be passed not passedzfd to be closed passedz-czimport sys; sys.exit(0)F)r¼  r  zoverriding close_fds)r   r  ræ  rð   r^   rÐ   rM   r.   r  Úupdater   r'   r(   r)   r*   rk   ré  r…  r‚   rD   r   ZassertWarnsÚRuntimeWarningr5   r  Zwarning)r	   r  r  rÒ   r  r$  r/   rE   r  r  Zto_be_closedÚcontextr
   r
   r   Útest_pass_fds¨	  s0    




z"POSIXProcessTestCase.test_pass_fdsc       
      C   sä   t jddd}t ¡ \}}|  tj|¡ |  tj|¡ t |d¡ t |d¡ ||f}tj|g}|t	t
t|ƒƒ7 }tj|tjd|d}| ¡ \}}tt
t| d¡ƒƒ}	|  |	t|ƒd| ¡ |  t |¡d¡ |  t |¡d¡ d S )	Nzfd_status.pyr   )r  TF)r&   r¼  r  r  z	output=%a)r   r  r^   rÐ   rM   r.   r  r(   r)   r¹   ré  r  r   r'   r*   rk   ræ  r…  r‚   r   rÍ  )
r	   rù  rÏ  Znon_inheritabler  r    r/   rE   r  r  r
   r
   r   Útest_pass_fds_inheritableÊ	  s"    
z.POSIXProcessTestCase.test_pass_fds_inheritablec       	   	   C   sú   t jddd}g }x8tdƒD ],}t tjtj¡}|  tj|¡ | 	|¡ qW t 
¡ \}}|  tj|¡ |  tj|¡ | d|¡ tjtj|g|d |d |d d|d t |d	¡}W d
Q R X dd„ | d¡D ƒ}|  |dddht|ƒB d|›¡ d
S )z7Regression test for https://bugs.python.org/issue32270.zfd_status.pyr   )r  r  r]   r   T)r%   r&   r   r¼  r  i   Nc             S   s   h | ]}t |ƒ’qS r
   )r…  )rµ   Znumr
   r
   r   ú	<setcomp>ý	  s    z@POSIXProcessTestCase.test_pass_fds_redirected.<locals>.<setcomp>r  zoutput=)r   r  rð   r^   rò   r  ÚO_RDWRrM   r.   rñ   rÐ   Úinsertr   r'   r(   r)   r…   r‚   r   Ú	frozenset)	r	   r  r  rÈ   r$  Zstdout_rZstdout_wrE   r  r
   r
   r   Útest_pass_fds_redirectedè	  s&    
z-POSIXProcessTestCase.test_pass_fds_redirectedc          	   C   s>   t  tjd¡&}tjtjddg||d}| ¡  W d Q R X d S )Nzr+z-czimport sys; sys.exit(0))r&   r%   )	r,   rò   r^   r  r   r'   r(   r)   r   )r	   Úinoutr/   r
   r
   r   Ú%test_stdout_stdin_are_single_inout_fd
  s    
z:POSIXProcessTestCase.test_stdout_stdin_are_single_inout_fdc          	   C   s>   t  tjd¡&}tjtjddg||d}| ¡  W d Q R X d S )Nzr+z-czimport sys; sys.exit(0))r&   r   )	r,   rò   r^   r  r   r'   r(   r)   r   )r	   r%  r/   r
   r
   r   Ú&test_stdout_stderr_are_single_inout_fd
  s    
z;POSIXProcessTestCase.test_stdout_stderr_are_single_inout_fdc          	   C   s>   t  tjd¡&}tjtjddg||d}| ¡  W d Q R X d S )Nzr+z-czimport sys; sys.exit(0))r   r%   )	r,   rò   r^   r  r   r'   r(   r)   r   )r	   r%  r/   r
   r
   r   Ú%test_stderr_stdin_are_single_inout_fd
  s    
z:POSIXProcessTestCase.test_stderr_stdin_are_single_inout_fdc             C   sR   t jddd}tjtj|gtjtjd}| ¡ \}}|  d|j	d| 
d¡ ¡ d S )Nzsigchild_ignore.pyr   )r  )r&   r   r   z6sigchild_ignore.py exited non-zero with this error:
%szutf-8)r   r  r   r'   r(   r)   r*   rk   r   r@   r†   )r	   Zsigchild_ignorer/   r&   r   r
   r
   r   Útest_wait_when_sigchild_ignored
  s    z4POSIXProcessTestCase.test_wait_when_sigchild_ignoredc          	   C   sz   t  d¡}tjtjddgtjdd}|j}|  |j	¡ z4|  
| d¡d¡ |  || |gg g d¡d ¡ W d | ¡  X d S )	Nr  z-cz$import sys;sys.stdout.write("apple")r   )r&   r1   r	  s   applg        )r   Úimport_moduler   r'   r(   r)   r*   r&   rM   r.   r   r…   rD   r  r   )r	   r  r/   rì  r
   r
   r   Útest_select_unbuffered
  s    

"z+POSIXProcessTestCase.test_select_unbufferedc          	   C   s€   t jtjddgt jt jd}|  |jj¡ |  |jj¡ t	|ƒ}|j
}t dtf¡
 d }W d Q R X |  |dd„ t jD ƒ¡ d S )Nz-cz import sys, time;time.sleep(0.2))r&   r   r   c             S   s   g | ]}t |ƒ‘qS r
   )Úid)rµ   Úor
   r
   r   r·   B
  s    zEPOSIXProcessTestCase.test_zombie_fast_process_del.<locals>.<listcomp>)r   r'   r(   r)   r*   rM   r&   r.   r   r,  Úpidr   Úcheck_warningsÚResourceWarningrD   r   )r	   r/   Úidentr.  r
   r
   r   Útest_zombie_fast_process_del0
  s    

z1POSIXProcessTestCase.test_zombie_fast_process_delc          
   C   sø   t jtjddgt jt jd}|  |jj¡ |  |jj¡ t	|ƒ}|j
}t dtf¡
 d }W d Q R X t |tj¡ |  |dd„ t jD ƒ¡ t d¡ |  t¡& t jtt jt jd}W d Q R X W d Q R X |  ttj|d¡ |  |d	d„ t jD ƒ¡ d S )
Nz-czimport time;time.sleep(3))r&   r   r   c             S   s   g | ]}t |ƒ‘qS r
   )r,  )rµ   r-  r
   r
   r   r·   X
  s    zJPOSIXProcessTestCase.test_leak_fast_process_del_killed.<locals>.<listcomp>gš™™™™™É?r   c             S   s   g | ]}t |ƒ‘qS r
   )r,  )rµ   r-  r
   r
   r   r·   d
  s    )r   r'   r(   r)   r*   rM   r&   r.   r   r,  r.  r   r/  r0  r^   r&  r9  rÅ  rD   r   r  r  r9   rõ   rz   Úwaitpidrš  )r	   r/   r1  r.  r  r
   r
   r   Ú!test_leak_fast_process_del_killedD
  s(    


z6POSIXProcessTestCase.test_leak_fast_process_del_killedc                sx   t jddd}t d¡‰ |  tjˆ ¡ tjtj	|gtj
d‡ fdd„d}| ¡ \}}ttt| d	¡ƒƒ}|  ˆ |¡ d S )
Nzfd_status.pyr   )r  r]   Tc                  s   t  dˆ ¡S )Nr]   )r^   rÒ  r
   )r$  r
   r   r‘  p
  r¥   zCPOSIXProcessTestCase.test_close_fds_after_preexec.<locals>.<lambda>)r&   r¼  r’  r  )r   r  r^   rÎ  rM   r.   r   r'   r(   r)   r*   rk   ræ  ré  r…  r‚   rš  )r	   r  r/   rE   r  r  r
   )r$  r   Útest_close_fds_after_preexecf
  s    
z1POSIXProcessTestCase.test_close_fds_after_preexecc             C   sÈ   dd l }t ¡ }z¤dd„ }t ¡  xŽddgd dgfdgdd dgfdgdgddgfdgdgd dffD ]L\}}}}|  t¡0 | ||dd	||d
d
d
d
dddddd|¡ W d Q R X qbW W d |sÂt ¡  X d S )Nr   c               S   s   d S )Nr
   r
   r
   r
   r   r‘  
  r¥   z5POSIXProcessTestCase.test_fork_exec.<locals>.<lambda>é{   s   exes   envs   argTr
   éÿÿÿÿr]   r  rX   r	  )r•  rŸ  r   r¢  r9   rc   rz  r¡  )r	   r•  Ú
gc_enabledÚfuncr    Zexe_listr   Zenv_listr
   r
   r   Útest_fork_execw
  s&    z#POSIXProcessTestCase.test_fork_execc             C   sÆ   dd l }G dd„ dƒ}t ¡ }z”t ¡  x†ddddd|d	ƒ|d
ƒffD ]f}| jtd |¡d6}| dgdgd|d dgddddd	d
ddddd ¡ W d Q R X |  dt	|j
ƒ¡ qFW W d |sÀt ¡  X d S )Nr   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )zJPOSIXProcessTestCase.test_fork_exec_sorted_fd_sanity_check.<locals>.BadIntTc             S   s
   || _ d S )N)rø  )r	   rø  r
   r
   r   rd   ›
  s    zSPOSIXProcessTestCase.test_fork_exec_sorted_fd_sanity_check.<locals>.BadInt.__init__c             S   s   | j rd| _ | jS t‚d S )NF)Úfirstrø  rT   )r	   r
   r
   r   Ú__int__
  s    zRPOSIXProcessTestCase.test_fork_exec_sorted_fd_sanity_check.<locals>.BadInt.__int__N)r   r   r   r;  rd   r<  r
   r
   r
   r   ÚBadInt™
  s   r=  )r7  r  rX   r	  rG   )r  r	  )é   é   é*   l            )rG   r	  )r×   é   rA  r™  r]   r  zfds_to_keep={})r   s   falseTs   envr7  rX   r	  r  )r•  rŸ  r   r¢  r9   rT   rê   rz  rD   r  r?   r¡  )r	   r•  r=  r8  r  rA   r
   r
   r   Ú%test_fork_exec_sorted_fd_sanity_check•
  s0    

z:POSIXProcessTestCase.test_fork_exec_sorted_fd_sanity_checkc          
   C   sZ   t  tjddg¡}|: tj |d¡ }t|j_	| 
¡  |j ¡  W d Q R X W d Q R X d S )Nz-cr¼   r%   )r   r'   r(   r)   r   rw  ÚobjectÚBrokenPipeErrorr.   ru  rk   Zassert_called_with)r	   r  Úmock_proc_stdinr
   r
   r   Ú,test_communicate_BrokenPipeError_stdin_close½
  s
    zAPOSIXProcessTestCase.test_communicate_BrokenPipeError_stdin_closec          
   C   sh   t  tjddg¡}|H tj |d¡.}t|j_	| 
d¡ |j d¡ |j ¡  W d Q R X W d Q R X d S )Nz-cr¼   r%   s   stuff)r   r'   r(   r)   r   rw  rC  rD  rN   ru  rk   Úassert_called_once_withr.   )r	   r  rE  r
   r
   r   Ú,test_communicate_BrokenPipeError_stdin_writeÆ
  s    
zAPOSIXProcessTestCase.test_communicate_BrokenPipeError_stdin_writec             C   sˆ   t jtjdgt jt jd}|` tj |d¡F}tt	j
dƒ.}t|j_| ¡ |j_| d¡ |j ¡  W d Q R X W d Q R X W d Q R X d S )Nz-h)r%   r&   r%   Úwbs   stuff)r   r'   r(   r)   r*   r   rw  rC  rò   r^   r  rD  rÙ   ru  rš   Úreturn_valuerk   rG  )r	   r  rE  Zdev_nullr
   r
   r   Ú,test_communicate_BrokenPipeError_stdin_flushÐ
  s    

zAPOSIXProcessTestCase.test_communicate_BrokenPipeError_stdin_flushc          
   C   sf   t jtjdgt jt jd}|> tj |d¡$}t|j	_
|jdd |j	 ¡  W d Q R X W d Q R X d S )Nz-h)r%   r&   r%   iç  )r8   )r   r'   r(   r)   r*   r   rw  rC  rD  r.   ru  rk   rG  )r	   r  rE  r
   r
   r   Ú9test_communicate_BrokenPipeError_stdin_close_with_timeoutß
  s    
zNPOSIXProcessTestCase.test_communicate_BrokenPipeError_stdin_close_with_timeoutNÚ
W_STOPCODEzneed _testcapi.W_STOPCODEc          	   C   sx   t jddg}t |¡}|j}t |d¡\}}|  |d¡ t 	d¡}t
jd||fd | ¡ }W dQ R X |  |d¡ dS )	zATest wait() behavior when waitpid returns WIFSTOPPED; issue29335.z-cr¼   r   rX   zsubprocess.os.waitpid)rJ  Néýÿÿÿ)r(   r)   r   r'   r.  r^   r3  r   Ú	_testcapirM  r   rw  r   )r	   r    r  r.  Ústatusr@   r
   r
   r   Útest_stoppedë
  s    

z!POSIXProcessTestCase.test_stopped)cr   r   r   r   rk  rl  rm  rn  r   r'   rp  r   rw  r{  r  rH  rI  r^   ru   r0  rƒ  r†  rˆ  r‹  rŒ  rŽ  r“  r–  r›  rœ  r£  r(   rM  rª  r°  ri   r³  r´  rµ  r»  rÁ  r²   rÂ  rÄ  rÆ  rÉ  rÊ  rË  rÌ  rÑ  rÓ  rÕ  rÖ  r×  rØ  rÙ  rÚ  rÛ  rÜ  rÝ  rã  rä  rå  rí  rñ  ró  rú  rÿ  r  r  r  ÚstatÚst_devr  r   Zrequires_mac_verr  r  r$  r&  r'  r(  r)  r+  r2  r4  r5  Úcpython_onlyr:  rB  rF  rH  rK  rL  rN  rO  rO  rQ  Ú__classcell__r
   r
   )rf  r   rc  ä  s¤   %

-0,)0Q""(	
rc  zWindows specific testsc               @   sÂ   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zej	dd„ ƒZ
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-S ).ÚWin32ProcessTestCasec             C   s6   d}d}t  ¡ }||_||_t jtjddg|d d S )Nr]   rX   z-czimport sys; sys.exit(0))r±  )r   ÚSTARTUPINFOÚdwFlagsÚwShowWindowr5   r(   r)   )r	   ÚSTARTF_USESHOWWINDOWÚSW_MAXIMIZEr±  r
   r
   r   Útest_startupinfo  s    z%Win32ProcessTestCase.test_startupinfoc             C   s0   d}d}t j||d}t jtjddg|d d S )Nr]   rX   )rX  rY  z-czimport sys; sys.exit(0))r±  )r   rW  r5   r(   r)   )r	   ZSTARTF_USERSHOWWINDOWr[  r±  r
   r
   r   Útest_startupinfo_keywords  s    z.Win32ProcessTestCase.test_startupinfo_keywordsc             C   sâ   t  ¡ }t j|_t j|_xÄtdƒD ]¸}tjddg}t	t
jdƒ@}t j||t j|d}| | ¡  W d Q R X |  |jd¡ W d Q R X |  |jt j¡ |  |j¡ |  |j¡ |  |j¡ |  |jt j¡ |  |jdg i¡ q"W d S )Nr  z-cr¼   r«  )r&   r   r±  r   Úhandle_list)r   rW  rZ  rX  ZSW_HIDErY  rð   r(   r)   rò   r^   r  r'   rI   rk   r   r@   r  Z	hStdInputZ
hStdOutputZ	hStdErrorÚlpAttributeList)r	   r±  rÈ   r½   Znullr  r
   r
   r   Útest_startupinfo_copy!  s(    z*Win32ProcessTestCase.test_startupinfo_copyc             C   s(   d}t j d¡ tjt jd |d d S )Né   z'    a DOS box should flash briefly ...
z# -c "import time; time.sleep(0.25)")r²  )r(   r   rN   r   r5   r)   )r	   ZCREATE_NEW_CONSOLEr
   r
   r   Útest_creationflags<  s
    z'Win32ProcessTestCase.test_creationflagsc             C   s$   | j ttjtjddgdd„ d d S )Nz-czimport sys; sys.exit(47)c               S   s   dS )Nr]   r
   r
   r
   r
   r   r‘  I  r¥   z8Win32ProcessTestCase.test_invalid_args.<locals>.<lambda>)r’  )r9   rT   r   r5   r(   r)   )r	   r
   r
   r   ri   D  s    
z&Win32ProcessTestCase.test_invalid_argsc          	   C   sB   G dd„ dt ƒ}|  t¡ tjtjddg|ƒ d W d Q R X d S )Nc               @   s   e Zd ZdZdS )z4Win32ProcessTestCase.test_issue31471.<locals>.BadEnvN)r   r   r   Úkeysr
   r
   r
   r   ÚBadEnvO  s   rd  z-cr¼   )r\   )Údictr9   rc   r   r'   r(   r)   )r	   rd  r
   r
   r   Útest_issue31471K  s    z$Win32ProcessTestCase.test_issue31471c             C   s&   t jtjddgdd}|  |d¡ d S )Nz-czimport sys; sys.exit(47)T)r¼  r4   )r   r5   r(   r)   r   )r	   r6   r
   r
   r   r  T  s    
z#Win32ProcessTestCase.test_close_fdsc       
   	   C   sÜ  dd l }t ¡ }|  tj|d ¡ |  tj|d ¡ g }x(|D ] }t |d¡ | | |¡¡ q>W tj	t
jdd |d ¡gtjdd}| ¡ \}}|  |jd¡ t| ¡ ƒ tj	t
jdd |d ¡gtjtjdd}| ¡ \}}|  |jd¡ |  d	|¡ g }t ¡ }	d
|i|	_tj	t
jdd |d ¡gtjtj|	dd}| ¡ \}}|  |jd¡ |  d	|¡ t dtf¡d t ¡ }	d
|d d … i|	_tj	t
jdd |d ¡gtjtj|	dd}| ¡ \}}|  |jd¡ W d Q R X d S )Nr   r]   Tz-cz2import msvcrt; print(msvcrt.open_osfhandle({}, 0))F)r&   r¼  )r&   r   r¼  s   OSErrorr^  )r&   r   r±  r¼  z.*overriding close_fds)r  r^   rÐ   rM   r.   r  rñ   Zget_osfhandler   r'   r(   r)   rê   r*   rk   r   r@   r…  r   rD   rW  r_  r   r/  r  )
r	   r  r  rú   r$  r/   r&   r   r^  r±  r
   r
   r   Útest_close_fds_with_stdio[  sN    







z.Win32ProcessTestCase.test_close_fds_with_stdioc             C   s(   t  ¡ }i |_t jtjddg|d d S )Nz-czimport sys; sys.exit(0))r±  )r   rW  r_  r5   r(   r)   )r	   r±  r
   r
   r   Útest_empty_attribute_listŒ  s    z.Win32ProcessTestCase.test_empty_attribute_listc             C   s,   t  ¡ }dg i|_t jtjddg|d d S )Nr^  z-czimport sys; sys.exit(0))r±  )r   rW  r_  r5   r(   r)   )r	   r±  r
   r
   r   Útest_empty_handle_list’  s    
z+Win32ProcessTestCase.test_empty_handle_listc          	   C   sN   t j ¡ }d|d< tjdgdtj|d}| |  d|j ¡ ¡ W d Q R X d S )NÚphysalisr[   ræ  r]   )r|   r&   r\   s   physalis)	r^   r_   r`   r   r'   r*   rD   r&   r…   )r	   ra   r/   r
   r
   r   r³  ˜  s    

z(Win32ProcessTestCase.test_shell_sequencec          	   C   sL   t j ¡ }d|d< tjddtj|d}| |  d|j ¡ ¡ W d Q R X d S )Nrj  r[   ræ  r]   )r|   r&   r\   s   physalis)	r^   r_   r`   r   r'   r*   rD   r&   r…   )r	   ra   r/   r
   r
   r   r´  ¢  s    
z&Win32ProcessTestCase.test_shell_stringc          
   C   s^   xXdD ]P}t j ¡ }d|d< tjddtj||d}| |  d|j ¡ |¡ W d Q R X qW d S )N)ÚansiZoemrj  r[   ræ  r]   )r|   r&   r\   râ   )	r^   r_   r`   r   r'   r*   rD   r&   r…   )r	   Úencra   r/   r
   r
   r   Útest_shell_encodings¬  s    

z)Win32ProcessTestCase.test_shell_encodingsc             C   s    t  tjd ¡}|  |d¡ d S )Nz -c "import sys; sys.exit(47)"r4   )r   r5   r(   r)   r   )r	   r6   r
   r
   r   rµ  ¸  s    z%Win32ProcessTestCase.test_call_stringc          	   G   sz   t jtjddgt jt jt jd}|@ |j d¡ t||ƒ|Ž  | ¡ \}}|  	|d¡ | 
¡ }W d Q R X |  |d¡ d S )Nz-czâif 1:
                             import sys, time
                             sys.stdout.write('x\n')
                             sys.stdout.flush()
                             time.sleep(30)
                             )r%   r&   r   r]   r¥   r   )r   r'   r(   r)   r*   r&   r…   r¿  rk   r   r   r‚  )r	   rÀ  r    r/   rÈ   r   r@   r
   r
   r   rÁ  ¾  s    

z"Win32ProcessTestCase._kill_processc          	   G   s„   t jtjddgt jt jt jd}|J |j d¡ t d¡ t	||ƒ|Ž  | 
¡ \}}|  |d¡ | ¡ }W d Q R X |  |d¡ d S )Nz-czàif 1:
                             import sys, time
                             sys.stdout.write('x\n')
                             sys.stdout.flush()
                             sys.exit(42)
                             )r%   r&   r   r]   r¥   r@  )r   r'   r(   r)   r*   r&   r…   r  r  r¿  rk   r   r   r   )r	   rÀ  r    r/   rÈ   r   r6   r
   r
   r   rÂ  Ó  s    


z'Win32ProcessTestCase._kill_dead_processc             C   s   |   dtj¡ d S )NrÃ  )rÁ  r9  rÈ  )r	   r
   r
   r   rÄ  ê  s    z%Win32ProcessTestCase.test_send_signalc             C   s   |   d¡ d S )Nr&  )rÁ  )r	   r
   r
   r   rÆ  í  s    zWin32ProcessTestCase.test_killc             C   s   |   d¡ d S )NrÇ  )rÁ  )r	   r
   r
   r   rÉ  ð  s    z#Win32ProcessTestCase.test_terminatec             C   s   |   dtj¡ d S )NrÃ  )rÂ  r9  rÈ  )r	   r
   r
   r   rÊ  ó  s    z*Win32ProcessTestCase.test_send_signal_deadc             C   s   |   d¡ d S )Nr&  )rÂ  )r	   r
   r
   r   rË  ö  s    z#Win32ProcessTestCase.test_kill_deadc             C   s   |   d¡ d S )NrÇ  )rÂ  )r	   r
   r
   r   rÌ  ù  s    z(Win32ProcessTestCase.test_terminate_deadN)r   r   r   r\  r]  r`  rb  ri   r   rT  rf  r  rg  rh  ri  r³  r´  rm  rµ  rÁ  rÂ  rÄ  rÆ  rÉ  rÊ  rË  rÌ  r
   r
   r
   r   rV  ÿ
  s,   	1

rV  c               @   s^   e Zd ZG dd„ dejƒZej ejd¡dd„ ƒZ	dd„ Z
dd	„ Zd
d„ Zdd„ Zdd„ ZdS )Ú	MiscTestsc                   s$   e Zd ZdZg Z‡ fdd„Z‡  ZS )zMiscTests.RecordingPopenz<A Popen that saves a reference to each instance for testing.c                s   t ƒ j||Ž | j | ¡ d S )N)rd  rd   Úinstances_createdrñ   )r	   r    r!   )rf  r
   r   rd     s    z!MiscTests.RecordingPopen.__init__)r   r   r   r#   ro  rd   rU  r
   r
   )rf  r   ÚRecordingPopenþ  s   rp  Z_communicatec             K   sB  t |_ztj tjd¡ì}t |_tj td| j¡: |  t ¡$ |t	j
ddgfdtji|—Ž W dQ R X W dQ R X x4|jdd… D ]"}|  |tjddd	|j› ¡ q„W g }x(|jD ]}|tjd
dkr¶| |¡ q¶W | j|jd|jd | jt|ƒd|jd W dQ R X W d| jj ¡ }| ¡  | ¡  |  g | jj¡ X dS )a‡  Fake a SIGINT happening during Popen._communicate() and ._wait().

        This avoids the need to actually try and get test environments to send
        and receive signals reliably across platforms.  The net effect of a ^C
        happening during a blocking subprocess execution which we want to clean
        up from is a KeyboardInterrupt coming out of communicate() or wait().
        Z_waitr'   z-czLimport time
time.sleep(9)
import sys
sys.stderr.write('\n!runaway child!\n')r&   Nr]   )r8   z.no open-ended wait() after the first allowed: g      Ð?r  )r   )ÚKeyboardInterruptru  r   rw  rC  r   r'   rp  r9   r(   r)   rª   Zcall_args_listr‚  r5   rñ   ZassertLessEqualZ
call_countr   rÇ   ro  rø   r&  r   )r	   ZpopenerZmock__communicater!   Z
mock__waitr5   Zsigint_callsr=  r
   r
   r   Ú_test_keyboardinterrupt_no_kill  s4    

"

z)MiscTests._test_keyboardinterrupt_no_killc             C   s   | j tjdd d S )NgTã¥›Ä @)r8   )rr  r   r5   )r	   r
   r
   r   Ú#test_call_keyboardinterrupt_no_kill2  s    z-MiscTests.test_call_keyboardinterrupt_no_killc             C   s   | j tjdd d S )NgTã¥›Ä @)r8   )rr  r   rR  )r	   r
   r
   r   Ú"test_run_keyboardinterrupt_no_kill5  s    z,MiscTests.test_run_keyboardinterrupt_no_killc             C   s   dd„ }|   |¡ d S )Nc           	   _   s    t j| |Ž
}t‚W d Q R X d S )N)r   r'   rq  )r    r!   Zunused_processr
   r
   r   Úpopen_via_context_manager9  s    z[MiscTests.test_context_manager_keyboardinterrupt_no_kill.<locals>.popen_via_context_manager)rr  )r	   ru  r
   r
   r   Ú.test_context_manager_keyboardinterrupt_no_kill8  s    z8MiscTests.test_context_manager_keyboardinterrupt_no_killc          	   C   s‚   |   t d¡d¡ |   t d¡d¡ d }z@t ¡ }tj |d¡}t t	rLdnd| ¡\}}|  
|d¡ W d |d k	r|t |¡ X d S )Nz
echo xyzzyÚxyzzy)r   rw  Zfooztype zcat r   )r   r   Z	getoutputZgetstatusoutputrK   rï   r^   ru   rv   rî   r‚  Úrmdir)r	   Údirr¹  rP  rE   r
   r
   r   Útest_getoutput>  s    zMiscTests.test_getoutputc             C   sr   ddh}t tjƒ}t ƒ }ddl}x<tj ¡ D ].\}}| d¡r@q,t||jfƒrPq,| 	|¡ q,W |  
||| ¡ dS )z*Ensure that __all__ is populated properly.r  ZHandler   NrÈ   )ræ  r   Ú__all__ÚtypesÚ__dict__Úitemsr²   Ú
isinstanceÚ
ModuleTyper  r   )r	   Zintentionally_excludedZexportedZpossible_exportsr|  r¹  rø  r
   r
   r   Útest__all__Q  s    

zMiscTests.test__all__N)r   r   r   r   r'   rp  r   rw  rC  rr  rs  rt  rv  rz  r  r
   r
   r
   r   rn  ü  s   ,rn  ZPollSelectorz!Test needs selectors.PollSelectorc               @   s   e Zd Zdd„ Zdd„ ZdS )ÚProcessTestCaseNoPollc             C   s   t j| _tjt _t | ¡ d S )N)r   Ú_PopenSelectorÚorig_selectorÚ	selectorsZSelectSelectorr$   r   )r	   r
   r
   r   r   c  s    zProcessTestCaseNoPoll.setUpc             C   s   | j t_t | ¡ d S )N)r„  r   rƒ  r$   r   )r	   r
   r
   r   r   h  s    zProcessTestCaseNoPoll.tearDownN)r   r   r   r   r   r
   r
   r
   r   r‚  `  s   r‚  zWindows-specific testsc                   sP   e Zd Z‡ fdd„Z‡ fdd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	‡  Z
S )ÚCommandsWithSpacesc                s>   t ƒ  ¡  t dd¡\}}| ¡ | _t |d¡ t |¡ d S )Nz.pyzte stsV   import sys;sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv])))	rd  r   rK   r.  r‰  r®  r^   rN   r.   )r	   rì  r®  )rf  r
   r   r   p  s
    

zCommandsWithSpaces.setUpc                s   t  | j¡ tƒ  ¡  d S )N)r^   r/  r®  rd  r   )r	   )rf  r
   r   r   y  s    zCommandsWithSpaces.tearDownc          	   O   sH   t j|d< t j||Ž}|$ |  |j ¡  d¡d| j ¡ W d Q R X d S )Nr&   Úmbcsz2 [%r, 'ab cd'])r   r*   r'   r   r&   r…   r†   r®  )r	   r    r!   r/   r
   r
   r   Úwith_spaces}  s    
zCommandsWithSpaces.with_spacesc             C   s    | j dtj| jdf dd d S )Nz"%s" "%s" "%s"zab cdr]   )r|   )rˆ  r(   r)   r®  )r	   r
   r
   r   Útest_shell_string_with_spaces†  s    z0CommandsWithSpaces.test_shell_string_with_spacesc             C   s   | j tj| jdgdd d S )Nzab cdr]   )r|   )rˆ  r(   r)   r®  )r	   r
   r
   r   Útest_shell_sequence_with_spaces‹  s    z2CommandsWithSpaces.test_shell_sequence_with_spacesc             C   s   |   dtj| jdf ¡ d S )Nz"%s" "%s" "%s"zab cd)rˆ  r(   r)   r®  )r	   r
   r
   r   Útest_noshell_string_with_spaces  s    z2CommandsWithSpaces.test_noshell_string_with_spacesc             C   s   |   tj| jdg¡ d S )Nzab cd)rˆ  r(   r)   r®  )r	   r
   r
   r   Ú!test_noshell_sequence_with_spaces”  s    z4CommandsWithSpaces.test_noshell_sequence_with_spaces)r   r   r   r   r   rˆ  r‰  rŠ  r‹  rŒ  rU  r
   r
   )rf  r   r†  m  s   		r†  c               @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚContextManagerTestsc          	   C   sl   t jtjddgt jt jd*}|  |j ¡ d¡ |  |j	 ¡ d¡ W d Q R X |  
|jj¡ |  
|j	j¡ d S )Nz-czAimport sys;sys.stdout.write('stdout');sys.stderr.write('stderr');)r&   r   s   stdouts   stderr)r   r'   r(   r)   r*   r   r&   r…   r   r   rÊ   rË   )r	   r  r
   r
   r   Ú	test_pipe›  s    
zContextManagerTests.test_pipec          	   C   s0   t  tjddg¡}W d Q R X |  |jd¡ d S )Nz-czimport sys; sys.exit(100)éd   )r   r'   r(   r)   r   r@   )r	   r  r
   r
   r   rV  ¨  s    


z#ContextManagerTests.test_returncodec          	   C   s@   t jtjddgt jd}| d¡ |  |jd¡ W d Q R X d S )Nz-cz2import sys;sys.exit(sys.stdin.read() == 'context'))r%   s   contextr]   )r   r'   r(   r)   r*   rk   r   r@   )r	   r  r
   r
   r   rÀ   ¯  s
    

z*ContextManagerTests.test_communicate_stdinc          
   C   s:   |   t¡& tjttjtjd}W d Q R X W d Q R X d S )N)r&   r   )r9   ry   r   r'   rz   r*   )r	   r  r
   r
   r   ri   ·  s
    z%ContextManagerTests.test_invalid_argsc             C   sz   t jtjddgt jtjd d}| ¡ }|j 	dtj ¡ |  
|j¡ |  t|jddd¡ |  |jd¡ |  |jj¡ dS )z9Broken pipe error should not prevent wait() (Issue 21619)z-cr¼   r  )r%   r1   r2  Nr   )r   r'   r(   r)   r*   r   rÑ   Ú	__enter__r%   rN   r  r@   r9   rõ   Ú__exit__r   rÊ   rË   )r	   r  r
   r
   r   Útest_broken_pipe_cleanup¾  s    z,ContextManagerTests.test_broken_pipe_cleanupN)r   r   r   rŽ  rV  rÀ   ri   r’  r
   r
   r
   r   r  ™  s
   r  Ú__main__)7rH  r   rô  r   r   r(   rM  r9  r,   rî  r^   rö   rK   r  r…  rL  r  rù   r  rŸ  rÝ   Ztest.supportr   Zctypesr  Zctypes.utilrO  ZPGOZSkipTestrî   rØ   rz   rŽ   ÚNotADirectoryErrorÚPermissionErrorry   ZTestCaser   Ú	Exceptionr   r'   r   r$   rQ  rI  rc  rN  rV  rn  rO  r‚  r†  r  r   Úmainr
   r
   r
   r   Ú<module>   s˜   




                    $ }d+6
