<s> import hotshot <EOL> import hotshot . log <EOL> import os <EOL> import pprint <EOL> import unittest <EOL> from test import test_support <EOL> from hotshot . log import ENTER , EXIT , LINE <EOL> def shortfilename ( fn ) : <EOL> if fn : <EOL> return os . path . splitext ( os . path . basename ( fn ) ) [ <NUM_LIT:0> ] <EOL> else : <EOL> return fn <EOL> class UnlinkingLogReader ( hotshot . log . LogReader ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , logfn ) : <EOL> self . __logfn = logfn <EOL> hotshot . log . LogReader . __init__ ( self , logfn ) <EOL> def next ( self , index = None ) : <EOL> try : <EOL> return hotshot . log . LogReader . next ( self ) <EOL> except StopIteration : <EOL> self . close ( ) <EOL> os . unlink ( self . __logfn ) <EOL> raise <EOL> class HotShotTestCase ( unittest . TestCase ) : <EOL> def new_profiler ( self , lineevents = <NUM_LIT:0> , linetimings = <NUM_LIT:1> ) : <EOL> self . logfn = test_support . TESTFN <EOL> return hotshot . Profile ( self . logfn , lineevents , linetimings ) <EOL> def get_logreader ( self ) : <EOL> return UnlinkingLogReader ( self . logfn ) <EOL> def get_events_wotime ( self ) : <EOL> L = [ ] <EOL> for event in self . get_logreader ( ) : <EOL> what , ( filename , lineno , funcname ) , tdelta = event <EOL> L . append ( ( what , ( shortfilename ( filename ) , lineno , funcname ) ) ) <EOL> return L <EOL> def check_events ( self , expected ) : <EOL> events = self . get_events_wotime ( ) <EOL> if events != expected : <EOL> self . fail ( <EOL> "<STR_LIT>" <EOL> % ( pprint . pformat ( events ) , pprint . pformat ( expected ) ) ) <EOL> def run_test ( self , callable , events , profiler = None ) : <EOL> if profiler is None : <EOL> profiler = self . new_profiler ( ) <EOL> self . failUnless ( not profiler . _prof . closed ) <EOL> profiler . runcall ( callable ) <EOL> self . failUnless ( not profiler . _prof . closed ) <EOL> profiler . close ( ) <EOL> self . failUnless ( profiler . _prof . closed ) <EOL> self . check_events ( events ) <EOL> def test_addinfo ( self ) : <EOL> def f ( p ) : <EOL> p . addinfo ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> profiler = self . new_profiler ( ) <EOL> profiler . runcall ( f , profiler ) <EOL> profiler . close ( ) <EOL> log = self . get_logreader ( ) <EOL> info = log . _info <EOL> list ( log ) <EOL> self . failUnless ( info [ "<STR_LIT>" ] == [ "<STR_LIT>" ] ) <EOL> def test_line_numbers ( self ) : <EOL> def f ( ) : <EOL> y = <NUM_LIT:2> <EOL> x = <NUM_LIT:1> <EOL> def g ( ) : <EOL> f ( ) <EOL> f_lineno = f . func_code . co_firstlineno <EOL> g_lineno = g . func_code . co_firstlineno <EOL> events = [ ( ENTER , ( "<STR_LIT>" , g_lineno , "<STR_LIT:g>" ) ) , <EOL> ( LINE , ( "<STR_LIT>" , g_lineno + <NUM_LIT:1> , "<STR_LIT:g>" ) ) , <EOL> ( ENTER , ( "<STR_LIT>" , f_lineno , "<STR_LIT:f>" ) ) , <EOL> ( LINE , ( "<STR_LIT>" , f_lineno + <NUM_LIT:1> , "<STR_LIT:f>" ) ) , <EOL> ( LINE , ( "<STR_LIT>" , f_lineno + <NUM_LIT:2> , "<STR_LIT:f>" ) ) , <EOL> ( EXIT , ( "<STR_LIT>" , f_lineno , "<STR_LIT:f>" ) ) , <EOL> ( EXIT , ( "<STR_LIT>" , g_lineno , "<STR_LIT:g>" ) ) , <EOL> ] <EOL> self . run_test ( g , events , self . new_profiler ( lineevents = <NUM_LIT:1> ) ) <EOL> def test_start_stop ( self ) : <EOL> profiler = self . new_profiler ( ) <EOL> profiler . start ( ) <EOL> profiler . stop ( ) <EOL> profiler . close ( ) <EOL> os . unlink ( self . logfn ) <EOL> def test_bad_sys_path ( self ) : <EOL> import sys <EOL> import os <EOL> orig_path = sys . path <EOL> coverage = hotshot . _hotshot . coverage <EOL> try : <EOL> sys . path = '<STR_LIT:abc>' <EOL> self . assertRaises ( RuntimeError , coverage , test_support . TESTFN ) <EOL> del sys . path <EOL> self . assertRaises ( RuntimeError , coverage , test_support . TESTFN ) <EOL> finally : <EOL> sys . path = orig_path <EOL> if os . path . exists ( test_support . TESTFN ) : <EOL> os . remove ( test_support . TESTFN ) <EOL> def test_main ( ) : <EOL> test_support . run_unittest ( HotShotTestCase ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_main ( ) </s>
