<s> import py <EOL> import sys <EOL> class TestInterpreter : <EOL> from pypy . interpreter . pycompiler import CPythonCompiler as CompilerClass <EOL> def codetest ( self , source , functionname , args ) : <EOL> """<STR_LIT>""" <EOL> from pypy . interpreter import baseobjspace <EOL> from pypy . interpreter import pyframe , gateway , module <EOL> space = self . space <EOL> source = str ( py . code . Source ( source ) . strip ( ) ) + '<STR_LIT:\n>' <EOL> w = space . wrap <EOL> w_code = space . builtin . call ( '<STR_LIT>' , <EOL> w ( source ) , w ( '<STR_LIT>' ) , w ( '<STR_LIT>' ) , w ( <NUM_LIT:0> ) , w ( <NUM_LIT:0> ) ) <EOL> tempmodule = module . Module ( space , w ( "<STR_LIT>" ) ) <EOL> w_glob = tempmodule . w_dict <EOL> space . setitem ( w_glob , w ( "<STR_LIT>" ) , space . builtin ) <EOL> code = space . unwrap ( w_code ) <EOL> code . exec_code ( space , w_glob , w_glob ) <EOL> wrappedargs = [ w ( a ) for a in args ] <EOL> wrappedfunc = space . getitem ( w_glob , w ( functionname ) ) <EOL> try : <EOL> w_output = space . call_function ( wrappedfunc , * wrappedargs ) <EOL> except baseobjspace . OperationError , e : <EOL> return '<STR_LIT>' % e . errorstr ( space ) <EOL> else : <EOL> return space . unwrap ( w_output ) <EOL> def setup_method ( self , arg ) : <EOL> ec = self . space . getexecutioncontext ( ) <EOL> self . saved_compiler = ec . compiler <EOL> ec . compiler = self . CompilerClass ( self . space ) <EOL> def teardown_method ( self , arg ) : <EOL> ec = self . space . getexecutioncontext ( ) <EOL> ec . compiler = self . saved_compiler <EOL> def test_exception_trivial ( self ) : <EOL> x = self . codetest ( '''<STR_LIT>''' , '<STR_LIT:f>' , [ ] ) <EOL> assert x == <NUM_LIT:1> <EOL> def test_exception ( self ) : <EOL> x = self . codetest ( '''<STR_LIT>''' , '<STR_LIT:f>' , [ ] ) <EOL> assert x == <NUM_LIT:1> <EOL> def test_finally ( self ) : <EOL> code = '''<STR_LIT>''' <EOL> assert self . codetest ( code , '<STR_LIT:f>' , [ <NUM_LIT:0> ] ) == - <NUM_LIT:12> <EOL> assert self . codetest ( code , '<STR_LIT:f>' , [ <NUM_LIT:1> ] ) == <NUM_LIT:1> <EOL> def test_except2 ( self ) : <EOL> x = self . codetest ( '''<STR_LIT>''' , '<STR_LIT:f>' , [ ] ) <EOL> assert x == <NUM_LIT:5> <EOL> def test_except3 ( self ) : <EOL> code = '''<STR_LIT>''' <EOL> assert self . codetest ( code , '<STR_LIT:f>' , [ <NUM_LIT:2> ] ) == <NUM_LIT:0> <EOL> assert self . codetest ( code , '<STR_LIT:f>' , [ <NUM_LIT:0> ] ) == "<STR_LIT>" <EOL> ess = "<STR_LIT>" <EOL> res = self . codetest ( code , '<STR_LIT:f>' , [ '<STR_LIT:x>' ] ) <EOL> assert res . find ( ess ) >= <NUM_LIT:0> <EOL> def test_break ( self ) : <EOL> code = '''<STR_LIT>''' <EOL> assert self . codetest ( code , '<STR_LIT:f>' , [ <NUM_LIT:4> ] ) == <NUM_LIT:1> + <NUM_LIT:2> + <NUM_LIT:3> <EOL> assert self . codetest ( code , '<STR_LIT:f>' , [ <NUM_LIT:9> ] ) == <NUM_LIT:1> + <NUM_LIT:2> + <NUM_LIT:3> + <NUM_LIT:4> <EOL> def test_continue ( self ) : <EOL> code = '''<STR_LIT>''' <EOL> assert self . codetest ( code , '<STR_LIT:f>' , [ <NUM_LIT:4> ] ) == <NUM_LIT:1> + <NUM_LIT:2> + <NUM_LIT:3> + <NUM_LIT> <EOL> assert self . codetest ( code , '<STR_LIT:f>' , [ <NUM_LIT:9> ] ) == ( <EOL> <NUM_LIT:1> + <NUM_LIT:2> + <NUM_LIT:3> + <NUM_LIT:5> + <NUM_LIT:6> + <NUM_LIT:7> + <NUM_LIT:8> + <NUM_LIT> ) <EOL> def test_import ( self ) : <EOL> real_call_function = self . space . call_function <EOL> def safe_call_function ( w_obj , * arg_w ) : <EOL> for arg in arg_w : <EOL> assert arg is not None <EOL> return real_call_function ( w_obj , * arg_w ) <EOL> self . space . call_function = safe_call_function <EOL> code = '''<STR_LIT>''' <EOL> self . codetest ( code , '<STR_LIT:f>' , [ ] ) <EOL> def test_extended_arg ( self ) : <EOL> longexpr = '<STR_LIT>' + '<STR_LIT>' * <NUM_LIT> <EOL> code = '''<STR_LIT>''' % ( ( longexpr , ) * <NUM_LIT:10> ) <EOL> assert self . codetest ( code , '<STR_LIT:f>' , [ <NUM_LIT:3> ] ) == <NUM_LIT:0> <EOL> def test_call_star_starstar ( self ) : <EOL> code = '''<STR_LIT>''' <EOL> assert self . codetest ( code , '<STR_LIT>' , [ <NUM_LIT> ] ) == [ <NUM_LIT> ] * <NUM_LIT> <EOL> def test_star_arg ( self ) : <EOL> code = '''<STR_LIT>''' <EOL> assert self . codetest ( code , '<STR_LIT:g>' , [ <NUM_LIT:12> , ( ) ] ) == ( ) <EOL> assert self . codetest ( code , '<STR_LIT:g>' , [ <NUM_LIT:12> , ( <NUM_LIT:3> , <NUM_LIT:4> ) ] ) == ( <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> assert self . codetest ( code , '<STR_LIT:g>' , [ <NUM_LIT:12> , [ ] ] ) == ( ) <EOL> assert self . codetest ( code , '<STR_LIT:g>' , [ <NUM_LIT:12> , [ <NUM_LIT:3> , <NUM_LIT:4> ] ] ) == ( <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> assert self . codetest ( code , '<STR_LIT:g>' , [ <NUM_LIT:12> , { } ] ) == ( ) <EOL> assert self . codetest ( code , '<STR_LIT:g>' , [ <NUM_LIT:12> , { <NUM_LIT:3> : <NUM_LIT:1> } ] ) == ( <NUM_LIT:3> , ) <EOL> def test_closure ( self ) : <EOL> code = '''<STR_LIT>''' <EOL> assert self . codetest ( code , '<STR_LIT>' , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) == <NUM_LIT:6> <EOL> def test_list_comprehension ( self ) : <EOL> code = '''<STR_LIT>''' <EOL> assert self . codetest ( code , '<STR_LIT:f>' , [ ] ) [ <NUM_LIT:0> ] == '<STR_LIT>' <EOL> def test_import_statement ( self ) : <EOL> for x in range ( <NUM_LIT:10> ) : <EOL> import os <EOL> code = '''<STR_LIT>''' <EOL> assert self . codetest ( code , '<STR_LIT:f>' , [ ] ) == os . name <EOL> class TestPyPyInterpreter ( TestInterpreter ) : <EOL> """<STR_LIT>""" <EOL> from pypy . interpreter . pycompiler import PythonAstCompiler as CompilerClass <EOL> def test_extended_arg ( self ) : <EOL> py . test . skip ( "<STR_LIT>" ) <EOL> class AppTestInterpreter : <EOL> def test_trivial ( self ) : <EOL> x = <NUM_LIT> <EOL> assert x == <NUM_LIT> <EOL> def test_trivial_call ( self ) : <EOL> def f ( ) : return <NUM_LIT> <EOL> assert f ( ) == <NUM_LIT> <EOL> def test_trivial_call2 ( self ) : <EOL> def f ( ) : return <NUM_LIT:1> + <NUM_LIT:1> <EOL> assert f ( ) == <NUM_LIT:2> <EOL> def test_print ( self ) : <EOL> import sys <EOL> save = sys . stdout <EOL> class Out ( object ) : <EOL> def __init__ ( self ) : <EOL> self . args = [ ] <EOL> def write ( self , * args ) : <EOL> self . args . extend ( args ) <EOL> out = Out ( ) <EOL> try : <EOL> sys . stdout = out <EOL> print <NUM_LIT:10> <EOL> assert out . args == [ '<STR_LIT>' , '<STR_LIT:\n>' ] <EOL> finally : <EOL> sys . stdout = save <EOL> def test_identity ( self ) : <EOL> def f ( x ) : return x <EOL> assert f ( <NUM_LIT> ) == <NUM_LIT> </s>
