1 """Disassembler of Python byte code into mnemonics."""
7 from opcode
import __all__
as _opcodes_all
9 __all__
= ["dis","disassemble","distb","disco"] + _opcodes_all
13 """Disassemble classes, methods, functions, or code.
15 With no argument, disassemble the last traceback.
21 if type(x
) is types
.InstanceType
:
23 if hasattr(x
, 'im_func'):
25 if hasattr(x
, 'func_code'):
27 if hasattr(x
, '__dict__'):
28 items
= x
.__dict
__.items()
30 for name
, x1
in items
:
31 if type(x1
) in (types
.MethodType
,
35 print "Disassembly of %s:" % name
38 except TypeError, msg
:
41 elif hasattr(x
, 'co_code'):
43 elif isinstance(x
, str):
47 "don't know how to disassemble %s objects" % \
51 """Disassemble a traceback (default: last traceback)."""
54 tb
= sys
.last_traceback
55 except AttributeError:
56 raise RuntimeError, "no last traceback to disassemble"
57 while tb
.tb_next
: tb
= tb
.tb_next
58 disassemble(tb
.tb_frame
.f_code
, tb
.tb_lasti
)
60 def disassemble(co
, lasti
=-1):
61 """Disassemble a code object."""
63 labels
= findlabels(code
)
64 linestarts
= dict(findlinestarts(co
))
75 print "%3d" % linestarts
[i
],
79 if i
== lasti
: print '-->',
81 if i
in labels
: print '>>',
83 print repr(i
).rjust(4),
84 print opname
[op
].ljust(20),
86 if op
>= HAVE_ARGUMENT
:
87 oparg
= ord(code
[i
]) + ord(code
[i
+1])*256 + extended_arg
90 if op
== EXTENDED_ARG
:
91 extended_arg
= oparg
*65536L
92 print repr(oparg
).rjust(5),
94 print '(' + repr(co
.co_consts
[oparg
]) + ')',
96 print '(' + co
.co_names
[oparg
] + ')',
98 print '(to ' + repr(i
+ oparg
) + ')',
100 print '(' + co
.co_varnames
[oparg
] + ')',
101 elif op
in hascompare
:
102 print '(' + cmp_op
[oparg
] + ')',
105 free
= co
.co_cellvars
+ co
.co_freevars
106 print '(' + free
[oparg
] + ')',
109 def disassemble_string(code
, lasti
=-1, varnames
=None, names
=None,
111 labels
= findlabels(code
)
117 if i
== lasti
: print '-->',
119 if i
in labels
: print '>>',
121 print repr(i
).rjust(4),
122 print opname
[op
].ljust(15),
124 if op
>= HAVE_ARGUMENT
:
125 oparg
= ord(code
[i
]) + ord(code
[i
+1])*256
127 print repr(oparg
).rjust(5),
130 print '(' + repr(constants
[oparg
]) + ')',
134 if names
is not None:
135 print '(' + names
[oparg
] + ')',
139 print '(to ' + repr(i
+ oparg
) + ')',
142 print '(' + varnames
[oparg
] + ')',
144 print '(%d)' % oparg
,
145 elif op
in hascompare
:
146 print '(' + cmp_op
[oparg
] + ')',
149 disco
= disassemble
# XXX For backwards compatibility
151 def findlabels(code
):
152 """Detect all offsets in a byte code which are jump targets.
154 Return the list of offsets.
164 if op
>= HAVE_ARGUMENT
:
165 oparg
= ord(code
[i
]) + ord(code
[i
+1])*256
173 if label
not in labels
:
177 def findlinestarts(code
):
178 """Find the offsets in a byte code which are start of lines in the source.
180 Generate pairs (offset, lineno) as described in Python/compile.c.
183 byte_increments
= [ord(c
) for c
in code
.co_lnotab
[0::2]]
184 line_increments
= [ord(c
) for c
in code
.co_lnotab
[1::2]]
187 lineno
= code
.co_firstlineno
189 for byte_incr
, line_incr
in zip(byte_increments
, line_increments
):
191 if lineno
!= lastlineno
:
196 if lineno
!= lastlineno
:
200 """Simple test program to disassemble a file."""
203 sys
.stderr
.write("usage: python dis.py [-|file]\n")
206 if not fn
or fn
== "-":
219 code
= compile(source
, fn
, "exec")
222 if __name__
== "__main__":