B
    u9a-•  ã               @   sL  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
rd   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 d/d0„ d0ejƒZ/e0d1krHe 1¡  dS )2é    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 )Nzline1
line2
)rI   r   zline1
r4   zline2
)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 )Nzline1
line2
)rI   r4   )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 )Nr'   )rI   r   )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 )NrV   z.bakT)rI   rU   z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 )NrV   T)rI   rU   z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 )NrV   T)rI   rU   z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               @   sV   e 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 )ÚMockFileInputNFr'   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   r    r1   ra   r%   r\   r]   ry   r_   r`   r   r   r   r   r³     s    
r³   c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚBaseFileInputGlobalMethodsTestc             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º   0  s   rº   c               @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚTest_fileinput_inputc          	   C   sV   t ƒ }tƒ |_|t_|  t¡}t ¡  W d Q R X |  d|j	j
¡ |  |tjd¡ d S )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 )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 )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 )	N)rI   rU   rµ   rJ   r   r&   zfileinput._staterI   rU   rµ   rJ   r   r&   )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Â   E  s   	rÂ   c               @   s   e Zd Zdd„ Zdd„ ZdS )ÚTest_fileinput_closec             C   s   d t _t  ¡  |  t j¡ d S )N)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 )Nr1   )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Ë   †  s   rË   c               @   s   e Zd Zdd„ Zdd„ ZdS )ÚTest_fileinput_nextfilec          	   C   sD   d t _|  t¡}t  ¡  W d Q R X |  d|jj¡ |  t j¡ d S )N)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 )Nra   )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Î   ™  s   
rÎ   c               @   s   e Zd Zdd„ Zdd„ ZdS )ÚTest_fileinput_filenamec          	   C   sD   d t _|  t¡}t  ¡  W d Q R X |  d|jj¡ |  t j¡ d S )N)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 )Nr%   )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Ð   ´  s   
rÐ   c               @   s   e Zd Zdd„ Zdd„ ZdS )ÚTest_fileinput_linenoc          	   C   sD   d t _|  t¡}t  ¡  W d Q R X |  d|jj¡ |  t j¡ d S )N)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 )Nr\   )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Ñ   Ï  s   
rÑ   c               @   s   e Zd Zdd„ Zdd„ ZdS )ÚTest_fileinput_filelinenoc          	   C   sD   d t _|  t¡}t  ¡  W d Q R X |  d|jj¡ |  t j¡ d S )N)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 )Nr]   )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Ò   ê  s   
rÒ   c               @   s   e Zd Zdd„ Zdd„ ZdS )ÚTest_fileinput_filenoc          	   C   sD   d t _|  t¡}t  ¡  W d Q R X |  d|jj¡ |  t j¡ d S )N)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 )Nry   )	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Ó     s   
rÓ   c               @   s   e Zd Zdd„ Zdd„ ZdS )ÚTest_fileinput_isfirstlinec          	   C   sD   d t _|  t¡}t  ¡  W d Q R X |  d|jj¡ |  t j¡ d S )N)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 )Nr_   )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Õ   !  s   
rÕ   c               @   s   e Zd Zdd„ Zdd„ ZdS )ÚTest_fileinput_isstdinc          	   C   sD   d t _|  t¡}t  ¡  W d Q R X |  d|jj¡ |  t j¡ d S )N)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 )Nr`   )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Ö   <  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               @   sx   e 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_compressedc             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²   Z
skipUnlessrß   râ   rã   rä   rå   ræ   rè   rÜ   Ústaticmethodré   r   r   r   r   rÚ   ^  s   rÚ   c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚTest_hook_encodedc             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ì   œ  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__)2rŸ   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
