o
    g                     @   st  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ddl	Z	zddl
Z
W n ey7   dZ
Y nw zddlZW n eyI   dZY nw ddlmZmZ ddlmZmZ ddlmZ ddlmZ ddlmZ ddlmZ dd	l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/d0 d0e	j Z0G d1d2 d2e	j Z1G d3d4 d4e	j Z2e3d5kr8e	4  dS dS )6z,
Tests for fileinput module.
Nick Mathewson
    N)BytesIOStringIO)	FileInputhook_encoded)Path)verbose)TESTFN)unlink)	os_helper)warnings_helper)support)mockc                   @   s   e Zd ZddddZdS )	BaseTestswmodec                C   sh   t  \}}| tj| d|v rd nd}t|||d}|| W d    |S 1 s-w   Y  |S )Nbutf-8encoding)tempfilemkstemp
addCleanupr
   r	   openwrite)selfZcontentr   fdnamer   f r   I/Users/admin/.pyenv/versions/3.10.0/lib/python3.10/test/test_fileinput.pywriteTmp,   s   
zBaseTests.writeTmpN)__name__
__module____qualname__r!   r   r   r   r    r   )   s    r   c                   @   sD   e Zd Zdd Zedd Zdd Zddd	ZdddZdd Z	dS )
LineReaderc                 C   s
   g | _ d S N
_linesreadr   r   r   r    __init__6      
zLineReader.__init__c                 C   s    z| j d d  W g | _ S g | _ w r&   r'   r)   r   r   r    	linesread9   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 )nextr0   r(   append)r   sizeliner   r   r    readlineD   s   zLineReader.readlinec                 C   s>   g }d}	 |   }|s|S || |t|7 }||kr|S q)Nr   T)r8   r5   len)r   Zhintlinesr6   r7   r   r   r    	readlinesI   s   
zLineReader.readlinesc                 C      d S r&   r   r)   r   r   r    closeU   s   zLineReader.closer&   )r9   )
r"   r#   r$   r*   propertyr,   r2   r8   r<   r>   r   r   r   r    r%   4   s    


r%   c                   @      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rGtd t||||fdd}t|}|	  | 
t|d | 
|d d | 
|d d | 
| d | 
| | trtd t||||fdd}d}|r|dkr| }|r|dks| 
| | | 
| d | 
| d | |  | |  trtd |  | 
| d | 
| d |	  trtd t||||dfdd}tj}	z*tdt_t|}| 
t|d | 
|d  d! | 
| d" |  W |	t_n|	t_w tr1td# t||||fdd}| 
| d$ | 
| d  |  | 
| d$ | 
| d  trftd% tj}
z&t||||fd
dd&}|D ]}|d d'  }t| qw|	  W |
t_n|
t_w t||||fdd}|D ]'}| 
|d' d( ||d d' }| |d  | 
t|d
|  q|	  d S ))Nr3   c                 s       | ]	}d |d  V  qdS )zLine %s of file 1
   Nr   .0ir   r   r    	<genexpr>[       z5BufferSizesTests.test_buffer_sizes.<locals>.<genexpr>   c                 s   rB   )zLine %s of file 2
rC   Nr   rD   r   r   r    rG   \   rH   
   c                 s   rB   )zLine %s of file 3
rC   Nr   rD   r   r   r    rG   ]   rH      c                 s   rB   )zLine %s of file 4
rC   Nr   rD   r   r   r    rG   ^   rH   rC   zLINE (\d+) OF FILE (\d+)z1. Simple iterationr   filesr         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rM   inplacer   r9   
)r!   joinrangerecompiler   printr   listr>   assertEqualr:   linenor1   r8   
filelinenoassertFalseisfirstlineisstdinnextfilesysstdinr   stdoutuppermatchassertNotEqualintgroup)r   t1t2t3t4patfir;   sZ	savestdinZ
savestdoutr7   mr   r   r    test_buffer_sizesY   s   



z"BufferSizesTests.test_buffer_sizesN)r"   r#   r$   rx   r   r   r   r    rA   X   s    rA   c                   @      e Zd Zdd Zdd ZdS )UnconditionallyRaisec                 C   s   || _ d| _d S NF)exception_typeinvoked)r   r|   r   r   r    r*      s   
zUnconditionallyRaise.__init__c                 O   s   d| _ |  r-   )r}   r|   r   argskwargsr   r   r    __call__   s   zUnconditionallyRaise.__call__Nr"   r#   r$   r*   r   r   r   r   r    rz      s    rz   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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d0d1 Zd2d3 Zd4d5 Zd6S )7FileInputTestsc                 C   s   |  d}|  d}|  d}|  d}t||||fdd}| }| |d | | d | | d | | | | }| | | | d | | d | | | |  d S )Nr3   zThe only line there is.
r   rL   rC   r   )	r!   r   r8   ra   rb   rc   r1   rd   r>   )r   rp   rq   rr   rs   ru   r7   r   r   r    test_zero_byte_files   s    




z#FileInputTests.test_zero_byte_filesc                 C   s`   |  d}|  d}t||fdd}t|}| |g d | | d | | d d S )NA
B
CD
E
Fr   rL   A
B
CzD
zE
F   rS   )r!   r   r`   ra   rc   rb   r   rp   rq   ru   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}| | d t|}| | d |  | | d t|}| | d d S )NzA
BzC
Dr   rL   r9   )r!   r   ra   filenor4   rm   rg   r`   )r   rp   rq   ru   r7   r   r   r    test_fileno   s   

zFileInputTests.test_filenoc                 C   s   zt ddd}| d W n	 ty   Y nw | jddd}tdtf t |d	dd
}W d    n1 s7w   Y  tdtf t|}W d    n1 sRw   Y  | |g d d S )Nr   r   )r   r   z-FileInput should reject invalid mode argument   A
B
CDwbr   r3   U)rM   r   r   )r   r   C
D)	r   fail
ValueErrorr!   r   Zcheck_warningsDeprecationWarningr`   ra   )r   ru   rp   r;   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    d S 1 s(w   Y  d S )Nz	sys.stdin   spam, bacon, sausage, and spamrU   rbrM   r   )r   Zpatchr   bufferr   r`   ra   )r   Zm_stdinru   r;   r   r   r    test_stdin_binary_mode   s   
"z%FileInputTests.test_stdin_binary_modec                 C   s\   t j}z&tdt _| tt jd tdgdd}t|}| |dg W |t _d S |t _w )Nr   r   rU   r   r   )rh   ri   r   rd   hasattrr   r`   ra   )r   Z
orig_stdinru   r;   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y   Y nw zt dd}| d W n	 ty.   Y nw G dd	 d	}| d
}| }t |g|d}|  W d    n1 sTw   Y  | |jd d S )NrC   c                 S   r=   r&   r   )r   rw   r   r   r    <lambda>      z7FileInputTests.test_file_opening_hook.<locals>.<lambda>)rY   r2   zGFileInput should raise if both inplace and openhook arguments are givenr2   z2FileInput should check openhook for being callablec                   @   ry   )z=FileInputTests.test_file_opening_hook.<locals>.CustomOpenHookc                 S   
   d| _ d S r{   )r}   r)   r   r   r    r*     r+   zFFileInputTests.test_file_opening_hook.<locals>.CustomOpenHook.__init__c                 _   s   d| _ t|ddiS )NTr   r   )r}   r   )r   r   Zkargsr   r   r    r     s   zFFileInputTests.test_file_opening_hook.<locals>.CustomOpenHook.__call__Nr   r   r   r   r    CustomOpenHook      r   rZ   zopenhook not invoked)r   r   r   r!   r8   
assertTruer}   )r   ru   r   tZcustom_open_hookr   r   r    test_file_opening_hook
  s&   


z%FileInputTests.test_file_opening_hookc              	   C   s2  t td}|d |dd  |d W d    n1 s!w   Y  | tt tttdd[}z| | d | | d	 | | d
 W n t	y]   | 
d Y nw | t	 t| W d    n1 srw   Y  | | d | | d W d    d S 1 sw   Y  d S )Nr   s   A
B
Cs
   123456789
i     asciirM   r2   r   r   r   zRead to end of filer3   )r   r   r   r   safe_unlinkr   r   ra   r8   UnicodeDecodeErrorr   assertRaisesr`   r   r   ru   r   r   r    test_readline%  s.   

"zFileInputTests.test_readlinec                 C   s   t td}|d W d    n1 sw   Y  | tt ttdd1}| | d | | d | | d | | d | | d W d    d S 1 sZw   Y  d S )	Nr   r   r   r   s   A
s   B
s   CD    )r   r   r   r   r   r   ra   r8   r   r   r   r    test_readline_binary_mode<  s   "z(FileInputTests.test_readline_binary_modec                 C   s   | j ddd}t|ddd}| }| |d tjd W d    n1 s)w   Y  t|d}| | d W d    d S 1 sGw   Y  d S )Ns   Initial text.r   r   r   T)r   rY   s	   New line.)	r!   r   r8   ra   rh   rj   r   r   read)r   Z	temp_fileZfobjr7   r   r   r   r    test_inplace_binary_write_modeI  s   "z-FileInputTests.test_inplace_binary_write_modec                 C   sX   dd }|  d}t|g|d}| }W d    n1 sw   Y  | |d d S )Nc                 S   
   t dS )N(I used to receive only filename and mode)ior   r1   r   r   r   r    old_hookT  r+   zFFileInputTests.test_file_hook_backward_compatibility.<locals>.old_hookrZ   r   r   )r!   r   r8   ra   )r   r   r   ru   resultr   r   r    %test_file_hook_backward_compatibilityS  s   

z4FileInputTests.test_file_hook_backward_compatibilityc                 C   s   |  d}|  d}t||fdd}t|}W d    n1 s!w   Y  | |g d | | d | | d | |jd d S )	Nr   r   r   rL   r   r   rS   r   )r!   r   r`   ra   rc   rb   _filesr   r   r   r    test_context_manager[  s   


z#FileInputTests.test_context_managerc                 C   sV   |  d}zt|dd}t1 sw   Y  W d S  ty*   | |jd Y d S w )Nr3   r   rL   r   )r!   r   OSErrorra   r   )r   rp   ru   r   r   r    test_close_on_exceptione  s   
z&FileInputTests.test_close_on_exceptionc                 C   s@   t g dd}| |jd W d    d S 1 sw   Y  d S )Nr   rL   )rU   )r   ra   r   )r   ru   r   r   r    .test_empty_files_list_specified_to_constructorm  s   "z=FileInputTests.test_empty_files_list_specified_to_constructor)categoryc                 C   sf   |  d}t|gdd}|d }| |d |d }| |d W d   dS 1 s,w   Y  dS )	zNTests invoking FileInput.__getitem__() with the current
           line numberline1
line2
r   rL   r   line1
rC   zline2
N)r!   r   ra   )r   r   ru   Zretval1Zretval2r   r   r    test__getitem__q  s   
"zFileInputTests.test__getitem__c              	   C   s   |  d}| td/ t|gd}| |d d W d    n1 s%w   Y  W d    d S W d    d S 1 s=w   Y  d S )Nr   zUse iterator protocol instead)rM   r   r   )r!   ZassertWarnsRegexr   r   ra   )r   r   ru   r   r   r    test__getitem___deprecation|  s   
"z*FileInputTests.test__getitem___deprecationc              	   C   ~   |  d}t|gdd!}| t}|d  W d   n1 s!w   Y  W d   n1 s0w   Y  | |jjd dS )zZTests invoking FileInput.__getitem__() with an index unequal to
           the line numberr   r   rL   rC   N)zaccessing lines out of order)r!   r   r   RuntimeErrorra   	exceptionr   r   r   ru   cmr   r   r    test__getitem__invalid_key     

z)FileInputTests.test__getitem__invalid_keyc              	   C   r   )zZTests invoking FileInput.__getitem__() with the line number but at
           end-of-inputr3   r   rL   r   N)zend of input reached)r!   r   r   
IndexErrorra   r   r   r   r   r   r    test__getitem__eof  r   z!FileInputTests.test__getitem__eofc                 C   s   t j}tt}z5| d}| t|d  t|gddd}t| |t _|	  W d   n1 s3w   Y  W |t _n|t _w | 
|jd dS )zTests invoking FileInput.nextfile() when the attempt to delete
           the backup file would raise OSError.  This error is expected to be
           silently ignoredrZ   z.bakTr   rX   Nzos.unlink() was not invoked)osr	   rz   r   r!   r   r   r   r4   rg   r   r}   )r   Zos_unlink_origZos_unlink_replacementr   ru   r   r   r    %test_nextfile_oserror_deleting_backup  s   

z4FileInputTests.test_nextfile_oserror_deleting_backupc                 C   |   t j}tt}z)| d}t|gddd}|t _|  W d   n1 s'w   Y  W |t _n|t _w | |jd dS )z{Tests invoking FileInput.readline() when os.fstat() raises OSError.
           This exception should be silently discarded.rZ   Tr   rX   Nos.fstat() was not invoked)	r   fstatrz   r   r!   r   r8   r   r}   )r   Zos_fstat_origZos_fstat_replacementr   ru   r   r   r    %test_readline_os_fstat_raises_OSError     

z4FileInputTests.test_readline_os_fstat_raises_OSErrorc                 C   r   )z{Tests invoking FileInput.readline() when os.chmod() raises OSError.
           This exception should be silently discarded.rZ   Tr   rX   Nr   )	r   chmodrz   r   r!   r   r8   r   r}   )r   Zos_chmod_origZos_chmod_replacementr   ru   r   r   r    %test_readline_os_chmod_raises_OSError  r   z4FileInputTests.test_readline_os_chmod_raises_OSErrorc              	   C   s   G dd dt }| }| d}t|gdd}|j}z||_| }W ||_n||_w W d    n1 s6w   Y  | |jd | |dd d S )	Nc                   @   ry   )zQFileInputTests.test_fileno_when_ValueError_raised.<locals>.FilenoRaisesValueErrorc                 S      t | t d S r&   )rz   r*   r   r)   r   r   r    r*        zZFileInputTests.test_fileno_when_ValueError_raised.<locals>.FilenoRaisesValueError.__init__c                 S   s   |    d S r&   )r   r)   r   r   r    r        zXFileInputTests.test_fileno_when_ValueError_raised.<locals>.FilenoRaisesValueError.filenoN)r"   r#   r$   r*   r   r   r   r   r    FilenoRaisesValueError  r   r   rZ   r   rL   z_file.fileno() was not invokedr9   zfileno() should return -1)rz   r!   r   _filer   r   r}   ra   )r   r   Z unconditionally_raise_ValueErrorr   ru   Zfile_backupr   r   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    d S 1 sow   Y  d S Nzline1
line2zline3
r   r   Zline2r3   )r%   r   r2   ra   r,   r8   r   srcru   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    d S 1 smw   Y  d S r   )r%   r   r2   ra   r,   r4   r   StopIterationr   r   r   r    test_iteration_buffering  s"   "z'FileInputTests.test_iteration_bufferingc                 C   s   t | d}t|dd.}| }| |d | | d | | d | | t	| W d    d S 1 s>w   Y  d S )NPathlib file.r   r   rC   )
r   r!   r   r8   ra   rb   rc   r1   r   fspath)r   rp   ru   r7   r   r   r    test_pathlib_file  s   "z FileInputTests.test_pathlib_filec                 C   s   t | d}t|ddd}| }| |d td|  W d    n1 s)w   Y  t|dd}| | d W d    d S 1 sHw   Y  d S )Nr   Tr   )rY   r   zModified %sr   zModified Pathlib file.
)r   r!   r   r8   ra   r_   r   r   )r   rp   ru   r7   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   r   Zignore_warningsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r      s<    







	
		r   c                   @   sf   e Zd ZdZdd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 testsNFr3   r)r   r2   r   errorsc                C   sJ   || _ || _|| _|| _|| _|| _|| _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>)rM   rY   backupr   r2   r   r   r   collectionsdefaultdictinvocation_countsreturn_values)r   rM   rY   r   r   r2   r   r   r   r   r    r*     s   
zMockFileInput.__init__c                 C   s   | j d  d7  < d S )Nr>   rC   )r   r)   r   r   r    r>   *  s   zMockFileInput.closec                 C      | j d  d7  < | jd S )Nrg   rC   r   r   r)   r   r   r    rg   -     
zMockFileInput.nextfilec                 C   r   )Nr1   rC   r   r)   r   r   r    r1   1  r   zMockFileInput.filenamec                 C   r   )Nrb   rC   r   r)   r   r   r    rb   5  r   zMockFileInput.linenoc                 C   r   )Nrc   rC   r   r)   r   r   r    rc   9  r   zMockFileInput.filelinenoc                 C   r   )Nr   rC   r   r)   r   r   r    r   =  r   zMockFileInput.filenoc                 C   r   )Nre   rC   r   r)   r   r   r    re   A  r   zMockFileInput.isfirstlinec                 C   r   )Nrf   rC   r   r)   r   r   r    rf   E  r   zMockFileInput.isstdin)NFr3   )r"   r#   r$   __doc__r*   r>   rg   r1   rb   rc   r   re   rf   r   r   r   r    r     s    r   c                   @   (   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    setUpM  s   
z$BaseFileInputGlobalMethodsTest.setUpc                 C   s   | j t_| jt_d S r&   )r   r   r   r   r   r)   r   r   r    tearDownR  s   z'BaseFileInputGlobalMethodsTest.tearDownc                 C   s2   |j | }| |d| t|j }| |d d S )NrC   )r   ra   r:   )r   Zmock_file_inputZmethod_nameZactual_countZactual_total_countr   r   r    assertExactlyOneInvocationV  s   

z9BaseFileInputGlobalMethodsTest.assertExactlyOneInvocationN)r"   r#   r$   r   r   r   r   r   r   r   r    r   I  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w   Y  | d|j	j
 | |tjd dS )zTests 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   inputra   r   r   assertIs)r   instancer   r   r   r    1test_state_is_not_None_and_state_file_is_not_Nonea  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_Nonen  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_Noney  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 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().)rM   rY   r   r   r2   r   r   rM   rY   r   r   r2   N)
r  r   r  r  r   rM   rY   r   r   r2   )r   rM   rY   r   r   r2   r   r   r   r   r    r    s   
z'Test_fileinput_input.do_test_call_inputN)r"   r#   r$   r   r  r  r	  r  r   r   r   r    r   ^  s    	r   c                   @       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   r>   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=Noner>   N)r   r   r   r>   r   r  r  r   r   r    test_state_is_not_None  s
   z+Test_fileinput_close.test_state_is_not_NoneNr"   r#   r$   r   r	  r  r   r   r   r    r    s    r  c                   @   r
  )Test_fileinput_nextfilez#Unit tests for fileinput.nextfile()c                 C   X   dt _| t}t   W d   n1 sw   Y  | d|jj | t j dS )zTests 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   rg   ra   r   r   r  r   r   r   r   r    r	       
z*Test_fileinput_nextfile.test_state_is_Nonec                 C   N   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.rg   N)r  r   r   r   r   rg   r   r  )r   Znextfile_retvalr  retvalr   r   r    r       
z.Test_fileinput_nextfile.test_state_is_not_NoneNr  r   r   r   r    r        
r  c                   @   r
  )Test_fileinput_filenamez#Unit tests for fileinput.filename()c                 C   r  )zTests 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   r1   ra   r   r   r  r  r   r   r    r	    r  z*Test_fileinput_filename.test_state_is_Nonec                 C   r  )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.r1   N)r  r   r   r   r   r1   r   r  )r   Zfilename_retvalr  r  r   r   r    r    r  z.Test_fileinput_filename.test_state_is_not_NoneNr  r   r   r   r    r    r  r  c                   @   r
  )Test_fileinput_linenoz!Unit tests for fileinput.lineno()c                 C   r  )zTests 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   rb   ra   r   r   r  r  r   r   r    r	    r  z(Test_fileinput_lineno.test_state_is_Nonec                 C   r  )zTests 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.rb   N)r  r   r   r   r   rb   r   r  )r   Zlineno_retvalr  r  r   r   r    r    r  z,Test_fileinput_lineno.test_state_is_not_NoneNr  r   r   r   r    r    r  r  c                   @   r
  )Test_fileinput_filelinenoz%Unit tests for fileinput.filelineno()c                 C   r  )zTests 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   rc   ra   r   r   r  r  r   r   r    r	    r  z,Test_fileinput_filelineno.test_state_is_Nonec                 C   r  )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.rc   N)r  r   r   r   r   rc   r   r  )r   Zfilelineno_retvalr  r  r   r   r    r    r  z0Test_fileinput_filelineno.test_state_is_not_NoneNr  r   r   r   r    r    r  r  c                   @   r
  )Test_fileinput_filenoz!Unit tests for fileinput.fileno()c                 C   r  )zTests 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   ra   r   r   r  r  r   r   r    r	    r  z(Test_fileinput_fileno.test_state_is_Nonec                 C   sT   t  }t }||jd< ||_|t_t }| |d | || | tj| dS )zTests 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    r  r  c                   @   r
  )Test_fileinput_isfirstlinez&Unit tests for fileinput.isfirstline()c                 C   r  )zTests 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   re   ra   r   r   r  r  r   r   r    r	  ;  r  z-Test_fileinput_isfirstline.test_state_is_Nonec                 C   r  )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.re   N)r  r   r   r   r   re   r   r  )r   Zisfirstline_retvalr  r  r   r   r    r  E  r  z1Test_fileinput_isfirstline.test_state_is_not_NoneNr  r   r   r   r    r  8  r  r  c                   @   r
  )Test_fileinput_isstdinz"Unit tests for fileinput.isstdin()c                 C   r  )zTests 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   rf   ra   r   r   r  r  r   r   r    r	  V  r  z)Test_fileinput_isstdin.test_state_is_Nonec                 C   r  )zTests 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.rf   N)r  r   r   r   r   rf   r   r  )r   Zisstdin_retvalr  r  r   r   r    r  `  r  z-Test_fileinput_isstdin.test_state_is_not_NoneNr  r   r   r   r    r  S  r  r  c                   @   ry   )InvocationRecorderc                 C   r   r   )invocation_countr)   r   r   r    r*   p  r+   zInvocationRecorder.__init__c                 O   s"   |  j d7  _ ||f| _tdS )NrC   s
   some bytes)r   last_invocationr   r   r~   r   r   r    r   s  s   

zInvocationRecorder.__call__Nr   r   r   r   r    r  n  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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   |  r   zTest_hook_compressed.setUpc                 C      |  dd d S )Nr3   rC   do_test_use_builtin_openr)   r   r   r    test_empty_string  r   z&Test_hook_compressed.test_empty_stringc                 C   r$  )NZabcd   r%  r)   r   r   r    test_no_ext  r   z Test_hook_compressed.test_no_extzRequires gzip and zlibc                 C   V   t j}| jt _ztdd}W |t _n|t _w | | jjd | | jjdi f d S )Ntest.gz3rC   )r+  r,  )gzipr   r#  r   hook_compressedra   r   r!  r   original_openr   r   r   r    test_gz_ext_fake     z%Test_hook_compressed.test_gz_ext_fakec                 C   sJ   t j}dd t _ztjdddd}W |t _n|t _w | t|dg d S )Nc                 S   r   )Ns   Ex-binary string)r   r   r   r   r   r    r     s   
 zATest_hook_compressed.test_gz_with_encoding_fake.<locals>.<lambda>r+  r,  r   r   zEx-binary string)r-  r   r   r.  ra   r`   r/  r   r   r    test_gz_with_encoding_fake  s   
z/Test_hook_compressed.test_gz_with_encoding_fakezRequires bz2c                 C   r*  )Ntest.bz24rC   )r4  r5  )bz2BZ2Filer#  r   r.  ra   r   r!  r/  r   r   r    test_bz2_ext_fake  r2  z&Test_hook_compressed.test_bz2_ext_fakec                 C   r$  )Nz	abcd.blah5r%  r)   r   r   r    test_blah_ext  r   z"Test_hook_compressed.test_blah_extc                 C   r$  )Nzabcd.Gz6r%  r)   r   r   r    test_gz_ext_builtin  r   z(Test_hook_compressed.test_gz_ext_builtinc                 C   r$  )Nzabcd.Bz27r%  r)   r   r   r    test_bz2_ext_builtin  r   z)Test_hook_compressed.test_bz2_ext_builtinc              	   C   sf   |  | j}zt||}W |  | n|  | w | | jjd | | jj||fdd df d S )NrC   locale)r   r   )replace_builtin_openr#  r   r.  ra   r   r!  )r   r1   r   r0  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_funcr0  r   r   r    r@    s   z)Test_hook_compressed.replace_builtin_openN)r"   r#   r$   r   r   r'  r)  unittestZ
skipUnlessr-  r1  r3  r6  r8  r:  r<  r>  r&  staticmethodr@  r   r   r   r    r"  y  s"    




	
r"  c                   @   r   )	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_w | |jd |j\}	}
| 	|	d | | 	|	d | | 	|

d| | 	|

d| | |
 d S )Nr   rC   r   r   r   )r  r   r   r  rA  r   ra   r   r!  r  poprd   )r   r   r   r   r#  r0  r1   r   Zopen_resultr   r   r   r   r    test  s$   
zTest_hook_encoded.testc                    s   t td}|d W d    n1 sw   Y   tt  fdd}|ddg  t |ddg W d    n1 sBw   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 sw   Y   || d S )Nr   r   rE  rM   r   r2   r   r   r   r`   ra   )r   expected_linesru   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   rK  r   r)   r    test_errors  s   zTest_hook_encoded.test_errorsc                    s  t td}|d W d    n1 sw   Y   tt  fdd}|dg d  t |dg d W d    n1 sDw   Y   t |dg d W d    n1 s`w   Y   t |d	g d
 W d    d S 1 s}w   Y  d S )Nr   s   A
B
CD+IKw-c                    sJ   t t| tdd}t|}W d    n1 sw   Y   || d S )Nzutf-7rH  rI  )r   rJ  ru   r;   r)   r   r    rK    s   
z+Test_hook_encoded.test_modes.<locals>.checkr   )r   r   r      D€ZrUr   r   )r   zB
zCrS  )	r   r   r   r   r   ZassertWarnsr   r   r   rQ  r   r)   r    
test_modes  s   "zTest_hook_encoded.test_modesN)r"   r#   r$   r   rG  rR  rT  r   r   r   r    rD    s
    rD  c                   @   r@   )MiscTestc                 C   r   r&   )r   Zcheck__all__r   r)   r   r   r    test_all  r   zMiscTest.test_allN)r"   r#   r$   rV  r   r   r   r    rU    s    rU  __main__)5r   r   r   rh   r]   r   r   rA  r   rB  r6  ImportErrorr-  r   r   r   r   pathlibr   Ztest.supportr   Ztest.support.os_helperr   r	   r   r
   r   rG  r   r   r   r%   ZTestCaserA   rz   r   r   r   r   r  r  r  r  r  r  r  r  r  r"  rD  rU  r"   mainr   r   r   r    <module>   sn    $S  i/?HA
