<s> from pypy . translator . stackless . test . test_transform import llinterp_stackless_function , run_stackless_function <EOL> from pypy . rlib import rstack <EOL> import py <EOL> import os , sys <EOL> def test_simple ( ) : <EOL> def g1 ( ) : <EOL> "<STR_LIT>" <EOL> def g2 ( ignored ) : <EOL> pass <EOL> g1 ( ) <EOL> def f ( n ) : <EOL> g1 ( ) <EOL> if n > <NUM_LIT:0> : <EOL> res = f ( n - <NUM_LIT:1> ) + <NUM_LIT:0> <EOL> else : <EOL> res = rstack . stack_frames_depth ( ) <EOL> g2 ( g1 ) <EOL> return res <EOL> def fn ( ) : <EOL> count0 = f ( <NUM_LIT:0> ) <EOL> count10 = f ( <NUM_LIT:10> ) <EOL> return count10 - count0 <EOL> res = llinterp_stackless_function ( fn ) <EOL> assert res == <NUM_LIT:10> <EOL> res = run_stackless_function ( fn ) <EOL> assert res == <NUM_LIT:10> <EOL> def test_with_ptr ( ) : <EOL> def f ( n ) : <EOL> if n > <NUM_LIT:0> : <EOL> res , dummy = f ( n - <NUM_LIT:1> ) <EOL> return ( res , dummy ) <EOL> else : <EOL> res = rstack . stack_frames_depth ( ) , <NUM_LIT:1> <EOL> return res <EOL> def fn ( ) : <EOL> count0 , _ = f ( <NUM_LIT:0> ) <EOL> count10 , _ = f ( <NUM_LIT:10> ) <EOL> return count10 - count0 <EOL> res = llinterp_stackless_function ( fn ) <EOL> assert res == <NUM_LIT:10> <EOL> res = run_stackless_function ( fn ) <EOL> assert res == <NUM_LIT:10> <EOL> def test_manytimes ( ) : <EOL> def f ( n ) : <EOL> if n > <NUM_LIT:0> : <EOL> res , dummy = f ( n - <NUM_LIT:1> ) <EOL> return ( res , dummy ) <EOL> else : <EOL> res = rstack . stack_frames_depth ( ) , <NUM_LIT:1> <EOL> return res <EOL> def fn ( ) : <EOL> count0 , _ = f ( <NUM_LIT:0> ) <EOL> count10 , _ = f ( <NUM_LIT:100> ) <EOL> return count10 - count0 <EOL> res = llinterp_stackless_function ( fn ) <EOL> assert res == <NUM_LIT:100> <EOL> res = run_stackless_function ( fn ) <EOL> assert res == <NUM_LIT:100> <EOL> def test_arguments ( ) : <EOL> def f ( n , d , t ) : <EOL> if n > <NUM_LIT:0> : <EOL> res , y , z = f ( n - <NUM_LIT:1> , d , t ) <EOL> return res , y , z <EOL> else : <EOL> res = rstack . stack_frames_depth ( ) , d , t <EOL> return res <EOL> def fn ( ) : <EOL> count0 , d , t = f ( <NUM_LIT:0> , <NUM_LIT> , ( <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> count10 , d , t = f ( <NUM_LIT:10> , <NUM_LIT> , ( <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> return count10 - count0 + int ( d ) <EOL> res = llinterp_stackless_function ( fn ) <EOL> assert res == <NUM_LIT:15> <EOL> res = run_stackless_function ( fn ) <EOL> assert res == <NUM_LIT:15> <EOL> def test_stack_capture ( ) : <EOL> def fn ( ) : <EOL> frame = rstack . stack_capture ( ) <EOL> return int ( bool ( frame ) ) <EOL> res = llinterp_stackless_function ( fn ) <EOL> assert res == <NUM_LIT:1> <EOL> def test_eliminate_tail_calls ( ) : <EOL> def f ( n ) : <EOL> if n > <NUM_LIT:0> : <EOL> res = f ( n - <NUM_LIT:1> ) <EOL> else : <EOL> res = rstack . stack_frames_depth ( ) <EOL> return res <EOL> def fn ( ) : <EOL> count0 = f ( <NUM_LIT:0> ) <EOL> count10 = f ( <NUM_LIT:10> ) <EOL> return count10 - count0 <EOL> res = llinterp_stackless_function ( fn ) <EOL> assert res == <NUM_LIT:0> <EOL> def test_depth_bug ( ) : <EOL> def g ( base ) : <EOL> print rstack . stack_frames_depth ( ) <EOL> return rstack . stack_frames_depth ( ) - base <EOL> def fn ( ) : <EOL> base = rstack . stack_frames_depth ( ) <EOL> print base <EOL> base = rstack . stack_frames_depth ( ) <EOL> print base <EOL> return g ( base ) + <NUM_LIT:100> <EOL> res = llinterp_stackless_function ( fn ) <EOL> assert res == <NUM_LIT> <EOL> def test_depth_along_yield_frame ( ) : <EOL> def h ( ) : <EOL> x = rstack . stack_frames_depth ( ) <EOL> x += <NUM_LIT:1> <EOL> x -= <NUM_LIT:1> <EOL> return x <EOL> def g ( base , lst ) : <EOL> lst . append ( rstack . stack_frames_depth ( ) - base ) <EOL> frametop_before_5 = rstack . yield_current_frame_to_caller ( ) <EOL> lst . append ( h ( ) ) <EOL> frametop_before_7 = frametop_before_5 . switch ( ) <EOL> lst . append ( rstack . stack_frames_depth ( ) ) <EOL> return frametop_before_7 <EOL> def f ( base ) : <EOL> lst = [ rstack . stack_frames_depth ( ) - base ] <EOL> frametop_before_4 = g ( base , lst ) <EOL> lst . append ( rstack . stack_frames_depth ( ) - base ) <EOL> frametop_before_6 = frametop_before_4 . switch ( ) <EOL> lst . append ( h ( ) - base ) <EOL> frametop_after_return = frametop_before_6 . switch ( ) <EOL> lst . append ( rstack . stack_frames_depth ( ) - base ) <EOL> assert not frametop_after_return <EOL> n = <NUM_LIT:0> <EOL> for i in lst : <EOL> n = n * <NUM_LIT:10> + i <EOL> return n <EOL> def loop ( base , n ) : <EOL> if n > <NUM_LIT:0> : <EOL> return loop ( base , n - <NUM_LIT:1> ) + <NUM_LIT:1> <EOL> else : <EOL> return f ( base ) + <NUM_LIT:1> <EOL> def entrypoint ( ) : <EOL> base = rstack . stack_frames_depth ( ) <EOL> return loop ( base , <NUM_LIT:5> ) - <NUM_LIT:6> <EOL> data = llinterp_stackless_function ( entrypoint ) <EOL> assert data == <NUM_LIT> <EOL> res = run_stackless_function ( entrypoint ) <EOL> assert res == <NUM_LIT> <EOL> def test_get_set_stack_depth_limit ( ) : <EOL> def f ( ) : <EOL> rstack . set_stack_depth_limit ( <NUM_LIT> ) <EOL> return rstack . get_stack_depth_limit ( ) <EOL> data = llinterp_stackless_function ( f , assert_unwind = False ) <EOL> assert data == <NUM_LIT> <EOL> def test_stack_limit ( ) : <EOL> def g ( ) : <EOL> return rstack . stack_frames_depth ( ) <EOL> def f ( ) : <EOL> rstack . set_stack_depth_limit ( <NUM_LIT:1> ) <EOL> try : <EOL> return g ( ) <EOL> except RuntimeError : <EOL> return - <NUM_LIT> <EOL> data = llinterp_stackless_function ( f ) <EOL> assert data == - <NUM_LIT> <EOL> def test_stack_limit_2 ( ) : <EOL> def g ( ) : <EOL> return rstack . stack_frames_depth ( ) <EOL> def f ( ) : <EOL> rstack . stack_frames_depth ( ) <EOL> rstack . set_stack_depth_limit ( <NUM_LIT:1> ) <EOL> try : <EOL> return g ( ) <EOL> except RuntimeError : <EOL> return - <NUM_LIT> <EOL> data = llinterp_stackless_function ( f ) <EOL> assert data == - <NUM_LIT> </s>
