
    dafo                     `   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		 ddl
Z
	 ddlZ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 ddlmZ ddlmZ dd	lmZ dd
l	mZ  G d d      Z G d d      Z G d dee	j<                        Z G d d      Z  G d dee	j<                        Z! G d d      Z" G d de	j<                        Z# G d de#      Z$ G d de#      Z% G d de#      Z& G d d e#      Z' G d! d"e#      Z( G d# d$e#      Z) G d% d&e#      Z* G d' d(e#      Z+ G d) d*e#      Z, G d+ d,      Z- G d- d.e	j<                        Z. G d/ d0e	j<                        Z/ G d1 d2e	j<                        Z0e1d3k(  r e	jd                          yy# e$ r dZ
Y rw xY w# e$ r dZY {w xY w)4z,
Tests for fileinput module.
Nick Mathewson
    N)BytesIOStringIO)	FileInputhook_encoded)verbose)TESTFNFakePath)unlink)	os_helper)support)mockc                       e Zd ZdddZy)	BaseTestswmodec                    t        j                         \  }}| j                  t        j                  |       d|v rd nd}t        |||      5 }|j                  |       d d d        |S # 1 sw Y   |S xY w)Nbutf-8encoding)tempfilemkstemp
addCleanupr   r
   openwrite)selfcontentr   fdnamer   fs          ./root/Python-3.12.4/Lib/test/test_fileinput.pywriteTmpzBaseTests.writeTmp*   si    ##%D	(($/$;4G"dX. 	!GGG		s   A**A4N)__name__
__module____qualname__r#        r"   r   r   '   s     ), r(   r   c                   >    e Zd Zd Zed        Zd ZddZd	dZd Z	y)

LineReaderc                     g | _         y N
_linesreadr   s    r"   __init__zLineReader.__init__4   s	    r(   c                 H    	 | j                   d d  g | _         S # g | _         w xY wr,   r-   r/   s    r"   	linesreadzLineReader.linesread7   s"    	!??1% DObDOs    	!c                 D    t        |j                  d            | _        | S NT)iter
splitlinesit)r   filenamer   s      r"   openhookzLineReader.openhook>   s    x**401r(   Nc                 h    t        | j                  d      }| j                  j                  |       |S )N )nextr7   r.   append)r   sizelines      r"   readlinezLineReader.readlineB   s)    DGGR t$r(   c                     g }d}	 | j                         }|s|S |j                  |       |t        |      z  }||k\  r|S ;Nr   )r@   r=   len)r   hintlinesr>   r?   s        r"   	readlineszLineReader.readlinesG   sM    ==?DLLCIDt| r(   c                      y r,   r'   r/   s    r"   closezLineReader.closeS   s    r(   r,   ))
r$   r%   r&   r0   propertyr2   r9   r@   rF   rH   r'   r(   r"   r*   r*   2   s/     ! !

r(   r*   c                       e Zd Zd Zy)BufferSizesTestsc                    | j                  dj                  d t        d      D                    }| j                  dj                  d t        d      D                    }| j                  dj                  d t        d      D                    }| j                  dj                  d t        d	      D                    }t        j                  d
      }t
        rt        d       t        ||||fd      }t        |      }|j                          | j                  t        |      d       | j                  |d   d       | j                  |d   d       | j                  |j                         d       | j                  |j                         |       t
        rt        d       t        ||||fd      }d}|r|dk7  r|j                         }|r|dk7  r| j                  |j                         |       | j                  |j                         d       | j                  |j                         d       | j!                  |j#                                | j!                  |j%                                t
        rt        d       |j'                          | j                  |j                         d       | j                  |j                         d       |j                          t
        rt        d       t        ||||dfd      }t(        j*                  }		 t-        d      t(        _        t        |      }| j                  t        |      d       | j                  |d   d        | j                  |j                         d!       |j'                          |	t(        _        t
        rt        d"       t        ||||fd      }| j                  |j                         d#       | j                  |j                         d        |j'                          | j                  |j                         d#       | j                  |j                         d        t
        rt        d$       t(        j.                  }
	 t        ||||fd	d%      }|D ]   }|d d& j1                         }t        |       " |j                          |
t(        _        t        ||||fd      }|D ]u  }| j                  |d&   d'       |j3                  |d d&       }| j5                  |d        | j                  t7        |j9                  d	            |j                                w |j                          y # |	t(        _        w xY w# |
t(        _        w xY w)(Nr;   c              3   ,   K   | ]  }d |dz   z    yw)zLine %s of file 1
   Nr'   .0is     r"   	<genexpr>z5BufferSizesTests.test_buffer_sizes.<locals>.<genexpr>Y        "TQ#8AaC#@"T      c              3   ,   K   | ]  }d |dz   z    yw)zLine %s of file 2
rO   Nr'   rP   s     r"   rS   z5BufferSizesTests.test_buffer_sizes.<locals>.<genexpr>Z   rT   rU   
   c              3   ,   K   | ]  }d |dz   z    yw)zLine %s of file 3
rO   Nr'   rP   s     r"   rS   z5BufferSizesTests.test_buffer_sizes.<locals>.<genexpr>[        "SQ#8AaC#@"SrU      c              3   ,   K   | ]  }d |dz   z    yw)zLine %s of file 4
rO   Nr'   rP   s     r"   rS   z5BufferSizesTests.test_buffer_sizes.<locals>.<genexpr>\   rZ   rU   rO   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r^   inplacer   rI   
)r#   joinrangerecompiler   printr   listrH   assertEqualrC   linenor8   r@   
filelinenoassertFalseisfirstlineisstdinnextfilesysstdinr   stdoutuppermatchassertNotEqualintgroup)r   t1t2t3t4patfirE   s	savestdin
savestdoutr?   ms                r"   test_buffer_sizesz"BufferSizesTests.test_buffer_sizesW   sS   ]]277"T%PR)"TTU]]277"T%PR)"TTU]]277"S%PQ("SST]]277"S%PQ("SSTjj45'(b"b"-@R

UR(q#78r$89b)+'(b"b"-@A--A A--+b)!,)*&- 
(<=b)

*b"b"c2WEII		" !EFCIHESZ,U2Y(;<R[[]I6KKM!CI*+b"b"-@a(-
a(-,ZZ
	$"b"b!11wOB CRy(d HHJ#CJb"b"-@ 	?DT"Xt,		$s)$A4(S_bmmo>		?
 	
; "CI* $CJs   A?U* <AU: *U7:VN)r$   r%   r&   r   r'   r(   r"   rL   rL   V   s    Pr(   rL   c                       e Zd Zd Zd Zy)UnconditionallyRaisec                      || _         d| _        y NF)exception_typeinvoked)r   r   s     r"   r0   zUnconditionallyRaise.__init__   s    ,r(   c                 0    d| _         | j                         r4   )r   r   r   argskwargss      r"   __call__zUnconditionallyRaise.__call__   s    !!##r(   Nr$   r%   r&   r0   r   r'   r(   r"   r   r      s    $r(   r   c                       e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)FileInputTestsc                    | j                  d      }| j                  d      }| j                  d      }| j                  d      }t        ||||fd      }|j                         }| j                  |d       | j                  |j	                         d       | j                  |j                         d       | j                  |j                         |       |j                         }| j                  |       | j                  |j	                         d       | j                  |j                         d       | j                  |j                         |       |j                          y )Nr;   zThe only line there is.
r   r]   rO   r   )	r#   r   r@   rr   rs   rt   r8   ru   rH   )r   r   r   r   r   r   r?   s          r"   test_zero_byte_filesz#FileInputTests.test_zero_byte_files   s   ]]2]]2]]67]]2b"b"-@{{}:;a(!,+{{}a(!,+

r(   c                 $   | j                  d      }| j                  d      }t        ||fd      }t        |      }| j                  |g d       | j                  |j	                         d       | j                  |j                         d       y )NA
B
CD
E
Fr   r]   A
B
CzD
zE
F   rd   )r#   r   rq   rr   rt   rs   r   r   r   r   rE   s        r"   %test_files_that_dont_end_with_newlinez4FileInputTests.test_files_that_dont_end_with_newline   st    ]]9%]]9%b"X8R FG!,a(r(   c                    | j                  d      }| j                  d      }t        ||fd      }| j                  |j                         d       t	        |      }| j                  |j                         d       |j                          | j                  |j                         d       t        |      }| j                  |j                         d       y )NzA
BzC
Dr   r]   rI   )r#   r   rr   filenor<   r~   rx   rq   )r   r   r   r   r?   s        r"   test_filenozFileInputTests.test_fileno   s    ]]6"]]6"b"X8b)BxBIIK,
b)Bxb)r(   c                     dD ]G  }| j                  |      5  | j                  t              5  t        |       d d d        d d d        I y # 1 sw Y   xY w# 1 sw Y   `xY w)N)r   rUUr   )subTestassertRaises
ValueErrorr   )r   r   s     r"   test_invalid_opening_modez(FileInputTests.test_invalid_opening_mode   sd    $ 	)D4( )&&z2 )4()) )	)) )) )s!   AAAAAA#	c                     t        j                  d      5 }t        d      |_        t	        dgd      }t        |      }| j                  |dg       d d d        y # 1 sw Y   y xY w)Nz	sys.stdin   spam, bacon, sausage, and spamrf   rbr^   r   )r   patchr   bufferr   rq   rr   )r   m_stdinr   rE   s       r"   test_stdin_binary_modez%FileInputTests.test_stdin_binary_mode   sa    ZZ$ 	I$%FGGN#T2BHEU%F$GH		I 	I 	Is   =AA%c                 .   t         j                  }	 t        d      t         _        | j                  t	        t         j                  d             t        dgd      }t        |      }| j                  |dg       |t         _        y # |t         _        w xY w)Nr   r   rf   r   r   )ry   rz   r   ru   hasattrr   rq   rr   )r   
orig_stdinr   rE   s       r"   test_detached_stdin_binary_modez.FileInputTests.test_detached_stdin_binary_mode   sp    YY
	# ABCIWSYY9:#T2BHEU%F$GH"CI
CIs   A)B Bc                    	 t        dd       }| j                  d       	 t        d      }| j                  d        G d d      }| j                  d	      } |       }t        |g|      5 }|j	                          d d d        | j                  |j                  d
       y # t        $ r Y w xY w# t        $ r Y w xY w# 1 sw Y   DxY w)NrO   c                      y r,   r'   )r!   r   s     r"   <lambda>z7FileInputTests.test_file_opening_hook.<locals>.<lambda>      r(   )rj   r9   zGFileInput should raise if both inplace and openhook arguments are givenr9   z2FileInput should check openhook for being callablec                       e Zd Zd Zd Zy)=FileInputTests.test_file_opening_hook.<locals>.CustomOpenHookc                     d| _         y r   )r   r/   s    r"   r0   zFFileInputTests.test_file_opening_hook.<locals>.CustomOpenHook.__init__  s	    $r(   c                 &    d| _         t        |ddiS )NTr   r   )r   r   )r   r   kargss      r"   r   zFFileInputTests.test_file_opening_hook.<locals>.CustomOpenHook.__call__  s    #T4G44r(   Nr   r'   r(   r"   CustomOpenHookr     s    %5r(   r   rk   zopenhook not invoked)r   failr   r#   r@   
assertTruer   )r   r   r   tcustom_open_hooks        r"   test_file_opening_hookz%FileInputTests.test_file_opening_hook   s    	1/@ABII @ A	A&BIIJK	5 	5 MM$)+s%56 	"KKM	(002HI'  		
  			 	s.   B% B4 /C%	B10B14	C ?C Cc                 :   t        t        d      5 }|j                  d       |j                  ddz         |j                  d       d d d        | j                  t        t               t        t        t        d            5 }	 | j                  |j                         d       | j                  |j                         d	       | j                  |j                         d
       | j                  t              5  t        |       d d d        | j                  |j                         d       | j                  |j                         d       d d d        y # 1 sw Y   xY w# t        $ r | j                  d       Y w xY w# 1 sw Y   xY w# 1 sw Y   y xY w)Nwbs   A
B
Cs
   123456789
i     asciir^   r9   r   r   C
zRead to end of filer;   )r   r   r   r   safe_unlinkr   r   rr   r@   UnicodeDecodeErrorr   r   rq   r   r!   r   s      r"   test_readlinezFileInputTests.test_readline  sJ   &$ 	1GGN#GGNT)*GGG	 	V,V ,W 57 	0:<1  6  6  6 ""#56 R R[[]B/R[[]B/	0 	0	 	 & 1		/01 	0 	0sU   7EFA E%&F;FAFE"%F?FFFF	
FFc                 0   t        t        d      5 }|j                  d       d d d        | j                  t        t               t        t        d      5 }| j                  |j                         d       | j                  |j                         d       | j                  |j                         d       | j                  |j                         d       | j                  |j                         d       d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)	Nr   s   A
B
CDr   r   s   A
s   B
s   CDr(   )r   r   r   r   r   r   rr   r@   r   s      r"   test_readline_binary_modez(FileInputTests.test_readline_binary_mode1  s    &$ 	%1GGO$	%V,V$/ 	12R[[]F3R[[]H5R[[]G4R[[]C0R[[]C0	1 	1		% 	%	1 	1s   D B!D D	Dc                 t   | j                  dd      }t        |dd      5 }|j                         }| j                  |d       t        j
                  j                  d       d d d        t        |d      5 }| j                  |j                         d       d d d        y # 1 sw Y   ?xY w# 1 sw Y   y xY w)Ns   Initial text.r   r   r   T)r   rj   s	   New line.)	r#   r   r@   rr   ry   r{   r   r   readr   	temp_filefobjr?   r!   s        r"   test_inplace_binary_write_modez-FileInputTests.test_inplace_binary_write_mode>  s    MM"2M>	ytT: 	+d==?DT#34JJ\*		+
 )T" 	5aQVVX|4	5 	5	+ 	+
	5 	5s   AB"8!B."B+.B7c                 l   | j                  dd      }t        |ddd      5 }|j                         }| j                  |d       t	        d	       d d d        t        |d
      5 }| j                  |j                         j                  d      d       d d d        y # 1 sw Y   NxY w# 1 sw Y   y xY w)Ns   Initial text r   r   Tr   replace)rj   r   errorsu   Initial text �u   New line r   s   
s
   New line ?)r#   r   r@   rr   rp   r   r   rstripr   s        r"   test_inplace_encoding_errorsz+FileInputTests.test_inplace_encoding_errorsH  s    MM"6TMB	y$ '	; 	#>B==?DT#89/"		#
 )T" 	FaQVVX__W5}E	F 	F	# 	#
	F 	Fs   .B%0B*B'*B3c                     d }| j                  d      }t        |g|      5 }|j                         }d d d        | j                  d       y # 1 sw Y   xY w)Nc                 ,    t        j                  d      S )N(I used to receive only filename and mode)ior   r8   r   s     r"   old_hookzFFileInputTests.test_file_hook_backward_compatibility.<locals>.old_hookS  s    ;;IJJr(   rk   r   r   )r#   r   r@   rr   )r   r   r   r   results        r"   %test_file_hook_backward_compatibilityz4FileInputTests.test_file_hook_backward_compatibilityR  sU    	KMM$sX. 	#"[[]F	#!KL	# 	#s   AAc                    | j                  d      }| j                  d      }t        ||fd      5 }t        |      }d d d        | j                  g d       | j                  j	                         d       | j                  |j                         d       | j                  |j                  d       y # 1 sw Y   zxY w)	Nr   r   r   r]   r   r   rd   r'   )r#   r   rq   rr   rt   rs   _filesr   s        r"   test_context_managerz#FileInputTests.test_context_managerZ  s    ]]9%]]9%b"X8 	BHE	 FG!,a(B'	 	s   B77C c                     | j                  d      }	 t        |d      5 }t        # 1 sw Y   y xY w# t        $ r | j                  j                  d       Y y w xY w)Nr;   r   r]   r'   )r#   r   OSErrorrr   r   )r   r   r   s      r"   test_close_on_exceptionz&FileInputTests.test_close_on_exceptiond  sX    ]]2	,g6 "  	,RYY+	,s   3 '03 3 %AAc                     t        g d      5 }| j                  |j                  d       d d d        y # 1 sw Y   y xY w)Nr   r]   )rf   )r   rr   r   )r   r   s     r"   .test_empty_files_list_specified_to_constructorz=FileInputTests.test_empty_files_list_specified_to_constructorl  s7    R'2 	0bRYY/	0 	0 	0s   4=c                    t         j                  }t        t              }	 | j	                  d      }| j                  t        |dz          t        |gdd      5 }t        |       |t         _        |j                          ddd       |t         _        | j                  |j                  d       y# 1 sw Y   1xY w# |t         _        w xY w)zTests invoking FileInput.nextfile() when the attempt to delete
           the backup file would raise OSError.  This error is expected to be
           silently ignoredrk   z.bakTr   ri   Nzos.unlink() was not invoked)osr
   r   r   r#   r   r   r   r<   rx   r   r   )r   os_unlink_origos_unlink_replacementr   r   s        r"   %test_nextfile_oserror_deleting_backupz4FileInputTests.test_nextfile_oserror_deleting_backupp  s    
  4W =	'd#AOOKV4!dWE R1	
 'BI 	-555	7 
 'BIs#   9B= 'B1B= 1B:6B= =C
c                 R   t         j                  }t        t              }	 | j	                  d      }t        |gdd      5 }|t         _        |j                          ddd       |t         _        | j                  |j                  d       y# 1 sw Y   1xY w# |t         _        w xY w)z{Tests invoking FileInput.readline() when os.fstat() raises OSError.
           This exception should be silently discarded.rk   Tr   ri   Nos.fstat() was not invoked)	r   fstatr   r   r#   r   r@   r   r   )r   os_fstat_origos_fstat_replacementr   r   s        r"   %test_readline_os_fstat_raises_OSErrorz4FileInputTests.test_readline_os_fstat_raises_OSError       3G<	%d#A!dWE / %BH 	,444	6  %BH#    B BB BB B&c                 R   t         j                  }t        t              }	 | j	                  d      }t        |gdd      5 }|t         _        |j                          ddd       |t         _        | j                  |j                  d       y# 1 sw Y   1xY w# |t         _        w xY w)z{Tests invoking FileInput.readline() when os.chmod() raises OSError.
           This exception should be silently discarded.rk   Tr   ri   Nr   )	r   chmodr   r   r#   r   r@   r   r   )r   os_chmod_origos_chmod_replacementr   r   s        r"   %test_readline_os_chmod_raises_OSErrorz4FileInputTests.test_readline_os_chmod_raises_OSError  r   r   c                 f    G d dt               } |       }| j                  d      }t        |gd      5 }|j                  }	 ||_        |j	                         }||_        	 d d d        | j                  |j                  d       | j                  dd       y # ||_        w xY w# 1 sw Y   ExY w)	Nc                       e Zd Zd Zd Zy)QFileInputTests.test_fileno_when_ValueError_raised.<locals>.FilenoRaisesValueErrorc                 8    t         j                  | t               y r,   )r   r0   r   r/   s    r"   r0   zZFileInputTests.test_fileno_when_ValueError_raised.<locals>.FilenoRaisesValueError.__init__  s    $--dJ?r(   c                 $    | j                          y r,   )r   r/   s    r"   r   zXFileInputTests.test_fileno_when_ValueError_raised.<locals>.FilenoRaisesValueError.fileno  s    r(   N)r$   r%   r&   r0   r   r'   r(   r"   FilenoRaisesValueErrorr    s    @ r(   r  rk   r   r]   z_file.fileno() was not invokedrI   zfileno() should return -1)r   r#   r   _filer   r   r   rr   )r   r   unconditionally_raise_ValueErrorr   r   file_backupr   s          r"   "test_fileno_when_ValueError_raisedz1FileInputTests.test_fileno_when_ValueError_raised  s    	 %9 	  ,B+C(MM$aS73 	'r((K';&	' 	8@@8	: 	%@A '	' 	's#   B'BB'	B$$B''B0c                    t               }t        ddg|j                        5 }| j                  |j                  g        | j                  |j                         d       | j                  |j                  dg       | j                  |j                         d       | j                  |j                  dg       | j                  |j                         d       | j                  |j                  ddg       | j                  |j                         d       | j                  |j                  dg       | j                  |j                         d       | j                  |j                  g        d d d        y # 1 sw Y   y xY wNzline1
line2zline3
r   zline1
line2r;   )r*   r   r9   rr   r2   r@   r   srcr   s      r"   test_readline_bufferingz&FileInputTests.test_readline_buffering  s'   lni8 #. 	013S]]B/R[[]I6S]]YK8R[[]G4S]]WI6R[[]I6S]]RO<R[[]B/S]]RD1R[[]B/S]]B/	0 	0 	0s   EE;;Fc                    t               }t        ddg|j                        5 }| j                  |j                  g        | j                  t        |      d       | j                  |j                  dg       | j                  t        |      d       | j                  |j                  dg       | j                  t        |      d       | j                  |j                  ddg       | j                  t        t
        |       | j                  |j                  dg       | j                  t        t
        |       | j                  |j                  g        d d d        y # 1 sw Y   y xY wr  )r*   r   r9   rr   r2   r<   r   StopIterationr  s      r"   test_iteration_bufferingz'FileInputTests.test_iteration_buffering  s   lni8 #. 	013S]]B/T"Xy1S]]YK8T"Xw/S]]WI6T"Xy1S]]RO<mT26S]]RD1mT26S]]B/	0 	0 	0s   D5E""E+c                    t        | j                  d            }t        |d      5 }|j                         }| j	                  |d       | j	                  |j                         d       | j	                  |j                         d       | j	                  |j                         t        j                  |             d d d        y # 1 sw Y   y xY w)NPath-like file.r   r   rO   )
r	   r#   r   r@   rr   rs   rt   r8   r   fspath)r   r   r   r?   s       r"   test_pathlike_filez!FileInputTests.test_pathlike_file  s    dmm$567rG, 	;;;=DT#45RYY[!,R]]_a0R[[]BIIbM:	; 	; 	;s   BCCc                 b   t        | j                  d            }t        |dd      5 }|j                         }| j	                  |d       t        d|z         d d d        t        |d      5 }| j	                  |j                         d       d d d        y # 1 sw Y   @xY w# 1 sw Y   y xY w)Nr  Tr   )rj   r   zModified %sr   zModified Path-like file.
)r	   r#   r   r@   rr   rp   r   r   )r   r   r   r?   r!   s        r"   test_pathlike_file_inplacez)FileInputTests.test_pathlike_file_inplace  s    dmm$567r4': 	(b;;=DT#45-$&'	( "w' 	E1QVVX'CD	E 	E		( 	(	E 	Es   1B/!B%B"%B.N)r$   r%   r&   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r'   r(   r"   r   r      s{    ()(
*)I	#J60.15FM(,07*6$6$B.0 0 ;Er(   r   c                   T    e Zd ZdZdddddddZd Zd Zd Zd	 Zd
 Z	d Z
d Zd Zy)MockFileInputzDA class that mocks out fileinput.FileInput for use during unit testsNr)r   r9   r   r   c                    || _         || _        || _        || _        || _        || _        || _        d | _        t        j                  d       | _
        i | _        y )Nc                       yrB   r'   r'   r(   r"   r   z(MockFileInput.__init__.<locals>.<lambda>   r   r(   )r^   rj   backupr   r9   r   r   r  collectionsdefaultdictinvocation_countsreturn_values)r   r^   rj   r$  r   r9   r   r   s           r"   r0   zMockFileInput.__init__  sU    
	  
!,!8!8!Cr(   c                 2    | j                   dxx   dz  cc<   y )NrH   rO   )r'  r/   s    r"   rH   zMockFileInput.close  s    w'1,'r(   c                 N    | j                   dxx   dz  cc<   | j                  d   S )Nrx   rO   r'  r(  r/   s    r"   rx   zMockFileInput.nextfile  )    z*a/*!!*--r(   c                 N    | j                   dxx   dz  cc<   | j                  d   S )Nr8   rO   r+  r/   s    r"   r8   zMockFileInput.filename
  r,  r(   c                 N    | j                   dxx   dz  cc<   | j                  d   S )Nrs   rO   r+  r/   s    r"   rs   zMockFileInput.lineno  )    x(A-(!!(++r(   c                 N    | j                   dxx   dz  cc<   | j                  d   S )Nrt   rO   r+  r/   s    r"   rt   zMockFileInput.filelineno  s)    |,1,!!,//r(   c                 N    | j                   dxx   dz  cc<   | j                  d   S )Nr   rO   r+  r/   s    r"   r   zMockFileInput.fileno  r/  r(   c                 N    | j                   dxx   dz  cc<   | j                  d   S )Nrv   rO   r+  r/   s    r"   rv   zMockFileInput.isfirstline  s)    }-2-!!-00r(   c                 N    | j                   dxx   dz  cc<   | j                  d   S )Nrw   rO   r+  r/   s    r"   rw   zMockFileInput.isstdin  s)    y)Q.)!!),,r(   )NFr;   )r$   r%   r&   __doc__r0   rH   rx   r8   rs   rt   r   rv   rw   r'   r(   r"   r   r     s@    N D4 -..,0,1-r(   r   c                   "    e Zd ZdZd Zd Zd Zy)BaseFileInputGlobalMethodsTestzQBase class for unit tests for the global function of
       the fileinput module.c                 v    t         j                  | _        t         j                  | _        t
        t         _        y r,   )	fileinput_state_orig_stater   _orig_FileInputr   r/   s    r"   setUpz$BaseFileInputGlobalMethodsTest.setUp&  s&    $++(22+	r(   c                 X    | j                   t        _        | j                  t        _        y r,   )r;  r8  r   r:  r9  r/   s    r"   tearDownz'BaseFileInputGlobalMethodsTest.tearDown+  s    "22	++	r(   c                     |j                   |   }| j                  |d|       t        |j                         }| j                  |d       y )NrO   )r'  rr   rC   )r   mock_file_inputmethod_nameactual_countactual_total_counts        r"   assertExactlyOneInvocationz9BaseFileInputGlobalMethodsTest.assertExactlyOneInvocation/  sF    &88Eq+6 !B!BC+Q/r(   N)r$   r%   r&   r4  r<  r>  rD  r'   r(   r"   r6  r6  "  s     ,
,0r(   r6  c                   (    e Zd ZdZd Zd Zd Zd Zy)Test_fileinput_inputz Unit tests for fileinput.input()c                 V   t               }t               |_        |t        _        | j                  t              5 }t        j                          ddd       | j                  dj                  j                         | j                  |t        j                  d       y# 1 sw Y   QxY w)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  r8  r9  r   RuntimeErrorinputrr   	exceptionr   assertIs)r   instancecms      r"   1test_state_is_not_None_and_state_file_is_not_NonezFTest_fileinput_input.test_state_is_not_None_and_state_file_is_not_None:  s}    
 !?#	|, 	OO	4bll6G6GHh	 0 02DE	 	s   BB(c                 \    t               }d|_        |t        _        | j	                          y)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  r8  r9  do_test_call_inputr   rN  s     r"   -test_state_is_not_None_and_state_file_is_NonezBTest_fileinput_input.test_state_is_not_None_and_state_file_is_NoneG  s&     !?#	!r(   c                 :    dt         _        | j                          y)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)r8  r9  rR  r/   s    r"   test_state_is_Nonez'Test_fileinput_input.test_state_is_NoneR  s      	!r(   c                    t               }t               }t               }t               }t               }t               }t        j                  ||||||      }| j                  |t        j                  d       | j                  ||j
                  d       | j                  ||j                  d       | j                  ||j                  d       | j                  ||j                  d       | j                  ||j                  d       y)	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().)r^   rj   r$  r   r9   r   rH  r^   rj   r$  r   r9   N)
rI  r8  rK  rM  r9  r^   rj   r$  r   r9   )r   r^   rj   r$  r   r9   r   r   s           r"   rR  z'Test_fileinput_input.do_test_call_input[  s    
 (x88 ugf8= 	fi..0BC 	eV\\73gv~~y9ffmmX6dFKK0h<r(   N)r$   r%   r&   r4  rP  rT  rV  rR  r'   r(   r"   rF  rF  7  s    *F	""=r(   rF  c                       e Zd ZdZd Zd Zy)Test_fileinput_closez Unit tests for fileinput.close()c                     dt         _        t        j                          | j                  t         j                         y)zPTests that fileinput.close() does nothing if fileinput._state
           is NoneN)r8  r9  rH   assertIsNoner/   s    r"   rV  z'Test_fileinput_close.test_state_is_Noney  s*      	)**+r(   c                     t               }|t        _        t        j                          | j	                  |d       | j                  t        j                         y)z`Tests that fileinput.close() invokes close() on fileinput._state
           and sets _state=NonerH   N)r   r8  r9  rH   rD  r[  rS  s     r"   test_state_is_not_Nonez+Test_fileinput_close.test_state_is_not_None  sA     !?#	''':)**+r(   Nr$   r%   r&   r4  rV  r]  r'   r(   r"   rY  rY  v  s    *,,r(   rY  c                       e Zd ZdZd Zd Zy)Test_fileinput_nextfilez#Unit tests for fileinput.nextfile()c                     dt         _        | j                  t              5 }t        j                          ddd       | j                  dj                  j                         | j                  t         j                         y# 1 sw Y   OxY w)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())	r8  r9  r   rJ  rx   rr   rL  r   r[  r   rO  s     r"   rV  z*Test_fileinput_nextfile.test_state_is_None  l      	|, 	! 	!/1B1BC)**+	! 	!   BBc                    t               }t               }||j                  d<   |t        _        t        j
                         }| j                  |d       | j                  ||       | j                  t        j                  |       y)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.rx   N)rI  r   r(  r8  r9  rx   rD  rM  )r   nextfile_retvalrN  retvals       r"   r]  z.Test_fileinput_nextfile.test_state_is_not_None  j    
 !( ?-<z*#	##%''*=fo.i&&1r(   Nr^  r'   r(   r"   r`  r`        -,2r(   r`  c                       e Zd ZdZd Zd Zy)Test_fileinput_filenamez#Unit tests for fileinput.filename()c                     dt         _        | j                  t              5 }t        j                          ddd       | j                  dj                  j                         | j                  t         j                         y# 1 sw Y   OxY w)zTests fileinput.filename() when fileinput._state is None.
           Ensure that it raises RuntimeError with a meaningful error message
           and does not modify fileinput._stateNrb  )	r8  r9  r   rJ  r8   rr   rL  r   r[  rc  s     r"   rV  z*Test_fileinput_filename.test_state_is_None  rd  re  c                    t               }t               }||j                  d<   |t        _        t        j
                         }| j                  |d       | j                  ||       | j                  t        j                  |       y)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.r8   N)rI  r   r(  r8  r9  r8   rD  rM  )r   filename_retvalrN  rh  s       r"   r]  z.Test_fileinput_filename.test_state_is_not_None  ri  r(   Nr^  r'   r(   r"   rl  rl    rj  r(   rl  c                       e Zd ZdZd Zd Zy)Test_fileinput_linenoz!Unit tests for fileinput.lineno()c                     dt         _        | j                  t              5 }t        j                          ddd       | j                  dj                  j                         | j                  t         j                         y# 1 sw Y   OxY w)zTests fileinput.lineno() when fileinput._state is None.
           Ensure that it raises RuntimeError with a meaningful error message
           and does not modify fileinput._stateNrb  )	r8  r9  r   rJ  rs   rr   rL  r   r[  rc  s     r"   rV  z(Test_fileinput_lineno.test_state_is_None  l      	|, 		/1B1BC)**+	 	re  c                    t               }t               }||j                  d<   |t        _        t        j
                         }| j                  |d       | j                  ||       | j                  t        j                  |       y)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.rs   N)rI  r   r(  r8  r9  rs   rD  rM  )r   lineno_retvalrN  rh  s       r"   r]  z,Test_fileinput_lineno.test_state_is_not_None  sj    
  ?+8x(#	!!#''(;fm,i&&1r(   Nr^  r'   r(   r"   rq  rq    s    +,2r(   rq  c                       e Zd ZdZd Zd Zy)Test_fileinput_filelinenoz%Unit tests for fileinput.filelineno()c                     dt         _        | j                  t              5 }t        j                          ddd       | j                  dj                  j                         | j                  t         j                         y# 1 sw Y   OxY w)zTests fileinput.filelineno() when fileinput._state is None.
           Ensure that it raises RuntimeError with a meaningful error message
           and does not modify fileinput._stateNrb  )	r8  r9  r   rJ  rt   rr   rL  r   r[  rc  s     r"   rV  z,Test_fileinput_filelineno.test_state_is_None  sl      	|, 	#  "	#/1B1BC)**+	# 	#re  c                    t               }t               }||j                  d<   |t        _        t        j
                         }| j                  |d       | j                  ||       | j                  t        j                  |       y)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.rt   N)rI  r   r(  r8  r9  rt   rD  rM  )r   filelineno_retvalrN  rh  s       r"   r]  z0Test_fileinput_filelineno.test_state_is_not_None  sl    
 #H ?/@|,#	%%''',?f/0i&&1r(   Nr^  r'   r(   r"   rw  rw    s    /,2r(   rw  c                       e Zd ZdZd Zd Zy)Test_fileinput_filenoz!Unit tests for fileinput.fileno()c                     dt         _        | j                  t              5 }t        j                          ddd       | j                  dj                  j                         | j                  t         j                         y# 1 sw Y   OxY w)zTests fileinput.fileno() when fileinput._state is None.
           Ensure that it raises RuntimeError with a meaningful error message
           and does not modify fileinput._stateNrb  )	r8  r9  r   rJ  r   rr   rL  r   r[  rc  s     r"   rV  z(Test_fileinput_fileno.test_state_is_None  rs  re  c                    t               }t               }||j                  d<   ||_        |t        _        t	        j                         }| j                  |d       | j                  ||       | j                  t        j
                  |       y)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)	rI  r   r(  fileno_retvalr8  r9  r   rD  rM  )r   r  rN  rh  s       r"   r]  z,Test_fileinput_fileno.test_state_is_not_None  sr    
  ?+8x(!.#	!!#''(;fm,i&&1r(   Nr^  r'   r(   r"   r|  r|    s    +,2r(   r|  c                       e Zd ZdZd Zd Zy)Test_fileinput_isfirstlinez&Unit tests for fileinput.isfirstline()c                     dt         _        | j                  t              5 }t        j                          ddd       | j                  dj                  j                         | j                  t         j                         y# 1 sw Y   OxY w)zTests fileinput.isfirstline() when fileinput._state is None.
           Ensure that it raises RuntimeError with a meaningful error message
           and does not modify fileinput._stateNrb  )	r8  r9  r   rJ  rv   rr   rL  r   r[  rc  s     r"   rV  z-Test_fileinput_isfirstline.test_state_is_None  sl      	|, 	$!!#	$/1B1BC)**+	$ 	$re  c                    t               }t               }||j                  d<   |t        _        t        j
                         }| j                  |d       | j                  ||       | j                  t        j                  |       y)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.rv   N)rI  r   r(  r8  r9  rv   rD  rM  )r   isfirstline_retvalrN  rh  s       r"   r]  z1Test_fileinput_isfirstline.test_state_is_not_None  sl    
 $X ?0B}-#	&&(''-@f01i&&1r(   Nr^  r'   r(   r"   r  r    s    0,2r(   r  c                       e Zd ZdZd Zd Zy)Test_fileinput_isstdinz"Unit tests for fileinput.isstdin()c                     dt         _        | j                  t              5 }t        j                          ddd       | j                  dj                  j                         | j                  t         j                         y# 1 sw Y   OxY w)zTests fileinput.isstdin() when fileinput._state is None.
           Ensure that it raises RuntimeError with a meaningful error message
           and does not modify fileinput._stateNrb  )	r8  r9  r   rJ  rw   rr   rL  r   r[  rc  s     r"   rV  z)Test_fileinput_isstdin.test_state_is_None/  sl      	|, 	 	 /1B1BC)**+	  	 re  c                    t               }t               }||j                  d<   |t        _        t        j
                         }| j                  |d       | j                  ||       | j                  t        j                  |       y)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.rw   N)rI  r   r(  r8  r9  rw   rD  rM  )r   isstdin_retvalrN  rh  s       r"   r]  z-Test_fileinput_isstdin.test_state_is_not_None9  sj    
   ?,:y)#	""$'')<fn-i&&1r(   Nr^  r'   r(   r"   r  r  ,  s    ,,2r(   r  c                       e Zd Zd Zd Zy)InvocationRecorderc                     d| _         y rB   )invocation_countr/   s    r"   r0   zInvocationRecorder.__init__I  s
     !r(   c                 h    | xj                   dz  c_         ||f| _        t        j                  d      S )NrO   s
   some bytes)r  last_invocationr   r   r   s      r"   r   zInvocationRecorder.__call__L  s.    " $f~zz-((r(   Nr   r'   r(   r"   r  r  G  s    ")r(   r  c                       e Zd ZdZd Zd Zd Z ej                  e	d      d        Z
 ej                  e	d      d        Z ej                  ed      d	        Zd
 Zd Zd Zd Zd Zd Zd Zed        Zy)Test_hook_compressedz*Unit tests for fileinput.hook_compressed()c                 "    t               | _        y r,   )r  	fake_openr/   s    r"   r<  zTest_hook_compressed.setUpU  s    +-r(   c                 (    | j                  dd       y )Nr;   r!  do_test_use_builtin_open_textr/   s    r"   test_empty_stringz&Test_hook_compressed.test_empty_stringX  s    **2s3r(   c                 (    | j                  dd       y Nabcdr!  r  r/   s    r"   test_no_extz Test_hook_compressed.test_no_ext[      **637r(   zRequires gzip and zlibc                 N   t         j                  }| j                  t         _        	 t        j                  dd      }|t         _        | j                  | j                  j                  d       | j                  | j                  j                  di f       y # |t         _        w xY w)Ntest.gzr!  rO   )r  r!  )gzipr   r  r8  hook_compressedrr   r  r  r   original_openr   s      r"   test_gz_ext_fakez%Test_hook_compressed.test_gz_ext_fake^  sy    		NN		&..y#>F%DI88!<77:JB9OP &DI   B B$c                     t         j                  }d t         _        	 t        j                  ddd      }|t         _        | j	                  t        |      dg       y # |t         _        w xY w)Nc                 ,    t        j                  d      S )Ns   Ex-binary string)r   r   r   s     r"   r   zATest_hook_compressed.test_gz_with_encoding_fake.<locals>.<lambda>m  s    2::6I+J r(   r  r!  r   r   zEx-binary string)r  r   r8  r  rr   rq   r  s      r"   test_gz_with_encoding_fakez/Test_hook_compressed.test_gz_with_encoding_fakej  sU    		J		&..y#PF%DIf(:';< &DIs   A A+zRequires bz2c                 N   t         j                  }| j                  t         _        	 t        j                  dd      }|t         _        | j                  | j                  j                  d       | j                  | j                  j                  di f       y # |t         _        w xY w)Ntest.bz2r!  rO   )r  r!  )bz2BZ2Filer  r8  r  rr   r  r  r  s      r"   test_bz2_ext_fakez&Test_hook_compressed.test_bz2_ext_faket  sy    nn	(..z3?F'CK88!<77:KR9PQ (CKr  c                 (    | j                  dd       y )Nz	abcd.blahr   do_test_use_builtin_open_binaryr/   s    r"   test_blah_extz"Test_hook_compressed.test_blah_ext  s    ,,[$?r(   c                 (    | j                  dd       y )Nzabcd.Gzr   r  r/   s    r"   test_gz_ext_builtinz(Test_hook_compressed.test_gz_ext_builtin  s    ,,Y=r(   c                 (    | j                  dd       y )Nzabcd.Bz2r   r  r/   s    r"   test_bz2_ext_builtinz)Test_hook_compressed.test_bz2_ext_builtin  s    ,,Z>r(   c                 (    | j                  dd       y )Nr  r   r  r/   s    r"   test_binary_mode_encodingz.Test_hook_compressed.test_binary_mode_encoding  s    ,,VT:r(   c                 (    | j                  dd       y r  r  r/   s    r"   test_text_mode_encodingz,Test_hook_compressed.test_text_mode_encoding  r  r(   c                 \   | j                  | j                        }	 t        j                  ||      }| j                  |       | j	                  | j                  j
                  d       | j	                  | j                  j                  ||fd d df       y # | j                  |       w xY w)NrO   r   r   replace_builtin_openr  r8  r  rr   r  r  r   r8   r   r  r   s        r"   r  z4Test_hook_compressed.do_test_use_builtin_open_binary  s    11$..A	5..x>F%%m488!<77#T*,NO	Q %%m4   B B+c                 \   | j                  | j                        }	 t        j                  ||      }| j                  |       | j	                  | j                  j
                  d       | j	                  | j                  j                  ||fdd df       y # | j                  |       w xY w)NrO   localer  r  r  s        r"   r  z2Test_hook_compressed.do_test_use_builtin_open_text  s    11$..A	5..x>F%%m488!<77#T*T,RS	U %%m4r  c                 <    t         j                  }| t         _        |S r,   )builtinsr   )new_open_funcr  s     r"   r  z)Test_hook_compressed.replace_builtin_open  s     %r(   N)r$   r%   r&   r4  r<  r  r  unittest
skipUnlessr  r  r  r  r  r  r  r  r  r  r  r  staticmethodr  r'   r(   r"   r  r  R  s    4.48 X78	Q 9	Q X78= 9= Xn-	R .	R@>?;8	Q	U  r(   r  c                   "    e Zd ZdZd Zd Zd Zy)Test_hook_encodedz'Unit tests for fileinput.hook_encoded()c                 f   t               }t               }t        j                  ||      }t               }t        j
                  }|t        _        	 t               }t               } |||      }|t        _        | j                  |j                  d       |j                  \  }	}
| j                  |	d   |       | j                  |	d   |       | j                  |
j                  d      |       | j                  |
j                  d      |       | j                  |
       y # |t        _        w xY w)Nr   rO   r   r   r   )rI  r8  r   r  r  r   rr   r  r  rM  popru   )r   r   r   r   r  r  r8   r   open_resultr   r   s              r"   testzTest_hook_encoded.test  s    8''@&(	 !	*xH8D 40K)HM33Q7 00fd1gx(d1gt$fjj,h7fjj*F3  *HMs   D# #D0c                 T    t        t        d      5 }|j                  d       d d d         j                  t        t                fd} |ddg        j                  t              5   |ddg       d d d         |ddg        |d	d
g       y # 1 sw Y   oxY w# 1 sw Y   *xY w)Nr   s   abcc                     t        t        dt        d|             5 }t        |      }d d d        j	                  |       y # 1 sw Y   xY w)Nr!  r   r  r^   r   r9   r   r   r   rq   rr   )r   expected_linesr   rE   r   s       r"   checkz,Test_hook_encoded.test_errors.<locals>.check  sM    c$0$HJ !MOR! UN3! !s   AAignoreabcstrictr   u   �abcbackslashreplacez\x80abc)r   r   r   r   r   r   r   r   r!   r  s   `  r"   test_errorszTest_hook_encoded.test_errors  s    &$ 	 1GGJ	 V,	4 	h 12 	%(UG$	%i+' :,/	  	 	% 	%s   B*BBB'c                 0    t        t        d      5 }|j                  d       d d d         j                  t        t                fd} |dg d        j                  t              5   |dg d       d d d        y # 1 sw Y   ]xY w# 1 sw Y   y xY w)Nr   s   A
B
CD+IKw-c                     t        t        | t        d            5 }t        |      }d d d        j	                  |       y # 1 sw Y   xY w)Nzutf-7r  r  )r   r  r   rE   r   s       r"   r  z+Test_hook_encoded.test_modes.<locals>.check  sJ    d$0$9; !>@R! UN3! !s   AAr!  )r   r   r      D€r   )r   zB
zCr  )r   r   r   r   r   r   r   r  s   `  r"   
test_modeszTest_hook_encoded.test_modes  s    &$ 	*1GG()	* 	V,	4 	c34z* 	<$:;	< 	<	* 	*	< 	<s   B +B B	BN)r$   r%   r&   r4  r  r  r  r'   r(   r"   r  r    s    1!00"<r(   r  c                       e Zd Zd Zy)MiscTestc                 8    t        j                  | t               y r,   )r   check__all__r8  r/   s    r"   test_allzMiscTest.test_all  s    T9-r(   N)r$   r%   r&   r  r'   r(   r"   r  r    s    .r(   r  __main__)3r4  r   r   ry   rn   r8  r%  r  r   r  r  ImportErrorr  r   r   r   r   test.supportr   test.support.os_helperr   r	   r
   r   r   r  r   r   r   r*   TestCaserL   r   r   r   r6  rF  rY  r`  rl  rq  rw  r|  r  r  r  r  r  r  r$   mainr'   r(   r"   <module>r     s   
 	 
 	      ! -   3 8 "  	 	" "HQy("3"3 Qf$ $EY 1 1 ED
-- --^0X%6%6 0*==9 ==~,9 ,&2< 262< 262: 262 > 262: 282!? 262; 26) )W8,, Wr:<)) :<z.x   . zHMMO {  
C  Ds"   F F" FF"F-,F-