B
    18™\-•  ã               @   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)ÚtempfileÚmkstempÚ
addCleanupr   r
   ÚopenÚwrite)ÚselfÚcontentr   ÚfdÚnameÚf© r   úW/oak/stanford/groups/akundaje/marinovg/programs/Python-3.7.3/Lib/test/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   Ú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.   r3   r4   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
r7   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
r7   Nr   )r9   r:   r   r   r   r;   X   s    é
   c             s   s   | ]}d |d  V  qdS )zLine %s of file 3
r7   Nr   )r9   r:   r   r   r   r;   Y   s    é   c             s   s   | ]}d |d  V  qdS )zLine %s of file 4
r7   Nr   )r9   r:   r   r   r   r;   Z   s    r7   )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+)r7   é   z%s. Simple iteration (bs=%s)r   )ÚfilesÚbufsizeé   é   zLine 5 of file 1
r8   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>   )rL   ÚinplacerM   r/   Ú
)ÚreÚcompiler   Úprintr   Úlistr4   ÚassertEqualr0   Úlinenor'   r.   Ú
filelinenoÚassertFalseÚisfirstlineÚisstdinÚnextfileÚsysÚstdinr   ÚstdoutÚupperÚmatchÚassertNotEqualÚintÚgroup)r   rF   rG   rH   rI   rE   rD   ÚpatÚstartÚfir2   ÚsZ	savestdinÚ
savestdoutr-   Úmr   r   r   rC   a   s„    



 z!BufferSizesTests.buffer_size_testN)r   r   )r   r   r   rJ   rC   r   r   r   r   r6   S   s   r6   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   rt   r   r   r   r"   ¯   s    zUnconditionallyRaise.__init__c             O   s   d| _ |  ¡ ‚d S )NT)ru   rt   )r   ÚargsÚkwargsr   r   r   Ú__call__²   s    zUnconditionallyRaise.__call__N)r   r   r   r"   rx   r   r   r   r   rs   ®   s   rs   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.
)rL   r7   r   )	r   r   r.   r^   r_   r`   r'   ra   r4   )r   rF   rG   rH   rI   ro   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)rL   zA
zB
ÚCzD
zE
ÚFrT   rK   )r   r   r]   r^   r`   r_   )r   rF   rG   ro   r2   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)rL   r/   )r   r   r^   Úfilenor*   rj   rd   r]   )r   rF   rG   ro   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)rL   r   zA
zB
zC
ÚD)r   ÚfailÚ
ValueErrorr   r	   rB   r]   r^   )r   ro   rF   r2   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 spamrU   Úrb)rL   r   )r   Úpatchr   Úbufferr   r]   r^   )r   Zm_stdinro   r2   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ˆ   rU   r†   )rL   r   )re   rf   r   ra   Úhasattrr   r]   r^   )r   Z
orig_stdinro   r2   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 )Nr7   c             S   s   d S )Nr   )r   rr   r   r   r   Ú<lambda>  ó    z7FileInputTests.test_file_opening_hook.<locals>.<lambda>)rX   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)ru   )r   r   r   r   r"     s    zFFileInputTests.test_file_opening_hook.<locals>.CustomOpenHook.__init__c             W   s   d| _ t|Ž S )NT)ru   r   )r   rv   r   r   r   rx     s    zFFileInputTests.test_file_opening_hook.<locals>.CustomOpenHook.__call__N)r   r   r   r"   rx   r   r   r   r   ÚCustomOpenHook  s   rŽ   rY   zopenhook not invoked)r   rƒ   r„   r   r.   Ú
assertTrueru   )r   ro   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)rL   r(   zA
zB
zC
zRead to end of filer)   )r   r   r   r   Úsafe_unlinkr   r   r^   r.   ÚUnicodeDecodeErrorrƒ   ÚassertRaisesr]   )r   r   ro   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†   )rL   r   s   A
s   B
s   CDr   )r   r   r   r   r”   r   r^   r.   )r   r   ro   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)rL   zA
zB
r{   zD
zE
r|   rT   rK   r   )r   r   r]   r^   r`   r_   Ú_files)r   rF   rG   ro   r2   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)   )rL   r   )r   r   ÚOSErrorr^   r™   )r   rF   ro   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)rL   )rU   )r   r^   r™   )r   ro   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
)rL   r   zline1
r7   zline2
N)r   r   r^   )r   r   ro   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
)rL   r7   N)zaccessing lines out of order)r   r   r–   ÚRuntimeErrorr^   Ú	exceptionrv   )r   r   ro   Ú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)   )rL   r   N)zend of input reached)r   r   r–   Ú
IndexErrorr^   r    rv   )r   r   ro   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 ignoredrY   z.bakT)rL   rX   Nzos.unlink() was not invoked)Úosr
   rs   r›   r   r   r   r   r*   rd   r   ru   )r   Zos_unlink_origZos_unlink_replacementr   ro   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.rY   T)rL   rX   Nzos.fstat() was not invoked)	r¥   Úfstatrs   r›   r   r   r.   r   ru   )r   Zos_fstat_origZos_fstat_replacementr   ro   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.rY   T)rL   rX   Nzos.fstat() was not invoked)	r¥   r©   rs   r›   r   r   r.   r   ru   )r   Zos_chmod_origZos_chmod_replacementr   ro   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)rs   r"   r„   )r   r   r   r   r"   º  s    zZFileInputTests.test_fileno_when_ValueError_raised.<locals>.FilenoRaisesValueError.__init__c             S   s   |   ¡  d S )N)rx   )r   r   r   r   r~   ¼  s    zXFileInputTests.test_fileno_when_ValueError_raised.<locals>.FilenoRaisesValueError.filenoN)r   r   r   r"   r~   r   r   r   r   ÚFilenoRaisesValueError¹  s   r«   rY   )rL   z_file.fileno() was not invokedr/   zfileno() should return -1)rs   r   r   Ú_filer~   r   ru   r^   )r   r«   Z unconditionally_raise_ValueErrorr   ro   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
)rL   r(   zline1
Úline2r)   )r    r   r(   r^   r#   r.   )r   Úsrcro   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
)rL   r(   zline1
r¯   r)   )r    r   r(   r^   r#   r*   r–   ÚStopIteration)r   r°   ro   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.r7   )
r   r   r   r.   r^   r_   r`   r'   r¥   Úfspath)r   rF   ro   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)rX   zModified %szModified Pathlib file.
)r   r   r   r.   r^   r\   r   Úread)r   rF   ro   r-   r   r   r   r   Útest_pathlib_file_inplaceø  s    
z(FileInputTests.test_pathlib_file_inplaceN)r   r   r   rz   r}   r   r…   r‰   r‹   r‘   r—   r˜   rš   rœ   r   rž   r¢   r¤   r¦   r¨   ÚunittestÚskipIfrŠ   r¥   rª   r®   r±   r³   rµ   r·   r   r   r   r   ry   ¶   s.   

			ry   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>)rL   rX   ÚbackuprM   r   r(   r¬   ÚcollectionsÚdefaultdictÚinvocation_countsÚreturn_values)r   rL   rX   r¼   rM   r   r(   r   r   r   r"     s    zMockFileInput.__init__c             C   s   | j d  d7  < d S )Nr4   r7   )r¿   )r   r   r   r   r4     s    zMockFileInput.closec             C   s   | j d  d7  < | jd S )Nrd   r7   )r¿   rÀ   )r   r   r   r   rd     s    zMockFileInput.nextfilec             C   s   | j d  d7  < | jd S )Nr'   r7   )r¿   rÀ   )r   r   r   r   r'     s    zMockFileInput.filenamec             C   s   | j d  d7  < | jd S )Nr_   r7   )r¿   rÀ   )r   r   r   r   r_     s    zMockFileInput.linenoc             C   s   | j d  d7  < | jd S )Nr`   r7   )r¿   rÀ   )r   r   r   r   r`      s    zMockFileInput.filelinenoc             C   s   | j d  d7  < | jd S )Nr~   r7   )r¿   rÀ   )r   r   r   r   r~   $  s    zMockFileInput.filenoc             C   s   | j d  d7  < | jd S )Nrb   r7   )r¿   rÀ   )r   r   r   r   rb   (  s    zMockFileInput.isfirstlinec             C   s   | j d  d7  < | jd S )Nrc   r7   )r¿   rÀ   )r   r   r   r   rc   ,  s    zMockFileInput.isstdin)NFr)   r   r»   N)r   r   r   Ú__doc__r"   r4   rd   r'   r_   r`   r~   rb   rc   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 )Nr7   )r¿   r^   r0   )r   Zmock_file_inputÚ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    rv   Ú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().)rL   rX   r¼   rM   r   r(   zfileinput._staterL   rX   r¼   rM   r   r(   N)rÌ   rÃ   rÍ   rÎ   rÄ   rL   rX   r¼   rM   r   r(   )r   rL   rX   r¼   rM   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Ä   r4   Ú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=Noner4   N)rº   rÃ   rÄ   r4   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Ÿ   rd   r^   r    rv   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.rd   N)rÌ   rº   rÀ   rÃ   rÄ   rd   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    rv   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    rv   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    rv   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Ÿ   r~   r^   r    rv   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.r~   N)	rÌ   rº   rÀ   Úfileno_retvalrÃ   rÄ   r~   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Ÿ   rb   r^   r    rv   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.rb   N)rÌ   rº   rÀ   rÃ   rÄ   rb   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Ÿ   rc   r^   r    rv   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.rc   N)rÌ   rº   rÀ   rÃ   rÄ   rc   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 )Nr7   )rá   Úlast_invocation)r   rv   rw   r   r   r   rx   Z  s    zInvocationRecorder.__call__N)r   r   r   r"   rx   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)   r7   )Ú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abcdrR   )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.gzrT   r7   )ztest.gzrT   )Ú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.bz2rO   r7   )ztest.bz2rO   )Úbz2Ú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.GzrK   )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 )Nr7   )Ú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¸   Ú
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)Úerrorsr7   r   Úencodingrø   )rÌ   rÃ   r   rà   rô   r   r^   rá   râ   rÎ   Úpopra   )r   rù   rø   r­   rä   rê   r'   r   Zopen_resultrv   rw   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ø   )rL   r   r(   )r   r   r   r]   r^   )rø   Úexpected_linesro   r2   )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)rL   r   r(   )r   r   r   r]   r^   )r   rü   ro   r2   )r   r   r   rý   Î  s    z+Test_hook_encoded.test_modes.<locals>.checkr»   zA
zB
zC
u   Dâ‚¬ÚrUr   r†   zB
zC)	r   r   r   r   r”   rA   rB   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   Ú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¥   re   rZ   rÃ   r½   rô   r   r¸   rì   ÚImportErrorrè   Úior   r   r   r   Úpathlibr   Ztest.supportr   r   r	   r
   r”   rû   r   r   r   r    ÚTestCaser6   rs   ry   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
