<s> from test . test_support import verify , vereq , TESTFN <EOL> import mmap <EOL> import os , re <EOL> PAGESIZE = mmap . PAGESIZE <EOL> def test_both ( ) : <EOL> "<STR_LIT>" <EOL> if os . path . exists ( TESTFN ) : <EOL> os . unlink ( TESTFN ) <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> print type ( m ) <EOL> print '<STR_LIT>' , m . find ( '<STR_LIT:foo>' ) / float ( PAGESIZE ) , '<STR_LIT>' <EOL> vereq ( m . find ( '<STR_LIT:foo>' ) , PAGESIZE ) <EOL> print '<STR_LIT>' , len ( m ) / float ( PAGESIZE ) , '<STR_LIT>' <EOL> vereq ( len ( m ) , <NUM_LIT:2> * PAGESIZE ) <EOL> print '<STR_LIT>' , repr ( m [ <NUM_LIT:0> ] ) <EOL> vereq ( m [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> print '<STR_LIT>' , repr ( m [ <NUM_LIT:0> : <NUM_LIT:3> ] ) <EOL> vereq ( m [ <NUM_LIT:0> : <NUM_LIT:3> ] , '<STR_LIT>' ) <EOL> print "<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> print '<STR_LIT>' , repr ( m [ <NUM_LIT:0> ] ) <EOL> vereq ( m [ <NUM_LIT:0> ] , '<STR_LIT:3>' ) <EOL> print '<STR_LIT>' , repr ( m [ <NUM_LIT:0> : <NUM_LIT:3> ] ) <EOL> vereq ( m [ <NUM_LIT:0> : <NUM_LIT:3> ] , '<STR_LIT>' ) <EOL> print '<STR_LIT>' , repr ( m [ PAGESIZE - <NUM_LIT:1> : PAGESIZE + <NUM_LIT:7> ] ) <EOL> vereq ( 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> print '<STR_LIT>' <EOL> else : <EOL> start , end = match . span ( <NUM_LIT:0> ) <EOL> length = end - start <EOL> print '<STR_LIT>' , <EOL> print start / float ( PAGESIZE ) , length <EOL> vereq ( start , PAGESIZE ) <EOL> vereq ( end , PAGESIZE + <NUM_LIT:6> ) <EOL> m . seek ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> print '<STR_LIT>' <EOL> vereq ( m . tell ( ) , <NUM_LIT:0> ) <EOL> m . seek ( <NUM_LIT> , <NUM_LIT:1> ) <EOL> print '<STR_LIT>' <EOL> vereq ( m . tell ( ) , <NUM_LIT> ) <EOL> m . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> print '<STR_LIT>' <EOL> vereq ( m . tell ( ) , len ( m ) ) <EOL> print '<STR_LIT>' <EOL> try : <EOL> m . seek ( - <NUM_LIT:1> ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> verify ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> print '<STR_LIT>' <EOL> try : <EOL> m . seek ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> verify ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> print '<STR_LIT>' <EOL> try : <EOL> m . seek ( - len ( m ) - <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> verify ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> print '<STR_LIT>' <EOL> try : <EOL> m . resize ( <NUM_LIT> ) <EOL> except SystemError : <EOL> pass <EOL> else : <EOL> verify ( len ( m ) == <NUM_LIT> , <EOL> "<STR_LIT>" % ( len ( m ) , ) ) <EOL> try : <EOL> m . seek ( <NUM_LIT> , <NUM_LIT:0> ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> verify ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> f = open ( TESTFN ) <EOL> f . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> verify ( f . tell ( ) == <NUM_LIT> , '<STR_LIT>' ) <EOL> f . close ( ) <EOL> verify ( m . size ( ) == <NUM_LIT> , '<STR_LIT>' ) <EOL> m . close ( ) <EOL> finally : <EOL> try : <EOL> f . close ( ) <EOL> except OSError : <EOL> pass <EOL> try : <EOL> os . unlink ( TESTFN ) <EOL> except OSError : <EOL> pass <EOL> try : <EOL> mapsize = <NUM_LIT:10> <EOL> print "<STR_LIT>" , mapsize , "<STR_LIT>" <EOL> open ( TESTFN , "<STR_LIT:wb>" ) . write ( "<STR_LIT:a>" * mapsize ) <EOL> print "<STR_LIT>" <EOL> f = open ( TESTFN , "<STR_LIT:rb>" ) <EOL> m = mmap . mmap ( f . fileno ( ) , mapsize , access = mmap . ACCESS_READ ) <EOL> verify ( m [ : ] == '<STR_LIT:a>' * mapsize , "<STR_LIT>" ) <EOL> print "<STR_LIT>" <EOL> try : <EOL> m [ : ] = '<STR_LIT:b>' * mapsize <EOL> except TypeError : <EOL> pass <EOL> else : <EOL> verify ( <NUM_LIT:0> , "<STR_LIT>" ) <EOL> print "<STR_LIT>" <EOL> try : <EOL> m [ <NUM_LIT:0> ] = '<STR_LIT:b>' <EOL> except TypeError : <EOL> pass <EOL> else : <EOL> verify ( <NUM_LIT:0> , "<STR_LIT>" ) <EOL> print "<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> verify ( <NUM_LIT:0> , "<STR_LIT>" ) <EOL> print "<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> verify ( <NUM_LIT:0> , "<STR_LIT>" ) <EOL> print "<STR_LIT>" <EOL> try : <EOL> m . resize ( <NUM_LIT:2> * mapsize ) <EOL> except SystemError : <EOL> pass <EOL> except TypeError : <EOL> pass <EOL> else : <EOL> verify ( <NUM_LIT:0> , "<STR_LIT>" ) <EOL> del m , f <EOL> verify ( open ( TESTFN , "<STR_LIT:rb>" ) . read ( ) == '<STR_LIT:a>' * mapsize , <EOL> "<STR_LIT>" ) <EOL> print "<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> verify ( <NUM_LIT:0> , "<STR_LIT>" ) <EOL> else : <EOL> if not sys . platform . startswith ( '<STR_LIT>' ) : <EOL> verify ( <NUM_LIT:0> , "<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> print "<STR_LIT>" <EOL> f = open ( TESTFN , "<STR_LIT>" ) <EOL> m = mmap . mmap ( f . fileno ( ) , mapsize , access = mmap . ACCESS_WRITE ) <EOL> print "<STR_LIT>" <EOL> m [ : ] = '<STR_LIT:c>' * mapsize <EOL> verify ( 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> verify ( stuff == '<STR_LIT:c>' * mapsize , <EOL> "<STR_LIT>" ) <EOL> print "<STR_LIT>" <EOL> f = open ( TESTFN , "<STR_LIT>" ) <EOL> m = mmap . mmap ( f . fileno ( ) , mapsize , access = mmap . ACCESS_COPY ) <EOL> print "<STR_LIT>" <EOL> m [ : ] = '<STR_LIT:d>' * mapsize <EOL> verify ( m [ : ] == '<STR_LIT:d>' * mapsize , <EOL> "<STR_LIT>" ) <EOL> m . flush ( ) <EOL> verify ( open ( TESTFN , "<STR_LIT:rb>" ) . read ( ) == '<STR_LIT:c>' * mapsize , <EOL> "<STR_LIT>" ) <EOL> try : <EOL> print "<STR_LIT>" <EOL> m . resize ( <NUM_LIT:2> * mapsize ) <EOL> except TypeError : <EOL> pass <EOL> else : <EOL> verify ( <NUM_LIT:0> , "<STR_LIT>" ) <EOL> del m , f <EOL> try : <EOL> print "<STR_LIT>" <EOL> f = open ( TESTFN , "<STR_LIT>" ) <EOL> m = mmap . mmap ( f . fileno ( ) , mapsize , access = <NUM_LIT:4> ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> verify ( <NUM_LIT:0> , "<STR_LIT>" ) <EOL> if os . name == "<STR_LIT>" : <EOL> f = open ( TESTFN , "<STR_LIT>" ) <EOL> try : <EOL> m = mmap . mmap ( f . fileno ( ) , mapsize , flags = mmap . MAP_PRIVATE , <EOL> prot = mmap . PROT_READ , access = mmap . ACCESS_WRITE ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> verify ( <NUM_LIT:0> , "<STR_LIT>" ) <EOL> f . close ( ) <EOL> finally : <EOL> try : <EOL> os . unlink ( TESTFN ) <EOL> except OSError : <EOL> pass <EOL> print '<STR_LIT>' <EOL> try : <EOL> mmap . mmap ( - <NUM_LIT:2> , <NUM_LIT> ) <EOL> except mmap . error : <EOL> pass <EOL> else : <EOL> verify ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> f = open ( TESTFN , '<STR_LIT>' ) <EOL> try : <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> vereq ( m . find ( slice ) , data . find ( slice ) ) <EOL> vereq ( m . find ( slice + '<STR_LIT:x>' ) , - <NUM_LIT:1> ) <EOL> m . close ( ) <EOL> finally : <EOL> os . unlink ( TESTFN ) <EOL> f = open ( TESTFN , '<STR_LIT>' ) <EOL> try : <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> finally : <EOL> os . unlink ( TESTFN ) <EOL> if hasattr ( os , "<STR_LIT>" ) : <EOL> print "<STR_LIT>" <EOL> f = open ( TESTFN , "<STR_LIT>" ) <EOL> try : <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> verify ( len ( mf ) == <NUM_LIT:2> ** <NUM_LIT:16> , "<STR_LIT>" ) <EOL> vereq ( mf . read ( <NUM_LIT:2> ** <NUM_LIT:16> ) , <NUM_LIT:2> ** <NUM_LIT:16> * "<STR_LIT:m>" ) <EOL> mf . close ( ) <EOL> f . close ( ) <EOL> finally : <EOL> os . unlink ( TESTFN ) <EOL> if hasattr ( os , "<STR_LIT>" ) : <EOL> print "<STR_LIT>" <EOL> f = open ( TESTFN , "<STR_LIT>" ) <EOL> try : <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> verify ( len ( mf ) == <NUM_LIT:2> ** <NUM_LIT:16> , "<STR_LIT>" ) <EOL> vereq ( mf . read ( <NUM_LIT:2> ** <NUM_LIT:16> ) , <NUM_LIT:2> ** <NUM_LIT:16> * "<STR_LIT:m>" ) <EOL> mf . close ( ) <EOL> f . close ( ) <EOL> finally : <EOL> os . unlink ( TESTFN ) <EOL> f = open ( TESTFN , '<STR_LIT>' ) <EOL> try : <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> verify ( mf [ : ] == "<STR_LIT>" , "<STR_LIT>" ) <EOL> mf . close ( ) <EOL> f . close ( ) <EOL> finally : <EOL> os . unlink ( TESTFN ) <EOL> if hasattr ( mmap , "<STR_LIT>" ) : <EOL> try : <EOL> mapsize = <NUM_LIT:10> <EOL> open ( TESTFN , "<STR_LIT:wb>" ) . write ( "<STR_LIT:a>" * mapsize ) <EOL> f = open ( TESTFN , "<STR_LIT:rb>" ) <EOL> m = mmap . mmap ( f . fileno ( ) , mapsize , prot = mmap . PROT_READ ) <EOL> try : <EOL> m . write ( "<STR_LIT:foo>" ) <EOL> except TypeError : <EOL> pass <EOL> else : <EOL> verify ( <NUM_LIT:0> , "<STR_LIT>" ) <EOL> finally : <EOL> os . unlink ( TESTFN ) <EOL> def test_anon ( ) : <EOL> print "<STR_LIT>" <EOL> m = mmap . mmap ( - <NUM_LIT:1> , PAGESIZE ) <EOL> for x in xrange ( PAGESIZE ) : <EOL> verify ( m [ x ] == '<STR_LIT>' , "<STR_LIT>" ) <EOL> for x in xrange ( PAGESIZE ) : <EOL> m [ x ] = ch = chr ( x & <NUM_LIT:255> ) <EOL> vereq ( m [ x ] , ch ) <EOL> test_both ( ) <EOL> test_anon ( ) <EOL> print '<STR_LIT>' </s>
