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
)
62 if op
== SET_LINENO
and i
> 0: print # Extra blank line
63 if i
== lasti
: print '-->',
65 if i
in labels
: print '>>',
67 print string
.rjust(`i`
, 4),
68 print string
.ljust(opname
[op
], 15),
70 if op
>= HAVE_ARGUMENT
:
71 oparg
= ord(code
[i
]) + ord(code
[i
+1])*256
73 print string
.rjust(`oparg`
, 5),
75 print '(' + `co
.co_consts
[oparg
]`
+ ')',
77 print '(' + co
.co_names
[oparg
] + ')',
79 print '(to ' + `i
+ oparg`
+ ')',
81 print '(' + co
.co_varnames
[oparg
] + ')',
82 elif op
in hascompare
:
83 print '(' + cmp_op
[oparg
] + ')',
86 disco
= disassemble
# XXX For backwards compatibility
89 """Detect all offsets in a byte code which are jump targets.
91 Return the list of offsets.
101 if op
>= HAVE_ARGUMENT
:
102 oparg
= ord(code
[i
]) + ord(code
[i
+1])*256
110 if label
not in labels
:
114 cmp_op
= ('<', '<=', '==', '!=', '>', '>=', 'in', 'not in', 'is',
115 'is not', 'exception match', 'BAD')
125 for op
in range(256): opname
[op
] = '<' + `op`
+ '>'
127 def def_op(name
, op
):
130 def name_op(name
, op
):
134 def jrel_op(name
, op
):
138 def jabs_op(name
, op
):
142 # Instruction opcodes for compiled code
144 def_op('STOP_CODE', 0)
147 def_op('ROT_THREE', 3)
150 def_op('UNARY_POSITIVE', 10)
151 def_op('UNARY_NEGATIVE', 11)
152 def_op('UNARY_NOT', 12)
153 def_op('UNARY_CONVERT', 13)
155 def_op('UNARY_INVERT', 15)
157 def_op('BINARY_POWER', 19)
159 def_op('BINARY_MULTIPLY', 20)
160 def_op('BINARY_DIVIDE', 21)
161 def_op('BINARY_MODULO', 22)
162 def_op('BINARY_ADD', 23)
163 def_op('BINARY_SUBTRACT', 24)
164 def_op('BINARY_SUBSCR', 25)
166 def_op('SLICE+0', 30)
167 def_op('SLICE+1', 31)
168 def_op('SLICE+2', 32)
169 def_op('SLICE+3', 33)
171 def_op('STORE_SLICE+0', 40)
172 def_op('STORE_SLICE+1', 41)
173 def_op('STORE_SLICE+2', 42)
174 def_op('STORE_SLICE+3', 43)
176 def_op('DELETE_SLICE+0', 50)
177 def_op('DELETE_SLICE+1', 51)
178 def_op('DELETE_SLICE+2', 52)
179 def_op('DELETE_SLICE+3', 53)
181 def_op('STORE_SUBSCR', 60)
182 def_op('DELETE_SUBSCR', 61)
184 def_op('BINARY_LSHIFT', 62)
185 def_op('BINARY_RSHIFT', 63)
186 def_op('BINARY_AND', 64)
187 def_op('BINARY_XOR', 65)
188 def_op('BINARY_OR', 66)
190 def_op('PRINT_EXPR', 70)
191 def_op('PRINT_ITEM', 71)
192 def_op('PRINT_NEWLINE', 72)
194 def_op('BREAK_LOOP', 80)
196 def_op('LOAD_LOCALS', 82)
197 def_op('RETURN_VALUE', 83)
199 def_op('EXEC_STMT', 85)
201 def_op('POP_BLOCK', 87)
202 def_op('END_FINALLY', 88)
203 def_op('BUILD_CLASS', 89)
205 HAVE_ARGUMENT
= 90 # Opcodes from here have an argument:
207 name_op('STORE_NAME', 90) # Index in name list
208 name_op('DELETE_NAME', 91) # ""
209 def_op('UNPACK_TUPLE', 92) # Number of tuple items
210 def_op('UNPACK_LIST', 93) # Number of list items
211 name_op('STORE_ATTR', 95) # Index in name list
212 name_op('DELETE_ATTR', 96) # ""
213 name_op('STORE_GLOBAL', 97) # ""
214 name_op('DELETE_GLOBAL', 98) # ""
216 def_op('LOAD_CONST', 100) # Index in const list
218 name_op('LOAD_NAME', 101) # Index in name list
219 def_op('BUILD_TUPLE', 102) # Number of tuple items
220 def_op('BUILD_LIST', 103) # Number of list items
221 def_op('BUILD_MAP', 104) # Always zero for now
222 name_op('LOAD_ATTR', 105) # Index in name list
223 def_op('COMPARE_OP', 106) # Comparison operator
224 hascompare
.append(106)
225 name_op('IMPORT_NAME', 107) # Index in name list
226 name_op('IMPORT_FROM', 108) # Index in name list
228 jrel_op('JUMP_FORWARD', 110) # Number of bytes to skip
229 jrel_op('JUMP_IF_FALSE', 111) # ""
230 jrel_op('JUMP_IF_TRUE', 112) # ""
231 jabs_op('JUMP_ABSOLUTE', 113) # Target byte offset from beginning of code
232 jrel_op('FOR_LOOP', 114) # Number of bytes to skip
234 name_op('LOAD_GLOBAL', 116) # Index in name list
236 jrel_op('SETUP_LOOP', 120) # Distance to target address
237 jrel_op('SETUP_EXCEPT', 121) # ""
238 jrel_op('SETUP_FINALLY', 122) # ""
240 def_op('LOAD_FAST', 124) # Local variable number
242 def_op('STORE_FAST', 125) # Local variable number
244 def_op('DELETE_FAST', 126) # Local variable number
247 def_op('SET_LINENO', 127) # Current line number
250 def_op('RAISE_VARARGS', 130)
251 def_op('CALL_FUNCTION', 131)
252 def_op('MAKE_FUNCTION', 132)
253 def_op('BUILD_SLICE', 133)