<s> from test . test_support import ( TESTFN , run_unittest , import_module , unlink , <EOL> requires , _2G , _4G ) <EOL> import unittest <EOL> import os , re , itertools , socket , sys <EOL> mmap = import_module ( '<STR_LIT>' ) <EOL> PAGESIZE = mmap . PAGESIZE <EOL> class MmapTests ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> if os . path . exists ( TESTFN ) : <EOL> os . unlink ( TESTFN ) <EOL> def tearDown ( self ) : <EOL> try : <EOL> os . unlink ( TESTFN ) <EOL> except OSError : <EOL> pass <EOL> def test_basic ( self ) : <EOL> f = open ( TESTFN , '<STR_LIT>' ) <EOL> try : <EOL> f . write ( '<STR_LIT>' * PAGESIZE ) <EOL> f . write ( '<STR_LIT:foo>' ) <EOL> f . write ( '<STR_LIT>' * ( PAGESIZE - <NUM_LIT:3> ) ) <EOL> f . flush ( ) <EOL> m = mmap . mmap ( f . fileno ( ) , <NUM_LIT:2> * PAGESIZE ) <EOL> f . close ( ) <EOL> tp = str ( type ( m ) ) <EOL> self . assertEqual ( m . find ( '<STR_LIT:foo>' ) , PAGESIZE ) <EOL> self . assertEqual ( len ( m ) , <NUM_LIT:2> * PAGESIZE ) <EOL> self . assertEqual ( m [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> self . assertEqual ( m [ <NUM_LIT:0> : <NUM_LIT:3> ] , '<STR_LIT>' ) <EOL> self . assertRaises ( IndexError , m . __getitem__ , len ( m ) ) <EOL> self . assertRaises ( IndexError , m . __setitem__ , len ( m ) , '<STR_LIT>' ) <EOL> m [ <NUM_LIT:0> ] = '<STR_LIT:3>' <EOL> m [ PAGESIZE + <NUM_LIT:3> : PAGESIZE + <NUM_LIT:3> + <NUM_LIT:3> ] = '<STR_LIT:bar>' <EOL> self . assertEqual ( m [ <NUM_LIT:0> ] , '<STR_LIT:3>' ) <EOL> self . assertEqual ( m [ <NUM_LIT:0> : <NUM_LIT:3> ] , '<STR_LIT>' ) <EOL> self . assertEqual ( m [ PAGESIZE - <NUM_LIT:1> : PAGESIZE + <NUM_LIT:7> ] , '<STR_LIT>' ) <EOL> m . flush ( ) <EOL> match = re . search ( '<STR_LIT>' , m ) <EOL> if match is None : <EOL> self . fail ( '<STR_LIT>' ) <EOL> else : <EOL> start , end = match . span ( <NUM_LIT:0> ) <EOL> length = end - start <EOL> self . assertEqual ( start , PAGESIZE ) <EOL> self . assertEqual ( end , PAGESIZE + <NUM_LIT:6> ) <EOL> m . seek ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> self . assertEqual ( m . tell ( ) , <NUM_LIT:0> ) <EOL> m . seek ( <NUM_LIT> , <NUM_LIT:1> ) <EOL> self . assertEqual ( m . tell ( ) , <NUM_LIT> ) <EOL> m . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> self . assertEqual ( m . tell ( ) , len ( m ) ) <EOL> self . assertRaises ( ValueError , m . seek , - <NUM_LIT:1> ) <EOL> self . assertRaises ( ValueError , m . seek , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> self . assertRaises ( ValueError , m . seek , - len ( m ) - <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> try : <EOL> m . resize ( <NUM_LIT> ) <EOL> except SystemError : <EOL> pass <EOL> else : <EOL> self . assertEqual ( len ( m ) , <NUM_LIT> ) <EOL> self . assertRaises ( ValueError , m . seek , <NUM_LIT> , <NUM_LIT:0> ) <EOL> f = open ( TESTFN ) <EOL> f . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> self . assertEqual ( f . tell ( ) , <NUM_LIT> ) <EOL> f . close ( ) <EOL> self . assertEqual ( m . size ( ) , <NUM_LIT> ) <EOL> m . close ( ) <EOL> finally : <EOL> try : <EOL> f . close ( ) <EOL> except OSError : <EOL> pass <EOL> def test_access_parameter ( self ) : <EOL> mapsize = <NUM_LIT:10> <EOL> with open ( TESTFN , "<STR_LIT:wb>" ) as f : <EOL> f . write ( "<STR_LIT:a>" * mapsize ) <EOL> f = open ( TESTFN , "<STR_LIT:rb>" ) <EOL> m = mmap . mmap ( f . fileno ( ) , mapsize , access = mmap . ACCESS_READ ) <EOL> self . assertEqual ( m [ : ] , '<STR_LIT:a>' * mapsize , "<STR_LIT>" ) <EOL> try : <EOL> m [ : ] = '<STR_LIT:b>' * mapsize <EOL> except TypeError : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> try : <EOL> m [ <NUM_LIT:0> ] = '<STR_LIT:b>' <EOL> except TypeError : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> try : <EOL> m . seek ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> m . write ( '<STR_LIT:abc>' ) <EOL> except TypeError : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> try : <EOL> m . seek ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> m . write_byte ( '<STR_LIT:d>' ) <EOL> except TypeError : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> try : <EOL> m . resize ( <NUM_LIT:2> * mapsize ) <EOL> except SystemError : <EOL> pass <EOL> except TypeError : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> f . close ( ) <EOL> del m , f <EOL> self . assertEqual ( open ( TESTFN , "<STR_LIT:rb>" ) . read ( ) , '<STR_LIT:a>' * mapsize , <EOL> "<STR_LIT>" ) <EOL> import sys <EOL> f = open ( TESTFN , "<STR_LIT>" ) <EOL> try : <EOL> m = mmap . mmap ( f . fileno ( ) , mapsize + <NUM_LIT:1> ) <EOL> except ValueError : <EOL> if sys . platform . startswith ( '<STR_LIT>' ) : <EOL> self . fail ( "<STR_LIT>" ) <EOL> else : <EOL> if not sys . platform . startswith ( '<STR_LIT>' ) : <EOL> self . fail ( "<STR_LIT>" ) <EOL> m . close ( ) <EOL> f . close ( ) <EOL> if sys . platform . startswith ( '<STR_LIT>' ) : <EOL> f = open ( TESTFN , '<STR_LIT>' ) <EOL> f . truncate ( mapsize ) <EOL> f . close ( ) <EOL> f = open ( TESTFN , "<STR_LIT>" ) <EOL> m = mmap . mmap ( f . fileno ( ) , mapsize , access = mmap . ACCESS_WRITE ) <EOL> m [ : ] = '<STR_LIT:c>' * mapsize <EOL> self . assertEqual ( m [ : ] , '<STR_LIT:c>' * mapsize , <EOL> "<STR_LIT>" ) <EOL> m . flush ( ) <EOL> m . close ( ) <EOL> f . close ( ) <EOL> f = open ( TESTFN , '<STR_LIT:rb>' ) <EOL> stuff = f . read ( ) <EOL> f . close ( ) <EOL> self . assertEqual ( stuff , '<STR_LIT:c>' * mapsize , <EOL> "<STR_LIT>" ) <EOL> f = open ( TESTFN , "<STR_LIT>" ) <EOL> m = mmap . mmap ( f . fileno ( ) , mapsize , access = mmap . ACCESS_COPY ) <EOL> m [ : ] = '<STR_LIT:d>' * mapsize <EOL> self . assertEqual ( m [ : ] , '<STR_LIT:d>' * mapsize , <EOL> "<STR_LIT>" ) <EOL> m . flush ( ) <EOL> self . assertEqual ( open ( TESTFN , "<STR_LIT:rb>" ) . read ( ) , '<STR_LIT:c>' * mapsize , <EOL> "<STR_LIT>" ) <EOL> self . assertRaises ( TypeError , m . resize , <NUM_LIT:2> * mapsize ) <EOL> f . close ( ) <EOL> del m , f <EOL> f = open ( TESTFN , "<STR_LIT>" ) <EOL> self . assertRaises ( ValueError , mmap . mmap , f . fileno ( ) , mapsize , access = <NUM_LIT:4> ) <EOL> f . close ( ) <EOL> if os . name == "<STR_LIT>" : <EOL> f = open ( TESTFN , "<STR_LIT>" ) <EOL> self . assertRaises ( ValueError , mmap . mmap , f . fileno ( ) , mapsize , <EOL> flags = mmap . MAP_PRIVATE , <EOL> prot = mmap . PROT_READ , access = mmap . ACCESS_WRITE ) <EOL> f . close ( ) <EOL> prot = mmap . PROT_READ | getattr ( mmap , '<STR_LIT>' , <NUM_LIT:0> ) <EOL> with open ( TESTFN , "<STR_LIT>" ) as f : <EOL> m = mmap . mmap ( f . fileno ( ) , mapsize , prot = prot ) <EOL> self . assertRaises ( TypeError , m . write , b"<STR_LIT>" ) <EOL> self . assertRaises ( TypeError , m . write_byte , <NUM_LIT:0> ) <EOL> m . close ( ) <EOL> def test_bad_file_desc ( self ) : <EOL> self . assertRaises ( mmap . error , mmap . mmap , - <NUM_LIT:2> , <NUM_LIT> ) <EOL> def test_tougher_find ( self ) : <EOL> f = open ( TESTFN , '<STR_LIT>' ) <EOL> data = '<STR_LIT>' <EOL> n = len ( data ) <EOL> f . write ( data ) <EOL> f . flush ( ) <EOL> m = mmap . mmap ( f . fileno ( ) , n ) <EOL> f . close ( ) <EOL> for start in range ( n + <NUM_LIT:1> ) : <EOL> for finish in range ( start , n + <NUM_LIT:1> ) : <EOL> slice = data [ start : finish ] <EOL> self . assertEqual ( m . find ( slice ) , data . find ( slice ) ) <EOL> self . assertEqual ( m . find ( slice + '<STR_LIT:x>' ) , - <NUM_LIT:1> ) <EOL> m . close ( ) <EOL> def test_find_end ( self ) : <EOL> f = open ( TESTFN , '<STR_LIT>' ) <EOL> data = '<STR_LIT>' <EOL> n = len ( data ) <EOL> f . write ( data ) <EOL> f . flush ( ) <EOL> m = mmap . mmap ( f . fileno ( ) , n ) <EOL> f . close ( ) <EOL> self . assertEqual ( m . find ( '<STR_LIT>' ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( m . find ( '<STR_LIT>' ) , <NUM_LIT:8> ) <EOL> self . assertEqual ( m . find ( '<STR_LIT>' , <NUM_LIT:0> , - <NUM_LIT:1> ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( m . find ( '<STR_LIT>' , <NUM_LIT:1> ) , <NUM_LIT:8> ) <EOL> self . assertEqual ( m . find ( '<STR_LIT>' , <NUM_LIT:1> , - <NUM_LIT:1> ) , <NUM_LIT:8> ) <EOL> self . assertEqual ( m . find ( '<STR_LIT>' , <NUM_LIT:1> , - <NUM_LIT:2> ) , - <NUM_LIT:1> ) <EOL> def test_rfind ( self ) : <EOL> f = open ( TESTFN , '<STR_LIT>' ) <EOL> data = '<STR_LIT>' <EOL> n = len ( data ) <EOL> f . write ( data ) <EOL> f . flush ( ) <EOL> m = mmap . mmap ( f . fileno ( ) , n ) <EOL> f . close ( ) <EOL> self . assertEqual ( m . rfind ( '<STR_LIT>' ) , <NUM_LIT:8> ) <EOL> self . assertEqual ( m . rfind ( '<STR_LIT>' ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( m . rfind ( '<STR_LIT>' , <NUM_LIT:0> , - <NUM_LIT:1> ) , <NUM_LIT:8> ) <EOL> self . assertEqual ( m . rfind ( '<STR_LIT>' , <NUM_LIT:0> , - <NUM_LIT:2> ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( m . rfind ( '<STR_LIT>' , <NUM_LIT:1> , - <NUM_LIT:1> ) , <NUM_LIT:8> ) <EOL> self . assertEqual ( m . rfind ( '<STR_LIT>' , <NUM_LIT:1> , - <NUM_LIT:2> ) , - <NUM_LIT:1> ) <EOL> def test_double_close ( self ) : <EOL> f = open ( TESTFN , '<STR_LIT>' ) <EOL> f . write ( <NUM_LIT:2> ** <NUM_LIT:16> * '<STR_LIT:a>' ) <EOL> f . close ( ) <EOL> f = open ( TESTFN ) <EOL> mf = mmap . mmap ( f . fileno ( ) , <NUM_LIT:2> ** <NUM_LIT:16> , access = mmap . ACCESS_READ ) <EOL> mf . close ( ) <EOL> mf . close ( ) <EOL> f . close ( ) <EOL> @ unittest . skipUnless ( hasattr ( os , "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> def test_entire_file ( self ) : <EOL> f = open ( TESTFN , "<STR_LIT>" ) <EOL> f . write ( <NUM_LIT:2> ** <NUM_LIT:16> * '<STR_LIT:m>' ) <EOL> f . close ( ) <EOL> f = open ( TESTFN , "<STR_LIT>" ) <EOL> mf = mmap . mmap ( f . fileno ( ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( mf ) , <NUM_LIT:2> ** <NUM_LIT:16> , "<STR_LIT>" ) <EOL> self . assertEqual ( mf . read ( <NUM_LIT:2> ** <NUM_LIT:16> ) , <NUM_LIT:2> ** <NUM_LIT:16> * "<STR_LIT:m>" ) <EOL> mf . close ( ) <EOL> f . close ( ) <EOL> @ unittest . skipUnless ( hasattr ( os , "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> def test_length_0_offset ( self ) : <EOL> with open ( TESTFN , "<STR_LIT:wb>" ) as f : <EOL> f . write ( ( <NUM_LIT> * <NUM_LIT:2> ) * b'<STR_LIT:m>' ) <EOL> with open ( TESTFN , "<STR_LIT:rb>" ) as f : <EOL> mf = mmap . mmap ( f . fileno ( ) , <NUM_LIT:0> , offset = <NUM_LIT> , access = mmap . ACCESS_READ ) <EOL> try : <EOL> self . assertRaises ( IndexError , mf . __getitem__ , <NUM_LIT> ) <EOL> finally : <EOL> mf . close ( ) <EOL> @ unittest . skipUnless ( hasattr ( os , "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> def test_length_0_large_offset ( self ) : <EOL> with open ( TESTFN , "<STR_LIT:wb>" ) as f : <EOL> f . write ( <NUM_LIT> * b'<STR_LIT:m>' ) <EOL> with open ( TESTFN , "<STR_LIT>" ) as f : <EOL> self . assertRaises ( ValueError , mmap . mmap , f . fileno ( ) , <NUM_LIT:0> , <EOL> offset = <NUM_LIT> ) <EOL> def test_move ( self ) : <EOL> f = open ( TESTFN , '<STR_LIT>' ) <EOL> f . write ( "<STR_LIT>" ) <EOL> f . flush ( ) <EOL> mf = mmap . mmap ( f . fileno ( ) , <NUM_LIT:10> ) <EOL> mf . move ( <NUM_LIT:5> , <NUM_LIT:0> , <NUM_LIT:5> ) <EOL> self . assertEqual ( mf [ : ] , "<STR_LIT>" , "<STR_LIT>" ) <EOL> mf . close ( ) <EOL> f . close ( ) <EOL> data = "<STR_LIT>" <EOL> for dest in range ( len ( data ) ) : <EOL> for src in range ( len ( data ) ) : <EOL> for count in range ( len ( data ) - max ( dest , src ) ) : <EOL> expected = data [ : dest ] + data [ src : src + count ] + data [ dest + count : ] <EOL> m = mmap . mmap ( - <NUM_LIT:1> , len ( data ) ) <EOL> m [ : ] = data <EOL> m . move ( dest , src , count ) <EOL> self . assertEqual ( m [ : ] , expected ) <EOL> m . close ( ) <EOL> m = mmap . mmap ( - <NUM_LIT:1> , <NUM_LIT:100> ) <EOL> offsets = [ - <NUM_LIT:100> , - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:100> ] <EOL> for source , dest , size in itertools . product ( offsets , offsets , offsets ) : <EOL> try : <EOL> m . move ( source , dest , size ) <EOL> except ValueError : <EOL> pass <EOL> offsets = [ ( - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) , ( - <NUM_LIT:1> , - <NUM_LIT:1> , <NUM_LIT:0> ) , ( - <NUM_LIT:1> , <NUM_LIT:0> , - <NUM_LIT:1> ) , ( <NUM_LIT:0> , - <NUM_LIT:1> , - <NUM_LIT:1> ) , <EOL> ( - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) , ( <NUM_LIT:0> , - <NUM_LIT:1> , <NUM_LIT:0> ) , ( <NUM_LIT:0> , <NUM_LIT:0> , - <NUM_LIT:1> ) ] <EOL> for source , dest , size in offsets : <EOL> self . assertRaises ( ValueError , m . move , source , dest , size ) <EOL> m . close ( ) <EOL> m = mmap . mmap ( - <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> self . assertRaises ( ValueError , m . move , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> self . assertRaises ( ValueError , m . move , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> self . assertRaises ( ValueError , m . move , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> m . move ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> m . move ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> def test_anonymous ( self ) : <EOL> m = mmap . mmap ( - <NUM_LIT:1> , PAGESIZE ) <EOL> for x in xrange ( PAGESIZE ) : <EOL> self . assertEqual ( m [ x ] , '<STR_LIT>' , "<STR_LIT>" ) <EOL> for x in xrange ( PAGESIZE ) : <EOL> m [ x ] = ch = chr ( x & <NUM_LIT:255> ) <EOL> self . assertEqual ( m [ x ] , ch ) <EOL> def test_extended_getslice ( self ) : <EOL> s = "<STR_LIT>" . join ( chr ( c ) for c in reversed ( range ( <NUM_LIT> ) ) ) <EOL> m = mmap . mmap ( - <NUM_LIT:1> , len ( s ) ) <EOL> m [ : ] = s <EOL> self . assertEqual ( m [ : ] , s ) <EOL> indices = ( <NUM_LIT:0> , None , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT> , <NUM_LIT> , - <NUM_LIT:1> , - <NUM_LIT:2> , - <NUM_LIT> , - <NUM_LIT> ) <EOL> for start in indices : <EOL> for stop in indices : <EOL> for step in indices [ <NUM_LIT:1> : ] : <EOL> self . assertEqual ( m [ start : stop : step ] , <EOL> s [ start : stop : step ] ) <EOL> def test_extended_set_del_slice ( self ) : <EOL> s = "<STR_LIT>" . join ( chr ( c ) for c in reversed ( range ( <NUM_LIT> ) ) ) <EOL> m = mmap . mmap ( - <NUM_LIT:1> , len ( s ) ) <EOL> indices = ( <NUM_LIT:0> , None , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT> , <NUM_LIT> , - <NUM_LIT:1> , - <NUM_LIT:2> , - <NUM_LIT> , - <NUM_LIT> ) <EOL> for start in indices : <EOL> for stop in indices : <EOL> for step in indices [ <NUM_LIT:1> : ] : <EOL> m [ : ] = s <EOL> self . assertEqual ( m [ : ] , s ) <EOL> L = list ( s ) <EOL> data = L [ start : stop : step ] <EOL> data = "<STR_LIT>" . join ( reversed ( data ) ) <EOL> L [ start : stop : step ] = data <EOL> m [ start : stop : step ] = data <EOL> self . assertEqual ( m [ : ] , "<STR_LIT>" . join ( L ) ) <EOL> def make_mmap_file ( self , f , halfsize ) : <EOL> f . write ( '<STR_LIT>' * halfsize ) <EOL> f . write ( '<STR_LIT:foo>' ) <EOL> f . write ( '<STR_LIT>' * ( halfsize - <NUM_LIT:3> ) ) <EOL> f . flush ( ) <EOL> return mmap . mmap ( f . fileno ( ) , <NUM_LIT:0> ) <EOL> def test_empty_file ( self ) : <EOL> f = open ( TESTFN , '<STR_LIT>' ) <EOL> f . close ( ) <EOL> with open ( TESTFN , "<STR_LIT:rb>" ) as f : <EOL> self . assertRaisesRegexp ( ValueError , <EOL> "<STR_LIT>" , <EOL> mmap . mmap , f . fileno ( ) , <NUM_LIT:0> , <EOL> access = mmap . ACCESS_READ ) <EOL> def test_offset ( self ) : <EOL> f = open ( TESTFN , '<STR_LIT>' ) <EOL> try : <EOL> halfsize = mmap . ALLOCATIONGRANULARITY <EOL> m = self . make_mmap_file ( f , halfsize ) <EOL> m . close ( ) <EOL> f . close ( ) <EOL> mapsize = halfsize * <NUM_LIT:2> <EOL> f = open ( TESTFN , "<STR_LIT>" ) <EOL> for offset in [ - <NUM_LIT:2> , - <NUM_LIT:1> , None ] : <EOL> try : <EOL> m = mmap . mmap ( f . fileno ( ) , mapsize , offset = offset ) <EOL> self . assertEqual ( <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> except ( ValueError , TypeError , OverflowError ) : <EOL> pass <EOL> else : <EOL> self . assertEqual ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> f . close ( ) <EOL> f = open ( TESTFN , "<STR_LIT>" ) <EOL> m = mmap . mmap ( f . fileno ( ) , mapsize - halfsize , offset = halfsize ) <EOL> self . assertEqual ( m [ <NUM_LIT:0> : <NUM_LIT:3> ] , '<STR_LIT:foo>' ) <EOL> f . close ( ) <EOL> try : <EOL> m . resize ( <NUM_LIT> ) <EOL> except SystemError : <EOL> pass <EOL> else : <EOL> self . assertEqual ( len ( m ) , <NUM_LIT> ) <EOL> self . assertRaises ( ValueError , m . seek , <NUM_LIT> , <NUM_LIT:0> ) <EOL> self . assertEqual ( m [ <NUM_LIT:0> : <NUM_LIT:3> ] , '<STR_LIT:foo>' ) <EOL> f = open ( TESTFN ) <EOL> f . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> self . assertEqual ( f . tell ( ) , halfsize + <NUM_LIT> ) <EOL> f . close ( ) <EOL> self . assertEqual ( m . size ( ) , halfsize + <NUM_LIT> ) <EOL> m . close ( ) <EOL> finally : <EOL> f . close ( ) <EOL> try : <EOL> os . unlink ( TESTFN ) <EOL> except OSError : <EOL> pass <EOL> def test_subclass ( self ) : <EOL> class anon_mmap ( mmap . mmap ) : <EOL> def __new__ ( klass , * args , ** kwargs ) : <EOL> return mmap . mmap . __new__ ( klass , - <NUM_LIT:1> , * args , ** kwargs ) <EOL> anon_mmap ( PAGESIZE ) <EOL> @ unittest . skipUnless ( hasattr ( mmap , '<STR_LIT>' ) , "<STR_LIT>" ) <EOL> def test_prot_readonly ( self ) : <EOL> mapsize = <NUM_LIT:10> <EOL> with open ( TESTFN , "<STR_LIT:wb>" ) as f : <EOL> f . write ( "<STR_LIT:a>" * mapsize ) <EOL> f = open ( TESTFN , "<STR_LIT:rb>" ) <EOL> m = mmap . mmap ( f . fileno ( ) , mapsize , prot = mmap . PROT_READ ) <EOL> self . assertRaises ( TypeError , m . write , "<STR_LIT:foo>" ) <EOL> f . close ( ) <EOL> def test_error ( self ) : <EOL> self . assertTrue ( issubclass ( mmap . error , EnvironmentError ) ) <EOL> self . assertIn ( "<STR_LIT>" , str ( mmap . error ) ) <EOL> def test_io_methods ( self ) : <EOL> data = "<STR_LIT>" <EOL> with open ( TESTFN , "<STR_LIT:wb>" ) as f : <EOL> f . write ( "<STR_LIT:x>" * len ( data ) ) <EOL> f = open ( TESTFN , "<STR_LIT>" ) <EOL> m = mmap . mmap ( f . fileno ( ) , len ( data ) ) <EOL> f . close ( ) <EOL> for i in xrange ( len ( data ) ) : <EOL> self . assertEqual ( m . tell ( ) , i ) <EOL> m . write_byte ( data [ i ] ) <EOL> self . assertEqual ( m . tell ( ) , i + <NUM_LIT:1> ) <EOL> self . assertRaises ( ValueError , m . write_byte , "<STR_LIT:x>" ) <EOL> self . assertEqual ( m [ : ] , data ) <EOL> m . seek ( <NUM_LIT:0> ) <EOL> for i in xrange ( len ( data ) ) : <EOL> self . assertEqual ( m . tell ( ) , i ) <EOL> self . assertEqual ( m . read_byte ( ) , data [ i ] ) <EOL> self . assertEqual ( m . tell ( ) , i + <NUM_LIT:1> ) <EOL> self . assertRaises ( ValueError , m . read_byte ) <EOL> m . seek ( <NUM_LIT:3> ) <EOL> self . assertEqual ( m . read ( <NUM_LIT:3> ) , "<STR_LIT>" ) <EOL> self . assertEqual ( m . tell ( ) , <NUM_LIT:6> ) <EOL> m . seek ( <NUM_LIT:3> ) <EOL> m . write ( "<STR_LIT:bar>" ) <EOL> self . assertEqual ( m . tell ( ) , <NUM_LIT:6> ) <EOL> self . assertEqual ( m [ : ] , "<STR_LIT>" ) <EOL> m . seek ( <NUM_LIT:8> ) <EOL> self . assertRaises ( ValueError , m . write , "<STR_LIT:bar>" ) <EOL> @ unittest . skipUnless ( os . name == '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_tagname ( self ) : <EOL> data1 = "<STR_LIT>" <EOL> data2 = "<STR_LIT>" <EOL> assert len ( data1 ) == len ( data2 ) <EOL> m1 = mmap . mmap ( - <NUM_LIT:1> , len ( data1 ) , tagname = "<STR_LIT:foo>" ) <EOL> m1 [ : ] = data1 <EOL> m2 = mmap . mmap ( - <NUM_LIT:1> , len ( data2 ) , tagname = "<STR_LIT:foo>" ) <EOL> m2 [ : ] = data2 <EOL> self . assertEqual ( m1 [ : ] , data2 ) <EOL> self . assertEqual ( m2 [ : ] , data2 ) <EOL> m2 . close ( ) <EOL> m1 . close ( ) <EOL> m1 = mmap . mmap ( - <NUM_LIT:1> , len ( data1 ) , tagname = "<STR_LIT:foo>" ) <EOL> m1 [ : ] = data1 <EOL> m2 = mmap . mmap ( - <NUM_LIT:1> , len ( data2 ) , tagname = "<STR_LIT>" ) <EOL> m2 [ : ] = data2 <EOL> self . assertEqual ( m1 [ : ] , data1 ) <EOL> self . assertEqual ( m2 [ : ] , data2 ) <EOL> m2 . close ( ) <EOL> m1 . close ( ) <EOL> @ unittest . skipUnless ( os . name == '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_crasher_on_windows ( self ) : <EOL> m = mmap . mmap ( - <NUM_LIT:1> , <NUM_LIT:1000> , tagname = "<STR_LIT:foo>" ) <EOL> try : <EOL> mmap . mmap ( - <NUM_LIT:1> , <NUM_LIT> , tagname = "<STR_LIT:foo>" ) [ : ] <EOL> except : <EOL> pass <EOL> m . close ( ) <EOL> with open ( TESTFN , "<STR_LIT:wb>" ) as f : <EOL> f . write ( "<STR_LIT:x>" * <NUM_LIT:10> ) <EOL> f = open ( TESTFN , "<STR_LIT>" ) <EOL> m = mmap . mmap ( f . fileno ( ) , <NUM_LIT:0> ) <EOL> f . close ( ) <EOL> try : <EOL> m . resize ( <NUM_LIT:0> ) <EOL> except : <EOL> pass <EOL> try : <EOL> m [ : ] <EOL> except : <EOL> pass <EOL> m . close ( ) <EOL> @ unittest . skipUnless ( os . name == '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_invalid_descriptor ( self ) : <EOL> s = socket . socket ( ) <EOL> try : <EOL> with self . assertRaises ( mmap . error ) : <EOL> m = mmap . mmap ( s . fileno ( ) , <NUM_LIT:10> ) <EOL> finally : <EOL> s . close ( ) <EOL> class LargeMmapTests ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> unlink ( TESTFN ) <EOL> def tearDown ( self ) : <EOL> unlink ( TESTFN ) <EOL> def _make_test_file ( self , num_zeroes , tail ) : <EOL> if sys . platform [ : <NUM_LIT:3> ] == '<STR_LIT>' or sys . platform == '<STR_LIT>' : <EOL> requires ( '<STR_LIT>' , <EOL> '<STR_LIT>' % str ( <NUM_LIT> ) ) <EOL> f = open ( TESTFN , '<STR_LIT>' ) <EOL> try : <EOL> f . seek ( num_zeroes ) <EOL> f . write ( tail ) <EOL> f . flush ( ) <EOL> except ( IOError , OverflowError ) : <EOL> f . close ( ) <EOL> raise unittest . SkipTest ( "<STR_LIT>" ) <EOL> return f <EOL> def test_large_offset ( self ) : <EOL> with self . _make_test_file ( <NUM_LIT> , b"<STR_LIT:U+0020>" ) as f : <EOL> m = mmap . mmap ( f . fileno ( ) , <NUM_LIT:0> , offset = <NUM_LIT> , access = mmap . ACCESS_READ ) <EOL> try : <EOL> self . assertEqual ( m [ <NUM_LIT> ] , b"<STR_LIT:U+0020>" ) <EOL> finally : <EOL> m . close ( ) <EOL> def test_large_filesize ( self ) : <EOL> with self . _make_test_file ( <NUM_LIT> , b"<STR_LIT:U+0020>" ) as f : <EOL> if sys . maxsize < <NUM_LIT> : <EOL> with self . assertRaises ( OverflowError ) : <EOL> mmap . mmap ( f . fileno ( ) , <NUM_LIT> , access = mmap . ACCESS_READ ) <EOL> with self . assertRaises ( ValueError ) : <EOL> mmap . mmap ( f . fileno ( ) , <NUM_LIT:0> , access = mmap . ACCESS_READ ) <EOL> m = mmap . mmap ( f . fileno ( ) , <NUM_LIT> , access = mmap . ACCESS_READ ) <EOL> try : <EOL> self . assertEqual ( m . size ( ) , <NUM_LIT> ) <EOL> finally : <EOL> m . close ( ) <EOL> def _test_around_boundary ( self , boundary ) : <EOL> tail = b'<STR_LIT>' <EOL> start = boundary - len ( tail ) // <NUM_LIT:2> <EOL> end = start + len ( tail ) <EOL> with self . _make_test_file ( start , tail ) as f : <EOL> m = mmap . mmap ( f . fileno ( ) , <NUM_LIT:0> , access = mmap . ACCESS_READ ) <EOL> try : <EOL> self . assertEqual ( m [ start : end ] , tail ) <EOL> finally : <EOL> m . close ( ) <EOL> @ unittest . skipUnless ( sys . maxsize > _4G , "<STR_LIT>" ) <EOL> def test_around_2GB ( self ) : <EOL> self . _test_around_boundary ( _2G ) <EOL> @ unittest . skipUnless ( sys . maxsize > _4G , "<STR_LIT>" ) <EOL> def test_around_4GB ( self ) : <EOL> self . _test_around_boundary ( _4G ) <EOL> def test_main ( ) : <EOL> run_unittest ( MmapTests , LargeMmapTests ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test_main ( ) </s>
