<s> """<STR_LIT>""" <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> from pikos . _internal import _lsprof_rt as _lsprof <EOL> def run ( statement , filename = None , sort = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> prof = Profile ( ) <EOL> result = None <EOL> try : <EOL> try : <EOL> prof = prof . run ( statement ) <EOL> except SystemExit : <EOL> pass <EOL> finally : <EOL> if filename is not None : <EOL> prof . dump_stats ( filename ) <EOL> else : <EOL> result = prof . print_stats ( sort ) <EOL> return result <EOL> def runctx ( statement , globals , locals , filename = None , sort = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> prof = Profile ( ) <EOL> result = None <EOL> try : <EOL> try : <EOL> prof = prof . runctx ( statement , globals , locals ) <EOL> except SystemExit : <EOL> pass <EOL> finally : <EOL> if filename is not None : <EOL> prof . dump_stats ( filename ) <EOL> else : <EOL> result = prof . print_stats ( sort ) <EOL> return result <EOL> def help ( ) : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> class Profile ( _lsprof . Profiler ) : <EOL> """<STR_LIT>""" <EOL> def print_stats ( self , sort = - <NUM_LIT:1> ) : <EOL> import pstats <EOL> pstats . Stats ( self ) . strip_dirs ( ) . sort_stats ( sort ) . print_stats ( ) <EOL> def dump_stats ( self , file ) : <EOL> import marshal <EOL> f = open ( file , '<STR_LIT:wb>' ) <EOL> self . create_stats ( ) <EOL> marshal . dump ( self . stats , f ) <EOL> f . close ( ) <EOL> def create_stats ( self ) : <EOL> self . disable ( ) <EOL> self . snapshot_stats ( ) <EOL> def snapshot_stats ( self ) : <EOL> entries = self . getstats ( ) <EOL> self . stats = { } <EOL> callersdicts = { } <EOL> for entry in entries : <EOL> func = label ( entry . code ) <EOL> nc = entry . callcount <EOL> cc = nc - entry . reccallcount <EOL> tt = entry . inlinetime <EOL> ct = entry . totaltime <EOL> callers = { } <EOL> callersdicts [ id ( entry . code ) ] = callers <EOL> self . stats [ func ] = cc , nc , tt , ct , callers <EOL> for entry in entries : <EOL> if entry . calls : <EOL> func = label ( entry . code ) <EOL> for subentry in entry . calls : <EOL> try : <EOL> callers = callersdicts [ id ( subentry . code ) ] <EOL> except KeyError : <EOL> continue <EOL> nc = subentry . callcount <EOL> cc = nc - subentry . reccallcount <EOL> tt = subentry . inlinetime <EOL> ct = subentry . totaltime <EOL> if func in callers : <EOL> prev = callers [ func ] <EOL> nc += prev [ <NUM_LIT:0> ] <EOL> cc += prev [ <NUM_LIT:1> ] <EOL> tt += prev [ <NUM_LIT:2> ] <EOL> ct += prev [ <NUM_LIT:3> ] <EOL> callers [ func ] = nc , cc , tt , ct <EOL> def run ( self , cmd ) : <EOL> import __main__ <EOL> dict = __main__ . __dict__ <EOL> return self . runctx ( cmd , dict , dict ) <EOL> def runctx ( self , cmd , globals , locals ) : <EOL> self . enable ( ) <EOL> try : <EOL> exec cmd in globals , locals <EOL> finally : <EOL> self . disable ( ) <EOL> return self <EOL> def runcall ( self , func , * args , ** kw ) : <EOL> self . enable ( ) <EOL> try : <EOL> return func ( * args , ** kw ) <EOL> finally : <EOL> self . disable ( ) <EOL> def label ( code ) : <EOL> if isinstance ( code , str ) : <EOL> return ( '<STR_LIT>' , <NUM_LIT:0> , code ) <EOL> else : <EOL> return ( code . co_filename , code . co_firstlineno , code . co_name ) <EOL> def main ( ) : <EOL> import os , sys <EOL> from optparse import OptionParser <EOL> usage = "<STR_LIT>" <EOL> parser = OptionParser ( usage = usage ) <EOL> parser . allow_interspersed_args = False <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" , default = None ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" , <EOL> default = - <NUM_LIT:1> ) <EOL> if not sys . argv [ <NUM_LIT:1> : ] : <EOL> parser . print_usage ( ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> ( options , args ) = parser . parse_args ( ) <EOL> sys . argv [ : ] = args <EOL> if len ( args ) > <NUM_LIT:0> : <EOL> progname = args [ <NUM_LIT:0> ] <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . dirname ( progname ) ) <EOL> with open ( progname , '<STR_LIT:rb>' ) as fp : <EOL> code = compile ( fp . read ( ) , progname , '<STR_LIT>' ) <EOL> globs = { <EOL> '<STR_LIT>' : progname , <EOL> '<STR_LIT>' : '<STR_LIT:__main__>' , <EOL> '<STR_LIT>' : None , <EOL> } <EOL> runctx ( code , globs , None , options . outfile , options . sort ) <EOL> else : <EOL> parser . print_usage ( ) <EOL> return parser <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
