o
    HXh                     @   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   =/opt/python-3.10.19/usr/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   )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   rC   )zLine %s of file 2
rD   Nr   rE   r   r   r    rH   \   rI   
   c                 s   rC   )zLine %s of file 3
rD   Nr   rE   r   r   r    rH   ]   rI      c                 s   rC   )zLine %s of file 4
rD   Nr   rE   r   r   r    rH   ^   rI   rD   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rN   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$   ry   r   r   r   r    rB   X   s    rB   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    r{      s    r{   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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d0d1 Zd2d3 Zd4d5 Zd6d7 Z d8S )9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   rM   rD   r   )	r!   r   r8   rb   rc   rd   r1   re   r?   )r   rq   rr   rs   rt   rv   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   rM   A
B
CzD
zE
F   rT   )r!   r   ra   rb   rd   rc   r   rq   rr   rv   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   rM   r9   )r!   r   rb   filenor4   rn   rh   ra   )r   rq   rr   rv   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)rN   r   r   )r   r   C
D)	r   fail
ValueErrorr!   r   Zcheck_warningsDeprecationWarningra   rb   )r   rv   rq   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 spamrV   rbrN   r   )r   Zpatchr   bufferr   ra   rb   )r   Zm_stdinrv   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   rV   r   r   )ri   rj   r   re   hasattrr   ra   rb   )r   Z
orig_stdinrv   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 )NrD   c                 S   r>   r&   r   )r   rx   r   r   r    <lambda>      z7FileInputTests.test_file_opening_hook.<locals>.<lambda>)rZ   r2   zGFileInput should raise if both inplace and openhook arguments are givenr2   z2FileInput should check openhook for being callablec                   @   rz   )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   r[   zopenhook not invoked)r   r   r   r!   r8   
assertTruer~   )r   rv   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rN   r2   r   r   r   zRead to end of filer3   )r   r   r   r   safe_unlinkr   r   rb   r8   UnicodeDecodeErrorr   assertRaisesra   r   r   rv   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   rb   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   rZ   s	   New line.)	r!   r   r8   rb   ri   rk   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   s   | j ddd}t|dddd}| }| |d td	 W d    n1 s(w   Y  t|d
}| | dd W d    d S 1 sIw   Y  d S )Ns   Initial text r   r   Tr   replace)rZ   r   errorsu   Initial text �u   New line r   s   
s
   New line ?)r!   r   r8   rb   r`   r   r   rstripr   r   r   r    test_inplace_encoding_errorsS  s   
"z+FileInputTests.test_inplace_encoding_errorsc                 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_hook^  r+   zFFileInputTests.test_file_hook_backward_compatibility.<locals>.old_hookr[   r   r   )r!   r   r8   rb   )r   r   r   rv   resultr   r   r    %test_file_hook_backward_compatibility]  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   rM   r   r   rT   r   )r!   r   ra   rb   rd   rc   _filesr   r   r   r    test_context_managere  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   rM   r   )r!   r   OSErrorrb   r   )r   rq   rv   r   r   r    test_close_on_exceptiono  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   rM   )rV   )r   rb   r   )r   rv   r   r   r    .test_empty_files_list_specified_to_constructorw  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   rM   r   line1
rD   zline2
N)r!   r   rb   )r   r   rv   Zretval1Zretval2r   r   r    test__getitem__{  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)rN   r   r   )r!   ZassertWarnsRegexr   r   rb   )r   r   rv   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   rM   rD   N)zaccessing lines out of order)r!   r   r   RuntimeErrorrb   	exceptionr   r   r   rv   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   rM   r   N)zend of input reached)r!   r   r   
IndexErrorrb   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 ignoredr[   z.bakTr   rY   Nzos.unlink() was not invoked)osr	   r{   r   r!   r   r   r   r4   rh   r   r~   )r   Zos_unlink_origZos_unlink_replacementr   rv   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.r[   Tr   rY   Nos.fstat() was not invoked)	r   fstatr{   r   r!   r   r8   r   r~   )r   Zos_fstat_origZos_fstat_replacementr   rv   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.r[   Tr   rY   Nr   )	r   chmodr{   r   r!   r   r8   r   r~   )r   Zos_chmod_origZos_chmod_replacementr   rv   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                   @   rz   )zQFileInputTests.test_fileno_when_ValueError_raised.<locals>.FilenoRaisesValueErrorc                 S      t | t d S r&   )r{   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   r[   r   rM   z_file.fileno() was not invokedr9   zfileno() should return -1)r{   r!   r   _filer   r   r~   rb   )r   r   Z unconditionally_raise_ValueErrorr   rv   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   rb   r,   r8   r   srcrv   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   rb   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   rD   )
r   r!   r   r8   rb   rc   rd   r1   r   fspath)r   rq   rv   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   )rZ   r   zModified %sr   zModified Pathlib file.
)r   r!   r   r8   rb   r`   r   r   )r   rq   rv   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   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   r   c                C   sJ   || _ || _|| _|| _|| _|| _|| _d | _t	dd | _
i | _d S )Nc                   S   s   dS r:   r   r   r   r   r    r   1  r   z(MockFileInput.__init__.<locals>.<lambda>)rN   rZ   backupr   r2   r   r   r   collectionsdefaultdictinvocation_countsreturn_values)r   rN   rZ   r   r   r2   r   r   r   r   r    r*   '  s   
zMockFileInput.__init__c                 C   s   | j d  d7  < d S )Nr?   rD   )r   r)   r   r   r    r?   4  s   zMockFileInput.closec                 C      | j d  d7  < | jd S )Nrh   rD   r   r   r)   r   r   r    rh   7     
zMockFileInput.nextfilec                 C   r   )Nr1   rD   r   r)   r   r   r    r1   ;  r   zMockFileInput.filenamec                 C   r   )Nrc   rD   r   r)   r   r   r    rc   ?  r   zMockFileInput.linenoc                 C   r   )Nrd   rD   r   r)   r   r   r    rd   C  r   zMockFileInput.filelinenoc                 C   r   )Nr   rD   r   r)   r   r   r    r   G  r   zMockFileInput.filenoc                 C   r   )Nrf   rD   r   r)   r   r   r    rf   K  r   zMockFileInput.isfirstlinec                 C   r   )Nrg   rD   r   r)   r   r   r    rg   O  r   zMockFileInput.isstdin)NFr3   )r"   r#   r$   __doc__r*   r?   rh   r1   rc   rd   r   rf   rg   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    setUpW  s   
z$BaseFileInputGlobalMethodsTest.setUpc                 C   s   | j t_| jt_d S r&   )r   r   r   r   r   r)   r   r   r    tearDown\  s   z'BaseFileInputGlobalMethodsTest.tearDownc                 C   s2   |j | }| |d| t|j }| |d d S )NrD   )r   rb   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    r   S  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   inputrb   r   r   assertIs)r   instancer   r   r   r    1test_state_is_not_None_and_state_file_is_not_Nonek  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_Nonex  s   zBTest_fileinput_input.test_state_is_not_None_and_state_file_is_Nonec                 C   s   dt _|   dS )a4  Tests invoking fileinput.input() when fileinput._state is None
           Expect it to create and return a new fileinput.FileInput object
           with all method parameters passed explicitly to the __init__()
           method; also ensure that fileinput._state is set to the returned
           instance.N)r   r   r
  r)   r   r   r    test_state_is_None  s   z'Test_fileinput_input.test_state_is_Nonec                 C   s   t  }t  }t  }t  }t  }t  }tj||||||d}| |tjd | ||jd | ||jd | ||jd | ||jd | ||j	d 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().)rN   rZ   r   r   r2   r   r  rN   rZ   r   r   r2   N)
r  r   r  r  r   rN   rZ   r   r   r2   )r   rN   rZ   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  h  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   rh   rb   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.rh   N)r  r   r   r   r   rh   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   rb   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   rc   rb   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.rc   N)r  r   r   r   r   rc   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   rd   rb   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.rd   N)r  r   r   r   r   rd   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   rb   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  3  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   rf   rb   r   r   r  r  r   r   r    r  E  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.rf   N)r  r   r   r   r   rf   r  r  )r   Zisfirstline_retvalr  r  r   r   r    r  O  r  z1Test_fileinput_isfirstline.test_state_is_not_NoneNr  r   r   r   r    r!  B  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   rg   rb   r   r   r  r  r   r   r    r  `  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.rg   N)r  r   r   r   r   rg   r  r  )r   Zisstdin_retvalr  r  r   r   r    r  j  r  z-Test_fileinput_isstdin.test_state_is_not_NoneNr  r   r   r   r    r"  ]  r  r"  c                   @   rz   )InvocationRecorderc                 C   r   r:   )invocation_countr)   r   r   r    r*   z  r+   zInvocationRecorder.__init__c                 O   s"   |  j d7  _ ||f| _tdS )NrD   s
   some bytes)r$  last_invocationr   r   r   r   r   r    r   }  s   

zInvocationRecorder.__call__Nr   r   r   r   r    r#  x  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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   r   do_test_use_builtin_open_textr)   r   r   r    test_empty_string  r   z&Test_hook_compressed.test_empty_stringc                 C   r(  Nabcdr   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.gzr   rD   )r0  r   )gzipr   r'  r   hook_compressedrb   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>r0  r   r   r   zEx-binary string)r1  r   r   r2  rb   ra   r3  r   r   r    test_gz_with_encoding_fake  s   
z/Test_hook_compressed.test_gz_with_encoding_fakezRequires bz2c                 C   r/  )Ntest.bz2r   rD   )r8  r   )bz2BZ2Filer'  r   r2  rb   r$  r%  r3  r   r   r    test_bz2_ext_fake  r6  z&Test_hook_compressed.test_bz2_ext_fakec                 C   r(  )Nz	abcd.blahr   do_test_use_builtin_open_binaryr)   r   r   r    test_blah_ext  r   z"Test_hook_compressed.test_blah_extc                 C   r(  )Nzabcd.Gzr   r<  r)   r   r   r    test_gz_ext_builtin  r   z(Test_hook_compressed.test_gz_ext_builtinc                 C   r(  )Nzabcd.Bz2r   r<  r)   r   r   r    test_bz2_ext_builtin  r   z)Test_hook_compressed.test_bz2_ext_builtinc                 C   r(  )Nr-  r   r<  r)   r   r   r    test_binary_mode_encoding  r   z.Test_hook_compressed.test_binary_mode_encodingc                 C   r(  r,  r)  r)   r   r   r    test_text_mode_encoding  r   z,Test_hook_compressed.test_text_mode_encodingc              	   C   sf   |  | j}zt||}W |  | n|  | w | | jjd | | jj||fd d df d S )NrD   r   r   replace_builtin_openr'  r   r2  rb   r$  r%  r   r1   r   r4  r   r   r   r    r=       
z4Test_hook_compressed.do_test_use_builtin_open_binaryc              	   C   sf   |  | j}zt||}W |  | n|  | w | | jjd | | jj||fdd df d S )NrD   localerC  rD  rF  r   r   r    r*    rG  z2Test_hook_compressed.do_test_use_builtin_open_textc                 C   s   t j}| t _|S r&   )builtinsr   )Znew_open_funcr4  r   r   r    rE    s   z)Test_hook_compressed.replace_builtin_openN)r"   r#   r$   r   r   r+  r.  unittestZ
skipUnlessr1  r5  r7  r9  r;  r>  r?  r@  rA  rB  r=  r*  staticmethodrE  r   r   r   r    r&    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   rD   r   r   r   )r  r   r   r#  rI  r   rb   r$  r%  r  popre   )r   r   r   r   r'  r4  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   rM  rN   r   r2   r   r   r   ra   rb   )r   expected_linesrv   r<   r)   r   r    check  s   

z,Test_hook_encoded.test_errors.<locals>.checkignoreabcstrictr   u   �abcbackslashreplacez\x80abc)r   r   r   r   r   r   r   r   r   rS  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-7rP  rQ  )r   rR  rv   r<   r)   r   r    rS    s   
z+Test_hook_encoded.test_modes.<locals>.checkr   )r   r   r      D€ZrUr   r   )r   zB
zCrZ  )	r   r   r   r   r   ZassertWarnsr   r   r   rX  r   r)   r    
test_modes  s   "zTest_hook_encoded.test_modesN)r"   r#   r$   r   rO  rY  r[  r   r   r   r    rL    s
    rL  c                   @   rA   )MiscTestc                 C   r   r&   )r   Zcheck__all__r   r)   r   r   r    test_all  r   zMiscTest.test_allN)r"   r#   r$   r]  r   r   r   r    r\    s    r\  __main__)5r   r   r   ri   r^   r   r   rI  r   rJ  r9  ImportErrorr1  r   r   r   r   pathlibr   Ztest.supportr   Ztest.support.os_helperr   r	   r   r
   r   rO  r   r   r   r%   ZTestCaserB   r{   r   r   r   r  r  r  r  r  r  r  r!  r"  r#  r&  rL  r\  r"   mainr   r   r   r    <module>   sn    $S  s/?YA
