a
    äzeW•  ã                   @   sL  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zddl	Z	W n e
yf   dZ	Y n0 zddlZW n e
yŠ   dZY n0 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rHe 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   sR   t  ¡ \}}|  tj|¡ t||ƒ}| |¡ W d   ƒ n1 sD0    Y  |S ©N)ÚtempfileÚmkstempÚ
addCleanupr   r
   ÚopenÚwrite)ÚselfZcontentr   ÚfdÚnameÚf© r   ú)/usr/lib/python3.9/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 r   ©Ú
_linesread©r   r   r   r   Ú__init__1   s    zLineReader.__init__c                 C   s$   z| j d d … W g | _ S g | _ 0 d S r   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   s@   g }d}|   ¡ }|s|S | |¡ |t|ƒ7 }||kr|S qd S ©Nr   )r2   r/   Úlen)r   ÚhintÚlinesr0   r1   r   r   r   Ú	readlinesD   s    
zLineReader.readlinesc                 C   s   d S r   r   r$   r   r   r   ÚcloseP   s    zLineReader.close)N)r3   )
r   r   r    r%   Úpropertyr&   r,   r2   r8   r9   r   r   r   r   r!   /   s   


r!   c                   @   s   e Zd Zdd„ ZdS )ÚBufferSizesTestsc                 C   s’  |   d dd„ tdƒD ƒ¡¡}|   d dd„ tdƒD ƒ¡¡}|   d dd„ tdƒD ƒ¡¡}|   d d	d„ td
ƒD ƒ¡¡}t d¡}trŽtdƒ t||||fd}t|ƒ}| 	¡  |  
t|ƒd¡ |  
|d d¡ |  
|d d¡ |  
| ¡ d¡ |  
| ¡ |¡ trtdƒ t||||fd}d}|r@|dkr@| ¡ }q$|  
| ¡ |¡ |  
| ¡ d¡ |  
| ¡ d¡ |  | ¡ ¡ |  | ¡ ¡ trštdƒ | ¡  |  
| ¡ d¡ |  
| ¡ d¡ | 	¡  trØtdƒ t||||dfd}tj}	zTtdƒt_t|ƒ}|  
t|ƒd¡ |  
|d d ¡ |  
| ¡ d!¡ | ¡  W |	t_n|	t_0 tr^td"ƒ t||||fd}|  
| ¡ d#¡ |  
| ¡ d ¡ | ¡  |  
| ¡ d#¡ |  
| ¡ d ¡ trÆtd$ƒ tj}
zJt||||fd
d%}|D ]}|d d&…  ¡ }t|ƒ qæ| 	¡  W |
t_n|
t_0 t||||fd}|D ]N}|  
|d& d'¡ | |d d&… ¡}|  |d ¡ |  
t| d
¡ƒ| ¡ ¡ q6| 	¡  d S )(Nr-   c                 s   s   | ]}d |d  V  qdS )zLine %s of file 1
é   Nr   ©Ú.0Úir   r   r   Ú	<genexpr>V   ó    z5BufferSizesTests.test_buffer_sizes.<locals>.<genexpr>é   c                 s   s   | ]}d |d  V  qdS )zLine %s of file 2
r<   Nr   r=   r   r   r   r@   W   rA   é
   c                 s   s   | ]}d |d  V  qdS )zLine %s of file 3
r<   Nr   r=   r   r   r   r@   X   rA   é   c                 s   s   | ]}d |d  V  qdS )zLine %s of file 4
r<   Nr   r=   r   r   r   r@   Y   rA   r<   zLINE (\d+) OF FILE (\d+)z1. Simple iteration©Úfilesé   é   zLine 5 of file 1
é   zLine 1 of file 4
z2. Status variablesÚxzLine 6 of file 2
é   é   z3. NextfilezLine 1 of file 3
é   z4. Stdinú-z Line 1 of stdin
Line 2 of stdin
é!   é    zLine 2 of stdin
z<stdin>z5. Boundary conditionsr   z
6. Inplace©rF   Úinplacer3   Ú
)r   ÚjoinÚrangeÚreÚcompiler   Úprintr   Úlistr9   ÚassertEqualr5   Úlinenor+   r2   Ú
filelinenoÚassertFalseÚisfirstlineÚisstdinÚnextfileÚsysÚstdinr   ÚstdoutÚupperÚmatchÚassertNotEqualÚintÚgroup)r   Út1Út2Út3Út4ÚpatÚfir7   ÚsZ	savestdinZ
savestdoutr1   Úmr   r   r   Útest_buffer_sizesT   sŠ    



z"BufferSizesTests.test_buffer_sizesN)r   r   r    rq   r   r   r   r   r;   S   s   r;   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 r'   )ru   rt   ©r   ÚargsÚkwargsr   r   r   Ú__call__ª   s    zUnconditionallyRaise.__call__N©r   r   r    r%   ry   r   r   r   r   rr   ¦   s   rr   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ejeddd„ ƒZdd„ Zejedd d!„ ƒZejedd"d#„ ƒZd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ Zd4S )5ÚFileInputTestsc                 C   sÌ   |   d¡}|   d¡}|   d¡}|   d¡}t||||fd}| ¡ }|  |d¡ |  | ¡ d¡ |  | ¡ d¡ |  | ¡ |¡ | ¡ }|  |¡ |  | ¡ d¡ |  | ¡ d¡ |  | ¡ |¡ | ¡  d S )Nr-   zThe only line there is.
rE   r<   r   )	r   r   r2   rZ   r[   r\   r+   r]   r9   )r   ri   rj   rk   rl   rn   r1   r   r   r   Útest_zero_byte_files°   s     




z#FileInputTests.test_zero_byte_filesc                 C   s^   |   d¡}|   d¡}t||fd}t|ƒ}|  |g d¢¡ |  | ¡ d¡ |  | ¡ d¡ d S )NúA
B
CúD
E
FrE   ©úA
úB
ÚCzD
zE
ÚFé   rL   )r   r   rY   rZ   r\   r[   ©r   ri   rj   rn   r7   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
DrE   r3   )r   r   rZ   Úfilenor.   rf   r`   rY   )r   ri   rj   rn   r1   r   r   r   Útest_filenoØ   s    

zFileInputTests.test_filenoc                 C   sº   zt dd}|  d¡ W n ty*   Y n0 | jddd}tdtfƒ t |dd}W d   ƒ n1 sh0    Y  tdtfƒ t|ƒ}W d   ƒ n1 sœ0    Y  |  |g d	¢¡ d S )
Nr   r   z-FileInput should reject invalid mode argumentó   A
B
CDÚwbr-   ÚU©rF   r   )r€   r   úC
ÚD)r   ÚfailÚ
ValueErrorr   r	   ÚDeprecationWarningrY   rZ   )r   rn   ri   r7   r   r   r   Útest_opening_modeä   s    
*&z FileInputTests.test_opening_modec                 C   s\   t  d¡>}tdƒ|_tdgdd}t|ƒ}|  |dg¡ W d   ƒ n1 sN0    Y  d S )Nz	sys.stdinó   spam, bacon, sausage, and spamrN   ÚrbrŒ   )r   Zpatchr   Úbufferr   rY   rZ   )r   Zm_stdinrn   r7   r   r   r   Útest_stdin_binary_modeó   s
    
z%FileInputTests.test_stdin_binary_modec                 C   s^   t j}zJtdƒt _|  tt jdƒ¡ tdgdd}t|ƒ}|  |dg¡ W |t _n|t _0 d S )Nr“   r•   rN   r”   rŒ   )ra   rb   r   r]   Úhasattrr   rY   rZ   )r   Z
orig_stdinrn   r7   r   r   r   Útest_detached_stdin_binary_modeú   s    
z.FileInputTests.test_detached_stdin_binary_modec                 C   sÄ   zt ddd„ d}|  d¡ W n ty0   Y n0 zt dd}|  d¡ W n ty\   Y n0 G dd	„ d	ƒ}|  d
¡}|ƒ }t |g|d}| ¡  W d   ƒ n1 s¨0    Y  |  |jd¡ d S )Nr<   c                 S   s   d S r   r   )r   rp   r   r   r   Ú<lambda>  rA   z7FileInputTests.test_file_opening_hook.<locals>.<lambda>)rR   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 rs   )ru   r$   r   r   r   r%     s    zFFileInputTests.test_file_opening_hook.<locals>.CustomOpenHook.__init__c                 W   s   d| _ t|Ž S r'   )ru   r   )r   rw   r   r   r   ry     s    zFFileInputTests.test_file_opening_hook.<locals>.CustomOpenHook.__call__Nrz   r   r   r   r   ÚCustomOpenHook  s   rš   rS   zopenhook not invoked)r   r   r   r   r2   Ú
assertTrueru   )r   rn   rš   ÚtZcustom_open_hookr   r   r   Útest_file_opening_hook  s     

&z%FileInputTests.test_file_opening_hookc              	   C   s2  t tdƒ2}| d¡ | dd ¡ | d¡ W d   ƒ n1 sB0    Y  |  tt¡ tttdƒd´}z4|  | ¡ d¡ |  | ¡ d	¡ |  | ¡ d
¡ W n t	yº   |  
d¡ Y n0 |  t	¡ t|ƒ W d   ƒ n1 sä0    Y  |  | ¡ d¡ |  | ¡ d¡ W d   ƒ n1 s$0    Y  d S )NrŠ   s   A
B
Cs
   123456789
iè  ó   €Úascii©rF   r,   r€   r   r   zRead to end of filer-   )r   r   r   r   Úsafe_unlinkr   r   rZ   r2   ÚUnicodeDecodeErrorr   ÚassertRaisesrY   ©r   r   rn   r   r   r   Útest_readline   s&    
(ÿ&zFileInputTests.test_readlinec                 C   sÀ   t tdƒ}| d¡ W d   ƒ n1 s*0    Y  |  tt¡ ttdd`}|  | ¡ d¡ |  | ¡ d¡ |  | ¡ d¡ |  | ¡ d¡ |  | ¡ d¡ W d   ƒ n1 s²0    Y  d S )	NrŠ   r‰   r”   rŒ   s   A
s   B
s   CDrA   )r   r   r   r   r¡   r   rZ   r2   r¤   r   r   r   Útest_readline_binary_mode7  s    (z(FileInputTests.test_readline_binary_modec                 C   sš   | j ddd}t|ddd0}| ¡ }|  |d¡ tj d¡ W d   ƒ n1 sR0    Y  t|dƒ }|  | ¡ d¡ W d   ƒ n1 sŒ0    Y  d S )Ns   Initial text.rŠ   r   r”   T)r   rR   s	   New line.)	r   r   r2   rZ   ra   rc   r   r   Úread)r   Z	temp_fileZfobjr1   r   r   r   r   Útest_inplace_binary_write_modeD  s    *z-FileInputTests.test_inplace_binary_write_modec                 C   sŒ   |   d¡}|   d¡}t||fd}t|ƒ}W d   ƒ n1 s@0    Y  |  |g d¢¡ |  | ¡ d¡ |  | ¡ d¡ |  |jd¡ d S )Nr}   r~   rE   r   r„   rL   r   )r   r   rY   rZ   r\   r[   Ú_filesr…   r   r   r   Útest_context_managerN  s    

&z#FileInputTests.test_context_managerc                 C   sb   |   d¡}z2t|d}t‚W d   ƒ n1 s00    Y  W n  ty\   |  |jd¡ Y n0 d S )Nr-   rE   r   )r   r   ÚOSErrorrZ   r©   )r   ri   rn   r   r   r   Útest_close_on_exceptionX  s    
&z&FileInputTests.test_close_on_exceptionc                 C   s<   t g d}|  |jd¡ W d   ƒ n1 s.0    Y  d S )NrE   )rN   )r   rZ   r©   )r   rn   r   r   r   Ú.test_empty_files_list_specified_to_constructor`  s    z=FileInputTests.test_empty_files_list_specified_to_constructor)Úcategoryc                 C   sb   |   d¡}t|gd8}|d }|  |d¡ |d }|  |d¡ W d  ƒ n1 sT0    Y  dS )zNTests invoking FileInput.__getitem__() with the current
           line numberúline1
line2
rE   r   úline1
r<   zline2
N)r   r   rZ   )r   rœ   rn   Zretval1Zretval2r   r   r   Útest__getitem__d  s    
zFileInputTests.test__getitem__c              	   C   sv   |   d¡}|  td¡L t|gd }|  |d d¡ W d   ƒ n1 sJ0    Y  W d   ƒ n1 sh0    Y  d S )Nr¯   zUse iterator protocol insteadrE   r   r°   )r   ZassertWarnsRegexr‘   r   rZ   )r   rœ   rn   r   r   r   Útest__getitem___deprecationo  s    
ÿz*FileInputTests.test__getitem___deprecationc              	   C   s|   |   d¡}t|gdB}|  t¡}|d  W d  ƒ n1 s@0    Y  W d  ƒ n1 s^0    Y  |  |jjd¡ dS )zZTests invoking FileInput.__getitem__() with an index unequal to
           the line numberr¯   rE   r<   N)zaccessing lines out of order)r   r   r£   ÚRuntimeErrorrZ   Ú	exceptionrw   ©r   rœ   rn   Úcmr   r   r   Útest__getitem__invalid_keyv  s
    
Dz)FileInputTests.test__getitem__invalid_keyc              	   C   s|   |   d¡}t|gdB}|  t¡}|d  W d  ƒ n1 s@0    Y  W d  ƒ n1 s^0    Y  |  |jjd¡ dS )zZTests invoking FileInput.__getitem__() with the line number but at
           end-of-inputr-   rE   r   N)zend of input reached)r   r   r£   Ú
IndexErrorrZ   r´   rw   rµ   r   r   r   Útest__getitem__eof€  s
    
Dz!FileInputTests.test__getitem__eofc                 C   s”   t j}ttƒ}zj|  d¡}|  tj|d ¡ t|gdd&}t|ƒ |t _| 	¡  W d  ƒ n1 sf0    Y  W |t _n|t _0 |  
|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 ignoredrS   z.bakTrQ   Nzos.unlink() was not invoked)Úosr
   rr   r«   r   r   r   r   r.   r`   r›   ru   )r   Zos_unlink_origZos_unlink_replacementrœ   rn   r   r   r   Ú%test_nextfile_oserror_deleting_backupŠ  s    
(ÿz4FileInputTests.test_nextfile_oserror_deleting_backupc                 C   sz   t j}ttƒ}zP|  d¡}t|gdd}|t _| ¡  W d  ƒ n1 sL0    Y  W |t _n|t _0 |  |jd¡ dS )z{Tests invoking FileInput.readline() when os.fstat() raises OSError.
           This exception should be silently discarded.rS   TrQ   Núos.fstat() was not invoked)	rº   Úfstatrr   r«   r   r   r2   r›   ru   )r   Zos_fstat_origZos_fstat_replacementrœ   rn   r   r   r   Ú%test_readline_os_fstat_raises_OSErrorŸ  s    
(ÿz4FileInputTests.test_readline_os_fstat_raises_OSErrorc                 C   sz   t j}ttƒ}zP|  d¡}t|gdd}|t _| ¡  W d  ƒ n1 sL0    Y  W |t _n|t _0 |  |jd¡ dS )z{Tests invoking FileInput.readline() when os.chmod() raises OSError.
           This exception should be silently discarded.rS   TrQ   Nr¼   )	rº   Úchmodrr   r«   r   r   r2   r›   ru   )r   Zos_chmod_origZos_chmod_replacementrœ   rn   r   r   r   Ú%test_readline_os_chmod_raises_OSError±  s    
(ÿz4FileInputTests.test_readline_os_chmod_raises_OSErrorc              	   C   s”   G dd„ dt ƒ}|ƒ }|  d¡}t|gd8}|j}z||_| ¡ }W ||_n||_0 W d   ƒ n1 sj0    Y  |  |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 r   )rr   r%   r   r$   r   r   r   r%   Å  s    zZFileInputTests.test_fileno_when_ValueError_raised.<locals>.FilenoRaisesValueError.__init__c                 S   s   |   ¡  d S r   )ry   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Á   rS   rE   z_file.fileno() was not invokedr3   zfileno() should return -1)rr   r   r   Ú_filer‡   r›   ru   rZ   )r   rÁ   Z unconditionally_raise_ValueErrorrœ   rn   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   ƒ n1 sÜ0    Y  d S ©Nzline1
line2zline3
r    r°   Zline2r-   )r!   r   r,   rZ   r&   r2   ©r   Úsrcrn   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   ƒ n1 sØ0    Y  d S rÅ   )r!   r   r,   rZ   r&   r.   r£   ÚStopIterationrÆ   r   r   r   Útest_iteration_bufferingê  s     ÿz'FileInputTests.test_iteration_bufferingc                 C   s„   t |  d¡ƒ}t|ƒZ}| ¡ }|  |d¡ |  | ¡ d¡ |  | ¡ d¡ |  | ¡ t 	|¡¡ W d   ƒ n1 sv0    Y  d S )NúPathlib file.r<   )
r   r   r   r2   rZ   r[   r\   r+   rº   Úfspath)r   ri   rn   r1   r   r   r   Útest_pathlib_fileú  s    
z FileInputTests.test_pathlib_filec                 C   s–   t |  d¡ƒ}t|dd0}| ¡ }|  |d¡ td| ƒ W d   ƒ n1 sP0    Y  t|ƒ }|  | ¡ d¡ W d   ƒ n1 sˆ0    Y  d S )NrË   T)rR   zModified %szModified Pathlib file.
)r   r   r   r2   rZ   rX   r   r§   )r   ri   rn   r1   r   r   r   r   Útest_pathlib_file_inplace  s    *
z(FileInputTests.test_pathlib_file_inplaceN)r   r   r    r|   r†   rˆ   r’   r–   r˜   r   r¥   r¦   r¨   rª   r¬   r­   r   Zignore_warningsr‘   r±   r²   r·   r¹   r»   r¾   rÀ   rÄ   rÈ   rÊ   rÍ   rÎ   r   r   r   r   r{   ®   s8   






	

		r{   c                   @   sb   e Zd ZdZd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S )ÚMockFileInputzDA class that mocks out fileinput.FileInput for use during unit testsNFr-   Úr)r   r,   c                C   s>   || _ || _|| _|| _|| _d | _t dd„ ¡| _i | _	d S )Nc                   S   s   dS r4   r   r   r   r   r   r™     rA   z(MockFileInput.__init__.<locals>.<lambda>)
rF   rR   Úbackupr   r,   rÂ   ÚcollectionsÚdefaultdictÚinvocation_countsÚreturn_values)r   rF   rR   rÑ   r   r,   r   r   r   r%     s    zMockFileInput.__init__c                 C   s   | j d  d7  < d S )Nr9   r<   )rÔ   r$   r   r   r   r9     s    zMockFileInput.closec                 C   s   | j d  d7  < | jd S )Nr`   r<   ©rÔ   rÕ   r$   r   r   r   r`     s    zMockFileInput.nextfilec                 C   s   | j d  d7  < | jd S )Nr+   r<   rÖ   r$   r   r   r   r+   "  s    zMockFileInput.filenamec                 C   s   | j d  d7  < | jd S )Nr[   r<   rÖ   r$   r   r   r   r[   &  s    zMockFileInput.linenoc                 C   s   | j d  d7  < | jd S )Nr\   r<   rÖ   r$   r   r   r   r\   *  s    zMockFileInput.filelinenoc                 C   s   | j d  d7  < | jd S )Nr‡   r<   rÖ   r$   r   r   r   r‡   .  s    zMockFileInput.filenoc                 C   s   | j d  d7  < | jd S )Nr^   r<   rÖ   r$   r   r   r   r^   2  s    zMockFileInput.isfirstlinec                 C   s   | j d  d7  < | jd S )Nr_   r<   rÖ   r$   r   r   r   r_   6  s    zMockFileInput.isstdin)NFr-   )r   r   r    Ú__doc__r%   r9   r`   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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 r   )Ú	fileinputÚ_stateÚ_orig_stater   Ú_orig_FileInputrÏ   r$   r   r   r   ÚsetUp>  s    z$BaseFileInputGlobalMethodsTest.setUpc                 C   s   | j t_| jt_d S r   )rÜ   rÙ   r   rÛ   rÚ   r$   r   r   r   ÚtearDownC  s    z'BaseFileInputGlobalMethodsTest.tearDownc                 C   s2   |j | }|  |d|¡ t|j ƒ}|  |d¡ d S ©Nr<   )rÔ   rZ   r5   )r   Zmock_file_inputZmethod_nameZactual_countZactual_total_countr   r   r   ÚassertExactlyOneInvocationG  s    

z9BaseFileInputGlobalMethodsTest.assertExactlyOneInvocationN)r   r   r    r×   rÝ   rÞ   rà   r   r   r   r   rØ   :  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   sj   t ƒ }tƒ |_|t_|  t¡}t ¡  W d  ƒ n1 s<0    Y  |  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 activeúfileinput._state)rÏ   ÚobjectrÂ   rÙ   rÚ   r£   r³   ÚinputrZ   r´   rw   ÚassertIs)r   Úinstancer¶   r   r   r   Ú1test_state_is_not_None_and_state_file_is_not_NoneR  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_None_  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_Nonej  s    z'Test_fileinput_input.test_state_is_Nonec                 C   s–   t ƒ }t ƒ }t ƒ }t ƒ }t ƒ }tj|||||d}|  |t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().)rF   rR   rÑ   r   r,   râ   rF   rR   rÑ   r   r,   N)
rã   rÙ   rä   rå   rÚ   rF   rR   rÑ   r   r,   )r   rF   rR   rÑ   r   r,   rÃ   r   r   r   rè   s  s    
ÿz'Test_fileinput_input.do_test_call_inputN)r   r   r    r×   rç   rê   rë   rè   r   r   r   r   rá   O  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Ú   r9   ÚassertIsNoner$   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=Noner9   N)rÏ   rÙ   rÚ   r9   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   sX   dt _|  t¡}t  ¡  W d  ƒ n1 s.0    Y  |  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³   r`   rZ   r´   rw   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.r`   N)rã   rÏ   rÕ   rÙ   rÚ   r`   rà   rå   )r   Znextfile_retvalræ   Úretvalr   r   r   rî   ­  s    
z.Test_fileinput_nextfile.test_state_is_not_NoneNrï   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   sX   dt _|  t¡}t  ¡  W d  ƒ n1 s.0    Y  |  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._stateNrñ   )	rÙ   rÚ   r£   r³   r+   rZ   r´   rw   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_NoneNrï   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   sX   dt _|  t¡}t  ¡  W d  ƒ n1 s.0    Y  |  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._stateNrñ   )	rÙ   rÚ   r£   r³   r[   rZ   r´   rw   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_NoneNrï   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   sX   dt _|  t¡}t  ¡  W d  ƒ n1 s.0    Y  |  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._stateNrñ   )	rÙ   rÚ   r£   r³   r\   rZ   r´   rw   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_NoneNrï   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   sX   dt _|  t¡}t  ¡  W d  ƒ n1 s.0    Y  |  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._stateNrñ   )	rÙ   rÚ   r£   r³   r‡   rZ   r´   rw   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_NoneNrï   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   sX   dt _|  t¡}t  ¡  W d  ƒ n1 s.0    Y  |  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._stateNrñ   )	rÙ   rÚ   r£   r³   r^   rZ   r´   rw   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î   5  s    
z1Test_fileinput_isfirstline.test_state_is_not_NoneNrï   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   sX   dt _|  t¡}t  ¡  W d  ƒ n1 s.0    Y  |  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._stateNrñ   )	rÙ   rÚ   r£   r³   r_   rZ   r´   rw   rí   rò   r   r   r   rë   F  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î   P  s    
z-Test_fileinput_isstdin.test_state_is_not_NoneNrï   r   r   r   r   rú   C  s   
rú   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚInvocationRecorderc                 C   s
   d| _ d S r4   )Úinvocation_countr$   r   r   r   r%   _  s    zInvocationRecorder.__init__c                 O   s   |  j d7  _ ||f| _d S rß   )rü   Úlast_invocationrv   r   r   r   ry   a  s    zInvocationRecorder.__call__Nrz   r   r   r   r   rû   ^  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 r   )rû   Ú	fake_openr$   r   r   r   rÝ   h  s    zTest_hook_compressed.setUpc                 C   s   |   dd¡ d S )Nr-   r<   ©Údo_test_use_builtin_openr$   r   r   r   Útest_empty_stringk  s    z&Test_hook_compressed.test_empty_stringc                 C   s   |   dd¡ d S )NZabcdé   r   r$   r   r   r   Útest_no_extn  s    z Test_hook_compressed.test_no_extzRequires gzip and zlibc                 C   sV   t j}| jt _zt dd¡}W |t _n|t _0 |  | jjd¡ |  | jjdi f¡ d S )Nútest.gzr„   r<   )r  r„   )Úgzipr   rÿ   rÙ   Úhook_compressedrZ   rü   rý   ©r   Úoriginal_openrÃ   r   r   r   Útest_gz_ext_fakeq  s    z%Test_hook_compressed.test_gz_ext_fakezRequires bz2c                 C   sV   t j}| jt _zt dd¡}W |t _n|t _0 |  | jjd¡ |  | jjdi f¡ d S )Nútest.bz2rH   r<   )r  rH   )Úbz2ÚBZ2Filerÿ   rÙ   r  rZ   rü   rý   r  r   r   r   Útest_bz2_ext_fake}  s    z&Test_hook_compressed.test_bz2_ext_fakec                 C   s   |   dd¡ d S )Nz	abcd.blahrD   r   r$   r   r   r   Útest_blah_ext‰  s    z"Test_hook_compressed.test_blah_extc                 C   s   |   dd¡ d S )Nzabcd.GzrL   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   s`   |   | j¡}zt ||¡}W |   |¡ n|   |¡ 0 |  | jjd¡ |  | jj||fi f¡ d S rß   )Úreplace_builtin_openrÿ   rÙ   r  rZ   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 r   )Úbuiltinsr   )Znew_open_funcr	  r   r   r   r    s    z)Test_hook_compressed.replace_builtin_openN)r   r   r    r×   rÝ   r  r  ÚunittestZ
skipUnlessr  r
  r  r  r  r  r  r  Ústaticmethodr  r   r   r   r   rþ   e  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 |t_n|t_0 |  |jd¡ |j\}	}
|  	|	d |¡ |  	|	d |¡ |  	|
 
d¡|¡ |  	|
 
d¡|¡ |  |
¡ d S )N©Úerrorsr<   r   Úencodingr  )rã   rÙ   r   rû   r  r   rZ   rü   rý   rå   Úpopr]   )r   r  r  rÃ   rÿ   r	  r+   r   Zopen_resultrw   rx   r   r   r   Útest¦  s$    
zTest_hook_encoded.testc                    sª   t tdƒ}| d¡ W d   ƒ n1 s*0    Y  ˆ  tt¡ ‡ fdd„}|ddgƒ ˆ  t¡ |ddgƒ W d   ƒ n1 s„0    Y  |dd	gƒ |d
dgƒ d S )NrŠ   s   €abcc                    sN   t tdtd| dd}t|ƒ}W d   ƒ n1 s40    Y  ˆ  ||¡ d S )NrÐ   zutf-8r  ©rF   r   r,   ©r   r   r   rY   rZ   )r  Úexpected_linesrn   r7   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   ƒ n1 s*0    Y  ˆ  tt¡ ‡ fdd„}|dg d¢ƒ ˆ  t¡ |dg d¢ƒ W d   ƒ n1 sˆ0    Y  ˆ  t¡ |dg d¢ƒ W d   ƒ n1 sÀ0    Y  ˆ  t¡ |d	g d
¢ƒ W d   ƒ n1 sø0    Y  d S )NrŠ   s   A
B
CD+IKw-c                    sJ   t t| tdƒd}t|ƒ}W d   ƒ n1 s00    Y  ˆ  ||¡ d S )Nzutf-7r  r  )r   r  rn   r7   r$   r   r   r   Õ  s    ÿ&z+Test_hook_encoded.test_modes.<locals>.checkrÐ   )r€   r   r   õ   Dâ‚¬ZrUr‹   r”   )r€   zB
zCr(  )	r   r   r   r   r¡   ZassertWarnsr‘   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 r   )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º   ra   rV   rÙ   rÒ   r  r   r  r  ÚImportErrorr  Úior   r   r   r   Úpathlibr   Ztest.supportr   r   r	   r
   r¡   r  r   r   r   r!   ZTestCaser;   rr   r{   rÏ   rØ   rá   rì   rð   rô   rõ   rö   r÷   rù   rú   rû   rþ   r  r*  r   Úmainr   r   r   r   Ú<module>   s^   


$S  a->>A
