<s> """<STR_LIT>""" <EOL> import sys <EOL> import types <EOL> from opcode import * <EOL> from opcode import __all__ as _opcodes_all <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] + _opcodes_all <EOL> del _opcodes_all <EOL> def dis ( x = None ) : <EOL> """<STR_LIT>""" <EOL> if x is None : <EOL> distb ( ) <EOL> return <EOL> if type ( x ) is types . InstanceType : <EOL> x = x . __class__ <EOL> if hasattr ( x , '<STR_LIT>' ) : <EOL> x = x . im_func <EOL> if hasattr ( x , '<STR_LIT>' ) : <EOL> x = x . func_code <EOL> if hasattr ( x , '<STR_LIT>' ) : <EOL> items = x . __dict__ . items ( ) <EOL> items . sort ( ) <EOL> for name , x1 in items : <EOL> if type ( x1 ) in ( types . MethodType , <EOL> types . FunctionType , <EOL> types . CodeType , <EOL> types . ClassType ) : <EOL> print "<STR_LIT>" % name <EOL> try : <EOL> dis ( x1 ) <EOL> except TypeError , msg : <EOL> print "<STR_LIT>" , msg <EOL> print <EOL> elif hasattr ( x , '<STR_LIT>' ) : <EOL> disassemble ( x ) <EOL> elif isinstance ( x , str ) : <EOL> disassemble_string ( x ) <EOL> else : <EOL> raise TypeError , "<STR_LIT>" % type ( x ) . __name__ <EOL> def distb ( tb = None ) : <EOL> """<STR_LIT>""" <EOL> if tb is None : <EOL> try : <EOL> tb = sys . last_traceback <EOL> except AttributeError : <EOL> raise RuntimeError , "<STR_LIT>" <EOL> while tb . tb_next : tb = tb . tb_next <EOL> disassemble ( tb . tb_frame . f_code , tb . tb_lasti ) <EOL> def disassemble ( co , lasti = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> code = co . co_code <EOL> labels = findlabels ( code ) <EOL> linestarts = dict ( findlinestarts ( co ) ) <EOL> n = len ( code ) <EOL> i = <NUM_LIT:0> <EOL> extended_arg = <NUM_LIT:0> <EOL> free = None <EOL> while i < n : <EOL> c = code [ i ] <EOL> op = ord ( c ) <EOL> if i in linestarts : <EOL> if i > <NUM_LIT:0> : <EOL> print <EOL> print "<STR_LIT>" % linestarts [ i ] , <EOL> else : <EOL> print '<STR_LIT:U+0020>' , <EOL> if i == lasti : print '<STR_LIT>' , <EOL> else : print '<STR_LIT:U+0020>' , <EOL> if i in labels : print '<STR_LIT>' , <EOL> else : print '<STR_LIT:U+0020>' , <EOL> print repr ( i ) . rjust ( <NUM_LIT:4> ) , <EOL> print opname [ op ] . ljust ( <NUM_LIT:20> ) , <EOL> i = i + <NUM_LIT:1> <EOL> if op >= HAVE_ARGUMENT : <EOL> oparg = ord ( code [ i ] ) + ord ( code [ i + <NUM_LIT:1> ] ) * <NUM_LIT> + extended_arg <EOL> extended_arg = <NUM_LIT:0> <EOL> i = i + <NUM_LIT:2> <EOL> if op == EXTENDED_ARG : <EOL> extended_arg = oparg * <NUM_LIT> L <EOL> print repr ( oparg ) . rjust ( <NUM_LIT:5> ) , <EOL> if op in hasconst : <EOL> print '<STR_LIT:(>' + repr ( co . co_consts [ oparg ] ) + '<STR_LIT:)>' , <EOL> elif op in hasname : <EOL> print '<STR_LIT:(>' + co . co_names [ oparg ] + '<STR_LIT:)>' , <EOL> elif op in hasjrel : <EOL> print '<STR_LIT>' + repr ( i + oparg ) + '<STR_LIT:)>' , <EOL> elif op in haslocal : <EOL> print '<STR_LIT:(>' + co . co_varnames [ oparg ] + '<STR_LIT:)>' , <EOL> elif op in hascompare : <EOL> print '<STR_LIT:(>' + cmp_op [ oparg ] + '<STR_LIT:)>' , <EOL> elif op in hasfree : <EOL> if free is None : <EOL> free = co . co_cellvars + co . co_freevars <EOL> print '<STR_LIT:(>' + free [ oparg ] + '<STR_LIT:)>' , <EOL> print <EOL> def disassemble_string ( code , lasti = - <NUM_LIT:1> , varnames = None , names = None , <EOL> constants = None ) : <EOL> labels = findlabels ( code ) <EOL> n = len ( code ) <EOL> i = <NUM_LIT:0> <EOL> while i < n : <EOL> c = code [ i ] <EOL> op = ord ( c ) <EOL> if i == lasti : print '<STR_LIT>' , <EOL> else : print '<STR_LIT:U+0020>' , <EOL> if i in labels : print '<STR_LIT>' , <EOL> else : print '<STR_LIT:U+0020>' , <EOL> print repr ( i ) . rjust ( <NUM_LIT:4> ) , <EOL> print opname [ op ] . ljust ( <NUM_LIT:15> ) , <EOL> i = i + <NUM_LIT:1> <EOL> if op >= HAVE_ARGUMENT : <EOL> oparg = ord ( code [ i ] ) + ord ( code [ i + <NUM_LIT:1> ] ) * <NUM_LIT> <EOL> i = i + <NUM_LIT:2> <EOL> print repr ( oparg ) . rjust ( <NUM_LIT:5> ) , <EOL> if op in hasconst : <EOL> if constants : <EOL> print '<STR_LIT:(>' + repr ( constants [ oparg ] ) + '<STR_LIT:)>' , <EOL> else : <EOL> print '<STR_LIT>' % oparg , <EOL> elif op in hasname : <EOL> if names is not None : <EOL> print '<STR_LIT:(>' + names [ oparg ] + '<STR_LIT:)>' , <EOL> else : <EOL> print '<STR_LIT>' % oparg , <EOL> elif op in hasjrel : <EOL> print '<STR_LIT>' + repr ( i + oparg ) + '<STR_LIT:)>' , <EOL> elif op in haslocal : <EOL> if varnames : <EOL> print '<STR_LIT:(>' + varnames [ oparg ] + '<STR_LIT:)>' , <EOL> else : <EOL> print '<STR_LIT>' % oparg , <EOL> elif op in hascompare : <EOL> print '<STR_LIT:(>' + cmp_op [ oparg ] + '<STR_LIT:)>' , <EOL> print <EOL> disco = disassemble <EOL> def findlabels ( code ) : <EOL> """<STR_LIT>""" <EOL> labels = [ ] <EOL> n = len ( code ) <EOL> i = <NUM_LIT:0> <EOL> while i < n : <EOL> c = code [ i ] <EOL> op = ord ( c ) <EOL> i = i + <NUM_LIT:1> <EOL> if op >= HAVE_ARGUMENT : <EOL> oparg = ord ( code [ i ] ) + ord ( code [ i + <NUM_LIT:1> ] ) * <NUM_LIT> <EOL> i = i + <NUM_LIT:2> <EOL> label = - <NUM_LIT:1> <EOL> if op in hasjrel : <EOL> label = i + oparg <EOL> elif op in hasjabs : <EOL> label = oparg <EOL> if label >= <NUM_LIT:0> : <EOL> if label not in labels : <EOL> labels . append ( label ) <EOL> return labels <EOL> def findlinestarts ( code ) : <EOL> """<STR_LIT>""" <EOL> byte_increments = [ ord ( c ) for c in code . co_lnotab [ <NUM_LIT:0> : : <NUM_LIT:2> ] ] <EOL> line_increments = [ ord ( c ) for c in code . co_lnotab [ <NUM_LIT:1> : : <NUM_LIT:2> ] ] <EOL> lastlineno = None <EOL> lineno = code . co_firstlineno <EOL> addr = <NUM_LIT:0> <EOL> for byte_incr , line_incr in zip ( byte_increments , line_increments ) : <EOL> if byte_incr : <EOL> if lineno != lastlineno : <EOL> yield ( addr , lineno ) <EOL> lastlineno = lineno <EOL> addr += byte_incr <EOL> lineno += line_incr <EOL> if lineno != lastlineno : <EOL> yield ( addr , lineno ) <EOL> def _test ( ) : <EOL> """<STR_LIT>""" <EOL> if sys . argv [ <NUM_LIT:1> : ] : <EOL> if sys . argv [ <NUM_LIT:2> : ] : <EOL> sys . stderr . write ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> fn = sys . argv [ <NUM_LIT:1> ] <EOL> if not fn or fn == "<STR_LIT:->" : <EOL> fn = None <EOL> else : <EOL> fn = None <EOL> if fn is None : <EOL> f = sys . stdin <EOL> else : <EOL> f = open ( fn ) <EOL> source = f . read ( ) <EOL> if fn is not None : <EOL> f . close ( ) <EOL> else : <EOL> fn = "<STR_LIT>" <EOL> code = compile ( source , fn , "<STR_LIT>" ) <EOL> dis ( code ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> _test ( ) </s>
