<s> import py <EOL> import sys <EOL> from pypy . rpython . lltypesystem . lltype import typeOf , pyobjectptr , Ptr , PyObject , Void , malloc , free <EOL> from pypy . rpython . lltypesystem . lloperation import llop <EOL> from pypy . rpython . llinterp import LLInterpreter , LLException , log <EOL> from pypy . rpython . rmodel import inputconst <EOL> from pypy . translator . translator import TranslationContext , graphof <EOL> from pypy . rpython . rint import signed_repr <EOL> from pypy . rpython . lltypesystem import rstr , lltype <EOL> from pypy . annotation import model as annmodel <EOL> from pypy . annotation . model import lltype_to_annotation <EOL> from pypy . rlib . rarithmetic import r_uint , ovfcheck <EOL> from pypy . rpython . ootypesystem import ootype <EOL> from pypy import conftest <EOL> class MallocMismatch ( Exception ) : <EOL> pass <EOL> def setup_module ( mod ) : <EOL> mod . logstate = py . log . _getstate ( ) <EOL> py . log . setconsumer ( "<STR_LIT>" , py . log . STDOUT ) <EOL> def teardown_module ( mod ) : <EOL> py . log . _setstate ( mod . logstate ) <EOL> def timelog ( prefix , call , * args , ** kwds ) : <EOL> res = call ( * args , ** kwds ) <EOL> return res <EOL> def gengraph ( func , argtypes = [ ] , viewbefore = '<STR_LIT>' , policy = None , <EOL> type_system = "<STR_LIT>" , backendopt = False , config = None , <EOL> ** extraconfigopts ) : <EOL> t = TranslationContext ( config = config ) <EOL> t . config . set ( ** extraconfigopts ) <EOL> a = t . buildannotator ( policy = policy ) <EOL> timelog ( "<STR_LIT>" , a . build_types , func , argtypes ) <EOL> if viewbefore == '<STR_LIT>' : <EOL> viewbefore = getattr ( conftest . option , '<STR_LIT>' , False ) <EOL> if viewbefore : <EOL> a . simplify ( ) <EOL> t . view ( ) <EOL> global typer <EOL> typer = t . buildrtyper ( type_system = type_system ) <EOL> timelog ( "<STR_LIT>" , typer . specialize ) <EOL> timelog ( "<STR_LIT>" , t . checkgraphs ) <EOL> if backendopt : <EOL> from pypy . translator . backendopt . all import backend_optimizations <EOL> backend_optimizations ( t ) <EOL> timelog ( "<STR_LIT>" , t . checkgraphs ) <EOL> if viewbefore : <EOL> t . view ( ) <EOL> desc = t . annotator . bookkeeper . getdesc ( func ) <EOL> graph = desc . specialize ( argtypes ) <EOL> return t , typer , graph <EOL> _lastinterpreted = [ ] <EOL> _tcache = { } <EOL> def clear_tcache ( ) : <EOL> del _lastinterpreted [ : ] <EOL> _tcache . clear ( ) <EOL> def get_interpreter ( func , values , view = '<STR_LIT>' , viewbefore = '<STR_LIT>' , policy = None , <EOL> someobjects = False , type_system = "<STR_LIT>" , backendopt = False , <EOL> config = None , malloc_check = True , ** extraconfigopts ) : <EOL> extra_key = [ ( key , value ) for key , value in extraconfigopts . iteritems ( ) ] <EOL> extra_key . sort ( ) <EOL> extra_key = tuple ( extra_key ) <EOL> key = ( ( func , ) + tuple ( [ typeOf ( x ) for x in values ] ) + <EOL> ( someobjects , backendopt , extra_key ) ) <EOL> try : <EOL> ( t , interp , graph ) = _tcache [ key ] <EOL> except KeyError : <EOL> def annotation ( x ) : <EOL> T = typeOf ( x ) <EOL> if T == Ptr ( PyObject ) and someobjects : <EOL> return object <EOL> else : <EOL> return lltype_to_annotation ( T ) <EOL> if policy is None and not someobjects : <EOL> from pypy . annotation . policy import AnnotatorPolicy <EOL> policy = AnnotatorPolicy ( ) <EOL> policy . allow_someobjects = False <EOL> t , typer , graph = gengraph ( func , [ annotation ( x ) for x in values ] , <EOL> viewbefore , policy , type_system = type_system , <EOL> backendopt = backendopt , config = config , <EOL> ** extraconfigopts ) <EOL> interp = LLInterpreter ( typer , malloc_check = malloc_check ) <EOL> _tcache [ key ] = ( t , interp , graph ) <EOL> _lastinterpreted . append ( key ) <EOL> if len ( _lastinterpreted ) >= <NUM_LIT:4> : <EOL> del _tcache [ _lastinterpreted . pop ( <NUM_LIT:0> ) ] <EOL> if view == '<STR_LIT>' : <EOL> view = getattr ( conftest . option , '<STR_LIT>' , False ) <EOL> if view : <EOL> t . view ( ) <EOL> return interp , graph <EOL> def interpret ( func , values , view = '<STR_LIT>' , viewbefore = '<STR_LIT>' , policy = None , <EOL> someobjects = False , type_system = "<STR_LIT>" , backendopt = False , <EOL> config = None , malloc_check = True , ** kwargs ) : <EOL> interp , graph = get_interpreter ( func , values , view , viewbefore , policy , <EOL> someobjects , type_system = type_system , <EOL> backendopt = backendopt , config = config , <EOL> malloc_check = malloc_check , ** kwargs ) <EOL> result = interp . eval_graph ( graph , values ) <EOL> if malloc_check and interp . mallocs : <EOL> raise MallocMismatch ( interp . mallocs ) <EOL> return result <EOL> def interpret_raises ( exc , func , values , view = '<STR_LIT>' , viewbefore = '<STR_LIT>' , <EOL> policy = None , someobjects = False , type_system = "<STR_LIT>" , <EOL> backendopt = False ) : <EOL> interp , graph = get_interpreter ( func , values , view , viewbefore , policy , <EOL> someobjects , type_system = type_system , <EOL> backendopt = backendopt ) <EOL> info = py . test . raises ( LLException , "<STR_LIT>" ) <EOL> try : <EOL> got = interp . find_exception ( info . value ) <EOL> except ValueError : <EOL> got = None <EOL> assert got is exc , "<STR_LIT>" <EOL> def test_int_ops ( ) : <EOL> res = interpret ( number_ops , [ <NUM_LIT:3> ] ) <EOL> assert res == <NUM_LIT:4> <EOL> def test_invert ( ) : <EOL> def f ( x ) : <EOL> return ~ x <EOL> res = interpret ( f , [ <NUM_LIT:3> ] ) <EOL> assert res == ~ <NUM_LIT:3> <EOL> assert interpret ( f , [ r_uint ( <NUM_LIT:3> ) ] ) == ~ r_uint ( <NUM_LIT:3> ) <EOL> def test_float_ops ( ) : <EOL> res = interpret ( number_ops , [ <NUM_LIT> ] ) <EOL> assert res == <NUM_LIT> <EOL> def test_ifs ( ) : <EOL> res = interpret ( simple_ifs , [ <NUM_LIT:0> ] ) <EOL> assert res == <NUM_LIT> <EOL> res = interpret ( simple_ifs , [ <NUM_LIT:1> ] ) <EOL> assert res == <NUM_LIT> <EOL> def test_raise ( ) : <EOL> res = interpret ( raise_exception , [ <NUM_LIT> ] ) <EOL> assert res == <NUM_LIT> <EOL> interpret_raises ( IndexError , raise_exception , [ <NUM_LIT> ] ) <EOL> interpret_raises ( ValueError , raise_exception , [ <NUM_LIT> ] ) <EOL> interpret_raises ( IndexError , raise_exception , [ <NUM_LIT> ] , type_system = "<STR_LIT>" ) <EOL> interpret_raises ( ValueError , raise_exception , [ <NUM_LIT> ] , type_system = "<STR_LIT>" ) <EOL> def test_call_raise ( ) : <EOL> res = interpret ( call_raise , [ <NUM_LIT> ] ) <EOL> assert res == <NUM_LIT> <EOL> interpret_raises ( IndexError , call_raise , [ <NUM_LIT> ] ) <EOL> interpret_raises ( ValueError , call_raise , [ <NUM_LIT> ] ) <EOL> def test_call_raise_twice ( ) : <EOL> res = interpret ( call_raise_twice , [ <NUM_LIT:6> , <NUM_LIT:7> ] ) <EOL> assert res == <NUM_LIT> <EOL> interpret_raises ( IndexError , call_raise_twice , [ <NUM_LIT:6> , <NUM_LIT> ] ) <EOL> res = interpret ( call_raise_twice , [ <NUM_LIT:6> , <NUM_LIT> ] ) <EOL> assert res == <NUM_LIT> <EOL> interpret_raises ( IndexError , call_raise_twice , [ <NUM_LIT> , <NUM_LIT:7> ] ) <EOL> interpret_raises ( ValueError , call_raise_twice , [ <NUM_LIT> , <NUM_LIT:7> ] ) <EOL> def test_call_raise_intercept ( ) : <EOL> res = interpret ( call_raise_intercept , [ <NUM_LIT> ] ) <EOL> assert res == <NUM_LIT> <EOL> res = interpret ( call_raise_intercept , [ <NUM_LIT> ] ) <EOL> assert res == <NUM_LIT> <EOL> interpret_raises ( TypeError , call_raise_intercept , [ <NUM_LIT> ] ) <EOL> def test_while_simple ( ) : <EOL> res = interpret ( while_simple , [ <NUM_LIT:3> ] ) <EOL> assert res == <NUM_LIT:6> <EOL> def test_number_comparisons ( ) : <EOL> for t in float , int : <EOL> val1 = t ( <NUM_LIT:3> ) <EOL> val2 = t ( <NUM_LIT:4> ) <EOL> gcres = interpret ( comparisons , [ val1 , val2 ] ) <EOL> res = [ getattr ( gcres , x ) for x in typeOf ( gcres ) . TO . _names ] <EOL> assert res == [ True , True , False , True , False , False ] <EOL> def test_some_builtin ( ) : <EOL> def f ( i , j ) : <EOL> x = range ( i ) <EOL> return x [ j - <NUM_LIT:1> ] <EOL> res = interpret ( f , [ <NUM_LIT:10> , <NUM_LIT:7> ] ) <EOL> assert res == <NUM_LIT:6> <EOL> def test_recursion_does_not_overwrite_my_variables ( ) : <EOL> def f ( i ) : <EOL> j = i + <NUM_LIT:1> <EOL> if i > <NUM_LIT:0> : <EOL> f ( i - <NUM_LIT:1> ) <EOL> return j <EOL> res = interpret ( f , [ <NUM_LIT:4> ] ) <EOL> assert res == <NUM_LIT:5> <EOL> def test_list_creation ( ) : <EOL> def f ( ) : <EOL> return [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> res = interpret ( f , [ ] ) <EOL> assert len ( res . ll_items ( ) ) == len ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> assert res . ll_items ( ) [ i ] == i + <NUM_LIT:1> <EOL> def test_list_itemops ( ) : <EOL> def f ( i ) : <EOL> l = [ <NUM_LIT:1> , i ] <EOL> l [ <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> del l [ <NUM_LIT:1> ] <EOL> return l [ - <NUM_LIT:1> ] <EOL> res = interpret ( f , [ <NUM_LIT:3> ] ) <EOL> assert res == <NUM_LIT:0> <EOL> def test_list_append ( ) : <EOL> def f ( i ) : <EOL> l = [ <NUM_LIT:1> ] <EOL> l . append ( i ) <EOL> return l [ <NUM_LIT:0> ] + l [ <NUM_LIT:1> ] <EOL> res = interpret ( f , [ <NUM_LIT:3> ] ) <EOL> assert res == <NUM_LIT:4> <EOL> def test_list_extend ( ) : <EOL> def f ( i ) : <EOL> l = [ <NUM_LIT:1> ] <EOL> l . extend ( [ i ] ) <EOL> return l [ <NUM_LIT:0> ] + l [ <NUM_LIT:1> ] <EOL> res = interpret ( f , [ <NUM_LIT:3> ] ) <EOL> assert res == <NUM_LIT:4> <EOL> def test_list_multiply ( ) : <EOL> def f ( i ) : <EOL> l = [ i ] <EOL> l = l * i <EOL> return len ( l ) <EOL> res = interpret ( f , [ <NUM_LIT:3> ] ) <EOL> assert res == <NUM_LIT:3> <EOL> def test_list_reverse ( ) : <EOL> def f ( ) : <EOL> l = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> l . reverse ( ) <EOL> return l <EOL> res = interpret ( f , [ ] ) <EOL> assert len ( res . ll_items ( ) ) == len ( [ <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:1> ] ) <EOL> print res <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> assert res . ll_items ( ) [ i ] == <NUM_LIT:3> - i <EOL> def test_list_pop ( ) : <EOL> def f ( ) : <EOL> l = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> l1 = l . pop ( <NUM_LIT:2> ) <EOL> l2 = l . pop ( <NUM_LIT:1> ) <EOL> l3 = l . pop ( - <NUM_LIT:1> ) <EOL> return [ l1 , l2 , l3 ] <EOL> res = interpret ( f , [ ] ) <EOL> assert len ( res . ll_items ( ) ) == <NUM_LIT:3> <EOL> def test_obj_obj_add ( ) : <EOL> def f ( x , y ) : <EOL> return x + y <EOL> _1L = pyobjectptr ( <NUM_LIT:1> L ) <EOL> _2L = pyobjectptr ( <NUM_LIT:2> L ) <EOL> res = interpret ( f , [ _1L , _2L ] , someobjects = True ) <EOL> assert res . _obj . value == <NUM_LIT:3> L <EOL> def test_ovf ( ) : <EOL> import sys <EOL> def f ( x ) : <EOL> try : <EOL> return ovfcheck ( sys . maxint + x ) <EOL> except OverflowError : <EOL> return <NUM_LIT:1> <EOL> res = interpret ( f , [ <NUM_LIT:1> ] ) <EOL> assert res == <NUM_LIT:1> <EOL> res = interpret ( f , [ <NUM_LIT:0> ] ) <EOL> assert res == sys . maxint <EOL> def g ( x ) : <EOL> try : <EOL> return ovfcheck ( abs ( x ) ) <EOL> except OverflowError : <EOL> return <NUM_LIT> <EOL> res = interpret ( g , [ - sys . maxint - <NUM_LIT:1> ] ) <EOL> assert res == <NUM_LIT> <EOL> res = interpret ( g , [ - <NUM_LIT:15> ] ) <EOL> assert res == <NUM_LIT:15> <EOL> def test_floordiv_ovf_zer ( ) : <EOL> import sys <EOL> def f ( x ) : <EOL> try : <EOL> return ovfcheck ( ( - sys . maxint - <NUM_LIT:1> ) // x ) <EOL> except OverflowError : <EOL> return <NUM_LIT:1> <EOL> except ZeroDivisionError : <EOL> return <NUM_LIT:0> <EOL> res = interpret ( f , [ <NUM_LIT:0> ] ) <EOL> assert res == <NUM_LIT:0> <EOL> res = interpret ( f , [ - <NUM_LIT:1> ] ) <EOL> assert res == <NUM_LIT:1> <EOL> res = interpret ( f , [ <NUM_LIT:30> ] ) <EOL> assert res == ( - sys . maxint - <NUM_LIT:1> ) // <NUM_LIT:30> <EOL> def test_mod_ovf_zer ( ) : <EOL> import sys <EOL> def f ( x ) : <EOL> try : <EOL> return ovfcheck ( ( - sys . maxint - <NUM_LIT:1> ) % x ) <EOL> except OverflowError : <EOL> return <NUM_LIT:1> <EOL> except ZeroDivisionError : <EOL> return <NUM_LIT:0> <EOL> res = interpret ( f , [ <NUM_LIT:0> ] ) <EOL> assert res == <NUM_LIT:0> <EOL> res = interpret ( f , [ - <NUM_LIT:1> ] ) <EOL> assert res == <NUM_LIT:1> <EOL> res = interpret ( f , [ <NUM_LIT:30> ] ) <EOL> assert res == ( - sys . maxint - <NUM_LIT:1> ) % <NUM_LIT:30> <EOL> def test_obj_obj_is ( ) : <EOL> def f ( x , y ) : <EOL> return x is y <EOL> o = pyobjectptr ( object ( ) ) <EOL> res = interpret ( f , [ o , o ] , someobjects = True ) <EOL> assert res is True <EOL> def test_funny_links ( ) : <EOL> from pypy . objspace . flow . model import Block , FunctionGraph , SpaceOperation , Variable , Constant , Link <EOL> for i in range ( <NUM_LIT:2> ) : <EOL> v_i = Variable ( "<STR_LIT:i>" ) <EOL> v_case = Variable ( "<STR_LIT>" ) <EOL> block = Block ( [ v_i ] ) <EOL> g = FunctionGraph ( "<STR_LIT>" , block ) <EOL> block . operations . append ( SpaceOperation ( "<STR_LIT>" , [ v_i , Constant ( <NUM_LIT:1> ) ] , v_case ) ) <EOL> block . exitswitch = v_case <EOL> tlink = Link ( [ Constant ( <NUM_LIT:1> ) ] , g . returnblock , True ) <EOL> flink = Link ( [ Constant ( <NUM_LIT:0> ) ] , g . returnblock , False ) <EOL> links = [ tlink , flink ] <EOL> if i : <EOL> links . reverse ( ) <EOL> block . closeblock ( * links ) <EOL> t = TranslationContext ( ) <EOL> a = t . buildannotator ( ) <EOL> a . build_graph_types ( g , [ annmodel . SomeInteger ( ) ] ) <EOL> rtyper = t . buildrtyper ( ) <EOL> rtyper . specialize ( ) <EOL> interp = LLInterpreter ( rtyper ) <EOL> assert interp . eval_graph ( g , [ <NUM_LIT:1> ] ) == <NUM_LIT:1> <EOL> assert interp . eval_graph ( g , [ <NUM_LIT:0> ] ) == <NUM_LIT:0> <EOL> class ExampleClass : <EOL> def __init__ ( self , x ) : <EOL> self . x = x + <NUM_LIT:1> <EOL> def test_basic_instantiation ( ) : <EOL> def f ( x ) : <EOL> return ExampleClass ( x ) . x <EOL> res = interpret ( f , [ <NUM_LIT:4> ] ) <EOL> assert res == <NUM_LIT:5> <EOL> def test_id ( ) : <EOL> from pypy . rlib . objectmodel import compute_unique_id <EOL> def getids ( i , j ) : <EOL> e1 = ExampleClass ( <NUM_LIT:1> ) <EOL> e2 = ExampleClass ( <NUM_LIT:2> ) <EOL> a = [ e1 , e2 ] [ i ] <EOL> b = [ e1 , e2 ] [ j ] <EOL> return ( compute_unique_id ( a ) == compute_unique_id ( b ) ) == ( a is b ) <EOL> for i in [ <NUM_LIT:0> , <NUM_LIT:1> ] : <EOL> for j in [ <NUM_LIT:0> , <NUM_LIT:1> ] : <EOL> result = interpret ( getids , [ i , j ] ) <EOL> assert result <EOL> def test_stack_malloc ( ) : <EOL> class A ( object ) : <EOL> pass <EOL> def f ( ) : <EOL> a = A ( ) <EOL> a . i = <NUM_LIT:1> <EOL> return a . i <EOL> interp , graph = get_interpreter ( f , [ ] ) <EOL> graph . startblock . operations [ <NUM_LIT:0> ] . args [ <NUM_LIT:1> ] = inputconst ( Void , { '<STR_LIT>' : "<STR_LIT>" } ) <EOL> result = interp . eval_graph ( graph , [ ] ) <EOL> assert result == <NUM_LIT:1> <EOL> def test_invalid_stack_access ( ) : <EOL> class A ( object ) : <EOL> pass <EOL> globala = A ( ) <EOL> globala . next = None <EOL> globala . i = <NUM_LIT:1> <EOL> def g ( a ) : <EOL> globala . next = a <EOL> def f ( ) : <EOL> a = A ( ) <EOL> a . i = <NUM_LIT:2> <EOL> g ( a ) <EOL> def h ( ) : <EOL> f ( ) <EOL> return globala . next . i <EOL> interp , graph = get_interpreter ( h , [ ] ) <EOL> fgraph = graph . startblock . operations [ <NUM_LIT:0> ] . args [ <NUM_LIT:0> ] . value . _obj . graph <EOL> assert fgraph . startblock . operations [ <NUM_LIT:0> ] . opname == '<STR_LIT>' <EOL> fgraph . startblock . operations [ <NUM_LIT:0> ] . args [ <NUM_LIT:1> ] = inputconst ( Void , { '<STR_LIT>' : "<STR_LIT>" } ) <EOL> py . test . raises ( RuntimeError , "<STR_LIT>" ) <EOL> _snap = globals ( ) . copy ( ) <EOL> def number_ops ( i ) : <EOL> j = i + <NUM_LIT:2> <EOL> k = j * <NUM_LIT:2> <EOL> m = k / <NUM_LIT:2> <EOL> return m - <NUM_LIT:1> <EOL> def comparisons ( x , y ) : <EOL> return ( x < y , <EOL> x <= y , <EOL> x == y , <EOL> x != y , <EOL> x >= y , <EOL> x > y , <EOL> ) <EOL> def simple_ifs ( i ) : <EOL> if i : <EOL> return <NUM_LIT> <EOL> else : <EOL> return <NUM_LIT> <EOL> def while_simple ( i ) : <EOL> sum = <NUM_LIT:0> <EOL> while i > <NUM_LIT:0> : <EOL> sum += i <EOL> i -= <NUM_LIT:1> <EOL> return sum <EOL> def raise_exception ( i ) : <EOL> if i == <NUM_LIT> : <EOL> raise IndexError <EOL> elif i == <NUM_LIT> : <EOL> raise ValueError <EOL> return i <EOL> def call_raise ( i ) : <EOL> return raise_exception ( i ) <EOL> def call_raise_twice ( i , j ) : <EOL> x = raise_exception ( i ) <EOL> try : <EOL> y = raise_exception ( j ) <EOL> except ValueError : <EOL> y = <NUM_LIT:1000> <EOL> return x + y <EOL> def call_raise_intercept ( i ) : <EOL> try : <EOL> return raise_exception ( i ) <EOL> except IndexError : <EOL> return i <EOL> except ValueError : <EOL> raise TypeError <EOL> def test_llinterp_fail ( ) : <EOL> def aa ( i ) : <EOL> if i : <EOL> raise TypeError ( ) <EOL> def bb ( i ) : <EOL> try : <EOL> aa ( i ) <EOL> except TypeError : <EOL> pass <EOL> t = TranslationContext ( ) <EOL> annotator = t . buildannotator ( ) <EOL> annotator . build_types ( bb , [ int ] ) <EOL> t . buildrtyper ( type_system = "<STR_LIT>" ) . specialize ( ) <EOL> graph = graphof ( t , bb ) <EOL> interp = LLInterpreter ( t . rtyper ) <EOL> res = interp . eval_graph ( graph , [ <NUM_LIT:1> ] ) <EOL> def test_half_exceptiontransformed_graphs ( ) : <EOL> from pypy . translator import exceptiontransform <EOL> def f1 ( x ) : <EOL> if x < <NUM_LIT:0> : <EOL> raise ValueError <EOL> return <NUM_LIT> <EOL> def g1 ( x ) : <EOL> try : <EOL> return f1 ( x ) <EOL> except ValueError : <EOL> return <NUM_LIT:5> <EOL> def f2 ( x ) : <EOL> if x < <NUM_LIT:0> : <EOL> raise ValueError <EOL> return <NUM_LIT> <EOL> def g2 ( x ) : <EOL> try : <EOL> return f2 ( x ) <EOL> except ValueError : <EOL> return <NUM_LIT:6> <EOL> f3 = lltype . functionptr ( lltype . FuncType ( [ lltype . Signed ] , lltype . Signed ) , <EOL> '<STR_LIT>' , _callable = f1 ) <EOL> def g3 ( x ) : <EOL> try : <EOL> return f3 ( x ) <EOL> except ValueError : <EOL> return <NUM_LIT:7> <EOL> def f ( flag , x ) : <EOL> if flag == <NUM_LIT:1> : <EOL> return g1 ( x ) <EOL> elif flag == <NUM_LIT:2> : <EOL> return g2 ( x ) <EOL> else : <EOL> return g3 ( x ) <EOL> t = TranslationContext ( ) <EOL> t . buildannotator ( ) . build_types ( f , [ int , int ] ) <EOL> t . buildrtyper ( ) . specialize ( ) <EOL> etrafo = exceptiontransform . ExceptionTransformer ( t ) <EOL> etrafo . create_exception_handling ( graphof ( t , f1 ) ) <EOL> etrafo . create_exception_handling ( graphof ( t , g2 ) ) <EOL> etrafo . create_exception_handling ( graphof ( t , g3 ) ) <EOL> graph = graphof ( t , f ) <EOL> interp = LLInterpreter ( t . rtyper ) <EOL> res = interp . eval_graph ( graph , [ <NUM_LIT:1> , - <NUM_LIT:64> ] ) <EOL> assert res == <NUM_LIT:5> <EOL> res = interp . eval_graph ( graph , [ <NUM_LIT:2> , - <NUM_LIT> ] ) <EOL> assert res == <NUM_LIT:6> <EOL> res = interp . eval_graph ( graph , [ <NUM_LIT:3> , - <NUM_LIT> ] ) <EOL> assert res == <NUM_LIT:7> <EOL> def test_exceptiontransformed_add_ovf ( ) : <EOL> from pypy . translator import exceptiontransform <EOL> def f ( x , y ) : <EOL> try : <EOL> return ovfcheck ( x + y ) <EOL> except OverflowError : <EOL> return - <NUM_LIT> <EOL> t = TranslationContext ( ) <EOL> t . buildannotator ( ) . build_types ( f , [ int , int ] ) <EOL> t . buildrtyper ( ) . specialize ( ) <EOL> etrafo = exceptiontransform . ExceptionTransformer ( t ) <EOL> graph = graphof ( t , f ) <EOL> etrafo . create_exception_handling ( graph ) <EOL> interp = LLInterpreter ( t . rtyper ) <EOL> res = interp . eval_graph ( graph , [ <NUM_LIT:1> , - <NUM_LIT:64> ] ) <EOL> assert res == - <NUM_LIT> <EOL> res = interp . eval_graph ( graph , [ <NUM_LIT:1> , sys . maxint ] ) <EOL> assert res == - <NUM_LIT> <EOL> def test_malloc_checker ( ) : <EOL> T = lltype . Struct ( '<STR_LIT:x>' ) <EOL> def f ( x ) : <EOL> t = malloc ( T , flavor = '<STR_LIT>' ) <EOL> if x : <EOL> free ( t , flavor = '<STR_LIT>' ) <EOL> interpret ( f , [ <NUM_LIT:1> ] ) <EOL> py . test . raises ( MallocMismatch , "<STR_LIT>" ) <EOL> def f ( ) : <EOL> t1 = malloc ( T , flavor = '<STR_LIT>' ) <EOL> t2 = malloc ( T , flavor = '<STR_LIT>' ) <EOL> free ( t1 , flavor = '<STR_LIT>' ) <EOL> free ( t2 , flavor = '<STR_LIT>' ) <EOL> interpret ( f , [ ] ) </s>
