
    ;e/                        d dl Z d dlZd dlZd dlmZ d dlmZ d dlZd dl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
          Z G d deej                  Z G d deej                  Z G d d          Z G d deej                  Z G d deej                  Zedk    r ej                     dS dS )    N)array)proxy)
gc_collect)TESTFN)	os_helper)warnings_helper)UserListc                   \    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S )AutoFileTestsc                 F    |                      t          d          | _        d S )Nwb)openr   fselfs    %/usr/lib/python3.11/test/test_file.pysetUpzAutoFileTests.setUp   s    64((    c                 x    | j         r| j                                          t          j        t                     d S N)r   closer   unlinkr   r   s    r   tearDownzAutoFileTests.tearDown   s2    6 	FLLNNN     r   c                 v   t          | j                  }|                    d           |                     | j                                        |                                           | j                                         d | _        t                       |                     t          t          |d           d S )Ns
   teststringtell)
r   r   writeassertEqualr   r   r   assertRaisesReferenceErrorgetattr)r   ps     r   testWeakRefszAutoFileTests.testWeakRefs   s    $&MM	111.'1f=====r   c                 >    | j         }|j         |j         |j         d S r   )r   namemodeclosedr   r   s     r   testAttributeszAutoFileTests.testAttributes%   s"    F			r   c                 `   | j                             d           | j                                          t          dd          }|                     t
          d          | _         | j                             |          }|                     d|                                d |                    d S )N   12b
   xxxxxxxxxxrb)	r   r   r   r   r   r   readintor   tobytes)r   ans      r   testReadintozAutoFileTests.testReadinto,   s    U#w64((FOOA		BQB00000r   c                    t          dd          }| j                                         |                     t          d          | _        t          | j        d          r(|                     t          | j        j        |           d S d S )Nr+   r,   zutf-8)encodingr.   )	r   r   r   r   r   hasattrr   	TypeErrorr.   )r   r0   s     r   testReadinto_textzAutoFileTests.testReadinto_text5   sw    #w6G4446:&& 	=i!<<<<<	= 	=r   c                 ,   t          ddg          }| j                            |           | j                                         |                     t
          d          | _        | j                                        }|                     |d           d S )N   1   2r-   r*   )r	   r   
writelinesr   r   r   readr   )r   lbufs      r   testWritelinesUserListz$AutoFileTests.testWritelinesUserList=   sx    dD\""!64((fkkmme$$$$$r   c                 V    |                      t          | j        j        g d           d S N)         r   r6   r   r;   r   s    r   testWritelinesIntegersz$AutoFileTests.testWritelinesIntegersF   s)    )TV%6			BBBBBr   c                 t    t          g d          }|                     t          | j        j        |           d S rA   )r	   r   r6   r   r;   )r   r=   s     r   testWritelinesIntegersUserListz,AutoFileTests.testWritelinesIntegersUserListJ   s6    WWW)TV%6:::::r   c                      G d d          }|                      t          | j        j         |             |            g           d S )Nc                       e Zd ZdS )8AutoFileTests.testWritelinesNonString.<locals>.NonStringN)__name__
__module____qualname__ r   r   	NonStringrK   Q   s        Dr   rP   rE   )r   rP   s     r   testWritelinesNonStringz%AutoFileTests.testWritelinesNonStringO   sk    	 	 	 	 	 	 	 	 	)TV%6$9;;		4	6 	6 	6 	6 	6r   c                    | j         }|                     |j        t                     |                     |                                           |                     |j                   t          |d          r(|                     t          t          f|j        d           |                                 |                     |j                   d S )Nr.    )r   r   r$   r   assertFalseisattyr&   r5   r   OSErrorr6   r.   r   
assertTruer'   s     r   
testErrorszAutoFileTests.testErrorsW   s    F((($$$"""1j!! 	Dw	2AJCCC				!!!!!r   c                    ddddddddd	d
ddg ffdg}|                     d           | j                            d d d            |                     | j        j                   |D ].\  }}t          | j        |          } | j        t          |g|R   /|                     | j                            d d d           d            	 ddz   d S #  |                      | j        j        t          j
                     d            Y d S xY w)N)filenorO   )flushrO   )rU   rO   )__next__rO   r<   rO   )r   )r   readlinerO   	readlinesrO   )seek)r   )r   rO   r;   )__iter__rO   )truncaterO   rB   r   )appendr   __exit__rW   r&   r    r   
ValueErrorr   sysexc_info)r   methods
methodnameargsmethods        r   testMethodszAutoFileTests.testMethodsb   s8   ! !#$#$!$ 2%(# 	'((( 	dD)))&&& ' 	9 	9JTVZ00FDj&8488888 	tT::DAAA	EaCCCC	E_TV_clnn=tDDDDDDs   C 6Dc                 P    |                      t          | j        j                   d S r   )r   rV   r   r<   r   s    r   testReadWhenWritingz!AutoFileTests.testReadWhenWriting   s"    '46;/////r   N)rL   rM   rN   r   r   r"   r(   r2   r7   r?   rF   rH   rQ   rX   rn   rp   rO   r   r   r   r      s        ) ) )! ! !
> > >  1 1 1= = =% % %C C C; ; ;
6 6 6	" 	" 	" E  E  ED0 0 0 0 0r   r   c                       e Zd Zej        ZdS )CAutoFileTestsNrL   rM   rN   ior   rO   r   r   rr   rr              7DDDr   rr   c                   .    e Zd Z eej                  ZdS )PyAutoFileTestsNrL   rM   rN   staticmethodpyior   rO   r   r   rw   rw              <	""DDDr   rw   c                   >    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	S )
OtherFileTestsc                 8    t          j        t                     d S r   )r   r   r   r   s    r   r   zOtherFileTests.tearDown   s         r   c                    |                      t          d                                           dD ]Z}	 |                      t          |          }|                                 |                     d|z             K# t          $ r Y Ww xY wd S )Nr   )rS   aUzwU+zU+z+UzrU+z%r is an invalid file mode)r   r   r   failrg   )r   r%   r   s      r   testModeStringszOtherFileTests.testModeStrings   s    		&$%%'''8 	? 	?D?IIfd++ 					6=>>>>	    	? 	?s   A;;
BBc                    t           j        dk    r|                     d           t           j                                        s|                     d           |                     t          t          f          5  t           j                            d           d d d            n# 1 swxY w Y   |                     t          t          f          5  t           j        	                                 d d d            d S # 1 swxY w Y   d S )Nosf1V5zE sys.stdin.seek(-1) may crash the interpreter on OSF1. Test manually.z stdin must be a TTY in this test)
rh   platformskipTeststdinrU   r   IOErrorrg   rb   rd   r   s    r   	testStdinzOtherFileTests.testStdin   sh   <8##MM"# # # y!! 	>MM<===455 	 	INN2	 	 	 	 	 	 	 	 	 	 	 	 	 	 	455 	! 	!I   	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	!s$   : B&&B*-B*C>>DDc                 l   d}	 |                      t          |          }|                                 |                     d|z             d S # t          $ r\}|j        d         dk    r:t          |          }t          |v s||vr$|                     d|z             Y d }~d S Y d }~d S Y d }~d S d }~ww xY w)Nqwertyzno error for invalid mode: %sr   z&bad error message for invalid mode: %s)r   r   r   r   rg   rl   str)r   bad_moder   msgss        r   testBadModeArgumentz"OtherFileTests.testBadModeArgument   s    	B		&(++A GGIIIII5@AAAAA  	L 	L 	Lx{aHHQ;;(!"3"3IIFJKKKKKKKKK  "3"3"3"3"3"3	Ls   A 
B3AB..B3c                    	 |                      t          d|          }|                    t          |                              d                     |                                 |                                 |                      t          d|          }t          |                                                    d                    }|                                 |                                 n># t          $ r1}| 
                    d|t          |          fz             Y d }~nd }~ww xY w|                     ||           d S )Nr   asciir-   z error setting buffer size %d: %s)r   r   r   r   encoder   intr<   decoderV   r   r   )r   r   r   dr   s        r   _checkBufferSizezOtherFileTests._checkBufferSize   s   
	J		&$**AGGCFFMM'**+++GGIIIGGIII		&$**AAFFHHOOG,,--AGGIIIGGIIII 	J 	J 	JII8As3xx=HIIIIIIII	JAs   C1C4 4
D/>'D**D/c                 4   dD ]K}t          j        | dt                    5  |                     |           d d d            n# 1 swxY w Y   L|                     t          d          5  |                     d           d d d            d S # 1 swxY w Y   d S )N)r   r   i   zline buffering)messagecategoryrB   )r   check_no_warningsRuntimeWarningr   assertWarnsRegex)r   r   s     r   testSetBufferSizez OtherFileTests.testSetBufferSize   s3     	) 	)A 243C4BD D D ) ) %%a((() ) ) ) ) ) ) ) ) ) ) ) ) ) ) "">3CDD 	% 	%!!!$$$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%s#   AA	
A	*BBBc                 b   |                      t          d          }	 |                    d           |                                 |                      t          d          }|                    d          }|dk    r|                     d|z             |                                dk    r*|                     d|                                z             |                                 |                                dk    r*|                     d|                                z             |                                 t          j	        
                    t                    }|dk    r|                     d	|z             |                                 d S # |                                 w xY w)
Nr   s   12345678901zrb+   s   12345z(Read on file opened for update failed %rzFile pos after read wrong %dz!File pos after ftruncate wrong %dz"File size after ftruncate wrong %d)r   r   r   r   r<   r   r   rd   ospathgetsize)r   r   datasizes       r   testTruncateOnWindowsz$OtherFileTests.testTruncateOnWindows   sS    IIfd##	GGN###GGIII		&''A66!99Dx		DtKLLLvvxx1}}		816688CDDDJJLLLvvxx1}}		=HIIIGGIII7??6**Dqyy		>EFFFGGIIIIIAGGIIIIs   E%F F.c                 F   d}d}|t          |          z  r
J d            |t          |          z  }g d}ddddt          d	d
          ffg}|                     t          d          }|                    ||z             |                    |           |                                 |D ]l\  }}|                     t          d          }	|                     t          |	          |           t          |	|          }
 |
|  |	                                 m|                     t          d          }	t          |          D ]}t          |	           |                    d          }	 |	                                }n%# t          $ r |                     d           Y nw xY w||k    r|                     d|d|           |                    d          }t          d	dt          |          z            }	 |	                    |           n%# t          $ r |                     d           Y nw xY w|                                }||k    r|                     d|d|           |                    d          }	 |	                    t          |                    }n%# t          $ r |                     d           Y nw xY w||k    r|                     d|d|           	 |	                                }n%# t          $ r |                     d           Y nw xY w||k    r|                     d|d|           |	                                 |                     t          d          }		 |	D ]}	 |	                                 |	                    |           |	                                 |	                                 n%# t          $ r |                     d           Y nw xY w|	                                 d S # |	                                 w xY w)Ni @  s   ham
z*dataoffset must be multiple of len(filler))s   spam, spam and eggs
s   eggs, spam, ham and spam
s   saussages, spam, spam and eggs
s   spam, ham, spam and eggs
s(   spam, spam, spam, spam, spam, ham, spam
s   wonderful spaaaaaam.
r^   r]   r`   r.   r+   sd                                                                                                       r   r-   r   zLreadline() after next() with supposedly empty iteration-buffer failed anywayz6readline() after next() with empty buffer failed. Got z, expected     zLreadinto() after next() with supposedly empty iteration-buffer failed anywayz6readinto() after next() with empty buffer failed. Got zHread() after next() with supposedly empty iteration-buffer failed anywayz2read() after next() with empty buffer failed. Got zMreadlines() after next() with supposedly empty iteration-buffer failed anywayz7readlines() after next() with empty buffer failed. Got z'read* failed after next() consumed file)lenr   r   r   r   r;   r   r   nextr    rangepopr_   rg   r   r.   r/   r<   ra   )r   
dataoffsetfillernchunks	testlinesrj   bagrk   rl   r   methitestlineliner>   liness                   r   testIterationzOtherFileTests.testIteration   s    
F+ 	9 	98	9 	9 	9F+
 
 
	 $\3DsH!5!5 78: ii%%		&7"###y!!!		 ' 	 	J		&$''AT!WWf---1j))DD$KKGGIIII IIfd##w 	 	AGGGG==##	:::<<DD 	: 	: 	:II 9 : : : : :	: 8III9=xxI J J J==##C3x==011	:JJsOOOO 	: 	: 	:II 9 : : : : :	: {{}}8III9=xxI J J J ==##	:66#h--((DD 	: 	: 	:II 9 : : : : :	: 8III9=xxI J J J	:KKMMEE 	: 	: 	:II 9 : : : : :	: IIII9=xxI J J J				 IIfd##	  E



3 E E E		CDDDDDE GGIIIIIAGGIIIIsy   +F   F"!F"<H H43H4"J% %KK,L L#"L#7P
 =AO P
 O1.P
 0O11P
 
P N)rL   rM   rN   r   r   r   r   r   r   r   r   rO   r   r   r}   r}      s        ! ! !
? 
? 
?! ! ! B B B   % % %  8_ _ _ _ _r   r}   c                       e Zd Zej        ZdS )COtherFileTestsNrs   rO   r   r   r   r   X  ru   r   r   c                   .    e Zd Z eej                  ZdS )PyOtherFileTestsNrx   rO   r   r   r   r   [  r{   r   r   __main__)rh   r   unittestr   weakrefr   rt   _pyiorz   test.supportr   test.support.os_helperr   r   r   collectionsr	   r   TestCaserr   rw   r}   r   r   rL   mainrO   r   r   <module>r      s   



 				              				     # # # # # # ) ) ) ) ) ) " " " " " " ( ( ( ( ( (            u0 u0 u0 u0 u0 u0 u0 u0n    ]H$5   # # # # #mX%6 # # #H H H H H H H HT    nh&7   # # # # #~x'8 # # # zHMOOOOO r   