Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2....
File: dis.py
"""Disassembler of Python byte code into mnemonics."""
[0] Fix | Delete
[1] Fix | Delete
import sys
[2] Fix | Delete
import types
[3] Fix | Delete
[4] Fix | Delete
from opcode import *
[5] Fix | Delete
from opcode import __all__ as _opcodes_all
[6] Fix | Delete
[7] Fix | Delete
__all__ = ["dis", "disassemble", "distb", "disco",
[8] Fix | Delete
"findlinestarts", "findlabels"] + _opcodes_all
[9] Fix | Delete
del _opcodes_all
[10] Fix | Delete
[11] Fix | Delete
_have_code = (types.MethodType, types.FunctionType, types.CodeType,
[12] Fix | Delete
types.ClassType, type)
[13] Fix | Delete
[14] Fix | Delete
def dis(x=None):
[15] Fix | Delete
"""Disassemble classes, methods, functions, or code.
[16] Fix | Delete
[17] Fix | Delete
With no argument, disassemble the last traceback.
[18] Fix | Delete
[19] Fix | Delete
"""
[20] Fix | Delete
if x is None:
[21] Fix | Delete
distb()
[22] Fix | Delete
return
[23] Fix | Delete
if isinstance(x, types.InstanceType):
[24] Fix | Delete
x = x.__class__
[25] Fix | Delete
if hasattr(x, 'im_func'):
[26] Fix | Delete
x = x.im_func
[27] Fix | Delete
if hasattr(x, 'func_code'):
[28] Fix | Delete
x = x.func_code
[29] Fix | Delete
if hasattr(x, '__dict__'):
[30] Fix | Delete
items = x.__dict__.items()
[31] Fix | Delete
items.sort()
[32] Fix | Delete
for name, x1 in items:
[33] Fix | Delete
if isinstance(x1, _have_code):
[34] Fix | Delete
print "Disassembly of %s:" % name
[35] Fix | Delete
try:
[36] Fix | Delete
dis(x1)
[37] Fix | Delete
except TypeError, msg:
[38] Fix | Delete
print "Sorry:", msg
[39] Fix | Delete
print
[40] Fix | Delete
elif hasattr(x, 'co_code'):
[41] Fix | Delete
disassemble(x)
[42] Fix | Delete
elif isinstance(x, str):
[43] Fix | Delete
disassemble_string(x)
[44] Fix | Delete
else:
[45] Fix | Delete
raise TypeError, \
[46] Fix | Delete
"don't know how to disassemble %s objects" % \
[47] Fix | Delete
type(x).__name__
[48] Fix | Delete
[49] Fix | Delete
def distb(tb=None):
[50] Fix | Delete
"""Disassemble a traceback (default: last traceback)."""
[51] Fix | Delete
if tb is None:
[52] Fix | Delete
try:
[53] Fix | Delete
tb = sys.last_traceback
[54] Fix | Delete
except AttributeError:
[55] Fix | Delete
raise RuntimeError, "no last traceback to disassemble"
[56] Fix | Delete
while tb.tb_next: tb = tb.tb_next
[57] Fix | Delete
disassemble(tb.tb_frame.f_code, tb.tb_lasti)
[58] Fix | Delete
[59] Fix | Delete
def disassemble(co, lasti=-1):
[60] Fix | Delete
"""Disassemble a code object."""
[61] Fix | Delete
code = co.co_code
[62] Fix | Delete
labels = findlabels(code)
[63] Fix | Delete
linestarts = dict(findlinestarts(co))
[64] Fix | Delete
n = len(code)
[65] Fix | Delete
i = 0
[66] Fix | Delete
extended_arg = 0
[67] Fix | Delete
free = None
[68] Fix | Delete
while i < n:
[69] Fix | Delete
c = code[i]
[70] Fix | Delete
op = ord(c)
[71] Fix | Delete
if i in linestarts:
[72] Fix | Delete
if i > 0:
[73] Fix | Delete
print
[74] Fix | Delete
print "%3d" % linestarts[i],
[75] Fix | Delete
else:
[76] Fix | Delete
print ' ',
[77] Fix | Delete
[78] Fix | Delete
if i == lasti: print '-->',
[79] Fix | Delete
else: print ' ',
[80] Fix | Delete
if i in labels: print '>>',
[81] Fix | Delete
else: print ' ',
[82] Fix | Delete
print repr(i).rjust(4),
[83] Fix | Delete
print opname[op].ljust(20),
[84] Fix | Delete
i = i+1
[85] Fix | Delete
if op >= HAVE_ARGUMENT:
[86] Fix | Delete
oparg = ord(code[i]) + ord(code[i+1])*256 + extended_arg
[87] Fix | Delete
extended_arg = 0
[88] Fix | Delete
i = i+2
[89] Fix | Delete
if op == EXTENDED_ARG:
[90] Fix | Delete
extended_arg = oparg*65536L
[91] Fix | Delete
print repr(oparg).rjust(5),
[92] Fix | Delete
if op in hasconst:
[93] Fix | Delete
print '(' + repr(co.co_consts[oparg]) + ')',
[94] Fix | Delete
elif op in hasname:
[95] Fix | Delete
print '(' + co.co_names[oparg] + ')',
[96] Fix | Delete
elif op in hasjrel:
[97] Fix | Delete
print '(to ' + repr(i + oparg) + ')',
[98] Fix | Delete
elif op in haslocal:
[99] Fix | Delete
print '(' + co.co_varnames[oparg] + ')',
[100] Fix | Delete
elif op in hascompare:
[101] Fix | Delete
print '(' + cmp_op[oparg] + ')',
[102] Fix | Delete
elif op in hasfree:
[103] Fix | Delete
if free is None:
[104] Fix | Delete
free = co.co_cellvars + co.co_freevars
[105] Fix | Delete
print '(' + free[oparg] + ')',
[106] Fix | Delete
print
[107] Fix | Delete
[108] Fix | Delete
def disassemble_string(code, lasti=-1, varnames=None, names=None,
[109] Fix | Delete
constants=None):
[110] Fix | Delete
labels = findlabels(code)
[111] Fix | Delete
n = len(code)
[112] Fix | Delete
i = 0
[113] Fix | Delete
while i < n:
[114] Fix | Delete
c = code[i]
[115] Fix | Delete
op = ord(c)
[116] Fix | Delete
if i == lasti: print '-->',
[117] Fix | Delete
else: print ' ',
[118] Fix | Delete
if i in labels: print '>>',
[119] Fix | Delete
else: print ' ',
[120] Fix | Delete
print repr(i).rjust(4),
[121] Fix | Delete
print opname[op].ljust(15),
[122] Fix | Delete
i = i+1
[123] Fix | Delete
if op >= HAVE_ARGUMENT:
[124] Fix | Delete
oparg = ord(code[i]) + ord(code[i+1])*256
[125] Fix | Delete
i = i+2
[126] Fix | Delete
print repr(oparg).rjust(5),
[127] Fix | Delete
if op in hasconst:
[128] Fix | Delete
if constants:
[129] Fix | Delete
print '(' + repr(constants[oparg]) + ')',
[130] Fix | Delete
else:
[131] Fix | Delete
print '(%d)'%oparg,
[132] Fix | Delete
elif op in hasname:
[133] Fix | Delete
if names is not None:
[134] Fix | Delete
print '(' + names[oparg] + ')',
[135] Fix | Delete
else:
[136] Fix | Delete
print '(%d)'%oparg,
[137] Fix | Delete
elif op in hasjrel:
[138] Fix | Delete
print '(to ' + repr(i + oparg) + ')',
[139] Fix | Delete
elif op in haslocal:
[140] Fix | Delete
if varnames:
[141] Fix | Delete
print '(' + varnames[oparg] + ')',
[142] Fix | Delete
else:
[143] Fix | Delete
print '(%d)' % oparg,
[144] Fix | Delete
elif op in hascompare:
[145] Fix | Delete
print '(' + cmp_op[oparg] + ')',
[146] Fix | Delete
print
[147] Fix | Delete
[148] Fix | Delete
disco = disassemble # XXX For backwards compatibility
[149] Fix | Delete
[150] Fix | Delete
def findlabels(code):
[151] Fix | Delete
"""Detect all offsets in a byte code which are jump targets.
[152] Fix | Delete
[153] Fix | Delete
Return the list of offsets.
[154] Fix | Delete
[155] Fix | Delete
"""
[156] Fix | Delete
labels = []
[157] Fix | Delete
n = len(code)
[158] Fix | Delete
i = 0
[159] Fix | Delete
while i < n:
[160] Fix | Delete
c = code[i]
[161] Fix | Delete
op = ord(c)
[162] Fix | Delete
i = i+1
[163] Fix | Delete
if op >= HAVE_ARGUMENT:
[164] Fix | Delete
oparg = ord(code[i]) + ord(code[i+1])*256
[165] Fix | Delete
i = i+2
[166] Fix | Delete
label = -1
[167] Fix | Delete
if op in hasjrel:
[168] Fix | Delete
label = i+oparg
[169] Fix | Delete
elif op in hasjabs:
[170] Fix | Delete
label = oparg
[171] Fix | Delete
if label >= 0:
[172] Fix | Delete
if label not in labels:
[173] Fix | Delete
labels.append(label)
[174] Fix | Delete
return labels
[175] Fix | Delete
[176] Fix | Delete
def findlinestarts(code):
[177] Fix | Delete
"""Find the offsets in a byte code which are start of lines in the source.
[178] Fix | Delete
[179] Fix | Delete
Generate pairs (offset, lineno) as described in Python/compile.c.
[180] Fix | Delete
[181] Fix | Delete
"""
[182] Fix | Delete
byte_increments = [ord(c) for c in code.co_lnotab[0::2]]
[183] Fix | Delete
line_increments = [ord(c) for c in code.co_lnotab[1::2]]
[184] Fix | Delete
[185] Fix | Delete
lastlineno = None
[186] Fix | Delete
lineno = code.co_firstlineno
[187] Fix | Delete
addr = 0
[188] Fix | Delete
for byte_incr, line_incr in zip(byte_increments, line_increments):
[189] Fix | Delete
if byte_incr:
[190] Fix | Delete
if lineno != lastlineno:
[191] Fix | Delete
yield (addr, lineno)
[192] Fix | Delete
lastlineno = lineno
[193] Fix | Delete
addr += byte_incr
[194] Fix | Delete
lineno += line_incr
[195] Fix | Delete
if lineno != lastlineno:
[196] Fix | Delete
yield (addr, lineno)
[197] Fix | Delete
[198] Fix | Delete
def _test():
[199] Fix | Delete
"""Simple test program to disassemble a file."""
[200] Fix | Delete
if sys.argv[1:]:
[201] Fix | Delete
if sys.argv[2:]:
[202] Fix | Delete
sys.stderr.write("usage: python dis.py [-|file]\n")
[203] Fix | Delete
sys.exit(2)
[204] Fix | Delete
fn = sys.argv[1]
[205] Fix | Delete
if not fn or fn == "-":
[206] Fix | Delete
fn = None
[207] Fix | Delete
else:
[208] Fix | Delete
fn = None
[209] Fix | Delete
if fn is None:
[210] Fix | Delete
f = sys.stdin
[211] Fix | Delete
else:
[212] Fix | Delete
f = open(fn)
[213] Fix | Delete
source = f.read()
[214] Fix | Delete
if fn is not None:
[215] Fix | Delete
f.close()
[216] Fix | Delete
else:
[217] Fix | Delete
fn = "<stdin>"
[218] Fix | Delete
code = compile(source, fn, "exec")
[219] Fix | Delete
dis(code)
[220] Fix | Delete
[221] Fix | Delete
if __name__ == "__main__":
[222] Fix | Delete
_test()
[223] Fix | Delete
[224] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function