<s> import py <EOL> from pypy . translator . translator import TranslationContext , graphof <EOL> from pypy . translator . simplify import join_blocks <EOL> from pypy . translator import exceptiontransform <EOL> from pypy . objspace . flow . model import summary <EOL> from pypy . rpython . test . test_llinterp import get_interpreter <EOL> from pypy . translator . backendopt . all import backend_optimizations <EOL> from pypy import conftest <EOL> import sys <EOL> def check_debug_build ( ) : <EOL> if not hasattr ( sys , '<STR_LIT>' ) and not conftest . option . view : <EOL> py . test . skip ( "<STR_LIT>" ) <EOL> _already_transformed = { } <EOL> def interpret ( func , values ) : <EOL> interp , graph = get_interpreter ( func , values ) <EOL> t = interp . typer . annotator . translator <EOL> if t not in _already_transformed : <EOL> etrafo = exceptiontransform . ExceptionTransformer ( t ) <EOL> etrafo . transform_completely ( ) <EOL> _already_transformed [ t ] = True <EOL> return interp . eval_graph ( graph , values ) <EOL> class BaseTestExceptionTransform : <EOL> type_system = None <EOL> def transform_func ( self , fn , inputtypes , backendopt = False ) : <EOL> t = TranslationContext ( ) <EOL> t . buildannotator ( ) . build_types ( fn , inputtypes ) <EOL> t . buildrtyper ( type_system = self . type_system ) . specialize ( ) <EOL> if conftest . option . view : <EOL> t . view ( ) <EOL> if backendopt : <EOL> backend_optimizations ( t ) <EOL> g = graphof ( t , fn ) <EOL> etrafo = exceptiontransform . ExceptionTransformer ( t ) <EOL> etrafo . create_exception_handling ( g ) <EOL> join_blocks ( g ) <EOL> if conftest . option . view : <EOL> t . view ( ) <EOL> return t , g <EOL> def compile ( self , fn , inputargs ) : <EOL> raise NotImplementedError <EOL> def test_simple ( self ) : <EOL> def one ( ) : <EOL> return <NUM_LIT:1> <EOL> def foo ( ) : <EOL> one ( ) <EOL> return one ( ) <EOL> t , g = self . transform_func ( foo , [ ] ) <EOL> assert len ( list ( g . iterblocks ( ) ) ) == <NUM_LIT:2> <EOL> result = interpret ( foo , [ ] ) <EOL> assert result == <NUM_LIT:1> <EOL> f = self . compile ( foo , [ ] ) <EOL> assert f ( ) == <NUM_LIT:1> <EOL> def test_passthrough ( self ) : <EOL> def one ( x ) : <EOL> if x : <EOL> raise ValueError ( ) <EOL> def foo ( ) : <EOL> one ( <NUM_LIT:0> ) <EOL> one ( <NUM_LIT:1> ) <EOL> t , g = self . transform_func ( foo , [ ] ) <EOL> f = self . compile ( foo , [ ] ) <EOL> py . test . raises ( ValueError , f ) <EOL> def test_catches ( self ) : <EOL> def one ( x ) : <EOL> if x == <NUM_LIT:1> : <EOL> raise ValueError ( ) <EOL> elif x == <NUM_LIT:2> : <EOL> raise TypeError ( ) <EOL> return x - <NUM_LIT:5> <EOL> def foo ( x ) : <EOL> x = one ( x ) <EOL> try : <EOL> x = one ( x ) <EOL> except ValueError : <EOL> return <NUM_LIT:1> + x <EOL> except TypeError : <EOL> return <NUM_LIT:2> + x <EOL> except : <EOL> return <NUM_LIT:3> + x <EOL> return <NUM_LIT:4> + x <EOL> t , g = self . transform_func ( foo , [ int ] ) <EOL> assert len ( list ( g . iterblocks ( ) ) ) == <NUM_LIT:9> <EOL> f = self . compile ( foo , [ int ] ) <EOL> result = interpret ( foo , [ <NUM_LIT:6> ] ) <EOL> assert result == <NUM_LIT:2> <EOL> result = f ( <NUM_LIT:6> ) <EOL> assert result == <NUM_LIT:2> <EOL> result = interpret ( foo , [ <NUM_LIT:7> ] ) <EOL> assert result == <NUM_LIT:4> <EOL> result = f ( <NUM_LIT:7> ) <EOL> assert result == <NUM_LIT:4> <EOL> result = interpret ( foo , [ <NUM_LIT:8> ] ) <EOL> assert result == <NUM_LIT:2> <EOL> result = f ( <NUM_LIT:8> ) <EOL> assert result == <NUM_LIT:2> <EOL> def test_bare_except ( self ) : <EOL> def one ( x ) : <EOL> if x == <NUM_LIT:1> : <EOL> raise ValueError ( ) <EOL> elif x == <NUM_LIT:2> : <EOL> raise TypeError ( ) <EOL> return x - <NUM_LIT:5> <EOL> def foo ( x ) : <EOL> x = one ( x ) <EOL> try : <EOL> x = one ( x ) <EOL> except : <EOL> return <NUM_LIT:1> + x <EOL> return <NUM_LIT:4> + x <EOL> t , g = self . transform_func ( foo , [ int ] ) <EOL> assert len ( list ( g . iterblocks ( ) ) ) == <NUM_LIT:5> <EOL> f = self . compile ( foo , [ int ] ) <EOL> result = interpret ( foo , [ <NUM_LIT:6> ] ) <EOL> assert result == <NUM_LIT:2> <EOL> result = f ( <NUM_LIT:6> ) <EOL> assert result == <NUM_LIT:2> <EOL> result = interpret ( foo , [ <NUM_LIT:7> ] ) <EOL> assert result == <NUM_LIT:3> <EOL> result = f ( <NUM_LIT:7> ) <EOL> assert result == <NUM_LIT:3> <EOL> result = interpret ( foo , [ <NUM_LIT:8> ] ) <EOL> assert result == <NUM_LIT:2> <EOL> result = f ( <NUM_LIT:8> ) <EOL> assert result == <NUM_LIT:2> <EOL> def test_raises ( self ) : <EOL> def foo ( x ) : <EOL> if x : <EOL> raise ValueError ( ) <EOL> t , g = self . transform_func ( foo , [ int ] ) <EOL> assert len ( list ( g . iterblocks ( ) ) ) == <NUM_LIT:3> <EOL> f = self . compile ( foo , [ int ] ) <EOL> f ( <NUM_LIT:0> ) <EOL> py . test . raises ( ValueError , f , <NUM_LIT:1> ) <EOL> def test_no_multiple_transform ( self ) : <EOL> def f ( x ) : <EOL> return x + <NUM_LIT:1> <EOL> t = TranslationContext ( ) <EOL> t . buildannotator ( ) . build_types ( f , [ int ] ) <EOL> t . buildrtyper ( type_system = self . type_system ) . specialize ( ) <EOL> g = graphof ( t , f ) <EOL> etrafo = exceptiontransform . ExceptionTransformer ( t ) <EOL> etrafo . create_exception_handling ( g ) <EOL> etrafo2 = exceptiontransform . ExceptionTransformer ( t ) <EOL> py . test . raises ( AssertionError , etrafo2 . create_exception_handling , g ) <EOL> def test_preserve_can_raise ( self ) : <EOL> def f ( x ) : <EOL> raise ValueError <EOL> t = TranslationContext ( ) <EOL> t . buildannotator ( ) . build_types ( f , [ int ] ) <EOL> t . buildrtyper ( type_system = self . type_system ) . specialize ( ) <EOL> g = graphof ( t , f ) <EOL> etrafo = exceptiontransform . ExceptionTransformer ( t ) <EOL> etrafo . create_exception_handling ( g ) <EOL> assert etrafo . raise_analyzer . analyze_direct_call ( g ) <EOL> class TestLLType ( BaseTestExceptionTransform ) : <EOL> type_system = '<STR_LIT>' <EOL> def compile ( self , fn , inputargs ) : <EOL> from pypy . translator . c . test . test_genc import compile <EOL> return compile ( fn , inputargs ) <EOL> def test_needs_keepalive ( self ) : <EOL> check_debug_build ( ) <EOL> from pypy . rpython . lltypesystem import lltype <EOL> X = lltype . GcStruct ( "<STR_LIT:X>" , <EOL> ( '<STR_LIT:y>' , lltype . Struct ( "<STR_LIT:Y>" , ( '<STR_LIT:z>' , lltype . Signed ) ) ) ) <EOL> def can_raise ( n ) : <EOL> if n : <EOL> raise Exception <EOL> else : <EOL> return <NUM_LIT:1> <EOL> def foo ( n ) : <EOL> x = lltype . malloc ( X ) <EOL> y = x . y <EOL> y . z = <NUM_LIT> <EOL> r = can_raise ( n ) <EOL> return r + y . z <EOL> f = self . compile ( foo , [ int ] ) <EOL> res = f ( <NUM_LIT:0> ) <EOL> assert res == <NUM_LIT> <EOL> def test_inserting_zeroing_op ( self ) : <EOL> from pypy . rpython . lltypesystem import lltype <EOL> S = lltype . GcStruct ( "<STR_LIT:S>" , ( '<STR_LIT:x>' , lltype . Signed ) ) <EOL> def f ( x ) : <EOL> s = lltype . malloc ( S ) <EOL> s . x = <NUM_LIT:0> <EOL> return s . x <EOL> t = TranslationContext ( ) <EOL> t . buildannotator ( ) . build_types ( f , [ int ] ) <EOL> t . buildrtyper ( type_system = self . type_system ) . specialize ( ) <EOL> g = graphof ( t , f ) <EOL> etrafo = exceptiontransform . ExceptionTransformer ( t ) <EOL> etrafo . create_exception_handling ( g ) <EOL> ops = dict . fromkeys ( [ o . opname for b , o in g . iterblockops ( ) ] ) <EOL> assert '<STR_LIT>' in ops <EOL> def test_llexternal ( self ) : <EOL> from pypy . rpython . lltypesystem . rffi import llexternal <EOL> from pypy . rpython . lltypesystem import lltype <EOL> z = llexternal ( '<STR_LIT:z>' , [ lltype . Signed ] , lltype . Signed ) <EOL> def f ( x ) : <EOL> y = - <NUM_LIT:1> <EOL> if x > <NUM_LIT:0> : <EOL> y = z ( x ) <EOL> return y + x <EOL> t , g = self . transform_func ( f , [ int ] , True ) <EOL> assert summary ( g ) == { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> } <EOL> def test_get_exception_addr ( self ) : <EOL> from pypy . rpython . lltypesystem import lltype , llmemory <EOL> from pypy . rpython . lltypesystem . lloperation import llop <EOL> def foo ( ) : <EOL> a = llop . get_exception_addr ( llmemory . Address ) <EOL> assert lltype . typeOf ( a ) is llmemory . Address <EOL> a = llop . get_exc_value_addr ( llmemory . Address ) <EOL> assert lltype . typeOf ( a ) is llmemory . Address <EOL> return <NUM_LIT> <EOL> f = self . compile ( foo , [ ] ) <EOL> res = f ( ) <EOL> assert res == <NUM_LIT> <EOL> class TestOOType ( BaseTestExceptionTransform ) : <EOL> type_system = '<STR_LIT>' <EOL> def compile ( self , fn , inputargs ) : <EOL> from pypy . translator . cli . test . runtest import compile_function <EOL> return compile_function ( fn , inputargs , auto_raise_exc = True , exctrans = True ) </s>
