<s> """<STR_LIT>""" <EOL> __revision__ = "<STR_LIT>" <EOL> import os <EOL> import string <EOL> import sys <EOL> try : <EOL> import weakref <EOL> except ImportError : <EOL> def logInstanceCreation ( instance , name = None ) : <EOL> pass <EOL> else : <EOL> def logInstanceCreation ( instance , name = None ) : <EOL> if name is None : <EOL> name = instance . __class__ . __name__ <EOL> if not tracked_classes . has_key ( name ) : <EOL> tracked_classes [ name ] = [ ] <EOL> tracked_classes [ name ] . append ( weakref . ref ( instance ) ) <EOL> tracked_classes = { } <EOL> def string_to_classes ( s ) : <EOL> if s == '<STR_LIT:*>' : <EOL> c = tracked_classes . keys ( ) <EOL> c . sort ( ) <EOL> return c <EOL> else : <EOL> return string . split ( s ) <EOL> def fetchLoggedInstances ( classes = "<STR_LIT:*>" ) : <EOL> classnames = string_to_classes ( classes ) <EOL> return map ( lambda cn : ( cn , len ( tracked_classes [ cn ] ) ) , classnames ) <EOL> def countLoggedInstances ( classes , file = sys . stdout ) : <EOL> for classname in string_to_classes ( classes ) : <EOL> file . write ( "<STR_LIT>" % ( classname , len ( tracked_classes [ classname ] ) ) ) <EOL> def listLoggedInstances ( classes , file = sys . stdout ) : <EOL> for classname in string_to_classes ( classes ) : <EOL> file . write ( '<STR_LIT>' % classname ) <EOL> for ref in tracked_classes [ classname ] : <EOL> obj = ref ( ) <EOL> if obj is not None : <EOL> file . write ( '<STR_LIT>' % repr ( obj ) ) <EOL> def dumpLoggedInstances ( classes , file = sys . stdout ) : <EOL> for classname in string_to_classes ( classes ) : <EOL> file . write ( '<STR_LIT>' % classname ) <EOL> for ref in tracked_classes [ classname ] : <EOL> obj = ref ( ) <EOL> if obj is not None : <EOL> file . write ( '<STR_LIT>' % obj ) <EOL> for key , value in obj . __dict__ . items ( ) : <EOL> file . write ( '<STR_LIT>' % ( key , value ) ) <EOL> if sys . platform [ : <NUM_LIT:5> ] == "<STR_LIT>" : <EOL> def memory ( ) : <EOL> mstr = open ( '<STR_LIT>' ) . read ( ) <EOL> mstr = string . split ( mstr ) [ <NUM_LIT> ] <EOL> return int ( mstr ) <EOL> else : <EOL> try : <EOL> import resource <EOL> except ImportError : <EOL> try : <EOL> import win32process <EOL> import win32api <EOL> except ImportError : <EOL> def memory ( ) : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> def memory ( ) : <EOL> process_handle = win32api . GetCurrentProcess ( ) <EOL> memory_info = win32process . GetProcessMemoryInfo ( process_handle ) <EOL> return memory_info [ '<STR_LIT>' ] <EOL> else : <EOL> def memory ( ) : <EOL> res = resource . getrusage ( resource . RUSAGE_SELF ) <EOL> return res [ <NUM_LIT:4> ] <EOL> def caller_stack ( * backlist ) : <EOL> import traceback <EOL> if not backlist : <EOL> backlist = [ <NUM_LIT:0> ] <EOL> result = [ ] <EOL> for back in backlist : <EOL> tb = traceback . extract_stack ( limit = <NUM_LIT:3> + back ) <EOL> key = tb [ <NUM_LIT:0> ] [ : <NUM_LIT:3> ] <EOL> result . append ( '<STR_LIT>' % func_shorten ( key ) ) <EOL> return result <EOL> caller_bases = { } <EOL> caller_dicts = { } <EOL> def caller_trace ( back = <NUM_LIT:0> ) : <EOL> import traceback <EOL> tb = traceback . extract_stack ( limit = <NUM_LIT:3> + back ) <EOL> tb . reverse ( ) <EOL> callee = tb [ <NUM_LIT:1> ] [ : <NUM_LIT:3> ] <EOL> caller_bases [ callee ] = caller_bases . get ( callee , <NUM_LIT:0> ) + <NUM_LIT:1> <EOL> for caller in tb [ <NUM_LIT:2> : ] : <EOL> caller = callee + caller [ : <NUM_LIT:3> ] <EOL> try : <EOL> entry = caller_dicts [ callee ] <EOL> except KeyError : <EOL> caller_dicts [ callee ] = entry = { } <EOL> entry [ caller ] = entry . get ( caller , <NUM_LIT:0> ) + <NUM_LIT:1> <EOL> callee = caller <EOL> def _dump_one_caller ( key , file , level = <NUM_LIT:0> ) : <EOL> l = [ ] <EOL> for c , v in caller_dicts [ key ] . items ( ) : <EOL> l . append ( ( - v , c ) ) <EOL> l . sort ( ) <EOL> leader = '<STR_LIT:U+0020>' * level <EOL> for v , c in l : <EOL> file . write ( "<STR_LIT>" % ( ( leader , - v ) + func_shorten ( c [ - <NUM_LIT:3> : ] ) ) ) <EOL> if caller_dicts . has_key ( c ) : <EOL> _dump_one_caller ( c , file , level + <NUM_LIT:1> ) <EOL> def dump_caller_counts ( file = sys . stdout ) : <EOL> keys = caller_bases . keys ( ) <EOL> keys . sort ( ) <EOL> for k in keys : <EOL> file . write ( "<STR_LIT>" <EOL> % ( func_shorten ( k ) + ( caller_bases [ k ] , ) ) ) <EOL> _dump_one_caller ( k , file ) <EOL> shorten_list = [ <EOL> ( '<STR_LIT>' , <NUM_LIT:1> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> ) , <EOL> ] <EOL> if os . sep != '<STR_LIT:/>' : <EOL> def platformize ( t ) : <EOL> return ( string . replace ( t [ <NUM_LIT:0> ] , '<STR_LIT:/>' , os . sep ) , t [ <NUM_LIT:1> ] ) <EOL> shorten_list = map ( platformize , shorten_list ) <EOL> del platformize <EOL> def func_shorten ( func_tuple ) : <EOL> f = func_tuple [ <NUM_LIT:0> ] <EOL> for t in shorten_list : <EOL> i = string . find ( f , t [ <NUM_LIT:0> ] ) <EOL> if i >= <NUM_LIT:0> : <EOL> if t [ <NUM_LIT:1> ] : <EOL> i = i + len ( t [ <NUM_LIT:0> ] ) <EOL> return ( f [ i : ] , ) + func_tuple [ <NUM_LIT:1> : ] <EOL> return func_tuple <EOL> TraceFP = { } <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> TraceDefault = '<STR_LIT>' <EOL> else : <EOL> TraceDefault = '<STR_LIT>' <EOL> def Trace ( msg , file = None , mode = '<STR_LIT:w>' ) : <EOL> """<STR_LIT>""" <EOL> global TraceDefault <EOL> if file is None : <EOL> file = TraceDefault <EOL> else : <EOL> TraceDefault = file <EOL> try : <EOL> fp = TraceFP [ file ] <EOL> except KeyError : <EOL> try : <EOL> fp = TraceFP [ file ] = open ( file , mode ) <EOL> except TypeError : <EOL> fp = file <EOL> fp . write ( msg ) <EOL> fp . flush ( ) </s>
