<s> import gc <EOL> import pprint <EOL> import sys <EOL> import unittest <EOL> from test import support <EOL> class TestGetProfile ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> sys . setprofile ( None ) <EOL> def tearDown ( self ) : <EOL> sys . setprofile ( None ) <EOL> def test_empty ( self ) : <EOL> self . assertIsNone ( sys . getprofile ( ) ) <EOL> def test_setget ( self ) : <EOL> def fn ( * args ) : <EOL> pass <EOL> sys . setprofile ( fn ) <EOL> self . assertIs ( sys . getprofile ( ) , fn ) <EOL> class HookWatcher : <EOL> def __init__ ( self ) : <EOL> self . frames = [ ] <EOL> self . events = [ ] <EOL> def callback ( self , frame , event , arg ) : <EOL> if ( event == "<STR_LIT>" <EOL> or event == "<STR_LIT>" <EOL> or event == "<STR_LIT>" ) : <EOL> self . add_event ( event , frame ) <EOL> def add_event ( self , event , frame = None ) : <EOL> """<STR_LIT>""" <EOL> if frame is None : <EOL> frame = sys . _getframe ( <NUM_LIT:1> ) <EOL> try : <EOL> frameno = self . frames . index ( frame ) <EOL> except ValueError : <EOL> frameno = len ( self . frames ) <EOL> self . frames . append ( frame ) <EOL> self . events . append ( ( frameno , event , ident ( frame ) ) ) <EOL> def get_events ( self ) : <EOL> """<STR_LIT>""" <EOL> disallowed = [ ident ( self . add_event . __func__ ) , ident ( ident ) ] <EOL> self . frames = None <EOL> return [ item for item in self . events if item [ <NUM_LIT:2> ] not in disallowed ] <EOL> class ProfileSimulator ( HookWatcher ) : <EOL> def __init__ ( self , testcase ) : <EOL> self . testcase = testcase <EOL> self . stack = [ ] <EOL> HookWatcher . __init__ ( self ) <EOL> def callback ( self , frame , event , arg ) : <EOL> self . dispatch [ event ] ( self , frame ) <EOL> def trace_call ( self , frame ) : <EOL> self . add_event ( '<STR_LIT>' , frame ) <EOL> self . stack . append ( frame ) <EOL> def trace_return ( self , frame ) : <EOL> self . add_event ( '<STR_LIT>' , frame ) <EOL> self . stack . pop ( ) <EOL> def trace_exception ( self , frame ) : <EOL> self . testcase . fail ( <EOL> "<STR_LIT>" ) <EOL> def trace_pass ( self , frame ) : <EOL> pass <EOL> dispatch = { <EOL> '<STR_LIT>' : trace_call , <EOL> '<STR_LIT>' : trace_exception , <EOL> '<STR_LIT>' : trace_return , <EOL> '<STR_LIT>' : trace_pass , <EOL> '<STR_LIT>' : trace_pass , <EOL> '<STR_LIT>' : trace_pass , <EOL> } <EOL> class TestCaseBase ( unittest . TestCase ) : <EOL> def check_events ( self , callable , expected ) : <EOL> events = capture_events ( callable , self . new_watcher ( ) ) <EOL> if events != expected : <EOL> self . fail ( "<STR_LIT>" <EOL> % ( pprint . pformat ( expected ) , pprint . pformat ( events ) ) ) <EOL> class ProfileHookTestCase ( TestCaseBase ) : <EOL> def new_watcher ( self ) : <EOL> return HookWatcher ( ) <EOL> def test_simple ( self ) : <EOL> def f ( p ) : <EOL> pass <EOL> f_ident = ident ( f ) <EOL> self . check_events ( f , [ ( <NUM_LIT:1> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:1> , '<STR_LIT>' , f_ident ) , <EOL> ] ) <EOL> def test_exception ( self ) : <EOL> def f ( p ) : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> f_ident = ident ( f ) <EOL> self . check_events ( f , [ ( <NUM_LIT:1> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:1> , '<STR_LIT>' , f_ident ) , <EOL> ] ) <EOL> def test_caught_exception ( self ) : <EOL> def f ( p ) : <EOL> try : <NUM_LIT:1> / <NUM_LIT:0> <EOL> except : pass <EOL> f_ident = ident ( f ) <EOL> self . check_events ( f , [ ( <NUM_LIT:1> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:1> , '<STR_LIT>' , f_ident ) , <EOL> ] ) <EOL> def test_caught_nested_exception ( self ) : <EOL> def f ( p ) : <EOL> try : <NUM_LIT:1> / <NUM_LIT:0> <EOL> except : pass <EOL> f_ident = ident ( f ) <EOL> self . check_events ( f , [ ( <NUM_LIT:1> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:1> , '<STR_LIT>' , f_ident ) , <EOL> ] ) <EOL> def test_nested_exception ( self ) : <EOL> def f ( p ) : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> f_ident = ident ( f ) <EOL> self . check_events ( f , [ ( <NUM_LIT:1> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:1> , '<STR_LIT>' , f_ident ) , <EOL> ] ) <EOL> def test_exception_in_except_clause ( self ) : <EOL> def f ( p ) : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> def g ( p ) : <EOL> try : <EOL> f ( p ) <EOL> except : <EOL> try : f ( p ) <EOL> except : pass <EOL> f_ident = ident ( f ) <EOL> g_ident = ident ( g ) <EOL> self . check_events ( g , [ ( <NUM_LIT:1> , '<STR_LIT>' , g_ident ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:3> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:3> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:1> , '<STR_LIT>' , g_ident ) , <EOL> ] ) <EOL> def test_exception_propogation ( self ) : <EOL> def f ( p ) : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> def g ( p ) : <EOL> try : f ( p ) <EOL> finally : p . add_event ( "<STR_LIT>" ) <EOL> f_ident = ident ( f ) <EOL> g_ident = ident ( g ) <EOL> self . check_events ( g , [ ( <NUM_LIT:1> , '<STR_LIT>' , g_ident ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:1> , '<STR_LIT>' , g_ident ) , <EOL> ( <NUM_LIT:1> , '<STR_LIT>' , g_ident ) , <EOL> ] ) <EOL> def test_raise_twice ( self ) : <EOL> def f ( p ) : <EOL> try : <NUM_LIT:1> / <NUM_LIT:0> <EOL> except : <NUM_LIT:1> / <NUM_LIT:0> <EOL> f_ident = ident ( f ) <EOL> self . check_events ( f , [ ( <NUM_LIT:1> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:1> , '<STR_LIT>' , f_ident ) , <EOL> ] ) <EOL> def test_raise_reraise ( self ) : <EOL> def f ( p ) : <EOL> try : <NUM_LIT:1> / <NUM_LIT:0> <EOL> except : raise <EOL> f_ident = ident ( f ) <EOL> self . check_events ( f , [ ( <NUM_LIT:1> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:1> , '<STR_LIT>' , f_ident ) , <EOL> ] ) <EOL> def test_raise ( self ) : <EOL> def f ( p ) : <EOL> raise Exception ( ) <EOL> f_ident = ident ( f ) <EOL> self . check_events ( f , [ ( <NUM_LIT:1> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:1> , '<STR_LIT>' , f_ident ) , <EOL> ] ) <EOL> def test_distant_exception ( self ) : <EOL> def f ( ) : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> def g ( ) : <EOL> f ( ) <EOL> def h ( ) : <EOL> g ( ) <EOL> def i ( ) : <EOL> h ( ) <EOL> def j ( p ) : <EOL> i ( ) <EOL> f_ident = ident ( f ) <EOL> g_ident = ident ( g ) <EOL> h_ident = ident ( h ) <EOL> i_ident = ident ( i ) <EOL> j_ident = ident ( j ) <EOL> self . check_events ( j , [ ( <NUM_LIT:1> , '<STR_LIT>' , j_ident ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' , i_ident ) , <EOL> ( <NUM_LIT:3> , '<STR_LIT>' , h_ident ) , <EOL> ( <NUM_LIT:4> , '<STR_LIT>' , g_ident ) , <EOL> ( <NUM_LIT:5> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:5> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:4> , '<STR_LIT>' , g_ident ) , <EOL> ( <NUM_LIT:3> , '<STR_LIT>' , h_ident ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' , i_ident ) , <EOL> ( <NUM_LIT:1> , '<STR_LIT>' , j_ident ) , <EOL> ] ) <EOL> def test_generator ( self ) : <EOL> def f ( ) : <EOL> for i in range ( <NUM_LIT:2> ) : <EOL> yield i <EOL> def g ( p ) : <EOL> for i in f ( ) : <EOL> pass <EOL> f_ident = ident ( f ) <EOL> g_ident = ident ( g ) <EOL> self . check_events ( g , [ ( <NUM_LIT:1> , '<STR_LIT>' , g_ident ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:1> , '<STR_LIT>' , g_ident ) , <EOL> ] ) <EOL> def test_stop_iteration ( self ) : <EOL> def f ( ) : <EOL> for i in range ( <NUM_LIT:2> ) : <EOL> yield i <EOL> raise StopIteration <EOL> def g ( p ) : <EOL> for i in f ( ) : <EOL> pass <EOL> f_ident = ident ( f ) <EOL> g_ident = ident ( g ) <EOL> self . check_events ( g , [ ( <NUM_LIT:1> , '<STR_LIT>' , g_ident ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:1> , '<STR_LIT>' , g_ident ) , <EOL> ] ) <EOL> class ProfileSimulatorTestCase ( TestCaseBase ) : <EOL> def new_watcher ( self ) : <EOL> return ProfileSimulator ( self ) <EOL> def test_simple ( self ) : <EOL> def f ( p ) : <EOL> pass <EOL> f_ident = ident ( f ) <EOL> self . check_events ( f , [ ( <NUM_LIT:1> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:1> , '<STR_LIT>' , f_ident ) , <EOL> ] ) <EOL> def test_basic_exception ( self ) : <EOL> def f ( p ) : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> f_ident = ident ( f ) <EOL> self . check_events ( f , [ ( <NUM_LIT:1> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:1> , '<STR_LIT>' , f_ident ) , <EOL> ] ) <EOL> def test_caught_exception ( self ) : <EOL> def f ( p ) : <EOL> try : <NUM_LIT:1> / <NUM_LIT:0> <EOL> except : pass <EOL> f_ident = ident ( f ) <EOL> self . check_events ( f , [ ( <NUM_LIT:1> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:1> , '<STR_LIT>' , f_ident ) , <EOL> ] ) <EOL> def test_distant_exception ( self ) : <EOL> def f ( ) : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> def g ( ) : <EOL> f ( ) <EOL> def h ( ) : <EOL> g ( ) <EOL> def i ( ) : <EOL> h ( ) <EOL> def j ( p ) : <EOL> i ( ) <EOL> f_ident = ident ( f ) <EOL> g_ident = ident ( g ) <EOL> h_ident = ident ( h ) <EOL> i_ident = ident ( i ) <EOL> j_ident = ident ( j ) <EOL> self . check_events ( j , [ ( <NUM_LIT:1> , '<STR_LIT>' , j_ident ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' , i_ident ) , <EOL> ( <NUM_LIT:3> , '<STR_LIT>' , h_ident ) , <EOL> ( <NUM_LIT:4> , '<STR_LIT>' , g_ident ) , <EOL> ( <NUM_LIT:5> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:5> , '<STR_LIT>' , f_ident ) , <EOL> ( <NUM_LIT:4> , '<STR_LIT>' , g_ident ) , <EOL> ( <NUM_LIT:3> , '<STR_LIT>' , h_ident ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' , i_ident ) , <EOL> ( <NUM_LIT:1> , '<STR_LIT>' , j_ident ) , <EOL> ] ) <EOL> def ident ( function ) : <EOL> if hasattr ( function , "<STR_LIT>" ) : <EOL> code = function . f_code <EOL> else : <EOL> code = function . __code__ <EOL> return code . co_firstlineno , code . co_name <EOL> def protect ( f , p ) : <EOL> try : f ( p ) <EOL> except : pass <EOL> protect_ident = ident ( protect ) <EOL> def capture_events ( callable , p = None ) : <EOL> if p is None : <EOL> p = HookWatcher ( ) <EOL> old_gc = gc . isenabled ( ) <EOL> gc . disable ( ) <EOL> try : <EOL> sys . setprofile ( p . callback ) <EOL> protect ( callable , p ) <EOL> sys . setprofile ( None ) <EOL> finally : <EOL> if old_gc : <EOL> gc . enable ( ) <EOL> return p . get_events ( ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> def show_events ( callable ) : <EOL> import pprint <EOL> pprint . pprint ( capture_events ( callable ) ) <EOL> def test_main ( ) : <EOL> support . run_unittest ( <EOL> TestGetProfile , <EOL> ProfileHookTestCase , <EOL> ProfileSimulatorTestCase <EOL> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_main ( ) </s>
