<s> import sys <EOL> import os <EOL> import gc <EOL> import unittest <EOL> from array import array <EOL> from weakref import proxy <EOL> from test . test_support import TESTFN , findfile , run_unittest <EOL> from test . test_support import gc_collect , impl_detail <EOL> from UserList import UserList <EOL> class AutoFileTests ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . f = open ( TESTFN , '<STR_LIT:wb>' ) <EOL> def tearDown ( self ) : <EOL> if self . f : <EOL> self . f . close ( ) <EOL> os . remove ( TESTFN ) <EOL> def testWeakRefs ( self ) : <EOL> p = proxy ( self . f ) <EOL> p . write ( '<STR_LIT>' ) <EOL> self . assertEquals ( self . f . name , p . name ) <EOL> self . f . close ( ) <EOL> self . f = None <EOL> gc_collect ( ) <EOL> self . assertRaises ( ReferenceError , getattr , p , '<STR_LIT:name>' ) <EOL> def testAttributes ( self ) : <EOL> f = self . f <EOL> softspace = f . softspace <EOL> f . name <EOL> f . mode <EOL> f . closed <EOL> f . softspace = softspace <EOL> for attr in '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' : <EOL> self . assertRaises ( ( AttributeError , TypeError ) , setattr , f , attr , '<STR_LIT>' ) <EOL> def testReadinto ( self ) : <EOL> self . f . write ( '<STR_LIT>' ) <EOL> self . f . close ( ) <EOL> a = array ( '<STR_LIT:c>' , '<STR_LIT:x>' * <NUM_LIT:10> ) <EOL> self . f = open ( TESTFN , '<STR_LIT:rb>' ) <EOL> n = self . f . readinto ( a ) <EOL> self . assertEquals ( '<STR_LIT>' , a . tostring ( ) [ : n ] ) <EOL> def testWritelinesUserList ( self ) : <EOL> l = [ '<STR_LIT:1>' , '<STR_LIT:2>' ] <EOL> self . f . writelines ( l ) <EOL> self . f . close ( ) <EOL> self . f = open ( TESTFN , '<STR_LIT:rb>' ) <EOL> buf = self . f . read ( ) <EOL> self . assertEquals ( buf , '<STR_LIT>' ) <EOL> def testWritelinesIntegers ( self ) : <EOL> self . assertRaises ( TypeError , self . f . writelines , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> def testWritelinesIntegersUserList ( self ) : <EOL> l = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> self . assertRaises ( TypeError , self . f . writelines , l ) <EOL> def testWritelinesNonString ( self ) : <EOL> class NonString : <EOL> pass <EOL> def testRepr ( self ) : <EOL> self . assert_ ( repr ( self . f ) . startswith ( "<STR_LIT>" + TESTFN ) ) <EOL> def testErrors ( self ) : <EOL> f = self . f <EOL> self . assertEquals ( f . name , TESTFN ) <EOL> self . assert_ ( not f . isatty ( ) ) <EOL> self . assert_ ( not f . closed ) <EOL> self . assertRaises ( TypeError , f . readinto , "<STR_LIT>" ) <EOL> f . close ( ) <EOL> self . assert_ ( f . closed ) <EOL> def testMethods ( self ) : <EOL> a = array ( '<STR_LIT:c>' , '<STR_LIT:x>' * <NUM_LIT:10> ) <EOL> methods = { '<STR_LIT>' : ( ) , '<STR_LIT>' : ( ) , '<STR_LIT>' : ( ) , '<STR_LIT>' : ( ) , <EOL> '<STR_LIT>' : ( ) , '<STR_LIT>' : ( a , ) , '<STR_LIT>' : ( ) , '<STR_LIT>' : ( ) , <EOL> '<STR_LIT>' : ( <NUM_LIT:0> , ) , '<STR_LIT>' : ( ) , '<STR_LIT>' : ( ) , '<STR_LIT>' : ( '<STR_LIT>' , ) , <EOL> '<STR_LIT>' : ( [ ] , ) , '<STR_LIT>' : ( ) , '<STR_LIT>' : ( ) } <EOL> if sys . platform . startswith ( '<STR_LIT>' ) : <EOL> del methods [ '<STR_LIT>' ] <EOL> self . f . __exit__ ( None , None , None ) <EOL> self . assert_ ( self . f . closed ) <EOL> for methodname , args in methods . items ( ) : <EOL> method = getattr ( self . f , methodname ) <EOL> self . assertRaises ( ValueError , method , * args ) <EOL> self . assertEquals ( self . f . __exit__ ( None , None , None ) , None ) <EOL> try : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> except : <EOL> self . assertEquals ( self . f . __exit__ ( * sys . exc_info ( ) ) , None ) <EOL> class OtherFileTests ( unittest . TestCase ) : <EOL> def testModeStrings ( self ) : <EOL> for mode in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> try : <EOL> f = open ( TESTFN , mode ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> f . close ( ) <EOL> self . fail ( '<STR_LIT>' % mode ) <EOL> def testStdin ( self ) : <EOL> if os . isatty ( sys . stdin . fileno ( ) ) : <EOL> if sys . platform != '<STR_LIT>' : <EOL> self . assertRaises ( IOError , sys . stdin . seek , - <NUM_LIT:1> ) <EOL> else : <EOL> print >> sys . __stdout__ , ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . assertRaises ( IOError , sys . stdin . truncate ) <EOL> def testUnicodeOpen ( self ) : <EOL> f = open ( unicode ( TESTFN ) , "<STR_LIT:w>" ) <EOL> self . assert_ ( repr ( f ) . startswith ( "<STR_LIT>" + TESTFN ) ) <EOL> f . close ( ) <EOL> os . unlink ( TESTFN ) <EOL> def testBadModeArgument ( self ) : <EOL> bad_mode = "<STR_LIT>" <EOL> try : <EOL> f = open ( TESTFN , bad_mode ) <EOL> except ValueError , msg : <EOL> if msg [ <NUM_LIT:0> ] != <NUM_LIT:0> : <EOL> s = str ( msg ) <EOL> if s . find ( TESTFN ) != - <NUM_LIT:1> or s . find ( bad_mode ) == - <NUM_LIT:1> : <EOL> self . fail ( "<STR_LIT>" % s ) <EOL> else : <EOL> f . close ( ) <EOL> self . fail ( "<STR_LIT>" % bad_mode ) <EOL> def testSetBufferSize ( self ) : <EOL> for s in ( - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT> ) : <EOL> try : <EOL> f = open ( TESTFN , '<STR_LIT:w>' , s ) <EOL> f . write ( str ( s ) ) <EOL> f . close ( ) <EOL> f . close ( ) <EOL> f = open ( TESTFN , '<STR_LIT:r>' , s ) <EOL> d = int ( f . read ( ) ) <EOL> f . close ( ) <EOL> f . close ( ) <EOL> except IOError , msg : <EOL> self . fail ( '<STR_LIT>' % ( s , str ( msg ) ) ) <EOL> self . assertEquals ( d , s ) <EOL> def testTruncateOnWindows ( self ) : <EOL> os . unlink ( TESTFN ) <EOL> def bug801631 ( ) : <EOL> f = open ( TESTFN , '<STR_LIT:wb>' ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . close ( ) <EOL> f = open ( TESTFN , '<STR_LIT>' ) <EOL> data = f . read ( <NUM_LIT:5> ) <EOL> if data != '<STR_LIT>' : <EOL> self . fail ( "<STR_LIT>" % data ) <EOL> if f . tell ( ) != <NUM_LIT:5> : <EOL> self . fail ( "<STR_LIT>" % f . tell ( ) ) <EOL> f . truncate ( ) <EOL> if f . tell ( ) != <NUM_LIT:5> : <EOL> self . fail ( "<STR_LIT>" % f . tell ( ) ) <EOL> f . close ( ) <EOL> size = os . path . getsize ( TESTFN ) <EOL> if size != <NUM_LIT:5> : <EOL> self . fail ( "<STR_LIT>" % size ) <EOL> try : <EOL> bug801631 ( ) <EOL> finally : <EOL> os . unlink ( TESTFN ) <EOL> @ impl_detail ( ) <EOL> def testIteration ( self ) : <EOL> dataoffset = <NUM_LIT> <EOL> filler = "<STR_LIT>" <EOL> assert not dataoffset % len ( filler ) , "<STR_LIT>" <EOL> nchunks = dataoffset // len ( filler ) <EOL> testlines = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] <EOL> methods = [ ( "<STR_LIT>" , ( ) ) , ( "<STR_LIT>" , ( ) ) , ( "<STR_LIT>" , ( ) ) , <EOL> ( "<STR_LIT>" , ( array ( "<STR_LIT:c>" , "<STR_LIT:U+0020>" * <NUM_LIT:100> ) , ) ) ] <EOL> try : <EOL> bag = open ( TESTFN , "<STR_LIT:w>" ) <EOL> bag . write ( filler * nchunks ) <EOL> bag . writelines ( testlines ) <EOL> bag . close ( ) <EOL> for methodname , args in methods : <EOL> f = open ( TESTFN ) <EOL> if f . next ( ) != filler : <EOL> self . fail , "<STR_LIT>" <EOL> meth = getattr ( f , methodname ) <EOL> try : <EOL> meth ( * args ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" % <EOL> ( methodname , args ) ) <EOL> f . close ( ) <EOL> f = open ( TESTFN ) <EOL> for i in range ( nchunks ) : <EOL> f . next ( ) <EOL> testline = testlines . pop ( <NUM_LIT:0> ) <EOL> try : <EOL> line = f . readline ( ) <EOL> except ValueError : <EOL> self . fail ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if line != testline : <EOL> self . fail ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( line , testline ) ) <EOL> testline = testlines . pop ( <NUM_LIT:0> ) <EOL> buf = array ( "<STR_LIT:c>" , "<STR_LIT:\x00>" * len ( testline ) ) <EOL> try : <EOL> f . readinto ( buf ) <EOL> except ValueError : <EOL> self . fail ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> line = buf . tostring ( ) <EOL> if line != testline : <EOL> self . fail ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( line , testline ) ) <EOL> testline = testlines . pop ( <NUM_LIT:0> ) <EOL> try : <EOL> line = f . read ( len ( testline ) ) <EOL> except ValueError : <EOL> self . fail ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if line != testline : <EOL> self . fail ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( line , testline ) ) <EOL> try : <EOL> lines = f . readlines ( ) <EOL> except ValueError : <EOL> self . fail ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if lines != testlines : <EOL> self . fail ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( line , testline ) ) <EOL> f = open ( TESTFN ) <EOL> try : <EOL> for line in f : <EOL> pass <EOL> try : <EOL> f . readline ( ) <EOL> f . readinto ( buf ) <EOL> f . read ( ) <EOL> f . readlines ( ) <EOL> except ValueError : <EOL> self . fail ( "<STR_LIT>" ) <EOL> finally : <EOL> f . close ( ) <EOL> finally : <EOL> os . unlink ( TESTFN ) <EOL> def test_main ( ) : <EOL> try : <EOL> run_unittest ( AutoFileTests , OtherFileTests ) <EOL> finally : <EOL> if os . path . exists ( TESTFN ) : <EOL> os . unlink ( TESTFN ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test_main ( ) </s>
