B
    u9a-•  ã               @   sP  d 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yddl	Z	W n e
k
rh   dZ	Y nX yddlZW n e
k
rŽ   dZY nX ddlmZmZ ddlmZmZ ddlmZ ddlmZmZmZ ddlmZ ddlmZ dd	lmZ G d
d„ dƒZG dd„ dƒZG dd„ deejƒZG dd„ dƒZ G dd„ deejƒZ!G dd„ dƒZ"G dd„ dejƒZ#G dd„ de#ƒZ$G dd„ de#ƒZ%G dd„ de#ƒZ&G dd„ de#ƒZ'G d d!„ d!e#ƒZ(G d"d#„ d#e#ƒZ)G d$d%„ d%e#ƒZ*G d&d'„ d'e#ƒZ+G d(d)„ d)e#ƒZ,G d*d+„ d+ƒZ-G d,d-„ d-ejƒZ.G d.d/„ d/ejƒZ/G d0d1„ d1ejƒZ0e1d2krLe 2¡  dS )3z,
Tests for fileinput module.
Nick Mathewson
é    N)ÚBytesIOÚStringIO)Ú	FileInputÚhook_encoded)ÚPath)ÚverboseÚTESTFNÚcheck_warnings)Úunlink)Úsupport)Úmockc               @   s   e Zd Zddœdd„ZdS )Ú	BaseTestsÚw)Úmodec         	   C   s>   t  ¡ \}}|  tj|¡ t||ƒ}| |¡ W d Q R X |S )N)ÚtempfileZmkstempÚ
addCleanupr   r
   ÚopenÚwrite)ÚselfZcontentr   ÚfdÚnameÚf© r   ú$/usr/lib/python3.7/test_fileinput.pyÚwriteTmp(   s
    zBaseTests.writeTmpN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   %   s   r   c               @   sD   e Zd Zdd„ Zedd„ ƒZdd„ Zddd	„Zddd„Zdd„ Z	dS )Ú
LineReaderc             C   s
   g | _ d S )N)Ú
_linesread)r   r   r   r   Ú__init__1   s    zLineReader.__init__c             C   s   z| j d d … S g | _ X d S )N)r   )r   r   r   r   Ú	linesread4   s    zLineReader.linesreadc             C   s   t | d¡ƒ| _| S )NT)ÚiterÚ
splitlinesÚit)r   Úfilenamer   r   r   r   Úopenhook;   s    zLineReader.openhookNc             C   s   t | jdƒ}| j |¡ |S )NÚ )Únextr$   r   Úappend)r   ÚsizeÚliner   r   r   Úreadline?   s    zLineReader.readlineéÿÿÿÿc             C   sD   g }d}x6|   ¡ }|s|S | |¡ |t|ƒ7 }||kr
|S q
W d S )Nr   )r,   r)   Úlen)r   ZhintÚlinesr*   r+   r   r   r   Ú	readlinesD   s    
zLineReader.readlinesc             C   s   d S )Nr   )r   r   r   r   ÚcloseP   s    zLineReader.close)N)r-   )
r   r   r   r    Úpropertyr!   r&   r,   r0   r1   r   r   r   r   r   /   s   

r   c               @   s   e Zd Zdd„ Zddd„ZdS )ÚBufferSizesTestsc          
   C   sÒ   xÌdD ]Ä\}}|   d dd„ tdƒD ƒ¡¡}|   d dd„ tdƒD ƒ¡¡}|   d dd„ td	ƒD ƒ¡¡}|   d d
d„ tdƒD ƒ¡¡}|r¶|  t¡ |  ||||||¡ W d Q R X q|  ||||||¡ qW d S )N))r   r   )é   é   r'   c             s   s   | ]}d |d  V  qdS )zLine %s of file 1
r4   Nr   )Ú.0Úir   r   r   ú	<genexpr>W   s    z5BufferSizesTests.test_buffer_sizes.<locals>.<genexpr>é   c             s   s   | ]}d |d  V  qdS )zLine %s of file 2
r4   Nr   )r6   r7   r   r   r   r8   X   s    é
   c             s   s   | ]}d |d  V  qdS )zLine %s of file 3
r4   Nr   )r6   r7   r   r   r   r8   Y   s    é   c             s   s   | ]}d |d  V  qdS )zLine %s of file 4
r4   Nr   )r6   r7   r   r   r   r8   Z   s    r4   )r   ÚjoinÚrangeÚassertWarnsÚDeprecationWarningÚbuffer_size_test)r   ÚroundÚbsÚt1Út2Út3Út4r   r   r   Útest_buffer_sizesT   s     z"BufferSizesTests.test_buffer_sizesr   c             C   sr  t  d¡}d|d  }tr.td|d |f ƒ t||||f|d}	t|	ƒ}
|	 ¡  |  t|
ƒd¡ |  |
d d	¡ |  |
d
 d¡ |  |	 	¡ d¡ |  |	 
¡ |¡ trºtd|d |f ƒ t||||f|d}	d}x|rê|dkrê|	 ¡ }qÔW |  |	 
¡ |¡ |  |	 	¡ d¡ |  |	 ¡ d¡ |  |	 ¡ ¡ |  |	 ¡ ¡ trRtd|d |f ƒ |	 ¡  |  |	 ¡ d¡ |  |	 	¡ d¡ |	 ¡  trœtd|d |f ƒ t||||df|d}	tj}zNtdƒt_t|	ƒ}
|  t|
ƒd¡ |  |
d d¡ |  |	 
¡ d¡ |	 ¡  W d |t_X tr*td|d |f ƒ t||||f|d}	|  |	 	¡ d¡ |  |	 
¡ d ¡ |	 ¡  |  |	 	¡ d¡ |  |	 
¡ d ¡ tr td|d |f ƒ tj}zJt||||fd|d}	x&|	D ]}|d d …  ¡ }t|ƒ qÄW |	 ¡  W d |t_X t||||f|d}	xV|	D ]N}|  |d  d!¡ | |d d … ¡}|  |d ¡ |  t| d¡ƒ|	 ¡ ¡ qW |	 ¡  d S )"NzLINE (\d+) OF FILE (\d+)r4   é   z%s. Simple iteration (bs=%s)r   )ÚfilesÚbufsizeé   é   zLine 5 of file 1
r5   zLine 1 of file 4
z%s. Status variables (bs=%s)ÚxzLine 6 of file 2
é   z%s. Nextfile (bs=%s)é   zLine 1 of file 3
é   z%s. Stdin (bs=%s)é   ú-z Line 1 of stdin
Line 2 of stdin
é!   é    zLine 2 of stdin
z<stdin>z%s. Boundary conditions (bs=%s)z%s. Inplace (bs=%s)r;   )rI   ÚinplacerJ   r-   Ú
)ÚreÚcompiler   Úprintr   Úlistr1   ÚassertEqualr.   Úlinenor%   r,   Ú
filelinenoÚassertFalseÚisfirstlineÚisstdinÚnextfileÚsysÚstdinr   ÚstdoutÚupperÚmatchÚassertNotEqualÚintÚgroup)r   rC   rD   rE   rF   rB   rA   ZpatÚstartÚfir/   ÚsZ	savestdinZ
savestdoutr+   Úmr   r   r   r@   a   s„    



 z!BufferSizesTests.buffer_size_testN)r   r   )r   r   r   rG   r@   r   r   r   r   r3   S   s   r3   c               @   s   e Zd Zdd„ Zdd„ ZdS )ÚUnconditionallyRaisec             C   s   || _ d| _d S )NF)Úexception_typeÚinvoked)r   ro   r   r   r   r    ¯   s    zUnconditionallyRaise.__init__c             O   s   d| _ |  ¡ ‚d S )NT)rp   ro   )r   ÚargsÚkwargsr   r   r   Ú__call__²   s    zUnconditionallyRaise.__call__N)r   r   r   r    rs   r   r   r   r   rn   ®   s   rn   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e eed#ƒ d$¡d%d&„ ƒZd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1S )2ÚFileInputTestsc             C   sÌ   |   d¡}|   d¡}|   d¡}|   d¡}t||||fd}| ¡ }|  |d¡ |  | ¡ d¡ |  | ¡ d¡ |  | ¡ |¡ | ¡ }|  |¡ |  | ¡ d¡ |  | ¡ d¡ |  | ¡ |¡ | ¡  d S )Nr'   zThe only line there is.
)rI   r4   r   )	r   r   r,   r[   r\   r]   r%   r^   r1   )r   rC   rD   rE   rF   rk   r+   r   r   r   Útest_zero_byte_files¸   s     




z#FileInputTests.test_zero_byte_filesc          	   C   sf   |   d¡}|   d¡}t||fd}t|ƒ}|  |dddddd	g¡ |  | ¡ d
¡ |  | ¡ d¡ d S )NzA
B
CzD
E
F)rI   zA
zB
ÚCzD
zE
ÚFrQ   rH   )r   r   rZ   r[   r]   r\   )r   rC   rD   rk   r/   r   r   r   Ú%test_files_that_dont_end_with_newlineÌ   s    

z4FileInputTests.test_files_that_dont_end_with_newlinec             C   s~   |   d¡}|   d¡}t||fd}|  | ¡ d¡ t|ƒ}|  | ¡ d¡ | ¡  |  | ¡ d¡ t|ƒ}|  | ¡ d¡ d S )NzA
BzC
D)rI   r-   )r   r   r[   Úfilenor(   rg   ra   rZ   )r   rC   rD   rk   r+   r   r   r   Útest_filenoà   s    

zFileInputTests.test_filenoc          	   C   s˜   yt dd}|  d¡ W n tk
r,   Y nX | jddd}tdtfƒ t |dd}W d Q R X tdtfƒ t|ƒ}W d Q R X |  |d	d
ddg¡ d S )Nr   )r   z-FileInput should reject invalid mode arguments   A
B
CDÚwbr'   ÚU)rI   r   zA
zB
zC
ÚD)r   ÚfailÚ
ValueErrorr   r	   r?   rZ   r[   )r   rk   rC   r/   r   r   r   Útest_opening_modeì   s    
z FileInputTests.test_opening_modec          	   C   sH   t  d¡4}tdƒ|_tdgdd}t|ƒ}|  |dg¡ W d Q R X d S )Nz	sys.stdins   spam, bacon, sausage, and spamrR   Úrb)rI   r   )r   Zpatchr   Úbufferr   rZ   r[   )r   Zm_stdinrk   r/   r   r   r   Útest_stdin_binary_modeû   s
    
z%FileInputTests.test_stdin_binary_modec             C   sX   t j}zDtdƒt _|  tt jdƒ¡ tdgdd}t|ƒ}|  |dg¡ W d |t _X d S )Ns   spam, bacon, sausage, and spamr‚   rR   r   )rI   r   )rb   rc   r   r^   Úhasattrr   rZ   r[   )r   Z
orig_stdinrk   r/   r   r   r   Útest_detached_stdin_binary_mode  s    
z.FileInputTests.test_detached_stdin_binary_modec          	   C   s´   yt ddd„ d}|  d¡ W n tk
r2   Y nX yt dd}|  d¡ W n tk
r`   Y nX G dd	„ d	ƒ}|  d
¡}|ƒ }t |g|d}| ¡  W d Q R X |  |jd¡ d S )Nr4   c             S   s   d S )Nr   )r   rm   r   r   r   Ú<lambda>  ó    z7FileInputTests.test_file_opening_hook.<locals>.<lambda>)rU   r&   zGFileInput should raise if both inplace and openhook arguments are given)r&   z2FileInput should check openhook for being callablec               @   s   e Zd Zdd„ Zdd„ ZdS )z=FileInputTests.test_file_opening_hook.<locals>.CustomOpenHookc             S   s
   d| _ d S )NF)rp   )r   r   r   r   r      s    zFFileInputTests.test_file_opening_hook.<locals>.CustomOpenHook.__init__c             W   s   d| _ t|Ž S )NT)rp   r   )r   rq   r   r   r   rs     s    zFFileInputTests.test_file_opening_hook.<locals>.CustomOpenHook.__call__N)r   r   r   r    rs   r   r   r   r   ÚCustomOpenHook  s   rˆ   rV   zopenhook not invoked)r   r~   r   r   r,   Ú
assertTruerp   )r   rk   rˆ   ÚtZcustom_open_hookr   r   r   Útest_file_opening_hook  s     

z%FileInputTests.test_file_opening_hookc          
   C   sö   t tdƒ(}| d¡ | dd ¡ | d¡ W d Q R X |  tt¡ tttdƒd˜}y4|  | ¡ d¡ |  | ¡ d	¡ |  | ¡ d
¡ W n t	k
r¨   |  
d¡ Y nX |  t	¡ t|ƒ W d Q R X |  | ¡ d¡ |  | ¡ d¡ W d Q R X d S )Nr{   s   A
B
Cs
   123456789
iè  ó   €Úascii)rI   r&   zA
zB
zC
zRead to end of filer'   )r   r   r   r   Úsafe_unlinkr   r   r[   r,   ÚUnicodeDecodeErrorr~   ÚassertRaisesrZ   )r   r   rk   r   r   r   Útest_readline(  s"    
zFileInputTests.test_readlinec          	   C   s˜   t tdƒ}| d¡ W d Q R X |  tt¡ ttddV}|  | ¡ d¡ |  | ¡ d¡ |  | ¡ d¡ |  | ¡ d¡ |  | ¡ d¡ W d Q R X d S )	Nr{   s   A
B
CDr   )rI   r   s   A
s   B
s   CDr‡   )r   r   r   r   rŽ   r   r[   r,   )r   r   rk   r   r   r   Útest_readline_binary_mode?  s    z(FileInputTests.test_readline_binary_modec          	   C   s€   |   d¡}|   d¡}t||fd}t|ƒ}W d Q R X |  |dddddd	g¡ |  | ¡ d
¡ |  | ¡ d¡ |  |jd¡ d S )NzA
B
CzD
E
F)rI   zA
zB
rv   zD
zE
rw   rQ   rH   r   )r   r   rZ   r[   r]   r\   Ú_files)r   rC   rD   rk   r/   r   r   r   Útest_context_managerL  s    

z#FileInputTests.test_context_managerc          	   C   sP   |   d¡}yt|d
}t‚W d Q R X W n" tk
rJ   |  |jd¡ Y nX d S )Nr'   )rI   r   )r   r   ÚOSErrorr[   r“   )r   rC   rk   r   r   r   Útest_close_on_exceptionV  s    
z&FileInputTests.test_close_on_exceptionc          	   C   s(   t g d}|  |jd¡ W d Q R X d S )N)rI   )rR   )r   r[   r“   )r   rk   r   r   r   Ú.test_empty_files_list_specified_to_constructor^  s    z=FileInputTests.test_empty_files_list_specified_to_constructorc          	   C   sN   |   d¡}t|gd.}|d }|  |d¡ |d }|  |d¡ W dQ R X dS )zNTests invoking FileInput.__getitem__() with the current
           line numberzline1
line2
)rI   r   zline1
r4   zline2
N)r   r   r[   )r   rŠ   rk   Zretval1Zretval2r   r   r   Útest__getitem__b  s    
zFileInputTests.test__getitem__c          
   C   sT   |   d¡}t|gd$}|  t¡}|d  W dQ R X W dQ R X |  |jjd¡ dS )zZTests invoking FileInput.__getitem__() with an index unequal to
           the line numberzline1
line2
)rI   r4   N)zaccessing lines out of order)r   r   r   ÚRuntimeErrorr[   Ú	exceptionrq   )r   rŠ   rk   Úcmr   r   r   Útest__getitem__invalid_keyl  s
    
z)FileInputTests.test__getitem__invalid_keyc          
   C   sT   |   d¡}t|gd$}|  t¡}|d  W dQ R X W dQ R X |  |jjd¡ dS )zZTests invoking FileInput.__getitem__() with the line number but at
           end-of-inputr'   )rI   r   N)zend of input reached)r   r   r   Ú
IndexErrorr[   rš   rq   )r   rŠ   rk   r›   r   r   r   Útest__getitem__eofu  s
    
z!FileInputTests.test__getitem__eofc          	   C   sz   t j}ttƒ}zP|  d¡}|  tj|d ¡ t|gdd}t|ƒ |t _| 	¡  W dQ R X W d|t _X |  
|jd¡ dS )z¨Tests invoking FileInput.nextfile() when the attempt to delete
           the backup file would raise OSError.  This error is expected to be
           silently ignoredrV   z.bakT)rI   rU   Nzos.unlink() was not invoked)Úosr
   rn   r•   r   r   r   r   r(   ra   r‰   rp   )r   Zos_unlink_origZos_unlink_replacementrŠ   rk   r   r   r   Ú%test_nextfile_oserror_deleting_backup~  s    
z4FileInputTests.test_nextfile_oserror_deleting_backupc          	   C   s`   t j}ttƒ}z6|  d¡}t|gdd}|t _| ¡  W dQ R X W d|t _X |  |jd¡ dS )z{Tests invoking FileInput.readline() when os.fstat() raises OSError.
           This exception should be silently discarded.rV   T)rI   rU   Nzos.fstat() was not invoked)	rŸ   Úfstatrn   r•   r   r   r,   r‰   rp   )r   Zos_fstat_origZos_fstat_replacementrŠ   rk   r   r   r   Ú%test_readline_os_fstat_raises_OSError“  s    
z4FileInputTests.test_readline_os_fstat_raises_OSErrorÚchmodzos.chmod does not existc          	   C   s`   t j}ttƒ}z6|  d¡}t|gdd}|t _| ¡  W dQ R X W d|t _X |  |jd¡ dS )z{Tests invoking FileInput.readline() when os.chmod() raises OSError.
           This exception should be silently discarded.rV   T)rI   rU   Nzos.fstat() was not invoked)	rŸ   r£   rn   r•   r   r   r,   r‰   rp   )r   Zos_chmod_origZos_chmod_replacementrŠ   rk   r   r   r   Ú%test_readline_os_chmod_raises_OSError¥  s    
z4FileInputTests.test_readline_os_chmod_raises_OSErrorc          	   C   sz   G dd„ dt ƒ}|ƒ }|  d¡}t|gd(}|j}z||_| ¡ }W d ||_X W d Q R X |  |jd¡ |  |dd¡ d S )Nc               @   s   e Zd Zdd„ Zdd„ ZdS )zQFileInputTests.test_fileno_when_ValueError_raised.<locals>.FilenoRaisesValueErrorc             S   s   t  | t¡ d S )N)rn   r    r   )r   r   r   r   r    º  s    zZFileInputTests.test_fileno_when_ValueError_raised.<locals>.FilenoRaisesValueError.__init__c             S   s   |   ¡  d S )N)rs   )r   r   r   r   ry   ¼  s    zXFileInputTests.test_fileno_when_ValueError_raised.<locals>.FilenoRaisesValueError.filenoN)r   r   r   r    ry   r   r   r   r   ÚFilenoRaisesValueError¹  s   r¥   rV   )rI   z_file.fileno() was not invokedr-   zfileno() should return -1)rn   r   r   Ú_filery   r‰   rp   r[   )r   r¥   Z unconditionally_raise_ValueErrorrŠ   rk   Zfile_backupÚresultr   r   r   Ú"test_fileno_when_ValueError_raised¸  s    
z1FileInputTests.test_fileno_when_ValueError_raisedc          	   C   sÖ   t ƒ }tddg|jd´}|  |jg ¡ |  | ¡ d¡ |  |jdg¡ |  | ¡ d¡ |  |jdg¡ |  | ¡ d¡ |  |jddg¡ |  | ¡ d¡ |  |jdg¡ |  | ¡ d¡ |  |jg ¡ W d Q R X d S )Nzline1
line2zline3
)rI   r&   zline1
Úline2r'   )r   r   r&   r[   r!   r,   )r   Úsrcrk   r   r   r   Útest_readline_bufferingÏ  s    z&FileInputTests.test_readline_bufferingc          	   C   sÒ   t ƒ }tddg|jd°}|  |jg ¡ |  t|ƒd¡ |  |jdg¡ |  t|ƒd¡ |  |jdg¡ |  t|ƒd¡ |  |jddg¡ |  tt|¡ |  |jdg¡ |  tt|¡ |  |jg ¡ W d Q R X d S )Nzline1
line2zline3
)rI   r&   zline1
r©   r'   )r   r   r&   r[   r!   r(   r   ÚStopIteration)r   rª   rk   r   r   r   Útest_iteration_bufferingß  s    z'FileInputTests.test_iteration_bufferingc          	   C   sp   t |  d¡ƒ}t|ƒP}| ¡ }|  |d¡ |  | ¡ d¡ |  | ¡ d¡ |  | ¡ t 	|¡¡ W d Q R X d S )NzPathlib file.r4   )
r   r   r   r,   r[   r\   r]   r%   rŸ   Úfspath)r   rC   rk   r+   r   r   r   Útest_pathlib_fileï  s    
z FileInputTests.test_pathlib_filec          	   C   sn   t |  d¡ƒ}t|dd&}| ¡ }|  |d¡ td| ƒ W d Q R X t|ƒ}|  | ¡ d¡ W d Q R X d S )NzPathlib file.T)rU   zModified %szModified Pathlib file.
)r   r   r   r,   r[   rY   r   Úread)r   rC   rk   r+   r   r   r   r   Útest_pathlib_file_inplaceø  s    
z(FileInputTests.test_pathlib_file_inplaceN)r   r   r   ru   rx   rz   r€   rƒ   r…   r‹   r‘   r’   r”   r–   r—   r˜   rœ   rž   r    r¢   ÚunittestZskipIfr„   rŸ   r¤   r¨   r«   r­   r¯   r±   r   r   r   r   rt   ¶   s.   

			rt   c               @   sZ   e Zd ZdZ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S )ÚMockFileInputzDA class that mocks out fileinput.FileInput for use during unit testsNFr'   r   Úrc             C   sD   || _ || _|| _|| _|| _|| _d | _t dd„ ¡| _	i | _
d S )Nc               S   s   dS )Nr   r   r   r   r   r   r†     r‡   z(MockFileInput.__init__.<locals>.<lambda>)rI   rU   ÚbackuprJ   r   r&   r¦   ÚcollectionsÚdefaultdictÚinvocation_countsÚreturn_values)r   rI   rU   rµ   rJ   r   r&   r   r   r   r      s    zMockFileInput.__init__c             C   s   | j d  d7  < d S )Nr1   r4   )r¸   )r   r   r   r   r1     s    zMockFileInput.closec             C   s   | j d  d7  < | jd S )Nra   r4   )r¸   r¹   )r   r   r   r   ra     s    zMockFileInput.nextfilec             C   s   | j d  d7  < | jd S )Nr%   r4   )r¸   r¹   )r   r   r   r   r%     s    zMockFileInput.filenamec             C   s   | j d  d7  < | jd S )Nr\   r4   )r¸   r¹   )r   r   r   r   r\     s    zMockFileInput.linenoc             C   s   | j d  d7  < | jd S )Nr]   r4   )r¸   r¹   )r   r   r   r   r]      s    zMockFileInput.filelinenoc             C   s   | j d  d7  < | jd S )Nry   r4   )r¸   r¹   )r   r   r   r   ry   $  s    zMockFileInput.filenoc             C   s   | j d  d7  < | jd S )Nr_   r4   )r¸   r¹   )r   r   r   r   r_   (  s    zMockFileInput.isfirstlinec             C   s   | j d  d7  < | jd S )Nr`   r4   )r¸   r¹   )r   r   r   r   r`   ,  s    zMockFileInput.isstdin)NFr'   r   r´   N)r   r   r   Ú__doc__r    r1   ra   r%   r\   r]   ry   r_   r`   r   r   r   r   r³     s    
r³   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚBaseFileInputGlobalMethodsTestzQBase class for unit tests for the global function of
       the fileinput module.c             C   s   t j| _t j| _tt _d S )N)Ú	fileinputÚ_stateÚ_orig_stater   Ú_orig_FileInputr³   )r   r   r   r   ÚsetUp4  s    z$BaseFileInputGlobalMethodsTest.setUpc             C   s   | j t_| jt_d S )N)r¿   r¼   r   r¾   r½   )r   r   r   r   ÚtearDown9  s    z'BaseFileInputGlobalMethodsTest.tearDownc             C   s2   |j | }|  |d|¡ t|j ƒ}|  |d¡ d S )Nr4   )r¸   r[   r.   )r   Zmock_file_inputZmethod_nameZactual_countZactual_total_countr   r   r   ÚassertExactlyOneInvocation=  s    

z9BaseFileInputGlobalMethodsTest.assertExactlyOneInvocationN)r   r   r   rº   rÀ   rÁ   rÂ   r   r   r   r   r»   0  s   r»   c               @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚTest_fileinput_inputz Unit tests for fileinput.input()c          	   C   sV   t ƒ }tƒ |_|t_|  t¡}t ¡  W dQ R X |  d|j	j
¡ |  |tjd¡ dS )zþTests invoking fileinput.input() when fileinput._state is not None
           and its _file attribute is also not None.  Expect RuntimeError to
           be raised with a meaningful error message and for fileinput._state
           to *not* be modified.N)zinput() already activezfileinput._state)r³   Úobjectr¦   r¼   r½   r   r™   Úinputr[   rš   rq   ÚassertIs)r   Úinstancer›   r   r   r   Ú1test_state_is_not_None_and_state_file_is_not_NoneH  s    zFTest_fileinput_input.test_state_is_not_None_and_state_file_is_not_Nonec             C   s   t ƒ }d|_|t_|  ¡  dS )a\  Tests invoking fileinput.input() when fileinput._state is not None
           but its _file attribute *is* None.  Expect it to create and return
           a new fileinput.FileInput object with all method parameters passed
           explicitly to the __init__() method; also ensure that
           fileinput._state is set to the returned instance.N)r³   r¦   r¼   r½   Údo_test_call_input)r   rÇ   r   r   r   Ú-test_state_is_not_None_and_state_file_is_NoneU  s    zBTest_fileinput_input.test_state_is_not_None_and_state_file_is_Nonec             C   s   dt _|  ¡  dS )a4  Tests invoking fileinput.input() when fileinput._state is None
           Expect it to create and return a new fileinput.FileInput object
           with all method parameters passed explicitly to the __init__()
           method; also ensure that fileinput._state is set to the returned
           instance.N)r¼   r½   rÉ   )r   r   r   r   Útest_state_is_None`  s    z'Test_fileinput_input.test_state_is_Nonec             C   s®   t ƒ }t ƒ }t ƒ }t ƒ }t ƒ }t ƒ }tj||||||d}|  |tjd¡ |  ||jd¡ |  ||jd¡ |  ||jd¡ |  ||jd¡ |  ||j	d¡ |  ||j
d¡ d	S )
a  Tests that fileinput.input() creates a new fileinput.FileInput
           object, passing the given parameters unmodified to
           fileinput.FileInput.__init__().  Note that this test depends on the
           monkey patching of fileinput.FileInput done by setUp().)rI   rU   rµ   rJ   r   r&   zfileinput._staterI   rU   rµ   rJ   r   r&   N)rÄ   r¼   rÅ   rÆ   r½   rI   rU   rµ   rJ   r   r&   )r   rI   rU   rµ   rJ   r   r&   r§   r   r   r   rÉ   i  s     

z'Test_fileinput_input.do_test_call_inputN)r   r   r   rº   rÈ   rÊ   rË   rÉ   r   r   r   r   rÃ   E  s
   	rÃ   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚTest_fileinput_closez Unit tests for fileinput.close()c             C   s   dt _t  ¡  |  t j¡ dS )zPTests that fileinput.close() does nothing if fileinput._state
           is NoneN)r¼   r½   r1   ÚassertIsNone)r   r   r   r   rË   ‰  s    z'Test_fileinput_close.test_state_is_Nonec             C   s0   t ƒ }|t_t ¡  |  |d¡ |  tj¡ dS )z`Tests that fileinput.close() invokes close() on fileinput._state
           and sets _state=Noner1   N)r³   r¼   r½   r1   rÂ   rÍ   )r   rÇ   r   r   r   Útest_state_is_not_None  s
    z+Test_fileinput_close.test_state_is_not_NoneN)r   r   r   rº   rË   rÎ   r   r   r   r   rÌ   †  s   rÌ   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚTest_fileinput_nextfilez#Unit tests for fileinput.nextfile()c          	   C   sD   dt _|  t¡}t  ¡  W dQ R X |  d|jj¡ |  t j¡ dS )z·Tests fileinput.nextfile() when fileinput._state is None.
           Ensure that it raises RuntimeError with a meaningful error message
           and does not modify fileinput._stateN)zno active input())	r¼   r½   r   r™   ra   r[   rš   rq   rÍ   )r   r›   r   r   r   rË   œ  s
    z*Test_fileinput_nextfile.test_state_is_Nonec             C   sN   t ƒ }tƒ }||jd< |t_t ¡ }|  |d¡ |  ||¡ |  tj|¡ dS )a  Tests fileinput.nextfile() when fileinput._state is not None.
           Ensure that it invokes fileinput._state.nextfile() exactly once,
           returns whatever it returns, and does not modify fileinput._state
           to point to a different object.ra   N)rÄ   r³   r¹   r¼   r½   ra   rÂ   rÆ   )r   Znextfile_retvalrÇ   Úretvalr   r   r   rÎ   ¦  s    
z.Test_fileinput_nextfile.test_state_is_not_NoneN)r   r   r   rº   rË   rÎ   r   r   r   r   rÏ   ™  s   
rÏ   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚTest_fileinput_filenamez#Unit tests for fileinput.filename()c          	   C   sD   dt _|  t¡}t  ¡  W dQ R X |  d|jj¡ |  t j¡ dS )z·Tests fileinput.filename() when fileinput._state is None.
           Ensure that it raises RuntimeError with a meaningful error message
           and does not modify fileinput._stateN)zno active input())	r¼   r½   r   r™   r%   r[   rš   rq   rÍ   )r   r›   r   r   r   rË   ·  s
    z*Test_fileinput_filename.test_state_is_Nonec             C   sN   t ƒ }tƒ }||jd< |t_t ¡ }|  |d¡ |  ||¡ |  tj|¡ dS )a  Tests fileinput.filename() when fileinput._state is not None.
           Ensure that it invokes fileinput._state.filename() exactly once,
           returns whatever it returns, and does not modify fileinput._state
           to point to a different object.r%   N)rÄ   r³   r¹   r¼   r½   r%   rÂ   rÆ   )r   Zfilename_retvalrÇ   rÐ   r   r   r   rÎ   Á  s    
z.Test_fileinput_filename.test_state_is_not_NoneN)r   r   r   rº   rË   rÎ   r   r   r   r   rÑ   ´  s   
rÑ   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚTest_fileinput_linenoz!Unit tests for fileinput.lineno()c          	   C   sD   dt _|  t¡}t  ¡  W dQ R X |  d|jj¡ |  t j¡ dS )zµTests fileinput.lineno() when fileinput._state is None.
           Ensure that it raises RuntimeError with a meaningful error message
           and does not modify fileinput._stateN)zno active input())	r¼   r½   r   r™   r\   r[   rš   rq   rÍ   )r   r›   r   r   r   rË   Ò  s
    z(Test_fileinput_lineno.test_state_is_Nonec             C   sN   t ƒ }tƒ }||jd< |t_t ¡ }|  |d¡ |  ||¡ |  tj|¡ dS )zýTests fileinput.lineno() when fileinput._state is not None.
           Ensure that it invokes fileinput._state.lineno() exactly once,
           returns whatever it returns, and does not modify fileinput._state
           to point to a different object.r\   N)rÄ   r³   r¹   r¼   r½   r\   rÂ   rÆ   )r   Zlineno_retvalrÇ   rÐ   r   r   r   rÎ   Ü  s    
z,Test_fileinput_lineno.test_state_is_not_NoneN)r   r   r   rº   rË   rÎ   r   r   r   r   rÒ   Ï  s   
rÒ   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚTest_fileinput_filelinenoz%Unit tests for fileinput.filelineno()c          	   C   sD   dt _|  t¡}t  ¡  W dQ R X |  d|jj¡ |  t j¡ dS )z¹Tests fileinput.filelineno() when fileinput._state is None.
           Ensure that it raises RuntimeError with a meaningful error message
           and does not modify fileinput._stateN)zno active input())	r¼   r½   r   r™   r]   r[   rš   rq   rÍ   )r   r›   r   r   r   rË   í  s
    z,Test_fileinput_filelineno.test_state_is_Nonec             C   sN   t ƒ }tƒ }||jd< |t_t ¡ }|  |d¡ |  ||¡ |  tj|¡ dS )a  Tests fileinput.filelineno() when fileinput._state is not None.
           Ensure that it invokes fileinput._state.filelineno() exactly once,
           returns whatever it returns, and does not modify fileinput._state
           to point to a different object.r]   N)rÄ   r³   r¹   r¼   r½   r]   rÂ   rÆ   )r   Zfilelineno_retvalrÇ   rÐ   r   r   r   rÎ   ÷  s    
z0Test_fileinput_filelineno.test_state_is_not_NoneN)r   r   r   rº   rË   rÎ   r   r   r   r   rÓ   ê  s   
rÓ   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚTest_fileinput_filenoz!Unit tests for fileinput.fileno()c          	   C   sD   dt _|  t¡}t  ¡  W dQ R X |  d|jj¡ |  t j¡ dS )zµTests fileinput.fileno() when fileinput._state is None.
           Ensure that it raises RuntimeError with a meaningful error message
           and does not modify fileinput._stateN)zno active input())	r¼   r½   r   r™   ry   r[   rš   rq   rÍ   )r   r›   r   r   r   rË     s
    z(Test_fileinput_fileno.test_state_is_Nonec             C   sT   t ƒ }tƒ }||jd< ||_|t_t ¡ }|  |d¡ |  ||¡ |  tj|¡ dS )zýTests fileinput.fileno() when fileinput._state is not None.
           Ensure that it invokes fileinput._state.fileno() exactly once,
           returns whatever it returns, and does not modify fileinput._state
           to point to a different object.ry   N)	rÄ   r³   r¹   Úfileno_retvalr¼   r½   ry   rÂ   rÆ   )r   rÕ   rÇ   rÐ   r   r   r   rÎ     s    
z,Test_fileinput_fileno.test_state_is_not_NoneN)r   r   r   rº   rË   rÎ   r   r   r   r   rÔ     s   
rÔ   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚTest_fileinput_isfirstlinez&Unit tests for fileinput.isfirstline()c          	   C   sD   dt _|  t¡}t  ¡  W dQ R X |  d|jj¡ |  t j¡ dS )zºTests fileinput.isfirstline() when fileinput._state is None.
           Ensure that it raises RuntimeError with a meaningful error message
           and does not modify fileinput._stateN)zno active input())	r¼   r½   r   r™   r_   r[   rš   rq   rÍ   )r   r›   r   r   r   rË   $  s
    z-Test_fileinput_isfirstline.test_state_is_Nonec             C   sN   t ƒ }tƒ }||jd< |t_t ¡ }|  |d¡ |  ||¡ |  tj|¡ dS )a  Tests fileinput.isfirstline() when fileinput._state is not None.
           Ensure that it invokes fileinput._state.isfirstline() exactly once,
           returns whatever it returns, and does not modify fileinput._state
           to point to a different object.r_   N)rÄ   r³   r¹   r¼   r½   r_   rÂ   rÆ   )r   Zisfirstline_retvalrÇ   rÐ   r   r   r   rÎ   .  s    
z1Test_fileinput_isfirstline.test_state_is_not_NoneN)r   r   r   rº   rË   rÎ   r   r   r   r   rÖ   !  s   
rÖ   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚTest_fileinput_isstdinz"Unit tests for fileinput.isstdin()c          	   C   sD   dt _|  t¡}t  ¡  W dQ R X |  d|jj¡ |  t j¡ dS )z¶Tests fileinput.isstdin() when fileinput._state is None.
           Ensure that it raises RuntimeError with a meaningful error message
           and does not modify fileinput._stateN)zno active input())	r¼   r½   r   r™   r`   r[   rš   rq   rÍ   )r   r›   r   r   r   rË   ?  s
    z)Test_fileinput_isstdin.test_state_is_Nonec             C   sN   t ƒ }tƒ }||jd< |t_t ¡ }|  |d¡ |  ||¡ |  tj|¡ dS )zÿTests fileinput.isstdin() when fileinput._state is not None.
           Ensure that it invokes fileinput._state.isstdin() exactly once,
           returns whatever it returns, and does not modify fileinput._state
           to point to a different object.r`   N)rÄ   r³   r¹   r¼   r½   r`   rÂ   rÆ   )r   Zisstdin_retvalrÇ   rÐ   r   r   r   rÎ   I  s    
z-Test_fileinput_isstdin.test_state_is_not_NoneN)r   r   r   rº   rË   rÎ   r   r   r   r   r×   <  s   
r×   c               @   s   e Zd Zdd„ Zdd„ ZdS )ÚInvocationRecorderc             C   s
   d| _ d S )Nr   )Úinvocation_count)r   r   r   r   r    X  s    zInvocationRecorder.__init__c             O   s   |  j d7  _ ||f| _d S )Nr4   )rÙ   Úlast_invocation)r   rq   rr   r   r   r   rs   Z  s    zInvocationRecorder.__call__N)r   r   r   r    rs   r   r   r   r   rØ   W  s   rØ   c               @   s|   e Zd ZdZdd„ Zdd„ Zdd„ Ze e	d¡d	d
„ ƒZ
e ed¡dd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZdS )ÚTest_hook_compressedz*Unit tests for fileinput.hook_compressed()c             C   s   t ƒ | _d S )N)rØ   Ú	fake_open)r   r   r   r   rÀ   a  s    zTest_hook_compressed.setUpc             C   s   |   dd¡ d S )Nr'   r4   )Údo_test_use_builtin_open)r   r   r   r   Útest_empty_stringd  s    z&Test_hook_compressed.test_empty_stringc             C   s   |   dd¡ d S )NZabcdrO   )rÝ   )r   r   r   r   Útest_no_extg  s    z Test_hook_compressed.test_no_extzRequires gzip and zlibc             C   sP   t j}| jt _zt dd¡}W d |t _X |  | jjd¡ |  | jjdi f¡ d S )Nztest.gzrQ   r4   )ztest.gzrQ   )Úgzipr   rÜ   r¼   Úhook_compressedr[   rÙ   rÚ   )r   Úoriginal_openr§   r   r   r   Útest_gz_ext_fakej  s    z%Test_hook_compressed.test_gz_ext_fakezRequires bz2c             C   sP   t j}| jt _zt dd¡}W d |t _X |  | jjd¡ |  | jjdi f¡ d S )Nztest.bz2rL   r4   )ztest.bz2rL   )Úbz2ZBZ2FilerÜ   r¼   rá   r[   rÙ   rÚ   )r   râ   r§   r   r   r   Útest_bz2_ext_fakev  s    z&Test_hook_compressed.test_bz2_ext_fakec             C   s   |   dd¡ d S )Nz	abcd.blahr;   )rÝ   )r   r   r   r   Útest_blah_ext‚  s    z"Test_hook_compressed.test_blah_extc             C   s   |   dd¡ d S )Nzabcd.GzrH   )rÝ   )r   r   r   r   Útest_gz_ext_builtin…  s    z(Test_hook_compressed.test_gz_ext_builtinc             C   s   |   dd¡ d S )Nzabcd.Bz2é   )rÝ   )r   r   r   r   Útest_bz2_ext_builtinˆ  s    z)Test_hook_compressed.test_bz2_ext_builtinc          	   C   sV   |   | j¡}zt ||¡}W d |   |¡ X |  | jjd¡ |  | jj||fi f¡ d S )Nr4   )Úreplace_builtin_openrÜ   r¼   rá   r[   rÙ   rÚ   )r   r%   r   râ   r§   r   r   r   rÝ   ‹  s    
z-Test_hook_compressed.do_test_use_builtin_openc             C   s   t j}| t _|S )N)Úbuiltinsr   )Znew_open_funcrâ   r   r   r   rê   –  s    z)Test_hook_compressed.replace_builtin_openN)r   r   r   rº   rÀ   rÞ   rß   r²   Z
skipUnlessrà   rã   rä   rå   ræ   rç   ré   rÝ   Ústaticmethodrê   r   r   r   r   rÛ   ^  s   rÛ   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚTest_hook_encodedz'Unit tests for fileinput.hook_encoded()c             C   sº   t ƒ }t ƒ }tj||d}tƒ }tj}|t_zt ƒ }t ƒ }|||ƒ}W d |t_X |  |jd¡ |j\}	}
|  	|	d |¡ |  	|	d |¡ |  	|
 
d¡|¡ |  	|
 
d¡|¡ |  |
¡ d S )N)Úerrorsr4   r   Úencodingrî   )rÄ   r¼   r   rØ   rë   r   r[   rÙ   rÚ   rÆ   Úpopr^   )r   rï   rî   r§   rÜ   râ   r%   r   Zopen_resultrq   rr   r   r   r   ÚtestŸ  s$    
zTest_hook_encoded.testc          	      s‚   t tdƒ}| d¡ W d Q R X ˆ  tt¡ ‡ fdd„}|ddgƒ ˆ  t¡ |ddgƒ W d Q R X |dd	gƒ |d
dgƒ d S )Nr{   s   €abcc          	      s:   t tdtd| dd}t|ƒ}W d Q R X ˆ  ||¡ d S )Nr´   zutf-8)rî   )rI   r   r&   )r   r   r   rZ   r[   )rî   Úexpected_linesrk   r/   )r   r   r   Úcheck¼  s    z,Test_hook_encoded.test_errors.<locals>.checkÚignoreÚabcÚstrictÚreplaceu   ï¿½abcÚbackslashreplacez\x80abc)r   r   r   r   rŽ   r   r   )r   r   ró   r   )r   r   Útest_errors·  s    zTest_hook_encoded.test_errorsc          	      sÆ   t tdƒ}| d¡ W d Q R X ˆ  tt¡ ‡ fdd„}|ddddd	gƒ ˆ  t¡ |d
dddd	gƒ W d Q R X ˆ  t¡ |ddddd	gƒ W d Q R X ˆ  t¡ |ddddd	gƒ W d Q R X d S )Nr{   s   A
B
CD+IKw-c          	      s6   t t| tdƒd}t|ƒ}W d Q R X ˆ  ||¡ d S )Nzutf-7)rI   r   r&   )r   r   r   rZ   r[   )r   rò   rk   r/   )r   r   r   ró   Î  s    z+Test_hook_encoded.test_modes.<locals>.checkr´   zA
zB
zC
u   Dâ‚¬ZrUr|   r   zB
zC)	r   r   r   r   rŽ   r>   r?   r   r   )r   r   ró   r   )r   r   Ú
test_modesÈ  s    zTest_hook_encoded.test_modesN)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 )ÚMiscTestc             C   s   t  | t¡ d S )N)r   Zcheck__all__r¼   )r   r   r   r   Útest_allß  s    zMiscTest.test_allN)r   r   r   rü   r   r   r   r   rû   Ý  s   rû   Ú__main__)3rº   rŸ   rb   rW   r¼   r¶   rë   r   r²   rä   ÚImportErrorrà   Úior   r   r   r   Zpathlibr   Ztest.supportr   r   r	   r
   rŽ   rñ   r   r   r   r   ZTestCaser3   rn   rt   r³   r»   rÃ   rÌ   rÏ   rÑ   rÒ   rÓ   rÔ   rÖ   r×   rØ   rÛ   rí   rû   r   Úmainr   r   r   r   Ú<module>   s^   


$[  N.A>A
