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d„ ZdS )ÚPopenExecuteChildRaisesc             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   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   ÚTimeoutExpiredr4   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	   r5   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)r3   )	r8   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)é   )	r8   r   r=   rB   r'   r(   r   r>   r?   )r	   r@   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   rB   r'   r(   ÚSTDOUTrC   )r	   rD   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   rB   r'   r(   rC   )r	   ÚtfrD   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   rB   r'   r(   rC   )r	   rD   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   )r8   Ú
ValueErrorr   rB   r'   r(   r%   ÚfailrC   r>   r    )r	   r@   rD   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$   rQ   z7Expected ValueError when stdin and input args supplied.r$   rQ   )rJ   rK   rL   r-   rM   rN   r8   rS   r   rB   r'   r(   rT   rC   r>   r    )r	   rO   r@   rD   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)é   )r7   zExpected TimeoutExpired.s   BDFL)
r8   r   r9   rB   r'   r(   rT   r   r>   rD   )r	   r@   rD   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   r4   r'   r(   r   )r	   Únewenvr5   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_stderrr8   Ú	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)   rL   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)   rL   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)   rL   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)r3   )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(   rr   )r	   rs   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(   rr   r8   ÚNONEXISTING_ERRORSÚNONEXISTING_CMD)r	   rq   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)rr   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]   rt   Ú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%   r3   zutf-8)r   r&   r)   rL   r%   r-   r   r   r?   r]   rt   Ú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~   )rJ   Ú
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~   )rJ   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]   rt   ru   Úcurdirr   Útemp_cwdr8   Ú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]   rt   ru   r‹   r   rŒ   r8   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]   rt   ru   r‹   r   rˆ   r8   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$   rM   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\   )rJ   rK   rL   r-   Úfilenor]   rM   Úlseekr   r&   r'   r(   r   r   r?   )r	   rO   Ú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\   )rJ   rK   rL   r-   rM   rN   r   r&   r'   r(   r   r   r?   )r	   rO   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)rJ   rK   rL   r-   r™   r   r&   r'   r(   r   r]   rš   r   r„   )r	   rO   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)rJ   rK   rL   r-   r   r&   r'   r(   r   rN   r   r„   )r	   rO   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)rJ   rK   rL   r-   r™   r   r&   r'   r(   r   r]   rš   r   r„   )r	   rO   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)rJ   rK   rL   r-   r   r&   r'   r(   r   rN   r   r„   )r	   rO   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)   rj   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)   rH   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)rJ   rK   rL   r-   r   r&   r'   r(   r   rN   r   r„   )r	   rO   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)   rL   r%   r-   r   rj   r   r?   rk   )r	   rl   r.   rm   rn   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ÚorangerZ   z-cz2import sys,os;sys.stdout.write(os.getenv("FRUIT")))r%   r[   s   orange)
r]   r^   r_   r   r&   r'   r(   r)   rj   r   )r	   r`   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 )Nc             S   s2   d| kp0d| kp0d| kp0| dkp0|   d¡p0| dkS )NZ	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>)r   r&   r'   r(   r)   rj   Ú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(   r8   rS   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=cabbagerZ   ZlemonzFRUIT=ORANGEzorange=lemonz3import sys, os;sys.stdout.write(os.getenv("FRUIT")))r%   r[   s   orange=lemon)r]   r^   r_   r8   rS   r   r&   r'   r(   r)   rj   r   )r	   r`   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)   rj   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)   rj   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)   rj   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)   rL   r%   r-   r   r$   rj   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   rY   g333333Ó?)r7   s   pineapple
pear
)r   r&   r'   r(   r)   r8   r9   rj   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š™™™™™Ù?)r7   i   )
r   r&   r'   r(   r)   r8   r9   rj   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(   rj   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(   rj   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)   rL   r%   r   r$   rj   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)   rL   r%   r-   r   r$   rM   rj   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$   rM   Úflushr   r%   Úreadliner-   rL   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)   rL   r%   r-   r   rj   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)   rj   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)   rj   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)   rL   r%   r-   r   rj   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   )rQ   z1
2
3
4)r'   r(   r   r&   r)   rj   r   )r	   rá   rl   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   )rQ   z[{}])r'   r(   r   r&   r)   rj   r   Úformat)r	   rè   r   rl   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   )Ú	mswindowsrJ   ÚmkdtempÚranger]   rt   ru   r   ZTESTFNÚappendÚopenÚO_WRONLYÚO_CREATÚOSErrorÚerrnoZEMFILEÚskipTestr-   Úpopr   r&   r'   r(   r)   rj   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\"br@   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)   rL   r$   r-   ÚassertIsNoneÚpollr]   rM   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?)r7   z0.0001rW   r   )r   r&   r'   r(   r8   r9   r   rC   Ústrr>   r   )r	   r.   r@   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­   )r8   rb   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   )r0   )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   r0   rÃ   r   )r   r&   r'   r(   r)   r©   r$   rM   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ï   r8   rx   r   r&   ry   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Ý   ry   r'   r(   r   r&   r)   rj   r   r?   )r	   r  rl   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Ústartr8   ÚEnvironmentErrorr   r&   ry   r)   ru   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 )Nz-czimport time; time.sleep(12)c                  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  )r7   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   ru   )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!)ru   r   rB   r'   r(   rÉ   r±   Úascii)r	   rl   rD   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   )rJ   Úmkstempr   r&   rô   r]   r-   Úremover   rt   Ú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)   rL   r%   r-   r   r$   rj   )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$   r1  i   )
r   r&   r'   r(   r)   rL   r$   r-   r   rj   )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   )	Úsignalr4  rL   r'   r(   r   r)   r&   rj   )r	   r7  Ú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 )Nz/proc/%d/fdz-cr»   )r$   r%   r   )r]   ÚgetpidÚlistdirr8   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    )r8   r   r   r4   r   r>   Úfilename)r	   r@   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~   )r8   r   r   r&   r   r>   rB  )r	   r@   r
   r
   r   Ú test_file_not_found_with_bad_cwd^  s    z0ProcessTestCase.test_file_not_found_with_bad_cwdN)pr   r   r   r/   r2   r6   r:   r<   rA   rE   rG   rI   rP   rR   rU   rV   rX   ra   rh   ri   ro   rp   rr   rw   rz   Ú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+  r0  r2  r3  Ú
skipUnlessÚhasattrr8  r]   r=  rt   Úisdirr>  r@  rC  rF  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 )Nz-c)r'   r(   r   Úrun)r	   rl   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)r3   )rS  r   r?   r8   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)Úcheckr3   )r8   r   r=   rS  r   r>   r?   )r	   r@   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)rV  r   )rS  r   r?   )r	   rT  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?)r7   )r8   r   r9   rS  )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)rS  r   r)   rC   r%   )r	   rT  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)rS  r   r)   rC   r   )r	   rT  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)rJ   rK   rL   r-   rM   rN   rS  r   r)   rC   r%   )r	   rO   rT  r
   r
   r   rP   Ž  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)rQ   r%   s   PEAR)rS  r   r)   rC   r%   )r	   rT  r
   r
   r   rR   ™  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$   rQ   r$   rQ   )rJ   rK   rL   r-   rM   rN   r8   rS   rS  rC   r>   r    )r	   rO   r@   rD   r
   r
   r   rV      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)rW   )r7   r%   s   BDFL)	r8   r   r9   rS  r)   r   r>   rD   r%   )r	   r@   rT  r
   r
   r   rX   ­  s    z)RunFuncTestCase.test_check_output_timeoutc             C   s2   t j ¡ }d|d< | jd|d}|  |jd¡ d S )NrY   rZ   zCimport sys, os;sys.exit(33 if os.getenv("FRUIT")=="banana" else 31))r[   é!   )r]   r^   r_   rS  r   r?   )r	   r`   rT  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)rS  rC   r%   r   )r	   rT  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^  )
rJ   rK   rL   r-   r8   rS   rS  rC   r>   r    )r	   rO   r@   rD   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^  )
rJ   rK   rL   r-   r8   rS   rS  rC   r>   r    )r	   rO   r@   rD   r
   r
   r   Ú#test_stderr_with_capture_output_arg×  s    z3RunFuncTestCase.test_stderr_with_capture_output_argN)r   r   r   rS  rU  rW  rX  rY  rZ  r[  rP   rR   rV   rX   r]  r_  r`  ra  r
   r
   r
   r   rP  e  s   rP  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]   Úchdirrd  rô   ÚstrerrorÚreprrT   )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 )Nz-cr   )r~   zExpected OSError: %s)rj  r   r&   r'   r(   rd  rô   r   rõ   rg  rT   )r	   ri  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 )Nz-cr   )r(   zExpected OSError: %s)rj  r   r&   r'   r(   rd  rô   r   rõ   rg  rT   )r	   ri  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 )Nz-cr   zExpected OSError: %s)	rj  r   r&   rd  rô   r   rõ   rg  rT   )r	   ri  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   rn  r
   r
   r
   r   ÚPopenNoDestructor)  s   ro  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 )Nc              W   s0   | d }d  tj¡ ¡ }t |d| d ¡ dS )Né   z{:x}s   OSError:ó   :r   )ré   rõ   ZEISDIRrÄ   r]   rM   )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_command)rt  r   ÚpatchÚChildProcessErrorr8   ÚIsADirectoryErrorro  )r	   Ú	fork_execrs  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 )Ns   ÿ Þ­c                 s   | d }t  |ˆ ¡ dS )Nrp  r   )r]   rM   )r    rr  )Ú
error_datar
   r   Ú	bad_errorC  s    zGPOSIXProcessTestCase.test_exception_errpipe_bad_data.<locals>.bad_errorzsubprocess.os.waitpid)rt  ru  )rt  r   rv  rw  r8   r   ÚSubprocessErrorro  rC   rh  r  r>   )r	   ry  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   rB   Ú
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   rB   r'   r(   rô   rõ   ZEPERMr]   Úgetpgidr>  Úintr  )r	   rD   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?   r8  Ú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 cmdr8  ZSIG)r   r=   r„  r8  r†  r  rC   Úlower)r	   rn   Ú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  rC   )r	   rn   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  rC   )r	   rn   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 )NrZ   Ú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?)rS   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É   Ú_posixsubprocessrS   rC   r    rT   )r	   r“  r.   rû   r
   r
   r   Útest_preexec_exception¥  s    $z+POSIXProcessTestCase.test_preexec_exceptionc               @   s   e Zd Zdd„ Zdd„ ZdS )z+POSIXProcessTestCase._TestExecuteChildPopenc             O   s   || _ tjj| f|ž|Ž d S )N)Ú	_testcaser   r&   rc   )r	   Ztestcaser    r!   r
   r
   r   rc   ·  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   rc   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 )Nc               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‘  )r8   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   r4   r'   r(   ÚenablerÉ   r8   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ö   rL   r   r4   r'   r(   ÚBlockingIOError)r	   r¤  r¥  r¦  rn   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À  r3   )rJ   r-  rñ   rM   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)r3   )Ústartupinfo)Úcreationflags)r8   rS   r   r4   r'   r(   )r	   r
   r
   r   rh   %  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Ž  rZ   zecho $FRUITr\   )r{   r%   r[   s    	
s   apple)
r]   r^   r_   r   r&   r)   r   r%   r„   r   )r	   r`   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Ž  rZ   zecho $FRUITr\   )r{   r%   r[   s    	
s   apple)
r]   r^   r_   r   r&   r)   r   r%   r„   r   )r	   r`   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À  r3   )rJ   r-  rñ   rM   r   r«  r'   r(   r]   r¬  r   r4   r.  r   )r	   r#  r­  r®  r5   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]   rt   ru   Úisfilerð   rö   Úislinkr   r&   r)   r   r%   r„   r   Úbytes)r	   ZshellsrJ  Ú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\   )r8  ÚSIGINTÚdefault_int_handlerr   r&   r'   r(   r)   r%   r„   Úgetattr)r	   Úmethodr    r9  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¾  rj   )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À  r8  r¼  rj   rC   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À  rj   r   r   r   r8  Ú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À  rj   r   r   r   r8  ÚSIGTERM)r	   r.   rÇ   r   r
   r
   r   Útest_terminate¡  s    
z#POSIXProcessTestCase.test_terminatec             C   s   |   dtj¡ d S )NrÂ  )rÁ  r8  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)   rj   r   r   r   rÒ  )	r	   r  r$   Ú	saved_fdsr#  rÏ  rÎ  rm   rn   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 )Nr   r\   z-cz*print('AssertionError:0:CLOEXEC failure.'))	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
   )rJ   r-  )r´   rú   r
   r
   r   r¶     s    z?POSIXProcessTestCase.test_remapping_std_fds.<locals>.<listcomp>rW   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]   ÚunlinkrM   rš   rÐ  Ú	enumeraterÑ  r   r&   r'   r(   r   rÒ  r„   r   r   r   r-   )
r	   ÚtempsÚtemp_fdsr#  r­  rÓ  Útemp_fdr.   rm   rn   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
   )rJ   r-  )r´   rú   r
   r
   r   r¶   4  s    z7POSIXProcessTestCase.check_swap_fds.<locals>.<listcomp>rW   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Ñ  rM   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.   rm   rn   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 )NrW   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ï   rJ   rK   r]   rÑ  r™   Úsetr÷   r-   ÚziprÜ   rÝ   r   r4   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_fdrl   Z
skipped_fdr5   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 )NrW   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:í³¿)rS   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   r4   r'   r(   rS   r  r”  r   r  r}  ZassertIsNotNonerT   )r	   rñ  rn   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Ä   rh  r]   r^   r_   r   rB   r'   r(   rk   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(   rt   r   r   r4   r   r^   r_   Úenvironb)r	   Zabs_programrt   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)   rL   rj   rå  rè  r„  r   r$   r™   r%   r   r   )	r	   ZsleeperÚ	fd_statusÚp1Úp2rD   Ú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%   rL   r   r$   rM   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Ï   rL   r-   rå  rï   rñ   Údevnullr—  ÚaddÚset_inheritabler   r&   r'   r(   r)   rj   rè  r„  r   r   r   rC   )
r	   r  r  rÇ   r#  r.   rD   Ú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 )Nz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.)r   r  r   r&   r'   r(   rÜ   rÝ   r)   rj   r€  r   rÆ   rå  rè  r„  r   r   r   )r	   r  r.   rD   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ë   rF   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   rF   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Ï   rL   r-   r  Úupdater   r&   r'   r(   r)   rj   rè  r„  r   rC   r   ZassertWarnsÚRuntimeWarningr4   r  Zwarning)r	   r  r  rÑ   r  r#  r.   rD   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Ï   rL   r-   r  r'   r(   r¸   rè  r  r   r&   r)   rj   rå  r„  r   r   rÌ  )
r	   rø  rÎ  Znon_inheritabler  r    r.   rD   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 )Nzfd_status.pyrÿ  )r   rŒ  r\   r   T)r$   r%   r   r»  r  i   c             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_RDWRrL   r-   rð   rÏ   Úinsertr   r&   r'   r(   r„   r   r   Ú	frozenset)	r	   r  r  rÇ   r#  Zstdout_rZstdout_wrD   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)   rj   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%   r0   r  s   applg        )r   Úimport_moduler   r&   r'   r(   r)   r%   rL   r-   r   r„   rC   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)   rL   r%   r-   r   r+  Úpidr   Úcheck_warningsÚResourceWarningrC   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)   rL   r%   r-   r   r+  r-  r   r.  r/  r]   r%  r8  rÄ  rC   r   r  r  r8   rô   ry   Úwaitpidr™  )r	   r.   r0  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Í  rL   r-   r   r&   r'   r(   r)   rj   rå  rè  r„  r   r™  )r	   r  r.   rD   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Œ  rW   r  )r”  rž  rŸ  r¡  r8   rb   ry  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   rc   ›
  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÷  rS   )r	   r
   r
   r   Ú__int__
  s    zRPOSIXProcessTestCase.test_fork_exec_sorted_fd_sanity_check.<locals>.BadInt.__int__N)r   r   r   r:  rc   r;  r
   r
   r
   r   ÚBadInt™
  s   r<  )r6  rŒ  rW   r  rF   )r  r  )é   é   é*   l            )rF   r  )rÖ   é   r@  r˜  r\   rŒ  zfds_to_keep={})r   s   falseTs   envr6  rW   r  r  )r”  rž  rŸ  r¡  r8   rS   ré   ry  rC   r  r>   r   )r	   r”  r<  r7  r  r@   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   rv  ÚobjectÚBrokenPipeErrorr-   rt  rj   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   rv  rB  rC  rM   rt  rj   Úassert_called_once_withr-   )r	   r  rD  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   rv  rB  rñ   r]   r  rC  rØ   rt  r™   Úreturn_valuerj   rF  )r	   r  rD  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ç  )r7   )r   r&   r'   r(   r)   r   rv  rB  rC  r-   rt  rj   rF  )r	   r  rD  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 )Nz-cr»   r   rW   zsubprocess.os.waitpid)rI  éýÿÿÿ)r'   r(   r   r&   r-  r]   r2  r   Ú	_testcapirL  r   rv  r   )r	   r    r  r-  Ústatusr?   r
   r
   r   Útest_stoppedë
  s    

z!POSIXProcessTestCase.test_stopped)cr   r   r   r   rj  rk  rl  rm  r   r&   ro  r   rv  rz  r~  rG  rH  r]   rt   r/  r‚  r…  r‡  rŠ  r‹  r  r’  r•  rš  r›  r¢  r'   rL  r©  r¯  rh   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*  r1  r3  r4  Úcpython_onlyr9  rA  rE  rG  rJ  rK  rM  rN  rN  rP  Ú__classcell__r
   r
   )re  r   rb  ä  s¤   %

-0,)0Q""(	
rb  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\   rW   z-czimport sys; sys.exit(0))r°  )r   ÚSTARTUPINFOÚdwFlagsÚwShowWindowr4   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\   rW   )rW  rX  z-czimport sys; sys.exit(0))r°  )r   rV  r4   r'   r(   )r	   ZSTARTF_USERSHOWWINDOWrZ  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   rV  rY  rW  ZSW_HIDErX  rï   r'   r(   rñ   r]   r  r&   rH   rj   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   rM   r   r4   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‘  )r8   rS   r   r4   r'   r(   )r	   r
   r
   r   rh   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   rc  z-cr»   )r[   )Údictr8   rb   r   r&   r'   r(   )r	   rc  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»  r3   )r   r4   r'   r(   r   )r	   r5   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Ï   rL   r-   r  rð   Zget_osfhandler   r&   r'   r(   ré   r)   rj   r   r?   r„  r   rC   rV  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   rV  r^  r4   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   rV  r^  r4   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ÚphysalisrZ   rå  r\   )r{   r%   r[   s   physalis)	r]   r^   r_   r   r&   r)   rC   r%   r„   )r	   r`   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 )Nri  rZ   rå  r\   )r{   r%   r[   s   physalis)	r]   r^   r_   r   r&   r)   rC   r%   r„   )r	   r`   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oemri  rZ   rå  r\   )r{   r%   r[   rá   )	r]   r^   r_   r   r&   r)   rC   r%   r„   )r	   Úencr`   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)"r3   )r   r4   r'   r(   r   )r	   r5   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¾  rj   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¾  rj   r   r   r   )r	   r¿  r    r.   rÇ   r   r5   r
   r
   r   rÁ  Ó  s    


z'Win32ProcessTestCase._kill_dead_processc             C   s   |   dtj¡ d S )NrÂ  )rÀ  r8  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Á  r8  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_  ra  rh   r   rS  re  r  rf  rg  rh  r²  r³  rl  r´  rÀ  rÁ  rÃ  rÅ  rÈ  rÉ  rÊ  rË  r
   r
   r
   r   rU  ÿ
  s,   	1

rU  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g Z‡ fdd„Z‡  ZS )zMiscTests.RecordingPopenc                s   t ƒ j||Ž | j | ¡ d S )N)rc  rc   Úinstances_createdrð   )r	   r    r!   )re  r
   r   rc     s    z!MiscTests.RecordingPopen.__init__)r   r   r   rn  rc   rT  r
   r
   )re  r   ÚRecordingPopenþ  s   ro  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 )NZ_waitr&   z-czLimport time
time.sleep(9)
import sys
sys.stderr.write('\n!runaway child!\n')r%   r\   )r7   z.no open-ended wait() after the first allowed: g      Ð?rŒ  )r   )ÚKeyboardInterruptrt  r   rv  rB  r   r&   ro  r8   r'   r(   r©   Zcall_args_listr  r4   rð   ZassertLessEqualZ
call_countr   rÆ   rn  r÷   r%  r   )r	   ZpopenerZmock__communicater!   Z
mock__waitr4   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ã¥›Ä @)r7   )rq  r   r4   )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ã¥›Ä @)r7   )rq  r   rQ  )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&   rp  )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)rq  )r	   rt  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   rv  Zfooztype zcat r   )r   r   Z	getoutputZgetstatusoutputrJ   rî   r]   rt   ru   rí   r  Úrmdir)r	   Údirr¸  rO  rD   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 )Nr   ZHandler   rÇ   )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&   ro  r   rv  rB  rq  rr  rs  ru  ry  r€  r
   r
   r
   r   rm  ü  s   ,rm  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])))	rc  r   rJ   r-  rˆ  r­  r]   rM   r-   )r	   rë  r­  )re  r
   r   r   p  s
    

zCommandsWithSpaces.setUpc                s   t  | j¡ tƒ  ¡  d S )N)r]   r.  r­  rc  r   )r	   )re  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‹  rT  r
   r
   )re  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   rU  ¨  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)   rj   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   )r8   rx   r   r&   ry   r)   )r	   r  r
   r
   r   rh   ·  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 )Nz-cr»   rŒ  )r$   r0   r1  r   )r   r&   r'   r(   r)   r   rÐ   Ú	__enter__r$   rM   r  r?   r8   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  rU  r¿   rh   r‘  r
   r
   r
   r   rŒ  ™  s
   rŒ  Ú__main__)7rG  r   ró  r   r   r'   rL  r8  r+   rí  r]   rõ   rJ   r  r„  rK  r  rø   r  rž  rÜ   Ztest.supportr   Zctypesr  Zctypes.utilrN  ZPGOZSkipTestrí   r×   ry   r   ÚNotADirectoryErrorÚPermissionErrorrx   ZTestCaser   Ú	Exceptionr   r&   r   r#   rP  rH  rb  rM  rU  rm  rN  r  r…  rŒ  r   Úmainr
   r
   r
   r   Ú<module>   s˜   




                    $ }d+6
