<s> import types <EOL> import re <EOL> import pypy . annotation . model as annmodel <EOL> import pypy . objspace . flow . model as flowmodel <EOL> class typerep ( object ) : <EOL> def __init__ ( self , x ) : <EOL> self . typ = getattr ( x , '<STR_LIT>' , type ( x ) ) <EOL> self . bound = None <EOL> if hasattr ( x , '<STR_LIT>' ) : <EOL> self . bound = x . im_self is not None <EOL> elif hasattr ( x , '<STR_LIT>' ) : <EOL> self . bound = x . __self__ is not None <EOL> def __hash__ ( self ) : <EOL> return hash ( self . typ ) <EOL> def __cmp__ ( self , other ) : <EOL> return cmp ( ( self . typ . __name__ , self . bound , self . typ ) , ( other . typ . __name__ , other . bound , other . typ ) ) <EOL> def __str__ ( self ) : <EOL> if self . bound is None : <EOL> s = self . typ . __name__ <EOL> elif self . bound : <EOL> s = '<STR_LIT>' % self . typ . __name__ <EOL> else : <EOL> s = '<STR_LIT>' % self . typ . __name__ <EOL> if self . typ . __module__ == '<STR_LIT>' : <EOL> s = "<STR_LIT>" % s <EOL> return s <EOL> def typereps ( bunch ) : <EOL> t = dict . fromkeys ( [ typerep ( x ) for x in bunch ] ) . keys ( ) <EOL> t . sort ( ) <EOL> return t <EOL> def roots ( classes ) : <EOL> work = list ( classes ) <EOL> res = [ ] <EOL> notbound = False <EOL> while None in work : <EOL> work . remove ( None ) <EOL> notbound = True <EOL> if len ( work ) == <NUM_LIT:1> : <EOL> return notbound , classes [ <NUM_LIT:0> ] <EOL> while work : <EOL> cand = work . pop ( ) <EOL> for cls in work : <EOL> if issubclass ( cls , cand ) : <EOL> continue <EOL> if issubclass ( cand , cls ) : <EOL> cand = cls <EOL> continue <EOL> res . append ( cand ) <EOL> work = [ cls for cls in work if not issubclass ( cls , cand ) ] <EOL> for x in res : <EOL> for y in res : <EOL> if x != y : <EOL> assert not issubclass ( x , y ) , "<STR_LIT>" % ( classes , x , y ) <EOL> assert not issubclass ( y , x ) , "<STR_LIT>" % ( classes , x , y ) <EOL> return notbound , tuple ( res ) <EOL> def callablereps ( bunch ) : <EOL> callables = [ func for clsdef , func in bunch ] <EOL> classes = [ clsdef and clsdef . cls for clsdef , func in bunch ] <EOL> return roots ( classes ) , tuple ( typereps ( callables ) ) <EOL> def prettyfunc ( func ) : <EOL> descr = "<STR_LIT>" % ( getattr ( func , '<STR_LIT>' , None ) or '<STR_LIT:?>' , func . func_code . co_firstlineno ) <EOL> funcname = getattr ( func , '<STR_LIT>' , None ) or '<STR_LIT>' <EOL> cls = getattr ( func , '<STR_LIT>' , None ) <EOL> if cls : <EOL> funcname = "<STR_LIT>" % ( cls . __name__ , funcname ) <EOL> return descr + funcname <EOL> def prettycallable ( ( cls , obj ) ) : <EOL> if cls is None or cls == ( True , ( ) ) : <EOL> cls = None <EOL> else : <EOL> notbound = False <EOL> if isinstance ( cls , tuple ) and isinstance ( cls [ <NUM_LIT:0> ] , bool ) : <EOL> notbound , cls = cls <EOL> if isinstance ( cls , tuple ) : <EOL> cls = "<STR_LIT>" % '<STR_LIT:|>' . join ( [ x . __name__ for x in cls ] ) <EOL> else : <EOL> cls = cls . __name__ <EOL> if notbound : <EOL> cls = "<STR_LIT>" % cls <EOL> if isinstance ( obj , types . FunctionType ) : <EOL> obj = prettyfunc ( obj ) <EOL> elif isinstance ( obj , tuple ) : <EOL> obj = "<STR_LIT>" % '<STR_LIT:|>' . join ( [ str ( x ) for x in obj ] ) <EOL> else : <EOL> obj = str ( obj ) <EOL> if obj . startswith ( '<STR_LIT:<>' ) : <EOL> obj = obj [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> if cls is None : <EOL> return str ( obj ) <EOL> else : <EOL> return "<STR_LIT>" % ( cls , obj ) <EOL> def prettybunch ( bunch ) : <EOL> if len ( bunch ) == <NUM_LIT:1> : <EOL> parts = [ "<STR_LIT>" , iter ( bunch ) . next ( ) ] <EOL> else : <EOL> parts = [ "<STR_LIT>" ] + typereps ( bunch ) <EOL> return '<STR_LIT:U+0020>' . join ( map ( str , parts ) ) <EOL> def pbcaccess ( translator ) : <EOL> annotator = translator . annotator <EOL> for inf in annotator . getpbcaccesssets ( ) . root_info . itervalues ( ) : <EOL> objs = inf . objects <EOL> print len ( objs ) , prettybunch ( objs ) , inf . attrs . keys ( ) <EOL> def pbcs ( translator ) : <EOL> bk = translator . annotator . bookkeeper <EOL> xs = bk . pbccache . keys ( ) <EOL> funcs = [ x for x in xs if isinstance ( x , types . FunctionType ) ] <EOL> staticmethods = [ x for x in xs if isinstance ( x , staticmethod ) ] <EOL> binstancemethods = [ x for x in xs if isinstance ( x , types . MethodType ) and x . im_self ] <EOL> ubinstancemethods = [ x for x in xs if isinstance ( x , types . MethodType ) and not x . im_self ] <EOL> typs = [ x for x in xs if isinstance ( x , ( type , types . ClassType ) ) ] <EOL> rest = [ x for x in xs if not isinstance ( x , ( types . FunctionType , staticmethod , types . MethodType , type , types . ClassType ) ) ] <EOL> for objs in ( funcs , staticmethods , binstancemethods , ubinstancemethods , typs , rest ) : <EOL> print len ( objs ) , prettybunch ( objs ) <EOL> def mutables ( translator ) : <EOL> bk = translator . annotator . bookkeeper <EOL> xs = bk . seen_mutable . keys ( ) <EOL> print len ( xs ) , prettybunch ( xs ) <EOL> def prettypatt ( patts ) : <EOL> accum = [ ] <EOL> patts . sort ( ) <EOL> for ( sh_cnt , sh_ks , sh_st , sh_stst ) in patts : <EOL> arg = [ ] <EOL> arg . append ( "<STR_LIT>" % sh_cnt ) <EOL> for kw in sh_ks : <EOL> arg . append ( "<STR_LIT>" % kw ) <EOL> if sh_st : <EOL> arg . append ( '<STR_LIT:*>' ) <EOL> if sh_stst : <EOL> arg . append ( '<STR_LIT>' ) <EOL> accum . append ( "<STR_LIT>" % '<STR_LIT:U+002CU+0020>' . join ( arg ) ) <EOL> return '<STR_LIT:U+0020>' . join ( accum ) <EOL> def pbccallsanity ( translator ) : <EOL> callb = translator . annotator . getpbccallables ( ) <EOL> bk = translator . annotator . bookkeeper <EOL> typs = [ x for x in callb if isinstance ( x , ( type , types . ClassType ) ) ] <EOL> for t in typs : <EOL> assert len ( callb [ t ] ) == <NUM_LIT:1> <EOL> assert callb [ t ] == { ( None , t ) : True } <EOL> print len ( typs ) , "<STR_LIT>" , prettycallable ( callablereps ( [ ( None , Exception ) , ( None , object ) ] ) ) <EOL> ubm = [ x for x in callb if isinstance ( x , types . MethodType ) and x . im_self is None ] <EOL> assert len ( ubm ) == <NUM_LIT:0> <EOL> bm = [ x for x in callb if isinstance ( x , types . MethodType ) and x . im_self is not None ] <EOL> frompbc = <NUM_LIT:0> <EOL> notfrompbc = [ ] <EOL> for b in bm : <EOL> assert len ( callb [ b ] ) == <NUM_LIT:1> <EOL> assert callb [ b ] == { ( None , b ) : True } <EOL> if b . im_class in bk . pbctypes or ( b . im_class is None and b . im_self in bk . pbccache ) : <EOL> frompbc += <NUM_LIT:1> <EOL> else : <EOL> notfrompbc . append ( b ) <EOL> class A : <EOL> def m ( ) : <EOL> pass <EOL> print frompbc , "<STR_LIT>" , prettycallable ( callablereps ( [ ( None , A ( ) . m ) ] ) ) , "<STR_LIT>" <EOL> print len ( bm ) - frompbc , "<STR_LIT>" , prettycallable ( callablereps ( [ ( None , A ( ) . m ) ] ) ) , "<STR_LIT>" <EOL> if len ( notfrompbc ) < <NUM_LIT> : <EOL> for b in notfrompbc : <EOL> print "<STR_LIT:U+0020>" * <NUM_LIT:4> , prettycallable ( ( None , b ) ) <EOL> fs = [ x for x in callb if isinstance ( x , types . FunctionType ) ] <EOL> assert len ( fs ) + len ( typs ) + frompbc + len ( notfrompbc ) == len ( callb ) <EOL> plain = [ ] <EOL> r = [ ] <EOL> for x in fs : <EOL> if len ( callb [ x ] ) == <NUM_LIT:1> and callb [ x ] . keys ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == None : <EOL> r . extend ( callb [ x ] . keys ( ) ) <EOL> plain . append ( x ) <EOL> print len ( plain ) , "<STR_LIT>" , prettycallable ( callablereps ( r ) ) <EOL> r = [ ] <EOL> for x in fs : <EOL> if x not in plain and len ( callb [ x ] ) == <NUM_LIT:1> : <EOL> r . extend ( callb [ x ] . keys ( ) ) <EOL> print len ( r ) , "<STR_LIT>" , prettycallable ( callablereps ( r ) ) <EOL> r = [ ] <EOL> b_nb = [ ] <EOL> for x in fs : <EOL> if len ( callb [ x ] ) == <NUM_LIT:2> and [ <NUM_LIT:1> for clsdef , f in callb [ x ] . keys ( ) if clsdef is None ] : <EOL> r . extend ( callb [ x ] . keys ( ) ) <EOL> b_nb . append ( x ) <EOL> print len ( r ) , "<STR_LIT>" , prettycallable ( callablereps ( r ) ) <EOL> print "<STR_LIT>" <EOL> for x in fs : <EOL> if len ( callb [ x ] ) >= <NUM_LIT:2> and x not in b_nb : <EOL> print '<STR_LIT:U+0020>' . join ( [ prettycallable ( ( classdef and classdef . cls , func ) ) for ( classdef , func ) in callb [ x ] . keys ( ) ] ) <EOL> def pretty_els ( objs ) : <EOL> accum = [ ] <EOL> for classdef , obj in objs : <EOL> cls = classdef and classdef . cls <EOL> accum . append ( prettycallable ( ( cls , obj ) ) ) <EOL> els = '<STR_LIT:U+0020>' . join ( accum ) <EOL> if len ( accum ) == <NUM_LIT:1> : <EOL> return els <EOL> else : <EOL> return "<STR_LIT>" % els <EOL> def pbccall ( translator ) : <EOL> fams = translator . annotator . getpbccallfamilies ( ) . root_info . itervalues ( ) <EOL> one_pattern_fams = { } <EOL> rest = [ ] <EOL> for fam in fams : <EOL> shapes = fam . patterns <EOL> if len ( shapes ) != <NUM_LIT:1> : <EOL> rest . append ( ( len ( fam . objects ) , fam . objects , shapes . keys ( ) ) ) <EOL> else : <EOL> kinds = callablereps ( fam . objects ) <EOL> flavor = tuple ( kinds ) , shapes . keys ( ) [ <NUM_LIT:0> ] <EOL> cntrs = one_pattern_fams . setdefault ( flavor , [ <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> cntrs [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> cntrs [ <NUM_LIT:1> ] += len ( fam . objects ) <EOL> def pretty_nfam ( nfam ) : <EOL> if nfam == <NUM_LIT:1> : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return "<STR_LIT>" % nfam <EOL> def pretty_nels ( kinds , nels , nfam ) : <EOL> if nels == <NUM_LIT:1> or nels == nfam : <EOL> return "<STR_LIT>" % prettycallable ( kinds ) <EOL> else : <EOL> return "<STR_LIT>" % ( nels , prettycallable ( kinds ) ) <EOL> items = one_pattern_fams . items ( ) <EOL> items . sort ( lambda a , b : cmp ( ( a [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , a [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) , ( b [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , b [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) ) ) <EOL> for ( kinds , patt ) , ( nfam , nels ) in items : <EOL> print pretty_nfam ( nfam ) , "<STR_LIT>" , pretty_nels ( kinds , nels , nfam ) , "<STR_LIT>" , prettypatt ( [ patt ] ) <EOL> print "<STR_LIT>" <EOL> manycallb = False <EOL> rest . sort ( lambda a , b : cmp ( ( a [ <NUM_LIT:0> ] , a [ <NUM_LIT:2> ] ) , ( b [ <NUM_LIT:0> ] , b [ <NUM_LIT:2> ] ) ) ) <EOL> for n , objs , patts in rest : <EOL> if len ( objs ) > <NUM_LIT:1> and not manycallb : <EOL> manycallb = True <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" , pretty_els ( objs ) , "<STR_LIT>" , prettypatt ( patts ) <EOL> def pbcbmsanity ( translator ) : <EOL> callb = translator . annotator . getpbccallables ( ) <EOL> bk = translator . annotator . bookkeeper <EOL> bmeths = [ x for x in callb if isinstance ( x , types . MethodType ) and x . im_self is not None ] <EOL> print "<STR_LIT>" % len ( bmeths ) <EOL> fams = translator . annotator . getpbccallfamilies ( ) <EOL> plural_bm_families = { } <EOL> one_el = <NUM_LIT:0> <EOL> for bm in bmeths : <EOL> notpbc = bm . im_self not in bk . pbccache <EOL> freestanding = bm . im_func in callb <EOL> if notpbc or freestanding : <EOL> print "<STR_LIT>" % bm , <EOL> if notpbc : <EOL> print "<STR_LIT>" , <EOL> if freestanding : <EOL> print "<STR_LIT>" <EOL> bm_fam = fams [ ( None , bm ) ] <EOL> if len ( bm_fam . objects ) == <NUM_LIT:1> : <EOL> one_el += <NUM_LIT:1> <EOL> else : <EOL> plural_bm_families [ bm_fam ] = True <EOL> print "<STR_LIT>" % one_el <EOL> print "<STR_LIT>" % len ( plural_bm_families ) <EOL> for bm_fam in plural_bm_families : <EOL> print pretty_els ( bm_fam . objects ) <EOL> return plural_bm_families <EOL> class Counters ( dict ) : <EOL> def __getitem__ ( self , outcome ) : <EOL> if ( isinstance ( outcome , annmodel . SomeObject ) or <EOL> isinstance ( outcome , tuple ) and outcome and <EOL> isinstance ( outcome [ <NUM_LIT:0> ] , annmodel . SomeObject ) ) : <EOL> for k in self . iterkeys ( ) : <EOL> if k == outcome : <EOL> outcome = k <EOL> break <EOL> else : <EOL> raise KeyError <EOL> return dict . __getitem__ ( self , outcome ) <EOL> def get ( self , outcome , defl ) : <EOL> try : <EOL> return self [ outcome ] <EOL> except KeyError : <EOL> return defl <EOL> def __setitem__ ( self , outcome , c ) : <EOL> if ( isinstance ( outcome , annmodel . SomeObject ) or <EOL> isinstance ( outcome , tuple ) and outcome and <EOL> isinstance ( outcome [ <NUM_LIT:0> ] , annmodel . SomeObject ) ) : <EOL> for k in self . iterkeys ( ) : <EOL> if k == outcome : <EOL> outcome = k <EOL> break <EOL> return dict . __setitem__ ( self , outcome , c ) <EOL> def keyrepr ( k ) : <EOL> if isinstance ( k , tuple ) : <EOL> return "<STR_LIT>" % '<STR_LIT:U+002CU+0020>' . join ( [ keyrepr ( x ) for x in k ] ) <EOL> else : <EOL> return str ( k ) <EOL> def statsfor ( t , category ) : <EOL> stats = t . annotator . bookkeeper . stats <EOL> for_category = stats . classify [ category ] <EOL> print "<STR_LIT>" % ( category , len ( for_category ) ) <EOL> counters = Counters ( ) <EOL> for pos , outcome in for_category . iteritems ( ) : <EOL> counters [ outcome ] = counters . get ( outcome , <NUM_LIT:0> ) + <NUM_LIT:1> <EOL> w = max ( [ len ( keyrepr ( o ) ) for o in counters . keys ( ) ] ) + <NUM_LIT:1> <EOL> if w < <NUM_LIT> : <EOL> for outcome , n in counters . iteritems ( ) : <EOL> print "<STR_LIT>" % ( w , keyrepr ( outcome ) , n ) <EOL> else : <EOL> for outcome , n in counters . iteritems ( ) : <EOL> print "<STR_LIT>" % ( keyrepr ( outcome ) , n ) <EOL> def statsforstrformat ( t ) : <EOL> stats = t . annotator . bookkeeper . stats <EOL> stats = stats . classify [ '<STR_LIT>' ] <EOL> result = { } <EOL> for fmt , args in stats . itervalues ( ) : <EOL> fmts = re . findall ( "<STR_LIT>" , fmt ) <EOL> if not isinstance ( args , tuple ) : <EOL> args = ( args , ) <EOL> for f , a in zip ( fmts , args ) : <EOL> result [ ( f , a ) ] = result . get ( ( f , a ) , <NUM_LIT:0> ) + <NUM_LIT:1> <EOL> for ( f , a ) , c in result . iteritems ( ) : <EOL> print "<STR_LIT>" % ( f , keyrepr ( a ) , c ) <EOL> def statbuiltins ( t ) : <EOL> stats = t . annotator . bookkeeper . stats . classify <EOL> for k in stats : <EOL> if k . startswith ( '<STR_LIT>' ) : <EOL> statsfor ( t , k ) <EOL> def dicts ( t ) : <EOL> ann = t . annotator <EOL> r = [ ] <EOL> def sdicts ( ) : <EOL> for so in ann . bindings . itervalues ( ) : <EOL> if isinstance ( so , annmodel . SomeDict ) : <EOL> yield so <EOL> for so in ann . bookkeeper . immutable_cache . itervalues ( ) : <EOL> if isinstance ( so , annmodel . SomeDict ) : <EOL> yield so <EOL> for so in sdicts ( ) : <EOL> sk , sv = so . dictdef . dictkey . s_value , so . dictdef . dictvalue . s_value <EOL> for x in r : <EOL> if x == ( sk , sv ) : <EOL> break <EOL> else : <EOL> r . append ( ( sk , sv ) ) <EOL> for x in r : <EOL> print x <EOL> def tryout ( f , * args ) : <EOL> try : <EOL> f ( * args ) <EOL> except : <EOL> import traceback <EOL> traceback . print_exc ( ) <EOL> def graph_footprint ( graph ) : <EOL> class Counter : <EOL> blocks = <NUM_LIT:0> <EOL> links = <NUM_LIT:0> <EOL> ops = <NUM_LIT:0> <EOL> count = Counter ( ) <EOL> def visit ( block ) : <EOL> if isinstance ( block , flowmodel . Block ) : <EOL> count . blocks += <NUM_LIT:1> <EOL> count . ops += len ( block . operations ) <EOL> elif isinstance ( block , flowmodel . Link ) : <EOL> count . links += <NUM_LIT:1> <EOL> flowmodel . traverse ( visit , graph ) <EOL> return count . blocks , count . links , count . ops <EOL> def duplication ( t ) : <EOL> d = { } <EOL> funcs = t . flowgraphs . keys ( ) <EOL> print len ( funcs ) <EOL> for f in funcs : <EOL> fingerprint = f . func_code , graph_footprint ( t . flowgraphs [ f ] ) <EOL> d . setdefault ( fingerprint , [ ] ) . append ( f ) <EOL> l = [ ] <EOL> for fingerprint , funcs in d . iteritems ( ) : <EOL> if len ( funcs ) > <NUM_LIT:1> : <EOL> l . append ( ( fingerprint [ <NUM_LIT:0> ] . co_name , len ( funcs ) ) ) <EOL> l . sort ( ) <EOL> for name , c in l : <EOL> print name , c <EOL> def backcalls ( t ) : <EOL> g = { } <EOL> for caller , callee in t . callgraph . itervalues ( ) : <EOL> g . setdefault ( caller , [ ] ) . append ( callee ) <EOL> back = [ ] <EOL> color = { } <EOL> WHITE , GRAY , BLACK = <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> <EOL> def visit ( fcur , witness = [ ] ) : <EOL> color [ fcur ] = GRAY <EOL> for f in dict . fromkeys ( g . get ( fcur , [ ] ) ) : <EOL> fcolor = color . get ( f , WHITE ) <EOL> if fcolor == WHITE : <EOL> visit ( f , witness + [ f ] ) <EOL> elif fcolor == GRAY : <EOL> print "<STR_LIT:*>" , witness , f <EOL> back . append ( ( fcur , f ) ) <EOL> color [ fcur ] = BLACK <EOL> visit ( t . entrypoint , [ t . entrypoint ] ) <EOL> return back <EOL> def worstblocks_topten ( t , n = <NUM_LIT:10> ) : <EOL> from pypy . tool . ansi_print import ansi_print <EOL> ann = t . annotator <EOL> h = [ ( count , block ) for block , count in ann . reflowcounter . iteritems ( ) ] <EOL> h . sort ( ) <EOL> if not h : <EOL> print "<STR_LIT>" <EOL> return <EOL> print <EOL> ansi_print ( '<STR_LIT>' % n , <NUM_LIT> ) <EOL> for i in range ( n ) : <EOL> if not h : <EOL> break <EOL> count , block = h . pop ( ) <EOL> ansi_print ( '<STR_LIT>' % ( i + <NUM_LIT:1> , count ) , <NUM_LIT> ) <EOL> t . about ( block ) <EOL> ansi_print ( "<STR_LIT>" , <NUM_LIT> ) <EOL> print </s>
