a
    "hW                     @   sH  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	yb   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 d/d0 d0ejZ/e0d1krDe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   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   ;/opt/python-3.9.21/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.readlinec                 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rt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rtd |  | 
| d | 
| d |	  trt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rt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 s0    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 s0    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 s0    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 s0    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 s0    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 )Nline1
line2
rE   r   line1
r<   zline2
)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 )Nr   rE   r<   )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 )Nr-   rE   r   )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 )NrS   z.bakTrQ   z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 NrS   TrQ   z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 r   )	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 s0    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 s0    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 s0    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                   @   s^   e 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 )MockFileInputNFr-   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    r%   r9   r`   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d ZdS )BaseFileInputGlobalMethodsTestc                 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   :  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   sj   t  }t |_|t_| t}t  W d    n1 s<0    Y  | d|j	j
 | |tjd d S )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 r   )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 r   )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 )N)rF   rR   r   r   r,   r   rF   rR   r   r   r,   )
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   O  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 r   )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 )Nr9   )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     s   r   c                   @   s   e Zd Zdd Zdd ZdS )Test_fileinput_nextfilec                 C   sX   d t _| t}t   W d    n1 s.0    Y  | d|jj | t j d S N)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 )Nr`   )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d Zdd ZdS )Test_fileinput_filenamec                 C   sX   d t _| t}t   W d    n1 s.0    Y  | d|jj | t j d S r   )	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 )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_NoneNr   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   sX   d t _| t}t   W d    n1 s.0    Y  | d|jj | t j d S r   )	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 )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_NoneNr   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   sX   d t _| t}t   W d    n1 s.0    Y  | d|jj | t j d S r   )	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 )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_NoneNr   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   sX   d t _| t}t   W d    n1 s.0    Y  | d|jj | t j d S r   )	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 )Nr   )	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d Zdd ZdS )Test_fileinput_isfirstlinec                 C   sX   d t _| t}t   W d    n1 s.0    Y  | d|jj | t j d S r   )	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 )Nr^   )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d Zdd ZdS )Test_fileinput_isstdinc                 C   sX   d t _| t}t   W d    n1 s.0    Y  | d|jj | t j d S r   )	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 )Nr_   )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                   @   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 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  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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 |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 s0    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 s0    Y   t |dg d W d    n1 s0    Y   t |d	g d
 W d    n1 s0    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    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__)2r   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
