<s> '''<STR_LIT>''' <EOL> import unittest <EOL> from test . test_support import verbose , TESTFN , run_unittest <EOL> from test . test_support import unlink as safe_unlink <EOL> import sys , re <EOL> from StringIO import StringIO <EOL> from fileinput import FileInput , hook_encoded <EOL> def writeTmp ( i , lines , mode = '<STR_LIT:w>' ) : <EOL> name = TESTFN + str ( i ) <EOL> f = open ( name , mode ) <EOL> f . writelines ( lines ) <EOL> f . close ( ) <EOL> return name <EOL> def remove_tempfiles ( * names ) : <EOL> for name in names : <EOL> safe_unlink ( name ) <EOL> class BufferSizesTests ( unittest . TestCase ) : <EOL> def test_buffer_sizes ( self ) : <EOL> for round , bs in ( <NUM_LIT:0> , <NUM_LIT:0> ) , ( <NUM_LIT:1> , <NUM_LIT:30> ) : <EOL> try : <EOL> t1 = writeTmp ( <NUM_LIT:1> , [ "<STR_LIT>" % ( i + <NUM_LIT:1> ) for i in range ( <NUM_LIT:15> ) ] ) <EOL> t2 = writeTmp ( <NUM_LIT:2> , [ "<STR_LIT>" % ( i + <NUM_LIT:1> ) for i in range ( <NUM_LIT:10> ) ] ) <EOL> t3 = writeTmp ( <NUM_LIT:3> , [ "<STR_LIT>" % ( i + <NUM_LIT:1> ) for i in range ( <NUM_LIT:5> ) ] ) <EOL> t4 = writeTmp ( <NUM_LIT:4> , [ "<STR_LIT>" % ( i + <NUM_LIT:1> ) for i in range ( <NUM_LIT:1> ) ] ) <EOL> self . buffer_size_test ( t1 , t2 , t3 , t4 , bs , round ) <EOL> finally : <EOL> remove_tempfiles ( t1 , t2 , t3 , t4 ) <EOL> def buffer_size_test ( self , t1 , t2 , t3 , t4 , bs = <NUM_LIT:0> , round = <NUM_LIT:0> ) : <EOL> pat = re . compile ( r'<STR_LIT>' ) <EOL> start = <NUM_LIT:1> + round * <NUM_LIT:6> <EOL> if verbose : <EOL> print '<STR_LIT>' % ( start + <NUM_LIT:0> , bs ) <EOL> fi = FileInput ( files = ( t1 , t2 , t3 , t4 ) , bufsize = bs ) <EOL> lines = list ( fi ) <EOL> fi . close ( ) <EOL> self . assertEqual ( len ( lines ) , <NUM_LIT> ) <EOL> self . assertEqual ( lines [ <NUM_LIT:4> ] , '<STR_LIT>' ) <EOL> self . assertEqual ( lines [ <NUM_LIT:30> ] , '<STR_LIT>' ) <EOL> self . assertEqual ( fi . lineno ( ) , <NUM_LIT> ) <EOL> self . assertEqual ( fi . filename ( ) , t4 ) <EOL> if verbose : <EOL> print '<STR_LIT>' % ( start + <NUM_LIT:1> , bs ) <EOL> fi = FileInput ( files = ( t1 , t2 , t3 , t4 ) , bufsize = bs ) <EOL> s = "<STR_LIT:x>" <EOL> while s and s != '<STR_LIT>' : <EOL> s = fi . readline ( ) <EOL> self . assertEqual ( fi . filename ( ) , t2 ) <EOL> self . assertEqual ( fi . lineno ( ) , <NUM_LIT> ) <EOL> self . assertEqual ( fi . filelineno ( ) , <NUM_LIT:6> ) <EOL> self . assertFalse ( fi . isfirstline ( ) ) <EOL> self . assertFalse ( fi . isstdin ( ) ) <EOL> if verbose : <EOL> print '<STR_LIT>' % ( start + <NUM_LIT:2> , bs ) <EOL> fi . nextfile ( ) <EOL> self . assertEqual ( fi . readline ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( fi . lineno ( ) , <NUM_LIT> ) <EOL> fi . close ( ) <EOL> if verbose : <EOL> print '<STR_LIT>' % ( start + <NUM_LIT:3> , bs ) <EOL> fi = FileInput ( files = ( t1 , t2 , t3 , t4 , '<STR_LIT:->' ) , bufsize = bs ) <EOL> savestdin = sys . stdin <EOL> try : <EOL> sys . stdin = StringIO ( "<STR_LIT>" ) <EOL> lines = list ( fi ) <EOL> self . assertEqual ( len ( lines ) , <NUM_LIT> ) <EOL> self . assertEqual ( lines [ <NUM_LIT:32> ] , '<STR_LIT>' ) <EOL> self . assertEqual ( fi . filename ( ) , '<STR_LIT>' ) <EOL> fi . nextfile ( ) <EOL> finally : <EOL> sys . stdin = savestdin <EOL> if verbose : <EOL> print '<STR_LIT>' % ( start + <NUM_LIT:4> , bs ) <EOL> fi = FileInput ( files = ( t1 , t2 , t3 , t4 ) , bufsize = bs ) <EOL> self . assertEqual ( fi . lineno ( ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( fi . filename ( ) , None ) <EOL> fi . nextfile ( ) <EOL> self . assertEqual ( fi . lineno ( ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( fi . filename ( ) , None ) <EOL> if verbose : <EOL> print '<STR_LIT>' % ( start + <NUM_LIT:5> , bs ) <EOL> savestdout = sys . stdout <EOL> try : <EOL> fi = FileInput ( files = ( t1 , t2 , t3 , t4 ) , inplace = <NUM_LIT:1> , bufsize = bs ) <EOL> for line in fi : <EOL> line = line [ : - <NUM_LIT:1> ] . upper ( ) <EOL> print line <EOL> fi . close ( ) <EOL> finally : <EOL> sys . stdout = savestdout <EOL> fi = FileInput ( files = ( t1 , t2 , t3 , t4 ) , bufsize = bs ) <EOL> for line in fi : <EOL> self . assertEqual ( line [ - <NUM_LIT:1> ] , '<STR_LIT:\n>' ) <EOL> m = pat . match ( line [ : - <NUM_LIT:1> ] ) <EOL> self . assertNotEqual ( m , None ) <EOL> self . assertEqual ( int ( m . group ( <NUM_LIT:1> ) ) , fi . filelineno ( ) ) <EOL> fi . close ( ) <EOL> class FileInputTests ( unittest . TestCase ) : <EOL> def test_zero_byte_files ( self ) : <EOL> try : <EOL> t1 = writeTmp ( <NUM_LIT:1> , [ "<STR_LIT>" ] ) <EOL> t2 = writeTmp ( <NUM_LIT:2> , [ "<STR_LIT>" ] ) <EOL> t3 = writeTmp ( <NUM_LIT:3> , [ "<STR_LIT>" ] ) <EOL> t4 = writeTmp ( <NUM_LIT:4> , [ "<STR_LIT>" ] ) <EOL> fi = FileInput ( files = ( t1 , t2 , t3 , t4 ) ) <EOL> line = fi . readline ( ) <EOL> self . assertEqual ( line , '<STR_LIT>' ) <EOL> self . assertEqual ( fi . lineno ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( fi . filelineno ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( fi . filename ( ) , t3 ) <EOL> line = fi . readline ( ) <EOL> self . assertFalse ( line ) <EOL> self . assertEqual ( fi . lineno ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( fi . filelineno ( ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( fi . filename ( ) , t4 ) <EOL> fi . close ( ) <EOL> finally : <EOL> remove_tempfiles ( t1 , t2 , t3 , t4 ) <EOL> def test_files_that_dont_end_with_newline ( self ) : <EOL> try : <EOL> t1 = writeTmp ( <NUM_LIT:1> , [ "<STR_LIT>" ] ) <EOL> t2 = writeTmp ( <NUM_LIT:2> , [ "<STR_LIT>" ] ) <EOL> fi = FileInput ( files = ( t1 , t2 ) ) <EOL> lines = list ( fi ) <EOL> self . assertEqual ( lines , [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:C>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:F>" ] ) <EOL> self . assertEqual ( fi . filelineno ( ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( fi . lineno ( ) , <NUM_LIT:6> ) <EOL> finally : <EOL> remove_tempfiles ( t1 , t2 ) <EOL> def test_unicode_filenames ( self ) : <EOL> try : <EOL> t1 = writeTmp ( <NUM_LIT:1> , [ "<STR_LIT>" ] ) <EOL> encoding = sys . getfilesystemencoding ( ) <EOL> if encoding is None : <EOL> encoding = '<STR_LIT:ascii>' <EOL> fi = FileInput ( files = unicode ( t1 , encoding ) ) <EOL> lines = list ( fi ) <EOL> self . assertEqual ( lines , [ "<STR_LIT>" , "<STR_LIT:B>" ] ) <EOL> finally : <EOL> remove_tempfiles ( t1 ) <EOL> def test_fileno ( self ) : <EOL> try : <EOL> t1 = writeTmp ( <NUM_LIT:1> , [ "<STR_LIT>" ] ) <EOL> t2 = writeTmp ( <NUM_LIT:2> , [ "<STR_LIT>" ] ) <EOL> fi = FileInput ( files = ( t1 , t2 ) ) <EOL> self . assertEqual ( fi . fileno ( ) , - <NUM_LIT:1> ) <EOL> line = fi . next ( ) <EOL> self . assertNotEqual ( fi . fileno ( ) , - <NUM_LIT:1> ) <EOL> fi . nextfile ( ) <EOL> self . assertEqual ( fi . fileno ( ) , - <NUM_LIT:1> ) <EOL> line = list ( fi ) <EOL> self . assertEqual ( fi . fileno ( ) , - <NUM_LIT:1> ) <EOL> finally : <EOL> remove_tempfiles ( t1 , t2 ) <EOL> def test_opening_mode ( self ) : <EOL> try : <EOL> fi = FileInput ( mode = "<STR_LIT:w>" ) <EOL> self . fail ( "<STR_LIT>" ) <EOL> except ValueError : <EOL> pass <EOL> try : <EOL> t1 = writeTmp ( <NUM_LIT:1> , [ "<STR_LIT>" ] , mode = "<STR_LIT:wb>" ) <EOL> fi = FileInput ( files = t1 , mode = "<STR_LIT>" ) <EOL> lines = list ( fi ) <EOL> self . assertEqual ( lines , [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:D>" ] ) <EOL> finally : <EOL> remove_tempfiles ( t1 ) <EOL> def test_file_opening_hook ( self ) : <EOL> try : <EOL> fi = FileInput ( inplace = <NUM_LIT:1> , openhook = lambda f , m : None ) <EOL> self . fail ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> except ValueError : <EOL> pass <EOL> try : <EOL> fi = FileInput ( openhook = <NUM_LIT:1> ) <EOL> self . fail ( "<STR_LIT>" ) <EOL> except ValueError : <EOL> pass <EOL> try : <EOL> t1 = writeTmp ( <NUM_LIT:1> , [ "<STR_LIT>" ] , mode = "<STR_LIT:wb>" ) <EOL> fi = FileInput ( files = t1 , openhook = hook_encoded ( "<STR_LIT>" ) ) <EOL> lines = list ( fi ) <EOL> self . assertEqual ( lines , [ "<STR_LIT>" , "<STR_LIT:O>" ] ) <EOL> finally : <EOL> remove_tempfiles ( t1 ) <EOL> def test_main ( ) : <EOL> run_unittest ( BufferSizesTests , FileInputTests ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_main ( ) </s>
