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