<s> from pypy . tool . udir import udir <EOL> import os <EOL> from pypy . rpython . test . test_llinterp import interpret <EOL> from pypy . rlib import rmmap as mmap <EOL> from pypy . rlib . rmmap import RTypeError , RValueError , alloc , free <EOL> import sys <EOL> class TestMMap : <EOL> def setup_class ( cls ) : <EOL> cls . tmpname = str ( udir . join ( '<STR_LIT>' ) ) <EOL> def test_page_size ( self ) : <EOL> def f ( ) : <EOL> assert mmap . PAGESIZE > <NUM_LIT:0> <EOL> assert isinstance ( mmap . PAGESIZE , int ) <EOL> interpret ( f , [ ] ) <EOL> def test_attributes ( self ) : <EOL> def f ( ) : <EOL> assert isinstance ( mmap . ACCESS_READ , int ) <EOL> assert isinstance ( mmap . ACCESS_WRITE , int ) <EOL> assert isinstance ( mmap . ACCESS_COPY , int ) <EOL> if os . name == "<STR_LIT>" : <EOL> assert isinstance ( mmap . MAP_ANON , int ) <EOL> assert isinstance ( mmap . MAP_ANONYMOUS , int ) <EOL> assert isinstance ( mmap . MAP_PRIVATE , int ) <EOL> assert isinstance ( mmap . MAP_SHARED , int ) <EOL> assert isinstance ( mmap . PROT_EXEC , int ) <EOL> assert isinstance ( mmap . PROT_READ , int ) <EOL> assert isinstance ( mmap . PROT_WRITE , int ) <EOL> interpret ( f , [ ] ) <EOL> def test_file_size ( self ) : <EOL> if os . name == "<STR_LIT>" : <EOL> skip ( "<STR_LIT>" ) <EOL> def func ( no ) : <EOL> try : <EOL> mmap . mmap ( no , <NUM_LIT> ) <EOL> except RValueError : <EOL> pass <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> f = open ( self . tmpname + "<STR_LIT:a>" , "<STR_LIT>" ) <EOL> f . write ( "<STR_LIT:c>" ) <EOL> f . flush ( ) <EOL> interpret ( func , [ f . fileno ( ) ] ) <EOL> f . close ( ) <EOL> def test_create ( self ) : <EOL> f = open ( self . tmpname + "<STR_LIT:b>" , "<STR_LIT>" ) <EOL> f . write ( "<STR_LIT:c>" ) <EOL> f . flush ( ) <EOL> def func ( no ) : <EOL> m = mmap . mmap ( no , <NUM_LIT:1> ) <EOL> assert m . read ( <NUM_LIT> ) == "<STR_LIT:c>" <EOL> interpret ( func , [ f . fileno ( ) ] ) <EOL> f . close ( ) <EOL> def test_close ( self ) : <EOL> f = open ( self . tmpname + "<STR_LIT:c>" , "<STR_LIT>" ) <EOL> f . write ( "<STR_LIT:c>" ) <EOL> f . flush ( ) <EOL> def func ( no ) : <EOL> m = mmap . mmap ( no , <NUM_LIT:1> ) <EOL> m . close ( ) <EOL> try : <EOL> m . read ( <NUM_LIT:1> ) <EOL> except RValueError : <EOL> pass <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> interpret ( func , [ f . fileno ( ) ] ) <EOL> def test_read_byte ( self ) : <EOL> f = open ( self . tmpname + "<STR_LIT:d>" , "<STR_LIT>" ) <EOL> f . write ( "<STR_LIT:c>" ) <EOL> f . flush ( ) <EOL> def func ( no ) : <EOL> m = mmap . mmap ( no , <NUM_LIT:1> ) <EOL> assert m . read_byte ( ) == "<STR_LIT:c>" <EOL> try : <EOL> m . read_byte ( ) <EOL> except RValueError : <EOL> pass <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> m . close ( ) <EOL> interpret ( func , [ f . fileno ( ) ] ) <EOL> f . close ( ) <EOL> def test_readline ( self ) : <EOL> import os <EOL> f = open ( self . tmpname + "<STR_LIT:e>" , "<STR_LIT>" ) <EOL> f . write ( "<STR_LIT>" ) <EOL> f . flush ( ) <EOL> def func ( no ) : <EOL> m = mmap . mmap ( no , <NUM_LIT:4> ) <EOL> if os . name == "<STR_LIT>" : <EOL> assert m . readline ( ) == "<STR_LIT>" <EOL> elif os . name == "<STR_LIT>" : <EOL> assert m . readline ( ) == "<STR_LIT>" <EOL> assert m . readline ( ) == "<STR_LIT>" <EOL> m . close ( ) <EOL> interpret ( func , [ f . fileno ( ) ] ) <EOL> f . close ( ) <EOL> def test_read ( self ) : <EOL> f = open ( self . tmpname + "<STR_LIT:f>" , "<STR_LIT>" ) <EOL> f . write ( "<STR_LIT>" ) <EOL> f . flush ( ) <EOL> def func ( no ) : <EOL> m = mmap . mmap ( no , <NUM_LIT:6> ) <EOL> assert m . read ( <NUM_LIT:1> ) == "<STR_LIT:f>" <EOL> assert m . read ( <NUM_LIT:6> ) == "<STR_LIT>" <EOL> assert m . read ( <NUM_LIT:1> ) == "<STR_LIT>" <EOL> m . close ( ) <EOL> interpret ( func , [ f . fileno ( ) ] ) <EOL> f . close ( ) <EOL> def test_find ( self ) : <EOL> f = open ( self . tmpname + "<STR_LIT:g>" , "<STR_LIT>" ) <EOL> f . write ( "<STR_LIT>" ) <EOL> f . flush ( ) <EOL> def func ( no ) : <EOL> m = mmap . mmap ( no , <NUM_LIT:7> ) <EOL> assert m . find ( "<STR_LIT:b>" ) == <NUM_LIT:3> <EOL> assert m . find ( "<STR_LIT:z>" ) == - <NUM_LIT:1> <EOL> assert m . find ( "<STR_LIT:o>" , <NUM_LIT:5> ) == - <NUM_LIT:1> <EOL> assert m . find ( "<STR_LIT>" ) == <NUM_LIT:2> <EOL> assert m . find ( "<STR_LIT>" ) == <NUM_LIT:6> <EOL> m . close ( ) <EOL> interpret ( func , [ f . fileno ( ) ] ) <EOL> f . close ( ) <EOL> def test_is_modifiable ( self ) : <EOL> f = open ( self . tmpname + "<STR_LIT:h>" , "<STR_LIT>" ) <EOL> f . write ( "<STR_LIT>" ) <EOL> f . flush ( ) <EOL> def func ( no ) : <EOL> m = mmap . mmap ( no , <NUM_LIT:6> , access = mmap . ACCESS_READ ) <EOL> try : <EOL> m . write ( '<STR_LIT:x>' ) <EOL> except RTypeError : <EOL> pass <EOL> else : <EOL> assert False <EOL> try : <EOL> m . resize ( <NUM_LIT:7> ) <EOL> except RTypeError : <EOL> pass <EOL> else : <EOL> assert False <EOL> m . close ( ) <EOL> interpret ( func , [ f . fileno ( ) ] ) <EOL> f . close ( ) <EOL> def test_seek ( self ) : <EOL> f = open ( self . tmpname + "<STR_LIT:i>" , "<STR_LIT>" ) <EOL> f . write ( "<STR_LIT>" ) <EOL> f . flush ( ) <EOL> def func ( no ) : <EOL> m = mmap . mmap ( no , <NUM_LIT:6> ) <EOL> m . seek ( <NUM_LIT:0> ) <EOL> assert m . tell ( ) == <NUM_LIT:0> <EOL> m . read ( <NUM_LIT:1> ) <EOL> m . seek ( <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> assert m . tell ( ) == <NUM_LIT:2> <EOL> m . seek ( <NUM_LIT:0> ) <EOL> m . seek ( - <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> assert m . tell ( ) == <NUM_LIT:5> <EOL> m . close ( ) <EOL> interpret ( func , [ f . fileno ( ) ] ) <EOL> f . close ( ) <EOL> def test_write ( self ) : <EOL> f = open ( self . tmpname + "<STR_LIT>" , "<STR_LIT>" ) <EOL> f . write ( "<STR_LIT>" ) <EOL> f . flush ( ) <EOL> def func ( no ) : <EOL> m = mmap . mmap ( no , <NUM_LIT:6> , access = mmap . ACCESS_WRITE ) <EOL> m . write ( "<STR_LIT>" ) <EOL> m . seek ( <NUM_LIT:0> ) <EOL> assert m . read ( <NUM_LIT:6> ) == "<STR_LIT>" <EOL> m . close ( ) <EOL> interpret ( func , [ f . fileno ( ) ] ) <EOL> f . close ( ) <EOL> def test_write_byte ( self ) : <EOL> f = open ( self . tmpname + "<STR_LIT:k>" , "<STR_LIT>" ) <EOL> f . write ( "<STR_LIT>" ) <EOL> f . flush ( ) <EOL> def func ( no ) : <EOL> m = mmap . mmap ( no , <NUM_LIT:6> , access = mmap . ACCESS_READ ) <EOL> m = mmap . mmap ( no , <NUM_LIT:6> , access = mmap . ACCESS_WRITE ) <EOL> m . write_byte ( "<STR_LIT:x>" ) <EOL> m . seek ( <NUM_LIT:0> ) <EOL> assert m . read ( <NUM_LIT:6> ) == "<STR_LIT>" <EOL> m . close ( ) <EOL> interpret ( func , [ f . fileno ( ) ] ) <EOL> f . close ( ) <EOL> def test_size ( self ) : <EOL> f = open ( self . tmpname + "<STR_LIT:l>" , "<STR_LIT>" ) <EOL> f . write ( "<STR_LIT>" ) <EOL> f . flush ( ) <EOL> def func ( no ) : <EOL> m = mmap . mmap ( no , <NUM_LIT:5> ) <EOL> assert m . file_size ( ) == <NUM_LIT:6> <EOL> m . close ( ) <EOL> interpret ( func , [ f . fileno ( ) ] ) <EOL> f . close ( ) <EOL> def test_tell ( self ) : <EOL> f = open ( self . tmpname + "<STR_LIT:m>" , "<STR_LIT>" ) <EOL> f . write ( "<STR_LIT:c>" ) <EOL> f . flush ( ) <EOL> def func ( no ) : <EOL> m = mmap . mmap ( no , <NUM_LIT:1> ) <EOL> assert m . tell ( ) >= <NUM_LIT:0> <EOL> m . close ( ) <EOL> interpret ( func , [ f . fileno ( ) ] ) <EOL> f . close ( ) <EOL> def test_move ( self ) : <EOL> f = open ( self . tmpname + "<STR_LIT:o>" , "<STR_LIT>" ) <EOL> f . write ( "<STR_LIT>" ) <EOL> f . flush ( ) <EOL> def func ( no ) : <EOL> m = mmap . mmap ( no , <NUM_LIT:6> , access = mmap . ACCESS_WRITE ) <EOL> m . move ( <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:3> ) <EOL> assert m . read ( <NUM_LIT:6> ) == "<STR_LIT>" <EOL> m . seek ( <NUM_LIT:0> ) <EOL> m . move ( <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:2> ) <EOL> a = m . read ( <NUM_LIT:6> ) <EOL> assert a == "<STR_LIT>" <EOL> m . close ( ) <EOL> interpret ( func , [ f . fileno ( ) ] ) <EOL> f . close ( ) <EOL> def test_resize ( self ) : <EOL> import sys <EOL> if ( "<STR_LIT>" in sys . platform ) or ( "<STR_LIT>" in sys . platform ) : <EOL> skip ( "<STR_LIT>" ) <EOL> import os <EOL> f = open ( self . tmpname + "<STR_LIT:p>" , "<STR_LIT>" ) <EOL> f . write ( "<STR_LIT>" ) <EOL> f . flush ( ) <EOL> def func ( no ) : <EOL> m = mmap . mmap ( no , <NUM_LIT:6> , access = mmap . ACCESS_WRITE ) <EOL> f_size = os . fstat ( no ) . st_size <EOL> assert m . file_size ( ) == f_size == <NUM_LIT:6> <EOL> m . resize ( <NUM_LIT:10> ) <EOL> f_size = os . fstat ( no ) . st_size <EOL> assert m . file_size ( ) == f_size == <NUM_LIT:10> <EOL> m . close ( ) <EOL> interpret ( func , [ f . fileno ( ) ] ) <EOL> f . close ( ) <EOL> def test_len ( self ) : <EOL> f = open ( self . tmpname + "<STR_LIT:q>" , "<STR_LIT>" ) <EOL> f . write ( "<STR_LIT>" ) <EOL> f . flush ( ) <EOL> def func ( no ) : <EOL> m = mmap . mmap ( no , <NUM_LIT:6> ) <EOL> assert m . len ( ) == <NUM_LIT:6> <EOL> m . close ( ) <EOL> interpret ( func , [ f . fileno ( ) ] ) <EOL> f . close ( ) <EOL> def test_get_item ( self ) : <EOL> f = open ( self . tmpname + "<STR_LIT:r>" , "<STR_LIT>" ) <EOL> f . write ( "<STR_LIT>" ) <EOL> f . flush ( ) <EOL> def func ( no ) : <EOL> m = mmap . mmap ( no , <NUM_LIT:6> ) <EOL> assert m . getitem ( <NUM_LIT:0> ) == '<STR_LIT:f>' <EOL> assert m . getitem ( - <NUM_LIT:1> ) == '<STR_LIT:r>' <EOL> m . close ( ) <EOL> interpret ( func , [ f . fileno ( ) ] ) <EOL> f . close ( ) <EOL> def test_set_item ( self ) : <EOL> f = open ( self . tmpname + "<STR_LIT:s>" , "<STR_LIT>" ) <EOL> f . write ( "<STR_LIT>" ) <EOL> f . flush ( ) <EOL> def func ( no ) : <EOL> m = mmap . mmap ( no , <NUM_LIT:6> , access = mmap . ACCESS_WRITE ) <EOL> m . setitem ( <NUM_LIT:0> , '<STR_LIT:x>' ) <EOL> assert m . getitem ( <NUM_LIT:0> ) == '<STR_LIT:x>' <EOL> m . setitem ( - <NUM_LIT:6> , '<STR_LIT:y>' ) <EOL> data = m . read ( <NUM_LIT:6> ) <EOL> assert data == "<STR_LIT>" <EOL> m . close ( ) <EOL> interpret ( func , [ f . fileno ( ) ] ) <EOL> f . close ( ) <EOL> def test_double_close ( self ) : <EOL> f = open ( self . tmpname + "<STR_LIT:s>" , "<STR_LIT>" ) <EOL> f . write ( "<STR_LIT>" ) <EOL> f . flush ( ) <EOL> def func ( no ) : <EOL> m = mmap . mmap ( no , <NUM_LIT:6> , access = mmap . ACCESS_WRITE ) <EOL> m . close ( ) <EOL> m . close ( ) <EOL> interpret ( func , [ f . fileno ( ) ] ) <EOL> f . close ( ) <EOL> def test_translated ( self ) : <EOL> from pypy . translator . c . test . test_genc import compile <EOL> def func ( no ) : <EOL> m = mmap . mmap ( no , <NUM_LIT:1> ) <EOL> r = m . read_byte ( ) <EOL> m . close ( ) <EOL> return r <EOL> compile ( func , [ int ] ) <EOL> def test_alloc_free ( ) : <EOL> map_size = <NUM_LIT> <EOL> data = alloc ( map_size ) <EOL> for i in range ( <NUM_LIT:0> , map_size , <NUM_LIT> ) : <EOL> data [ i ] = chr ( i & <NUM_LIT> ) <EOL> for i in range ( <NUM_LIT:0> , map_size , <NUM_LIT> ) : <EOL> assert data [ i ] == chr ( i & <NUM_LIT> ) <EOL> free ( data , map_size ) <EOL> def test_compile_alloc_free ( ) : <EOL> from pypy . translator . c . test . test_genc import compile <EOL> fn = compile ( test_alloc_free , [ ] ) <EOL> fn ( ) </s>
