1 """Disassembler of Python byte code into mnemonics."""
8 """Disassemble classes, methods, functions, or code.
10 With no argument, disassemble the last traceback.
16 if type(x
) is types
.InstanceType
:
18 if hasattr(x
, '__dict__'):
19 items
= x
.__dict
__.items()
21 for name
, x1
in items
:
22 if type(x1
) in (types
.MethodType
,
25 print "Disassembly of %s:" % name
28 except TypeError, msg
:
32 if hasattr(x
, 'im_func'):
34 if hasattr(x
, 'func_code'):
36 if hasattr(x
, 'co_code'):
40 "don't know how to disassemble %s objects" % \
44 """Disassemble a traceback (default: last traceback)."""
47 tb
= sys
.last_traceback
48 except AttributeError:
49 raise RuntimeError, "no last traceback to disassemble"
50 while tb
.tb_next
: tb
= tb
.tb_next
51 disassemble(tb
.tb_frame
.f_code
, tb
.tb_lasti
)
53 def disassemble(co
, lasti
=-1):
54 """Disassemble a code object."""
56 labels
= findlabels(code
)
63 if op
== SET_LINENO
and i
> 0: print # Extra blank line
64 if i
== lasti
: print '-->',
66 if i
in labels
: print '>>',
68 print string
.rjust(`i`
, 4),
69 print string
.ljust(opname
[op
], 20),
71 if op
>= HAVE_ARGUMENT
:
72 oparg
= ord(code
[i
]) + ord(code
[i
+1])*256 + extended_arg
75 if op
== EXTENDED_ARG
:
76 extended_arg
= oparg
*65536L
77 print string
.rjust(`oparg`
, 5),
79 print '(' + `co
.co_consts
[oparg
]`
+ ')',
81 print '(' + co
.co_names
[oparg
] + ')',
83 print '(to ' + `i
+ oparg`
+ ')',
85 print '(' + co
.co_varnames
[oparg
] + ')',
86 elif op
in hascompare
:
87 print '(' + cmp_op
[oparg
] + ')',
90 disco
= disassemble
# XXX For backwards compatibility
93 """Detect all offsets in a byte code which are jump targets.
95 Return the list of offsets.
105 if op
>= HAVE_ARGUMENT
:
106 oparg
= ord(code
[i
]) + ord(code
[i
+1])*256
114 if label
not in labels
:
118 cmp_op
= ('<', '<=', '==', '!=', '>', '>=', 'in', 'not in', 'is',
119 'is not', 'exception match', 'BAD')
129 for op
in range(256): opname
[op
] = '<' + `op`
+ '>'
131 def def_op(name
, op
):
134 def name_op(name
, op
):
138 def jrel_op(name
, op
):
142 def jabs_op(name
, op
):
146 # Instruction opcodes for compiled code
148 def_op('STOP_CODE', 0)
151 def_op('ROT_THREE', 3)
153 def_op('ROT_FOUR', 5)
155 def_op('UNARY_POSITIVE', 10)
156 def_op('UNARY_NEGATIVE', 11)
157 def_op('UNARY_NOT', 12)
158 def_op('UNARY_CONVERT', 13)
160 def_op('UNARY_INVERT', 15)
162 def_op('BINARY_POWER', 19)
164 def_op('BINARY_MULTIPLY', 20)
165 def_op('BINARY_DIVIDE', 21)
166 def_op('BINARY_MODULO', 22)
167 def_op('BINARY_ADD', 23)
168 def_op('BINARY_SUBTRACT', 24)
169 def_op('BINARY_SUBSCR', 25)
171 def_op('SLICE+0', 30)
172 def_op('SLICE+1', 31)
173 def_op('SLICE+2', 32)
174 def_op('SLICE+3', 33)
176 def_op('STORE_SLICE+0', 40)
177 def_op('STORE_SLICE+1', 41)
178 def_op('STORE_SLICE+2', 42)
179 def_op('STORE_SLICE+3', 43)
181 def_op('DELETE_SLICE+0', 50)
182 def_op('DELETE_SLICE+1', 51)
183 def_op('DELETE_SLICE+2', 52)
184 def_op('DELETE_SLICE+3', 53)
186 def_op('INPLACE_ADD', 55)
187 def_op('INPLACE_SUBTRACT', 56)
188 def_op('INPLACE_MULTIPLY', 57)
189 def_op('INPLACE_DIVIDE', 58)
190 def_op('INPLACE_MODULO', 59)
191 def_op('STORE_SUBSCR', 60)
192 def_op('DELETE_SUBSCR', 61)
194 def_op('BINARY_LSHIFT', 62)
195 def_op('BINARY_RSHIFT', 63)
196 def_op('BINARY_AND', 64)
197 def_op('BINARY_XOR', 65)
198 def_op('BINARY_OR', 66)
199 def_op('INPLACE_POWER', 67)
201 def_op('PRINT_EXPR', 70)
202 def_op('PRINT_ITEM', 71)
203 def_op('PRINT_NEWLINE', 72)
204 def_op('PRINT_ITEM_TO', 73)
205 def_op('PRINT_NEWLINE_TO', 74)
206 def_op('INPLACE_LSHIFT', 75)
207 def_op('INPLACE_RSHIFT', 76)
208 def_op('INPLACE_AND', 77)
209 def_op('INPLACE_XOR', 78)
210 def_op('INPLACE_OR', 79)
211 def_op('BREAK_LOOP', 80)
213 def_op('LOAD_LOCALS', 82)
214 def_op('RETURN_VALUE', 83)
215 def_op('IMPORT_STAR', 84)
216 def_op('EXEC_STMT', 85)
218 def_op('POP_BLOCK', 87)
219 def_op('END_FINALLY', 88)
220 def_op('BUILD_CLASS', 89)
222 HAVE_ARGUMENT
= 90 # Opcodes from here have an argument:
224 name_op('STORE_NAME', 90) # Index in name list
225 name_op('DELETE_NAME', 91) # ""
226 def_op('UNPACK_SEQUENCE', 92) # Number of tuple items
228 name_op('STORE_ATTR', 95) # Index in name list
229 name_op('DELETE_ATTR', 96) # ""
230 name_op('STORE_GLOBAL', 97) # ""
231 name_op('DELETE_GLOBAL', 98) # ""
232 def_op('DUP_TOPX', 99) # number of items to duplicate
233 def_op('LOAD_CONST', 100) # Index in const list
235 name_op('LOAD_NAME', 101) # Index in name list
236 def_op('BUILD_TUPLE', 102) # Number of tuple items
237 def_op('BUILD_LIST', 103) # Number of list items
238 def_op('BUILD_MAP', 104) # Always zero for now
239 name_op('LOAD_ATTR', 105) # Index in name list
240 def_op('COMPARE_OP', 106) # Comparison operator
241 hascompare
.append(106)
242 name_op('IMPORT_NAME', 107) # Index in name list
243 name_op('IMPORT_FROM', 108) # Index in name list
245 jrel_op('JUMP_FORWARD', 110) # Number of bytes to skip
246 jrel_op('JUMP_IF_FALSE', 111) # ""
247 jrel_op('JUMP_IF_TRUE', 112) # ""
248 jabs_op('JUMP_ABSOLUTE', 113) # Target byte offset from beginning of code
249 jrel_op('FOR_LOOP', 114) # Number of bytes to skip
251 name_op('LOAD_GLOBAL', 116) # Index in name list
253 jrel_op('SETUP_LOOP', 120) # Distance to target address
254 jrel_op('SETUP_EXCEPT', 121) # ""
255 jrel_op('SETUP_FINALLY', 122) # ""
257 def_op('LOAD_FAST', 124) # Local variable number
259 def_op('STORE_FAST', 125) # Local variable number
261 def_op('DELETE_FAST', 126) # Local variable number
264 def_op('SET_LINENO', 127) # Current line number
267 def_op('RAISE_VARARGS', 130) # Number of raise arguments (1, 2, or 3)
268 def_op('CALL_FUNCTION', 131) # #args + (#kwargs << 8)
269 def_op('MAKE_FUNCTION', 132) # Number of args with default values
270 def_op('BUILD_SLICE', 133) # Number of items
272 def_op('CALL_FUNCTION_VAR', 140) # #args + (#kwargs << 8)
273 def_op('CALL_FUNCTION_KW', 141) # #args + (#kwargs << 8)
274 def_op('CALL_FUNCTION_VAR_KW', 142) # #args + (#kwargs << 8)
276 def_op('EXTENDED_ARG', 143)
280 """Simple test program to disassemble a file."""
283 sys
.stderr
.write("usage: python dis.py [-|file]\n")
286 if not fn
or fn
== "-":
299 code
= compile(source
, fn
, "exec")
302 if __name__
== "__main__":