<s> import sys <EOL> import os <EOL> import unittest <EOL> from array import array <EOL> from weakref import proxy <EOL> import io <EOL> import _pyio as pyio <EOL> from test . support import TESTFN , run_unittest <EOL> from collections import UserList <EOL> class AutoFileTests : <EOL> def setUp ( self ) : <EOL> self . f = self . 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 ( b'<STR_LIT>' ) <EOL> self . assertEqual ( self . f . tell ( ) , p . tell ( ) ) <EOL> self . f . close ( ) <EOL> self . f = None <EOL> self . assertRaises ( ReferenceError , getattr , p , '<STR_LIT>' ) <EOL> def testAttributes ( self ) : <EOL> f = self . f <EOL> f . name <EOL> f . mode <EOL> f . closed <EOL> def testReadinto ( self ) : <EOL> self . f . write ( b'<STR_LIT>' ) <EOL> self . f . close ( ) <EOL> a = array ( '<STR_LIT:b>' , b'<STR_LIT:x>' * <NUM_LIT:10> ) <EOL> self . f = self . open ( TESTFN , '<STR_LIT:rb>' ) <EOL> n = self . f . readinto ( a ) <EOL> self . assertEqual ( b'<STR_LIT>' , a . tobytes ( ) [ : n ] ) <EOL> def testReadinto_text ( self ) : <EOL> a = array ( '<STR_LIT:b>' , b'<STR_LIT:x>' * <NUM_LIT:10> ) <EOL> self . f . close ( ) <EOL> self . f = self . open ( TESTFN , '<STR_LIT:r>' ) <EOL> if hasattr ( self . f , "<STR_LIT>" ) : <EOL> self . assertRaises ( TypeError , self . f . readinto , a ) <EOL> def testWritelinesUserList ( self ) : <EOL> l = UserList ( [ b'<STR_LIT:1>' , b'<STR_LIT:2>' ] ) <EOL> self . f . writelines ( l ) <EOL> self . f . close ( ) <EOL> self . f = self . open ( TESTFN , '<STR_LIT:rb>' ) <EOL> buf = self . f . read ( ) <EOL> self . assertEqual ( buf , b'<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 = UserList ( [ <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> self . assertRaises ( TypeError , self . f . writelines , <EOL> [ NonString ( ) , NonString ( ) ] ) <EOL> def testErrors ( self ) : <EOL> f = self . f <EOL> self . assertEqual ( f . name , TESTFN ) <EOL> self . assertTrue ( not f . isatty ( ) ) <EOL> self . assertTrue ( not f . closed ) <EOL> if hasattr ( f , "<STR_LIT>" ) : <EOL> self . assertRaises ( ( IOError , TypeError ) , f . readinto , "<STR_LIT>" ) <EOL> f . close ( ) <EOL> self . assertTrue ( f . closed ) <EOL> def testMethods ( self ) : <EOL> methods = [ ( '<STR_LIT>' , ( ) ) , <EOL> ( '<STR_LIT>' , ( ) ) , <EOL> ( '<STR_LIT>' , ( ) ) , <EOL> ( '<STR_LIT>' , ( ) ) , <EOL> ( '<STR_LIT>' , ( ) ) , <EOL> ( '<STR_LIT>' , ( b"<STR_LIT>" , ) ) , <EOL> ( '<STR_LIT>' , ( ) ) , <EOL> ( '<STR_LIT>' , ( ) ) , <EOL> ( '<STR_LIT>' , ( <NUM_LIT:0> , ) ) , <EOL> ( '<STR_LIT>' , ( ) ) , <EOL> ( '<STR_LIT>' , ( b"<STR_LIT>" , ) ) , <EOL> ( '<STR_LIT>' , ( [ ] , ) ) , <EOL> ( '<STR_LIT>' , ( ) ) , <EOL> ] <EOL> methods . append ( ( '<STR_LIT>' , ( ) ) ) <EOL> self . f . __exit__ ( None , None , None ) <EOL> self . assertTrue ( self . f . closed ) <EOL> for methodname , args in methods : <EOL> method = getattr ( self . f , methodname ) <EOL> self . assertRaises ( ValueError , method , * args ) <EOL> self . assertEqual ( self . f . __exit__ ( None , None , None ) , None ) <EOL> try : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> except : <EOL> self . assertEqual ( self . f . __exit__ ( * sys . exc_info ( ) ) , None ) <EOL> def testReadWhenWriting ( self ) : <EOL> self . assertRaises ( IOError , self . f . read ) <EOL> class CAutoFileTests ( AutoFileTests , unittest . TestCase ) : <EOL> open = io . open <EOL> class PyAutoFileTests ( AutoFileTests , unittest . TestCase ) : <EOL> open = staticmethod ( pyio . open ) <EOL> class OtherFileTests : <EOL> def testModeStrings ( self ) : <EOL> for mode in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> try : <EOL> f = self . open ( TESTFN , mode ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> f . close ( ) <EOL> self . fail ( '<STR_LIT>' % mode ) <EOL> def testBadModeArgument ( self ) : <EOL> bad_mode = "<STR_LIT>" <EOL> try : <EOL> f = self . open ( TESTFN , bad_mode ) <EOL> except ValueError as msg : <EOL> if msg . args [ <NUM_LIT:0> ] != <NUM_LIT:0> : <EOL> s = str ( msg ) <EOL> if TESTFN in s or bad_mode not in s : <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 = self . open ( TESTFN , '<STR_LIT:wb>' , s ) <EOL> f . write ( str ( s ) . encode ( "<STR_LIT:ascii>" ) ) <EOL> f . close ( ) <EOL> f . close ( ) <EOL> f = self . open ( TESTFN , '<STR_LIT:rb>' , s ) <EOL> d = int ( f . read ( ) . decode ( "<STR_LIT:ascii>" ) ) <EOL> f . close ( ) <EOL> f . close ( ) <EOL> except IOError as msg : <EOL> self . fail ( '<STR_LIT>' % ( s , str ( msg ) ) ) <EOL> self . assertEqual ( d , s ) <EOL> def testTruncateOnWindows ( self ) : <EOL> os . unlink ( TESTFN ) <EOL> f = self . open ( TESTFN , '<STR_LIT:wb>' ) <EOL> try : <EOL> f . write ( b'<STR_LIT>' ) <EOL> f . close ( ) <EOL> f = self . open ( TESTFN , '<STR_LIT>' ) <EOL> data = f . read ( <NUM_LIT:5> ) <EOL> if data != b'<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> finally : <EOL> f . close ( ) <EOL> os . unlink ( TESTFN ) <EOL> def testIteration ( self ) : <EOL> dataoffset = <NUM_LIT> <EOL> filler = b"<STR_LIT>" <EOL> assert not dataoffset % len ( filler ) , "<STR_LIT>" <EOL> nchunks = dataoffset // len ( filler ) <EOL> testlines = [ <EOL> b"<STR_LIT>" , <EOL> b"<STR_LIT>" , <EOL> b"<STR_LIT>" , <EOL> b"<STR_LIT>" , <EOL> b"<STR_LIT>" , <EOL> b"<STR_LIT>" <EOL> ] <EOL> methods = [ ( "<STR_LIT>" , ( ) ) , ( "<STR_LIT>" , ( ) ) , ( "<STR_LIT>" , ( ) ) , <EOL> ( "<STR_LIT>" , ( array ( "<STR_LIT:b>" , b"<STR_LIT:U+0020>" * <NUM_LIT:100> ) , ) ) ] <EOL> try : <EOL> bag = self . open ( TESTFN , "<STR_LIT:wb>" ) <EOL> bag . write ( filler * nchunks ) <EOL> bag . writelines ( testlines ) <EOL> bag . close ( ) <EOL> for methodname , args in methods : <EOL> f = self . open ( TESTFN , '<STR_LIT:rb>' ) <EOL> if next ( f ) != filler : <EOL> self . fail , "<STR_LIT>" <EOL> meth = getattr ( f , methodname ) <EOL> meth ( * args ) <EOL> f . close ( ) <EOL> f = self . open ( TESTFN , '<STR_LIT:rb>' ) <EOL> for i in range ( nchunks ) : <EOL> next ( f ) <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:b>" , b"<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 . tobytes ( ) <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 . close ( ) <EOL> f = self . open ( TESTFN , '<STR_LIT:rb>' ) <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> class COtherFileTests ( OtherFileTests , unittest . TestCase ) : <EOL> open = io . open <EOL> class PyOtherFileTests ( OtherFileTests , unittest . TestCase ) : <EOL> open = staticmethod ( pyio . open ) <EOL> def tearDownModule ( ) : <EOL> if os . path . exists ( TESTFN ) : <EOL> os . unlink ( TESTFN ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
