a zeW@sHddlZddlZddlZddlZddlZddlZddlZddlZz ddlZWne ybdZYn0z ddl Z Wne ydZ Yn0ddl m Z m Z ddlmZmZddlmZddlmZmZmZddlmZddlmZddlmZGd d d ZGd d d ZGd ddeejZGdddZGdddeejZ GdddZ!GdddejZ"Gddde"Z#Gddde"Z$Gddde"Z%Gddde"Z&Gdd d e"Z'Gd!d"d"e"Z(Gd#d$d$e"Z)Gd%d&d&e"Z*Gd'd(d(e"Z+Gd)d*d*Z,Gd+d,d,ejZ-Gd-d.d.ejZ.Gd/d0d0ejZ/e0d1krDe1dS)2N)BytesIOStringIO) FileInput hook_encoded)Path)verboseTESTFNcheck_warnings)unlink)support)mockc@seZdZddddZdS) BaseTestswmodecCsRt\}}|tj|t||}||Wdn1sD0Y|SN)tempfilemkstemp addCleanupr r openwrite)selfZcontentrfdnamefrG/opt/bitninja-python-dojo/embedded/lib/python3.9/test/test_fileinput.pywriteTmp(s   (zBaseTests.writeTmpN)__name__ __module__ __qualname__rrrrrr %sr c@sDeZdZddZeddZddZddd Zdd d Zd dZ dS) LineReadercCs g|_dSr _linesreadrrrr__init__1szLineReader.__init__cCs$z|jddWg|_Sg|_0dSrr"r$rrr linesread4s zLineReader.linesreadcCst|d|_|SNT)iter splitlinesit)rfilenamerrrropenhook;szLineReader.openhookNcCst|jd}|j||S)N)nextr*r#append)rsizelinerrrreadline?s  zLineReader.readlinecCs@g}d}|}|s|S|||t|7}||kr|SqdSNr)r2r/len)rhintlinesr0r1rrr readlinesDs  zLineReader.readlinescCsdSrrr$rrrclosePszLineReader.close)N)r3) rrr r%propertyr&r,r2r8r9rrrrr!/s   r!c@seZdZddZdS)BufferSizesTestsc Cs|dddtdD}|dddtdD}|dddtdD}|dd dtd D}td }trtd t||||fd }t|}| | t |d| |dd| |dd| | d| | |trtdt||||fd }d}|r@|dkr@|}q$| | || | d| |d||||trtd|| |d| | d| trtdt||||dfd }tj} zTtdt_t|}| t |d| |dd | | d!|W| t_n| t_0tr^td"t||||fd }| | d#| | d|| | d#| | dtrtd$tj} zJt||||fd d%}|D]} | dd&} t| q| W| t_n| t_0t||||fd }|D]N} | | d&d'|| dd&} || d| t| d |q6| dS)(Nr-css|]}d|dVqdS)zLine %s of file 1 Nr.0irrr Vz5BufferSizesTests.test_buffer_sizes..css|]}d|dVqdS)zLine %s of file 2 r<Nrr=rrrr@WrA css|]}d|dVqdS)zLine %s of file 3 r<Nrr=rrrr@XrAcss|]}d|dVqdS)zLine %s of file 4 r<Nrr=rrrr@YrAr<zLINE (\d+) OF FILE (\d+)z1. Simple iterationfileszLine 5 of file 1 zLine 1 of file 4 z2. Status variablesxzLine 6 of file 2 z 3. NextfilezLine 1 of file 3 z4. Stdin-z Line 1 of stdin Line 2 of stdin ! zLine 2 of stdin zz5. Boundary conditionsrz 6. InplacerFinplacer3 )rjoinrangerecompilerprintrlistr9 assertEqualr5linenor+r2 filelineno assertFalse isfirstlineisstdinnextfilesysstdinrstdoutuppermatchassertNotEqualintgroup) rt1t2t3t4patfir7sZ savestdinZ savestdoutr1mrrrtest_buffer_sizesTs       z"BufferSizesTests.test_buffer_sizesN)rrr rqrrrrr;Ssr;c@seZdZddZddZdS)UnconditionallyRaisecCs||_d|_dSNF)exception_typeinvoked)rrtrrrr%szUnconditionallyRaise.__init__cOsd|_|dSr')rurtrargskwargsrrr__call__szUnconditionallyRaise.__call__Nrrr r%ryrrrrrrsrrc@seZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZejedddZddZejedd d!Zejedd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3Zd4S)5FileInputTestscCs|d}|d}|d}|d}t||||fd}|}||d||d||d||||}||||d||d||||dS)Nr-zThe only line there is. rEr<r) rrr2rZr[r\r+r]r9)rrirjrkrlrnr1rrrtest_zero_byte_filess       z#FileInputTests.test_zero_byte_filescCs^|d}|d}t||fd}t|}||gd||d||ddS)NA B CD E FrEA B CzD zE FrL)rrrYrZr\r[rrirjrnr7rrr%test_files_that_dont_end_with_newlines  z4FileInputTests.test_files_that_dont_end_with_newlinecCs~|d}|d}t||fd}||dt|}||d|||dt|}||ddS)NzA BzC DrEr3)rrrZfilenor.rfr`rY)rrirjrnr1rrr test_filenos  zFileInputTests.test_filenocCsztdd}|dWnty*Yn0|jddd}tdtft|dd}Wdn1sh0Ytdtft|}Wdn1s0Y||gd dS) Nrrz-FileInput should reject invalid mode argumentA B C Dwbr-UrFr)rrC D)rfail ValueErrorrr DeprecationWarningrYrZ)rrnrir7rrrtest_opening_modes  *&z FileInputTests.test_opening_modecCs\td>}td|_tdgdd}t|}||dgWdn1sN0YdS)Nz sys.stdinspam, bacon, sausage, and spamrNrbr)r ZpatchrbufferrrYrZ)rZm_stdinrnr7rrrtest_stdin_binary_modes   z%FileInputTests.test_stdin_binary_modecCs^tj}zJtdt_|ttjdtdgdd}t|}||dgW|t_n|t_0dS)NrrrNrr)rarbrr]hasattrrrYrZ)rZ orig_stdinrnr7rrrtest_detached_stdin_binary_modes z.FileInputTests.test_detached_stdin_binary_modecCsztdddd}|dWnty0Yn0ztdd}|dWnty\Yn0Gdd d }|d }|}t|g|d}|Wdn1s0Y||jd dS) Nr<cSsdSrr)rrprrrrAz7FileInputTests.test_file_opening_hook..)rRr,zGFileInput should raise if both inplace and openhook arguments are given)r,z2FileInput should check openhook for being callablec@seZdZddZddZdS)z=FileInputTests.test_file_opening_hook..CustomOpenHookcSs d|_dSrs)rur$rrrr%szFFileInputTests.test_file_opening_hook..CustomOpenHook.__init__cWsd|_t|Sr')rur)rrwrrrryszFFileInputTests.test_file_opening_hook..CustomOpenHook.__call__NrzrrrrCustomOpenHooksrrSzopenhook not invoked)rrrrr2 assertTrueru)rrnrtZcustom_open_hookrrrtest_file_opening_hooks     &z%FileInputTests.test_file_opening_hookc Cs2ttd2}|d|dd|dWdn1sB0Y|tttttdd}z4||d||d ||d Wnt y| d Yn0| t t |Wdn1s0Y||d ||d Wdn1s$0YdS) NrsA B C s 123456789 iasciirFr,rrrzRead to end of filer-) rrrr safe_unlinkrrrZr2UnicodeDecodeErrorr assertRaisesrYrrrnrrr test_readline s&  (   &zFileInputTests.test_readlinecCsttd}|dWdn1s*0Y|ttttdd`}||d||d||d||d||dWdn1s0YdS) NrrrrsA sB sC DrA)rrrrrrrZr2rrrrtest_readline_binary_mode7s ( z(FileInputTests.test_readline_binary_modecCs|jddd}t|ddd0}|}||dtjdWdn1sR0Yt|d }||dWdn1s0YdS)Ns Initial text.rrrT)rrRs New line.) rrr2rZrarcrrread)rZ temp_fileZfobjr1rrrrtest_inplace_binary_write_modeDs * z-FileInputTests.test_inplace_binary_write_modecCs|d}|d}t||fd}t|}Wdn1s@0Y||gd||d||d||jddS)Nr}r~rErrrLr)rrrYrZr\r[_filesrrrrtest_context_managerNs  &z#FileInputTests.test_context_managercCsb|d}z2t|d}tWdn1s00YWn ty\||jdYn0dS)Nr-rEr)rrOSErrorrZr)rrirnrrrtest_close_on_exceptionXs   & z&FileInputTests.test_close_on_exceptioncCs<tgd}||jdWdn1s.0YdS)NrE)rN)rrZr)rrnrrr.test_empty_files_list_specified_to_constructor`s z=FileInputTests.test_empty_files_list_specified_to_constructor)categorycCsb|d}t|gd8}|d}||d|d}||dWdn1sT0YdS)N line1 line2 rErline1 r<zline2 )rrrZ)rrrnZretval1Zretval2rrrtest__getitem__ds   zFileInputTests.test__getitem__c Csv|d}|tdLt|gd }||ddWdn1sJ0YWdn1sh0YdS)NrzUse iterator protocol insteadrErr)rZassertWarnsRegexrrrZ)rrrnrrrtest__getitem___deprecationos  z*FileInputTests.test__getitem___deprecationc Cs||d}t|gdB}|t}|dWdn1s@0YWdn1s^0Y||jjddS)NrrEr<)zaccessing lines out of order)rrr RuntimeErrorrZ exceptionrwrrrncmrrrtest__getitem__invalid_keyvs   Dz)FileInputTests.test__getitem__invalid_keyc Cs||d}t|gdB}|t}|dWdn1s@0YWdn1s^0Y||jjddS)Nr-rEr)zend of input reached)rrr IndexErrorrZrrwrrrrtest__getitem__eofs   Dz!FileInputTests.test__getitem__eofcCstj}tt}zj|d}|tj|dt|gdd&}t||t_| Wdn1sf0YW|t_n|t_0| |j ddS)NrSz.bakTrQzos.unlink() was not invoked) osr rrrrrr rr.r`rru)rZos_unlink_origZos_unlink_replacementrrnrrr%test_nextfile_oserror_deleting_backups (z4FileInputTests.test_nextfile_oserror_deleting_backupcCsztj}tt}zP|d}t|gdd}|t_|Wdn1sL0YW|t_n|t_0||jddSNrSTrQzos.fstat() was not invoked) rfstatrrrrrr2rru)rZ os_fstat_origZos_fstat_replacementrrnrrr%test_readline_os_fstat_raises_OSErrors (z4FileInputTests.test_readline_os_fstat_raises_OSErrorcCsztj}tt}zP|d}t|gdd}|t_|Wdn1sL0YW|t_n|t_0||jddSr) rchmodrrrrrr2rru)rZ os_chmod_origZos_chmod_replacementrrnrrr%test_readline_os_chmod_raises_OSErrors (z4FileInputTests.test_readline_os_chmod_raises_OSErrorc CsGdddt}|}|d}t|gd8}|j}z||_|}W||_n||_0Wdn1sj0Y||jd||dddS)Nc@seZdZddZddZdS)zQFileInputTests.test_fileno_when_ValueError_raised..FilenoRaisesValueErrorcSst|tdSr)rrr%rr$rrrr%szZFileInputTests.test_fileno_when_ValueError_raised..FilenoRaisesValueError.__init__cSs |dSr)ryr$rrrrszXFileInputTests.test_fileno_when_ValueError_raised..FilenoRaisesValueError.filenoN)rrr r%rrrrrFilenoRaisesValueErrorsrrSrEz_file.fileno() was not invokedr3zfileno() should return -1)rrrr_filerrrurZ)rrZ unconditionally_raise_ValueErrorrrnZ file_backupresultrrr"test_fileno_when_ValueError_raiseds  .z1FileInputTests.test_fileno_when_ValueError_raisedcCst}tddg|jd}||jg||d||jdg||d||jdg||d||jddg||d||jdg||d||jgWdn1s0YdSNz line1 line2zline3 rrZline2r-)r!rr,rZr&r2rsrcrnrrrtest_readline_bufferings z&FileInputTests.test_readline_bufferingcCst}tddg|jd}||jg|t|d||jdg|t|d||jdg|t|d||jddg|tt|||jdg|tt|||jgWdn1s0YdSr)r!rr,rZr&r.r StopIterationrrrrtest_iteration_bufferings z'FileInputTests.test_iteration_bufferingcCst|d}t|Z}|}||d||d||d||t |Wdn1sv0YdS)N Pathlib file.r<) rrrr2rZr[r\r+rfspath)rrirnr1rrrtest_pathlib_files  z FileInputTests.test_pathlib_filecCst|d}t|dd0}|}||dtd|Wdn1sP0Yt| }||dWdn1s0YdS)NrT)rRz Modified %szModified Pathlib file. )rrrr2rZrXrr)rrirnr1rrrrtest_pathlib_file_inplaces * z(FileInputTests.test_pathlib_file_inplaceN)rrr r|rrrrrrrrrrrrr Zignore_warningsrrrrrrrrrrrrrrrrrr{s8           r{c@s^eZdZddddddZdd Zd d Zd d ZddZddZddZ ddZ ddZ dS) MockFileInputNFr-r)rr,cCs>||_||_||_||_||_d|_tdd|_i|_ dS)NcSsdSr4rrrrrrrAz(MockFileInput.__init__..) rFrRbackuprr,r collections defaultdictinvocation_counts return_values)rrFrRrrr,rrrr%szMockFileInput.__init__cCs|jdd7<dS)Nr9r<)rr$rrrr9szMockFileInput.closecCs|jdd7<|jdS)Nr`r<rrr$rrrr`szMockFileInput.nextfilecCs|jdd7<|jdS)Nr+r<rr$rrrr+"szMockFileInput.filenamecCs|jdd7<|jdS)Nr[r<rr$rrrr[&szMockFileInput.linenocCs|jdd7<|jdS)Nr\r<rr$rrrr\*szMockFileInput.filelinenocCs|jdd7<|jdS)Nrr<rr$rrrr.szMockFileInput.filenocCs|jdd7<|jdS)Nr^r<rr$rrrr^2szMockFileInput.isfirstlinecCs|jdd7<|jdS)Nr_r<rr$rrrr_6szMockFileInput.isstdin)NFr-) rrr r%r9r`r+r[r\rr^r_rrrrr s rc@s$eZdZddZddZddZdS)BaseFileInputGlobalMethodsTestcCstj|_tj|_tt_dSr) fileinput_state _orig_stater_orig_FileInputrr$rrrsetUp>sz$BaseFileInputGlobalMethodsTest.setUpcCs|jt_|jt_dSr)rrrrrr$rrrtearDownCsz'BaseFileInputGlobalMethodsTest.tearDowncCs2|j|}||d|t|j}||ddSNr<)rrZr5)rZmock_file_inputZ method_nameZ actual_countZactual_total_countrrrassertExactlyOneInvocationGs  z9BaseFileInputGlobalMethodsTest.assertExactlyOneInvocationN)rrr rrrrrrrr:src@s,eZdZddZddZddZddZd S) Test_fileinput_inputcCsjt}t|_|t_|t}tWdn1s<0Y|d|j j | |tjddS)N)zinput() already activefileinput._state) robjectrrrrrinputrZrrwassertIs)rinstancerrrr1test_state_is_not_None_and_state_file_is_not_NoneRs &zFTest_fileinput_input.test_state_is_not_None_and_state_file_is_not_NonecCst}d|_|t_|dSr)rrrrdo_test_call_inputrrrrr-test_state_is_not_None_and_state_file_is_None_szBTest_fileinput_input.test_state_is_not_None_and_state_file_is_NonecCsdt_|dSr)rrrr$rrrtest_state_is_Nonejsz'Test_fileinput_input.test_state_is_NonecCst}t}t}t}t}tj|||||d}||tjd|||jd|||jd|||jd|||jd|||j ddS)N)rFrRrrr,rrFrRrrr,) rrrrrrFrRrrr,)rrFrRrrr,rrrrrss z'Test_fileinput_input.do_test_call_inputN)rrr rrrrrrrrrOs   rc@seZdZddZddZdS)Test_fileinput_closecCsdt_t|tjdSr)rrr9 assertIsNoner$rrrrsz'Test_fileinput_close.test_state_is_NonecCs0t}|t_t||d|tjdS)Nr9)rrrr9rrrrrrtest_state_is_not_Nones  z+Test_fileinput_close.test_state_is_not_NoneNrrr rrrrrrrsrc@seZdZddZddZdS)Test_fileinput_nextfilecCsXdt_|t}tWdn1s.0Y|d|jj|tjdSN)zno active input()) rrrrr`rZrrwrrrrrrrs  &z*Test_fileinput_nextfile.test_state_is_NonecCsNt}t}||jd<|t_t}||d||||tj|dS)Nr`)rrrrrr`rr)rZnextfile_retvalrretvalrrrrs   z.Test_fileinput_nextfile.test_state_is_not_NoneNrrrrrrs rc@seZdZddZddZdS)Test_fileinput_filenamecCsXdt_|t}tWdn1s.0Y|d|jj|tjdSr) rrrrr+rZrrwrrrrrrs  &z*Test_fileinput_filename.test_state_is_NonecCsNt}t}||jd<|t_t}||d||||tj|dS)Nr+)rrrrrr+rr)rZfilename_retvalrrrrrrs   z.Test_fileinput_filename.test_state_is_not_NoneNrrrrrrs rc@seZdZddZddZdS)Test_fileinput_linenocCsXdt_|t}tWdn1s.0Y|d|jj|tjdSr) rrrrr[rZrrwrrrrrrs  &z(Test_fileinput_lineno.test_state_is_NonecCsNt}t}||jd<|t_t}||d||||tj|dS)Nr[)rrrrrr[rr)rZ lineno_retvalrrrrrrs   z,Test_fileinput_lineno.test_state_is_not_NoneNrrrrrrs rc@seZdZddZddZdS)Test_fileinput_filelinenocCsXdt_|t}tWdn1s.0Y|d|jj|tjdSr) rrrrr\rZrrwrrrrrrs  &z,Test_fileinput_filelineno.test_state_is_NonecCsNt}t}||jd<|t_t}||d||||tj|dS)Nr\)rrrrrr\rr)rZfilelineno_retvalrrrrrrs   z0Test_fileinput_filelineno.test_state_is_not_NoneNrrrrrrs rc@seZdZddZddZdS)Test_fileinput_filenocCsXdt_|t}tWdn1s.0Y|d|jj|tjdSr) rrrrrrZrrwrrrrrrs  &z(Test_fileinput_fileno.test_state_is_NonecCsTt}t}||jd<||_|t_t}||d||||tj|dS)Nr) rrr fileno_retvalrrrrr)rrrrrrrrs   z,Test_fileinput_fileno.test_state_is_not_NoneNrrrrrr s rc@seZdZddZddZdS)Test_fileinput_isfirstlinecCsXdt_|t}tWdn1s.0Y|d|jj|tjdSr) rrrrr^rZrrwrrrrrr+s  &z-Test_fileinput_isfirstline.test_state_is_NonecCsNt}t}||jd<|t_t}||d||||tj|dS)Nr^)rrrrrr^rr)rZisfirstline_retvalrrrrrr5s   z1Test_fileinput_isfirstline.test_state_is_not_NoneNrrrrrr(s rc@seZdZddZddZdS)Test_fileinput_isstdincCsXdt_|t}tWdn1s.0Y|d|jj|tjdSr) rrrrr_rZrrwrrrrrrFs  &z)Test_fileinput_isstdin.test_state_is_NonecCsNt}t}||jd<|t_t}||d||||tj|dS)Nr_)rrrrrr_rr)rZisstdin_retvalrrrrrrPs   z-Test_fileinput_isstdin.test_state_is_not_NoneNrrrrrrCs rc@seZdZddZddZdS)InvocationRecordercCs d|_dSr4)invocation_countr$rrrr%_szInvocationRecorder.__init__cOs|jd7_||f|_dSr)rlast_invocationrvrrrryaszInvocationRecorder.__call__Nrzrrrrr^src@sxeZdZddZddZddZeeddd Z ee d d d Z d dZ ddZ ddZddZeddZdS)Test_hook_compressedcCs t|_dSr)r fake_openr$rrrrhszTest_hook_compressed.setUpcCs|dddS)Nr-r<do_test_use_builtin_openr$rrrtest_empty_stringksz&Test_hook_compressed.test_empty_stringcCs|dddS)NZabcdrr$rrr test_no_extnsz Test_hook_compressed.test_no_extzRequires gzip and zlibcCsVtj}|jt_ztdd}W|t_n|t_0||jjd||jjdifdS)Ntest.gzrr<)rr)gziprrrhook_compressedrZrrr original_openrrrrtest_gz_ext_fakeqsz%Test_hook_compressed.test_gz_ext_fakez Requires bz2cCsVtj}|jt_ztdd}W|t_n|t_0||jjd||jjdifdS)Ntest.bz2rHr<)r rH)bz2BZ2FilerrrrZrrrrrrtest_bz2_ext_fake}sz&Test_hook_compressed.test_bz2_ext_fakecCs|dddS)Nz abcd.blahrDrr$rrr test_blah_extsz"Test_hook_compressed.test_blah_extcCs|dddS)Nzabcd.GzrLrr$rrrtest_gz_ext_builtinsz(Test_hook_compressed.test_gz_ext_builtincCs|dddS)Nzabcd.Bz2rr$rrrtest_bz2_ext_builtinsz)Test_hook_compressed.test_bz2_ext_builtinc Cs`||j}zt||}W||n ||0||jjd||jj||fifdSr)replace_builtin_openrrrrZrr)rr+rrrrrrrs   z-Test_hook_compressed.do_test_use_builtin_opencCstj}|t_|Sr)builtinsr)Z new_open_funcrrrrrsz)Test_hook_compressed.replace_builtin_openN)rrr rrrunittestZ skipUnlessrr r r rrrr staticmethodrrrrrres    rc@s$eZdZddZddZddZdS)Test_hook_encodedc Cst}t}tj||d}t}tj}|t_z t}t}|||}W|t_n|t_0||jd|j\} } | | d|| | d|| | d|| | d|| | dS)Nerrorsr<rencodingr) rrrrrrrZrrrpopr]) rrrrrrr+rZ open_resultrwrxrrrtests$  zTest_hook_encoded.testcsttd}|dWdn1s*0Yttfdd}|ddgt|ddgWdn1s0Y|dd g|d d gdS) NrsabccsNttdtd|dd}t|}Wdn1s40Y||dS)Nrzutf-8rrFrr,rrrrYrZ)rexpected_linesrnr7r$rrchecks  &z,Test_hook_encoded.test_errors..checkignoreabcstrictreplaceu�abcbackslashreplacez\x80abc)rrrrrrrrrrrr$r test_errorss (    * zTest_hook_encoded.test_errorscsttd}|dWdn1s*0Yttfdd}|dgdt|dgdWdn1s0Yt|dgdWdn1s0Yt|d gd Wdn1s0YdS) Nrs A B C D+IKw-csJtt|tdd}t|}Wdn1s00Y||dS)Nzutf-7rr)rrrnr7r$rrrs &z+Test_hook_encoded.test_modes..checkr)rrrD€ZrUrr)rzB zC r') rrrrrZ assertWarnsrrrr%rr$r test_modess (   , , zTest_hook_encoded.test_modesN)rrr rr&r(rrrrrsrc@seZdZddZdS)MiscTestcCst|tdSr)r Z check__all__rr$rrrtest_allszMiscTest.test_allN)rrr r*rrrrr)sr)__main__)2rrarVrrrrrr  ImportErrorriorrrrpathlibrZ test.supportrrr r rrr r r r!ZTestCaser;rrr{rrrrrrrrrrrrrrr)rmainrrrrs\           $Sa->>A