
|W9                 @   sA  d  d l  m Z d  d l Z d  d l j j Z d  d l Z d  d l	 Z	 d  d l
 Z
 e
 j j j d  Z d  d l m Z e	 j d: k r d d   Z d d	   Z n d
 d   Z d d	   Z d d   Z Gd d   d  Z d d   Z d d   Z d d   Z e d d    Z d d   Z d d   Z Gd d   d  Z Gd d   d  Z Gd  d!   d! e  Z Gd" d#   d# e  Z Gd$ d%   d% e  Z  e d& d'    Z! Gd( d)   d)  Z" d* d+   Z# e d, d-    Z$ e d. d/    Z% e e
 j j j& d0 d1 d2 g  d3 d4     Z' e
 j j j& d5 d6 d7 g  d8 d9    Z( d S);    )with_statementNznot hasattr(os, 'dup'))print_   c          
   C   s  t  |  t  r |  j d  }  t  |  t  } | sd d d d t j   k s^ t j |   rm t j |   n d d d t j   k s t j t   r t j t   n d d d	 t j   k s t j t  r t j t  n d	 d
 t j |  i } t	 t j
 |    d  } |  S)NzUTF-8 z5assert %(py4)s
{%(py4)s = %(py0)s(%(py1)s, %(py2)s)
}py1objpy0
isinstancepy2bytespy4)r	   strencoder   @py_builtinslocals
@pytest_ar_should_repr_global_name	_safereprAssertionError_format_explanation)r   @py_assert3@py_format5 r   >/home/ronny/Projects/pytest-dev/py/testing/io_/test_capture.pytobytes   s        r   c          
   C   s  t  |  t  r t |  d  }  t  |  t  } | sd d d d t j   k s^ t j |   rm t j |   n d d d t j   k s t j t   r t j t   n d d d	 t j   k s t j t  r t j t  n d	 d
 t j |  i } t t j	 |    d  } |  S)NzUTF-8r   z5assert %(py4)s
{%(py4)s = %(py0)s(%(py1)s, %(py2)s)
}r   r   r   r	   r
   r   r   )
r	   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   totext   s        r   c          
   C   s  t  |  t  r |  j d  }  t  |  t  } | sd d d d t j   k s^ t j |   rm t j |   n d d d t j   k s t j t   r t j t   n d d d	 t j   k s t j t  r t j t  n d	 d
 t j |  i } t	 t j
 |    d  } |  S)NzUTF-8r   z5assert %(py4)s
{%(py4)s = %(py0)s(%(py1)s, %(py2)s)
}r   r   r   r	   r
   r   r   )r	   unicoder   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r      s        c          
   C   s  t  |  t  r t |  d  }  t  |  t  } | sd d d d t j   k s^ t j |   rm t j |   n d d d t j   k s t j t   r t j t   n d d d	 t j   k s t j t  r t j t  n d	 d
 t j |  i } t t j	 |    d  } |  S)NzUTF-8r   z5assert %(py4)s
{%(py4)s = %(py0)s(%(py1)s, %(py2)s)
}r   r   r   r	   r
   r   r   )
r	   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r      s        c             C   s   t  j |  t |   d  S)N)oswriter   )fdr   r   r   r   oswritebytes!   s    r    c               @   s(   e  Z d  Z d d   Z d d   Z d S)
TestTextIOc             C   s   t  j j   } | j d  | j   } d } | | k } | s t j d
 | f d | | f  d d t j   k s t j	 |  r t j
 |  n d d t j
 |  i } d d	 | i } t t j |    d  } } | j   d  S)Nhello==%(py0)s == %(py3)sr   spy3r   assert %(py5)spy5)r#   )r$   r'   )pyioTextIOr   getvaluer   _call_reprcomparer   r   r   r   r   r   close)selffr%   @py_assert2@py_assert1@py_format4@py_format6r   r   r   	test_text%   s       j   
zTestTextIO.test_textc          
   C   sm  t  j j   } t j d k rA | j d  t  j j t d  n(| j t	 d d   | j d  | j
   } | j   t | t	  } | scd d d	 d
 t j   k s t j |  r t j |  n d
 d d t j   k s t j t  r t j t  n d d d t j   k s&t j t	  r5t j t	  n d d t j |  i } t t j |    d  } d  S)Nr   r      öz f.write(bytes('hello', 'UTF-8'))zUTF-8r"   r   z5assert %(py4)s
{%(py4)s = %(py0)s(%(py1)s, %(py2)s)
}r   r%   r   r	   r
   r   r   )r   r   )r)   r*   r+   sysversion_infor   testraises	TypeErrorr   r,   r.   r	   r   r   r   r   r   r   r   )r/   r0   r%   r   r   r   r   r   test_unicode_and_str_mixture,   s    
    z'TestTextIO.test_unicode_and_str_mixtureN)__name__
__module____qualname__r5   r<   r   r   r   r   r!   $   s   r!   c           
   C   sK  t  j j   }  |  j t d   t  j j t d  |  j   } d } t |  } | | k } | s9t	 j
 d | f d | | f  d t	 j |  d d t j   k s t	 j |  r t	 j |  n d d d	 t j   k s t	 j t  r t	 j t  n d	 d
 t	 j |  i } d d | i } t t	 j |    d  } } } d  S)Nr"   zf.write(totext('hello'))r#   0%(py0)s == %(py6)s
{%(py6)s = %(py2)s(%(py4)s)
}py6r   r%   r
   r   r   r   assert %(py8)spy8)r#   )r@   rB   )r)   r*   BytesIOr   r   r9   r:   r;   r,   r   r-   r   r   r   r   r   r   )r0   r%   r   @py_assert5r2   @py_format7@py_format9r   r   r   test_bytes_io8   s           rH   c              C   s#  d d l  m }  |    } | j } |   } | } | s d d d d t j   k sc t j |  rr t j |  n d d t j |  d t j |  i } t t j	 |    d  } } } t
 j j t | j  t
 j j t | j  t
 j j t t |  t
 j j t | j  | j   d  S)	Nr   )DontReadFromInputr   zEassert not %(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.isatty
}()
}r   r0   r
   r   )Zpy._io.capturerI   isattyr   r   r   r   r   r   r   r)   r9   r:   IOErrorread	readlinesiter
ValueErrorfilenor.   )rI   r0   r2   r   rE   r4   r   r   r   test_dontreadfrominput?   s    		 	   b  rQ   c             C   s;   |  j  d  } | j d  j d  } |  j | j  | S)Ntestdirr   zwb+)getfuncargvalue
makepyfileopenaddfinalizerr.   )requestrR   r0   r   r   r   pytest_funcarg__tmpfileI   s    rX   c             C   s  g  } x)t  d  D]} t j j |  d d } | |  k } | s t j d! | f d" | |  f  d d t j   k s t j |  r t j	 |  n d d d	 t j   k s t j |   r t j	 |   n d	 i } d# d | i } t
 t j |    d  } | j } |   } |  j } |   }	 | |	 k }
 |
 s't j d$ |
 f d% | |	 f  d d	 t j   k swt j |   rt j	 |   n d	 d d t j   k st j |  rt j	 |  n d d t j	 |  d t j	 |  d t j	 |	  d t j	 |  i } d& d | i } t
 t j |    d  } } }
 } }	 | | k } | st j d' | f d( | | f  d d t j   k st j |  rt j	 |  n d d d t j   k st j |  rt j	 |  n d i } d) d | i } t
 t j |    d  } t | d d
 d | | j |  q Wx( t  d  D] } | | } | j   q?W|  j d  |  j   } d } t |  }
 | |
 k } | snt j d* | f d+ | |
 f  d t j	 |
  d t j	 |  d d t j   k st j t  r	t j	 t  n d d d t j   k s0t j |  r?t j	 |  n d i } d, d | i } t
 t j |    d  } } }
 |  j   d  S)-N   encodingzutf-8!=%(py0)s != %(py2)sr   nfr
   tmpfiler   assert %(py4)sr   z%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.fileno
}()
} != %(py10)s
{%(py10)s = %(py8)s
{%(py8)s = %(py6)s.fileno
}()
}rA   py10rC   assert %(py12)spy12not in%(py0)s not in %(py2)sflistendfiler   Z01234in0%(py1)s in %(py6)s
{%(py6)s = %(py3)s(%(py4)s)
}r   r&   reprr%   assert %(py8)s)r[   )r\   r_   )r[   )r`   rb   )rd   )re   r_   )ri   )rj   rl   )ranger)   r*   dupfiler   r-   r   r   r   r   r   r   rP   r   appendr.   seekrL   rk   )r^   rf   ir]   r2   @py_format3r   r   @py_assert7@py_assert9rE   @py_format11@py_format13r0   r%   @py_assert0r1   rF   rG   r   r   r   test_dupfileO   sR         	 	 	 	           
       rx   c              C   s  Gd d   d t   }  |    } t j } | j } | |  } | | k } | sht j d | f d | | f  d t j |  d d t j   k s t j	 t  r t j t  n d d d	 t j   k s t j	 |  r t j |  n d	 d
 t j |  d t j |  d d	 t j   k s*t j	 |  r9t j |  n d	 i } d d | i } t
 t j |    d } } } } t j j t   t j j | d d Wd QRXd S)zT
    dupfile should trap an AttributeError and return f if no mode is supplied.
    c               @   s"   e  Z d  Z d Z d d   Z d S)z-test_dupfile_no_mode.<locals>.SomeFileWrapperz4An object with a fileno method but no mode attributec             S   s   d S)N   r   )r/   r   r   r   rP   g   s    z4test_dupfile_no_mode.<locals>.SomeFileWrapper.filenoN)r=   r>   r?   __doc__rP   r   r   r   r   SomeFileWrappere   s   r{   ise%(py7)s
{%(py7)s = %(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.io
}.dupfile
}(%(py5)s)
} is %(py9)spy7r   r)   r(   r^   r
   r   py9r   assert %(py11)spy11NraisingT)r|   )r}   r   )objectr)   r*   rn   r   r-   r   r   r   r   r   r   r9   r:   AttributeError)r{   r^   r2   r   @py_assert6@py_assert8@py_format10@py_format12r   r   r   test_dupfile_no_modea   s    		 	       r   c             C   s  t  j   } y t j j d |  } Wn+ t j j j k
 rS t j j d  Yn X|    t j j d |  } t d d   | j	 d  D  } t d d   | j	 d  D  } d } | | } | | k  } | st
 j d | f d | | f  d
 d t j   k st
 j |  r(t
 j |  n d d d t j   k sOt
 j |  r^t
 j |  n d d t
 j |  i }	 t
 j |  d d |	 i }
 t t
 j |
    d  } } } d  S)Nz
lsof -p %dzcould not run 'lsof'c             S   s"   g  |  ] } d  | k r |  q S)REGr   ).0xr   r   r   
<listcomp>v   s   	 zlsof_check.<locals>.<listcomp>
c             S   s"   g  |  ] } d  | k r |  q S)r   r   )r   r   r   r   r   r   w   s   	 r   <%(py0)s < (%(py2)s + %(py4)s)r   len2r
   len1r   z
>assert %(py7)sr~   )r   )r   )r   getpidr)   processcmdexecErrorr9   skiplensplitr   r-   r   r   r   r   _format_assertmsgr   r   )funcpidoutout2r   r   r   rE   r2   r4   @py_format8r   r   r   
lsof_checkn   s"    "" 
      r   c               @   sv   e  Z d  Z e 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)TestFDCapturec             C   s  | j    } t j j | d d } t d  } t j | |  | j   } | j   } | } | s d d d t	 j
   k s t j |  r t j |  n d i } t t j |    d  } t j j | d d } | j   t j | |  | j   } | j   } d }	 | |	 k } | st j d | f d | |	 f  d d t	 j
   k sdt j |  rst j |  n d d
 t j |	  i }
 d d |
 i } t t j |    d  } }	 d  S)NnowFr"   r   assert not %(py0)sr   r%   r#   %(py0)s == %(py3)sr&   assert %(py5)sr(   r   )r#   )r   r   )rP   r)   r*   	FDCapturer   r   r   donerL   r   r   r   r   r   r   r   startr-   )r/   r^   r   capdatar0   r%   r2   @py_format2r1   r3   r4   r   r   r   test_not_now}   s.      @  
   j   zTestFDCapture.test_not_nowc             C   s  | j    } t j j |  } t d  } t j | |  | j   } | j   } d } | | k } | s t	 j
 d
 | f d | | f  d d t j   k s t	 j |  r t	 j |  n d d t	 j |  i }	 d d	 |	 i }
 t t	 j |
    d  } } | j   d  S)Nr"   r#   %(py0)s == %(py3)sr   r%   r&   r   assert %(py5)sr(   )r#   )r   r   )rP   r)   r*   r   r   r   r   r   rL   r   r-   r   r   r   r   r   r   r.   )r/   r^   r   r   r   r0   r%   r1   r2   r3   r4   r   r   r   test_simple   s       j   
zTestFDCapture.test_simplec             C   s(   x! t  d  D] } |  j |  q Wd  S)N
   )rm   r   )r/   r^   rq   r   r   r   test_simple_many   s    zTestFDCapture.test_simple_manyc                s   t     f d d    d  S)Nc                  s     j    S)N)r   r   )r/   r^   r   r   <lambda>   s    zATestFDCapture.test_simple_many_check_open_files.<locals>.<lambda>)r   )r/   r^   r   )r/   r^   r   !test_simple_many_check_open_files   s    z/TestFDCapture.test_simple_many_check_open_filesc             C   sN   | j    } t j j |  } | j   } t j j t | j  | j	   d  S)N)
rP   r)   r*   r   r   r9   r:   rO   r   r.   )r/   r^   r   r   r0   r   r   r   test_simple_fail_second_start   s
    z+TestFDCapture.test_simple_fail_second_startc             C   s   t  j j d d d } t d d t j | j   } | j   } d } | | k } | s t j	 d | f d | | f  d	 d
 t
 j   k s t j |  r t j |  n d
 d t j |  i } d d | i } t t j |    d  } } d  S)N   patchsysTr"   rh   zhello
r#   %(py0)s == %(py3)sr   r%   r&   r   assert %(py5)sr(   )r#   )r   r   )r)   r*   r   r   r7   stderrr   rL   r   r-   r   r   r   r   r   r   )r/   r   r0   r%   r1   r2   r3   r4   r   r   r   test_stderr   s       j   zTestFDCapture.test_stderrc       
   
   C   sf  | j  t d   | j d  t j j d d | } t j d d  j   } | j	   } d } t |  } | | k } | sTt
 j d | f d | | f  d t
 j |  d d	 t j   k s t
 j |  r t
 j |  n d	 d
 d t j   k st
 j t  rt
 j t  n d d t
 j |  i } d d | i }	 t t
 j |	    d  } } } d  S)N3r   r^   d   r#   0%(py0)s == %(py6)s
{%(py6)s = %(py2)s(%(py4)s)
}rA   r   r   r
   r   r   r   assert %(py8)srC   )r#   )r   r   )r   r   rp   r)   r*   r   r   rL   stripr   r   r-   r   r   r   r   r   r   )
r/   r^   r   r   r0   r   rE   r2   rF   rG   r   r   r   
test_stdin   s           zTestFDCapture.test_stdinc             C   s  t  d  t  d  } } z6 t j j | j    } | j |  | j |  Wd  | j   X| j   } | j	   } t
 |  } | | k } | st j d | f d | | f  d t j |  d d t j   k s t j |  r t j |  n d d d	 t j   k st j |  r)t j |  n d	 d
 d t j   k sPt j t
  r_t j t
  n d i }	 d d |	 i }
 t t j |
    d  } } t | j d  j	   } | | k } | sxt j d | f d | | f  d d t j   k st j |  rt j |  n d d
 d t j   k s:t j |  rIt j |  n d i } d d | i } t t j |    d  } d  S)Nfoobarr#   0%(py0)s == %(py5)s
{%(py5)s = %(py2)s(%(py3)s)
}r(   r   scapr&   data1r
   r   r   assert %(py7)sr~   rb%(py0)s == %(py2)sstmpdata2assert %(py4)sr   )r#   )r   r   )r#   )r   r   )r   r)   r*   r   rP   r   writeorgr.   r   rL   r   r   r-   r   r   r   r   r   r   rU   name)r/   r^   r   r   r   r0   r   @py_assert4r2   r4   r   r   rr   r   r   r   r   test_writeorg   s,          
     zTestFDCapture.test_writeorgN)r=   r>   r?   needsdup
pytestmarkr   r   r   r   r   r   r   r   r   r   r   r   r   z   s   

r   c               @   s   e  Z d  Z d d   Z d d   Z d d   Z d d   Z d	 d
   Z e j	 j
 j d 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 d d   Z d  d!   Z d" S)#TestStdCapturec             K   s   t  j j |   S)N)r)   r*   
StdCapture)r/   kwr   r   r   
getcapture   s    zTestStdCapture.getcapturec       	      C   s  |  j    } t j j d  t j j d  | j   \ } } | j   } d } | | k } | s t j d | f d | | f  d d t	 j
   k s t j |  r t j |  n d d t j |  i } d d
 | i } t t j |    d  } } | j   } d } | | k } | st j d | f d | | f  d d t	 j
   k sat j |  rpt j |  n d d t j |  i } d d
 | i } t t j |    d  } } d  S)Nr"   worldr#   %(py0)s == %(py3)sr   r%   r&   r   assert %(py5)sr(   )r#   )r   r   )r#   )r   r   )r   r7   stdoutr   r   r   rL   r   r-   r   r   r   r   r   r   )	r/   r   outfileerrfiler%   r1   r2   r3   r4   r   r   r   test_capturing_done_simple   s(       j   
   j   z)TestStdCapture.test_capturing_done_simplec             C   s  |  j    } t d  t j j d  | j   \ } } d } | | k } | s t j d | f d | | f  d d t j	   k s t j
 |  r t j |  n d d t j |  i } d d | i } t t j |    d  } } d } | | k } | st j d | f d | | f  d d t j	   k sCt j
 |  rRt j |  n d d t j |  i } d d | i } t t j |    d  } } d  S)Nzhello worldzhello error
zhello world
r#   %(py0)s == %(py3)sr   r   r&   r   assert %(py5)sr(   err)r#   )r   r   )r#   )r   r   )r   printr7   r   r   resetr   r-   r   r   r   r   r   r   )r/   r   r   r   r1   r2   r3   r4   r   r   r   test_capturing_reset_simple   s$    
   j   
   j   z*TestStdCapture.test_capturing_reset_simplec             C   sy  |  j    } zt d  t j j d  | j   \ } } d } | | k } | s t j d | f d | | f  d d t j	   k s t j
 |  r t j |  n d d t j |  i } d d | i } t t j |    d  } } d } | | k } | st j d | f d | | f  d d t j	   k sFt j
 |  rUt j |  n d d t j |  i } d d | i } t t j |    d  } } t j j d  Wd  | j   \ } } Xd } | | k } | skt j d | f d | | f  d d t j	   k st j
 |  r-t j |  n d d t j |  i } d d | i } t t j |    d  } } d  S)Nzhello worldzhello error
zhello world
r#   %(py0)s == %(py3)sr   r   r&   r   assert %(py5)sr(   r   Zerror2)r#   )r   r   )r#   )r   r   )r#   )r   r   )r   r   r7   r   r   
readouterrr   r-   r   r   r   r   r   r   r   )r/   r   r   r   r1   r2   r3   r4   r   r   r   test_capturing_readouterr   s8    
   j   
   j   
   j   z(TestStdCapture.test_capturing_readouterrc             C   s  |  j    } t d  | j   \ } } t j } | j } d } d } | | |  } | | k }	 |	 sht j d |	 f d | | f  d t j |  d d t	 j
   k s t j |  r t j |  n d d	 d
 t	 j
   k s t j t  r t j t  n d
 d t j |  d t j |  d t j |  d t j |  i }
 d d |
 i } t t j |    d  }	 } } } } } d  S)Nu
   hxÄÄu   hxÄÄ
utf8r#   v%(py0)s == %(py12)s
{%(py12)s = %(py6)s
{%(py6)s = %(py4)s
{%(py4)s = %(py2)s.builtin
}._totext
}(%(py8)s, %(py10)s)
}rA   r   r   r
   r)   r   rc   ra   rC   r   assert %(py14)spy14)r#   )r   r   )r   r   r   r)   builtin_totextr   r-   r   r   r   r   r   r   )r/   r   r   r   r   rE   rs   rt   @py_assert11r2   rv   @py_format15r   r   r   !test_capturing_readouterr_unicode   s    
	 	         z0TestStdCapture.test_capturing_readouterr_unicodezsys.version_info >= (3,)reasonz*text output different for bytes on python3c             C   s  |  j    } t d  | j   \ } } t j } | j } d } d } | | |  } | | k }	 |	 sht j d |	 f d | | f  d t j |  d d t	 j
   k s t j |  r t j |  n d d	 d
 t	 j
   k s t j t  r t j t  n d
 d t j |  d t j |  d t j |  d t j |  i }
 d d |
 i } t t j |    d  }	 } } } } } d  S)N   ¦u   �
zunicode-escaper#   v%(py0)s == %(py12)s
{%(py12)s = %(py6)s
{%(py6)s = %(py4)s
{%(py4)s = %(py2)s.builtin
}._totext
}(%(py8)s, %(py10)s)
}rA   r   r   r
   r)   r   rc   ra   rC   r   assert %(py14)sr   )r#   )r   r   )r   r   r   r)   r   r   r   r-   r   r   r   r   r   r   )r/   r   r   r   r   rE   rs   rt   r   r2   rv   r   r   r   r   /test_capturing_readouterr_decode_error_handling   s    
	 	         z>TestStdCapture.test_capturing_readouterr_decode_error_handlingc          
   C   s  |  j  d d  } t j j d  t j j d  t j j d  | j   \ } } | j } |   } d } | | k } | s+t j d | f d | | f  d	 t j	 |  d
 d t
 j   k s t j |  r t j	 |  n d d t j	 |  d t j	 |  i } d d | i }	 t t j |	    d  } } } } | } | sd d
 d t
 j   k sqt j |  rt j	 |  n d i }
 t t j |
    d  } d  S)NmixedTzhello r   .zhello world.r#   D%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.strip
}()
} == %(py7)sr~   r   r   r
   r   r   assert %(py9)sr   assert not %(py0)sr   )r#   )r   r   r   )r   r7   r   r   r   r   r   r   r-   r   r   r   r   r   r   )r/   r   r   r   r2   r   r   rE   r   r   r   r   r   r   test_capturing_mixed   s&    	 	         @  z#TestStdCapture.test_capturing_mixedc       	      C   s[  |  j    } t d  | j   \ } } t j j t | j  d } | | k } | s t j d | f d | | f  d d t	 j
   k s t j |  r t j |  n d d t j |  i } d d
 | i } t t j |    d  } } | } | sQd d d t	 j
   k s#t j |  r2t j |  n d i } t t j |    d  } d  S)Nr"   zhello
r#   %(py0)s == %(py3)sr   r   r&   r   assert %(py5)sr(   assert not %(py0)sr   )r#   )r   r   r   )r   r   r   r)   r9   r:   rO   r   r-   r   r   r   r   r   r   )	r/   r   r   r   r1   r2   r3   r4   r   r   r   r   test_reset_twice_error   s     
   j   
  @  z%TestStdCapture.test_reset_twice_errorc             C   s  t  j } t  j } |  j   } t  j j d  t  j j d  t j j   t  _ t j j   t  _ t d  t  j j d  | j	   \ } } d } | | k } | s5t
 j d | f d | | f  d d t j   k s t
 j |  r t
 j |  n d d	 t
 j |  i } d d | i }	 t t
 j |	    d  } } d } | | k } | st
 j d | f d | | f  d d t j   k st
 j |  rt
 j |  n d d	 t
 j |  i } d d | i }	 t t
 j |	    d  } } t  j } | | k }
 |
 st
 j d |
 f d | | f  d d t j   k sMt
 j t   r\t
 j t   n d d t
 j |  d d t j   k st
 j |  rt
 j |  n d i } d d | i } t t
 j |    d  } }
 t  j } | | k }
 |
 st
 j d  |
 f d! | | f  d d t j   k s7t
 j t   rFt
 j t   n d d t
 j |  d d t j   k s|t
 j |  rt
 j |  n d i } d" d | i } t t
 j |    d  } }
 d  S)#Nr"   r   znot seenz	not seen
r#   %(py0)s == %(py3)sr   r   r&   r   assert %(py5)sr(   r   .%(py2)s
{%(py2)s = %(py0)s.stdout
} == %(py4)sr7   r
   r   oldoutassert %(py6)srA   .%(py2)s
{%(py2)s = %(py0)s.stderr
} == %(py4)solderr)r#   )r   r   )r#   )r   r   )r#   )r  r  )r#   )r  r  )r7   r   r   r   r   r)   r*   r+   r   r   r   r-   r   r   r   r   r   r   )r/   r  r  r   r   r   r1   r2   r3   r4   r   r   rF   r   r   r   *test_capturing_modify_sysouterr_in_between  sL    		
   j   
   j   
	      
	      z9TestStdCapture.test_capturing_modify_sysouterr_in_betweenc             C   s  |  j    } t d  |  j    } t d  | j   \ } } | j   \ } } d } | | k } | s t j d | f d | | f  d d t j   k s t j |  r t j |  n d d t j |  i }	 d d |	 i }
 t	 t j
 |
    d  } } d } | | k } | st j d | f d | | f  d d t j   k s[t j |  rjt j |  n d d t j |  i }	 d d |	 i }
 t	 t j
 |
    d  } } d  S)Ncap1cap2zcap1
r#   %(py0)s == %(py3)sr   out1r&   r   assert %(py5)sr(   zcap2
r   )r#   )r	  r  )r#   )r	  r  )r   r   r   r   r-   r   r   r   r   r   r   )r/   r  r  r   err2r
  err1r1   r2   r3   r4   r   r   r   test_capturing_error_recursive  s(    

   j   
   j   z-TestStdCapture.test_capturing_error_recursivec       	      C   sg  |  j  d d d d  } t j j d  t j j d  | j   \ } } d } | | k } | s t j d | f d | | f  d	 d t j	   k s t j
 |  r t j |  n d d
 t j |  i } d d | i } t t j |    d  } } | } | s]d d	 d t j	   k s/t j
 |  r>t j |  n d i } t t j |    d  } d  S)Nr   Tr   Fr"   r   r#   %(py0)s == %(py3)sr   r&   r   assert %(py5)sr(   assert not %(py0)s)r#   )r  r  r  )r   r7   r   r   r   r   r   r-   r   r   r   r   r   r   )	r/   r   r   r   r1   r2   r3   r4   r   r   r   r   test_just_out_capture!  s        j   
  @  z$TestStdCapture.test_just_out_capturec       	      C   sg  |  j  d d d d  } t j j d  t j j d  | j   \ } } d } | | k } | s t j d | f d | | f  d	 d t j	   k s t j
 |  r t j |  n d d
 t j |  i } d d | i } t t j |    d  } } | } | s]d d	 d t j	   k s/t j
 |  r>t j |  n d i } t t j |    d  } d  S)Nr   Fr   Tr"   r   r#   %(py0)s == %(py3)sr   r&   r   assert %(py5)sr(   assert not %(py0)s)r#   )r  r  r  )r   r7   r   r   r   r   r   r-   r   r   r   r   r   r   )	r/   r   r   r   r1   r2   r3   r4   r   r   r   r   test_just_err_capture)  s        j   
  @  z$TestStdCapture.test_just_err_capturec       
      C   s  t  j } |  j d d  } t  j } | j   \ } } t  j } | | k } | st j d | f d | | f  d d t j   k s t j |  r t j	 |  n d d d t j   k s t j t   r t j	 t   n d d	 t j	 |  i } d d | i }	 t
 t j |	    d  } } t  j } | | k } | s t j d | f d | | f  d d t j   k s}t j t   rt j	 t   n d d t j	 |  d	 d t j   k st j |  rt j	 |  n d i } d d | i }	 t
 t j |	    d  } } d  S)Nin_Tr[   -%(py0)s != %(py4)s
{%(py4)s = %(py2)s.stdin
}r   newstdinr
   r7   r   r   assert %(py6)srA   r|   -%(py2)s
{%(py2)s = %(py0)s.stdin
} is %(py4)sold)r[   )r  r  )r|   )r  r  )r7   stdinr   r   r   r-   r   r   r   r   r   r   )
r/   r  r   r  r   r   r   r2   r   rF   r   r   r   test_stdin_restored1  s$    			      
	      z"TestStdCapture.test_stdin_restoredc             C   sS   t  d  t  d  t  d  |  j   } t j j t d  | j   \ } } d  S)Nz/XXX this test may well hang instead of crashingz8XXX which indicates an error in the underlying capturingzXXX mechanismszsys.stdin.read())r   r   r)   r9   r:   rK   r   )r/   r   r   r   r   r   r   test_stdin_nulled_by_default9  s    


z+TestStdCapture.test_stdin_nulled_by_defaultc       	      C   s  |  j  d d d d d d  } zt d  t j j d  | j   \ } } d } | | k } | s t j d | f d | | f  d d t j	   k s t j
 |  r t j |  n d d t j |  i } d d | i } t t j |    d  } } | } | s`d d d t j	   k s2t j
 |  rAt j |  n d i } t t j |    d  } t d  t j j d  | j   t d  t j j d  Wd  | j   \ } } Xd } | | k } | sbt j d | f d | | f  d d t j	   k st j
 |  r$t j |  n d d t j |  i } d d | i } t t j |    d  } } | } | sd d d t j	   k st j
 |  rt j |  n d i } t t j |    d  } d  S)Nr   Tr   Fr  r"   zerror
zhello
r#   %(py0)s == %(py3)sr   r&   r   assert %(py5)sr(   assert not %(py0)sz
in betweenzin between
afterzerror_after
zafter
)r#   )r   r!  r"  )r#   )r   r!  r"  )r   r   r7   r   r   suspendr   r-   r   r   r   r   r   r   resumer   )	r/   r   r   r   r1   r2   r3   r4   r   r   r   r   test_suspend_resumeA  sF    
   j   
  @  


   j   
  @  z"TestStdCapture.test_suspend_resumeN)r=   r>   r?   r   r   r   r   r   r)   r9   markskipifr   r   r   r  r  r  r  r  r  r&  r   r   r   r   r      s    
	
r   c               @   s   e  Z d  Z d d   Z d S)TestStdCaptureNotNowc             K   s*   d | d <t  j j |   } | j   | S)NFr   )r)   r*   r   startall)r/   r   r   r   r   r   r   T  s    

zTestStdCaptureNotNow.getcaptureN)r=   r>   r?   r   r   r   r   r   r)  S  s   r)  c               @   sF   e  Z d  Z e Z d d   Z d d   Z d d   Z d d   Z d	 S)
TestStdCaptureFDc             K   s   t  j j |   S)N)r)   r*   StdCaptureFD)r/   r   r   r   r   r   ]  s    zTestStdCaptureFD.getcapturec             C   s  |  j    } t d d  t j j t d   t j j   t d d  t d d  t j j d  t j j   t d d  | j   \ } } d } | | k } | s9t	 j
 d | f d | | f  d d t j   k s t	 j |  r t	 j |  n d d t	 j |  i } d d | i } t t	 j |    d  } } d } | | k } | st	 j
 d | f d | | f  d d t j   k st	 j |  rt	 j |  n d d t	 j |  i } d d | i } t t	 j |    d  } } d  S)Nry   1r   r   abc123r#   %(py0)s == %(py3)sr   r   r&   r   assert %(py5)sr(   abcr   )r#   )r2  r3  )r#   )r2  r3  )r   r    r7   r   r   r   flushr   r   r   r-   r   r   r   r   r   r   )r/   r   r   r   r1   r2   r3   r4   r   r   r   test_intermingling`  s0       j   
   j   z#TestStdCaptureFD.test_interminglingc          
   C   sR  d d   } t  j j j | d d d \ } } } d } | | k } | s t j d | f d | | f  d	 d
 t j   k s t j |  r t j	 |  n d
 d t j	 |  i } d d | i } t
 t j |    d  } } | j } d }	 | |	  }
 |
 sd d d t j	 |
  d	 d t j   k s?t j |  rNt j	 |  n d d t j	 |  d t j	 |	  i } t
 t j |    d  } }	 }
 | j } d }	 | |	  }
 |
 s@d d d t j	 |
  d	 d t j   k st j |  rt j	 |  n d d t j	 |  d t j	 |	  i } t
 t j |    d  } }	 }
 d  S)Nc             S   s*   t  |   t j j j j t |   d S)N*   )r   r)   stdr7   r   r   r   )r   yr   r   r   r   o  s    
z/TestStdCaptureFD.test_callcapture.<locals>.funcr   r9     r7  r#   %(py0)s == %(py3)sr   resr&   r   assert %(py5)sr(   r   zLassert %(py6)s
{%(py6)s = %(py2)s
{%(py2)s = %(py0)s.startswith
}(%(py4)s)
}rA   r   r
   r   4r   )r#   )r;  r=  )r)   r*   r,  callr   r-   r   r   r   r   r   r   
startswith)r/   r   r<  r   r   r1   r2   r3   r4   r   rE   rF   r   r   r   test_callcapturen  s.    '   j   
	    q  	    q  z!TestStdCaptureFD.test_callcapturec             C   s   d d   } t  |  d  S)Nc              S   s4   x- t  d  D] }  t j j   } | j   q Wd  S)Nr   )rm   r)   r*   r,  r   )rq   r   r   r   r   r0   z  s    z%TestStdCaptureFD.test_many.<locals>.f)r   )r/   capfdr0   r   r   r   	test_manyy  s    zTestStdCaptureFD.test_manyN)	r=   r>   r?   r   r   r   r6  rA  rC  r   r   r   r   r+  Z  s
   r+  c               @   s"   e  Z d  Z e Z d d   Z d S)TestStdCaptureFDNotNowc             K   s*   d | d <t  j j |   } | j   | S)NFr   )r)   r*   r,  r*  )r/   r   r   r   r   r   r     s    

z!TestStdCaptureFDNotNow.getcaptureN)r=   r>   r?   r   r   r   r   r   r   r   rD    s   rD  c             C   s+  t  j j d |   } t j d d j d   t j d d j d   | j   \ } } | |  k } | s!t j d | f d | |  f  d	 d
 t	 j
   k s t j |  r t j |  n d
 d d t	 j
   k s t j |   r t j |   n d i } d d | i } t t j |    d  } d  S)Nr   ry   r"   asciir   r   r#   %(py0)s == %(py2)sr   outfr
   r^   r   assert %(py4)sr   )r#   )rF  rH  )r)   r*   r,  r   r   r   r   r   r-   r   r   r   r   r   r   )r^   rB  rG  Zerrfr2   rr   r   r   r   r   test_stdcapture_fd_tmpfile  s         rI  c               @   s"   e  Z d  Z e Z d d   Z d S)TestStdCaptureFDinvalidFDc             C   s  | j  d  | j d  } | j } d } | | k } | s t j d | f d | | f  d d t j   k s t j |  r t j |  n d d t j |  d	 t j |  i } d d | i } t	 t j
 |    d  } } } | j   d } d } | | k }	 |	 syt j d |	 f d | | f  d t j |  d t j |  i }
 d d |
 i } t	 t j
 |    d  } }	 } d  S)Na  
            import py, os
            def test_stdout():
                os.close(1)
                cap = py.io.StdCaptureFD(out=True, err=False, in_=False)
                cap.done()
            def test_stderr():
                os.close(2)
                cap = py.io.StdCaptureFD(out=False, err=True, in_=False)
                cap.done()
            def test_stdin():
                os.close(0)
                cap = py.io.StdCaptureFD(out=False, err=False, in_=True)
                cap.done()
        z--capture=fdr   r#   +%(py2)s
{%(py2)s = %(py0)s.ret
} == %(py5)sr   resultr(   r
   r   assert %(py7)sr~   passedr   %(py1)s == %(py4)sr   r   assert %(py6)srA   )r#   )rK  rM  )r#   )rO  rP  )rT   	runpytestretr   r-   r   r   r   r   r   r   parseoutcomes)r/   rR   rL  r2   r   r   r4   r   rw   r1   r   rF   r   r   r   test_stdcapture_fd_invalid_fd  s&    	    y       C   z7TestStdCaptureFDinvalidFD.test_stdcapture_fd_invalid_fdN)r=   r>   r?   r   r   rT  r   r   r   r   rJ    s   rJ  c              C   s7   t  j j d d  }  |  j   |  j   |  j   d  S)Nr   F)r)   r*   r   r   r   )capsysr   r   r   "test_capture_not_started_but_reset  s    

rV  c              C   s  t  j j   }  zt  j j d d  } t j j d  t j j d  t d d  t d d  | j	   \ } } d } | | k } | st
 j d | f d | | f  d d t j   k s t
 j |  r t
 j |  n d d t
 j |  i } d d | i } t t
 j |    d  } } d } | | k } | st
 j d | f d | | f  d d t j   k s~t
 j |  rt
 j |  n d d t
 j |  i } d d | i } t t
 j |    d  } } Wd  |  j	   Xd  S)Nr   Fr"   r   ry   r-  r   2r#   %(py0)s == %(py3)sr   r   r&   r   assert %(py5)sr(   r   )r#   )rX  rY  )r#   )rX  rY  )r)   r*   r   r,  r7   r   r   r   r    r   r   r-   r   r   r   r   r   r   )rU  r   r   r   r1   r2   r3   r4   r   r   r   test_capture_no_sys  s.       j   
   j   rZ  c              C   sy  d d   }  t  j j d d  } z+ t  j j j |  d d d \ } } } Wd  | j   Xd } | | k } | st j d | f d | | f  d d t j	   k s t j
 |  r t j |  n d d t j |  i } d d | i } t t j |    d  } } | j } d }	 | |	  }
 |
 sd d d t j |
  d d t j	   k sft j
 |  rut j |  n d d t j |  d t j |	  i } t t j |    d  } }	 }
 | j } d }	 | |	  }
 |
 sgd d d t j |
  d d t j	   k st j
 |  r*t j |  n d d t j |  d t j |	  i } t t j |    d  } }	 }
 d  S)Nc             S   s>   t  d d  t  d d  t |   t j j t |   d S)Nry   r"   r   r7  )r    r   r7   r   r   r   )r   r9  r   r   r   r     s
    
z#test_callcapture_nofd.<locals>.funcr   Fr   r9  r:  r7  r#   %(py0)s == %(py3)sr   r<  r&   r   assert %(py5)sr(   r   zLassert %(py6)s
{%(py6)s = %(py2)s
{%(py2)s = %(py0)s.startswith
}(%(py4)s)
}rA   r   r
   r   r>  r   )r#   )r[  r\  )r)   r*   r,  r   r?  r   r   r-   r   r   r   r   r   r   r@  )r   rB  r<  r   r   r1   r2   r3   r4   r   rE   rF   r   r   r   test_callcapture_nofd  s4    +   j   
	    q  	    q  r]  useTFc             C   s5  | s d }  t  j j d d d |  d d  } | j   | j j } | j   | j   | j j } | | k } | s+t j	 d | f d | | f  d d	 t
 j   k s t j |  r t j |  n d	 d
 d t
 j   k s t j |  r t j |  n d i } d d | i } t t j |    d  } d  S)NTr   Fr   r   r#   %(py0)s == %(py2)sr   capfile2r
   capfiler   assert %(py4)sr   )r#   )r_  rb  )r)   r*   r,  r*  r   r^   r$  r%  r   r-   r   r   r   r   r   r   )r^   r^  r   ra  r`  r2   rr   r   r   r   r   'test_fdcapture_tmpfile_remains_the_same  s    !


     rc  methodr   r,  c             C   sp  | d k r, t  t d  r, t j j d  |  j d | f  } |  j |  } | j j d d g  d } | j	 } | j
 } |   } | | k } | sVt j d | f d | | f  d
 t j |  d t j |  d t j |  d d t j   k s	t j |  rt j |  n d d t j |  i }	 d d |	 i }
 t t j |
    d  } } } } } d  S)Nr,  dupzneed os.dupa  
        import sys, os
        import py, logging
        cap = py.io.%s(out=False, in_=False)

        logging.warn("hello1")
        outerr = cap.suspend()
        print ("suspend, captured %%s" %%(outerr,))
        logging.warn("hello2")

        cap.resume()
        logging.warn("hello3")

        outerr = cap.suspend()
        print ("suspend2, captured %%s" %% (outerr,))
    zsuspend, captured*hello1*z.suspend2, captured*hello2*WARNING:root:hello3*atexitrd   b%(py1)s not in %(py9)s
{%(py9)s = %(py7)s
{%(py7)s = %(py5)s
{%(py5)s = %(py3)s.stderr
}.str
}()
}r~   r(   r   r&   rL  r   r   assert %(py11)sr   )rd   )rg  rh  )hasattrr   r)   r9   r   rT   	runpythonr   fnmatch_linesr   r   r   r-   r   r   r   r   r   r   )rR   rd  prL  rw   r   r   r   r1   r   r   r   r   r   'test_capturing_and_logging_fundamentals  s$    	
 	 	 	      rm  )r   r   ))
__future__r   builtinsr   _pytest.assertion.rewrite	assertionrewriter   r   r7   r)   r9   r'  r(  r   Z
py.builtinr   r8   r   r   r    r!   rH   rQ   rX   rx   r   r   r   r   r)  r+  rD  rI  rJ  rV  rZ  r]  multirc  rm  r   r   r   r   <module>   s@     
J&	-