33import sys
44import types
55
6- __all__ = ["dis" ,"disassemble" ,"distb" ,"disco" ,"opname" ,"cmp_op" ,
7- "hasconst" ,"hasname" ,"hasjrel" ,"hasjabs" ,"haslocal" ,
8- "hascompare" , "hasfree" ]
6+ from opcode import *
7+ from opcode import __all__ as _opcodes_all
8+
9+ __all__ = ["dis" ,"disassemble" ,"distb" ,"disco" ] + _opcodes_all
10+ del _opcodes_all
911
1012def dis (x = None ):
1113 """Disassemble classes, methods, functions, or code.
@@ -28,7 +30,8 @@ def dis(x=None):
2830 for name , x1 in items :
2931 if type (x1 ) in (types .MethodType ,
3032 types .FunctionType ,
31- types .CodeType ):
33+ types .CodeType ,
34+ types .ClassType ):
3235 print "Disassembly of %s:" % name
3336 try :
3437 dis (x1 )
@@ -37,6 +40,8 @@ def dis(x=None):
3740 print
3841 elif hasattr (x , 'co_code' ):
3942 disassemble (x )
43+ elif isinstance (x , str ):
44+ disassemble_string (x )
4045 else :
4146 raise TypeError , \
4247 "don't know how to disassemble %s objects" % \
@@ -124,6 +129,48 @@ def disassemble(co, lasti=-1):
124129 print '(' + free [oparg ] + ')' ,
125130 print
126131
132+ def disassemble_string (code , lasti = - 1 , varnames = None , names = None ,
133+ constants = None ):
134+ labels = findlabels (code )
135+ n = len (code )
136+ i = 0
137+ while i < n :
138+ c = code [i ]
139+ op = ord (c )
140+ if op == opmap ['SET_LINENO' ] and i > 0 :
141+ print # Extra blank line
142+ if i == lasti : print '-->' ,
143+ else : print ' ' ,
144+ if i in labels : print '>>' ,
145+ else : print ' ' ,
146+ print `i` .rjust (4 ),
147+ print opname [op ].ljust (15 ),
148+ i = i + 1
149+ if op >= HAVE_ARGUMENT :
150+ oparg = ord (code [i ]) + ord (code [i + 1 ])* 256
151+ i = i + 2
152+ print `oparg` .rjust (5 ),
153+ if op in hasconst :
154+ if constants :
155+ print '(' + `constants[oparg]` + ')' ,
156+ else :
157+ print '(%d)' % oparg ,
158+ elif op in hasname :
159+ if names is not None :
160+ print '(' + names [oparg ] + ')' ,
161+ else :
162+ print '(%d)' % oparg ,
163+ elif op in hasjrel :
164+ print '(to ' + `i + oparg` + ')' ,
165+ elif op in haslocal :
166+ if varnames :
167+ print '(' + varnames [oparg ] + ')' ,
168+ else :
169+ print '(%d)' % oparg ,
170+ elif op in hascompare :
171+ print '(' + cmp_op [oparg ] + ')' ,
172+ print
173+
127174disco = disassemble # XXX For backwards compatibility
128175
129176def findlabels (code ):
@@ -152,180 +199,6 @@ def findlabels(code):
152199 labels .append (label )
153200 return labels
154201
155- cmp_op = ('<' , '<=' , '==' , '!=' , '>' , '>=' , 'in' , 'not in' , 'is' ,
156- 'is not' , 'exception match' , 'BAD' )
157-
158- hasconst = []
159- hasname = []
160- hasjrel = []
161- hasjabs = []
162- haslocal = []
163- hascompare = []
164- hasfree = []
165-
166- opname = ['' ] * 256
167- for op in range (256 ): opname [op ] = '<' + `op` + '>'
168- del op
169-
170- def def_op (name , op ):
171- opname [op ] = name
172-
173- def name_op (name , op ):
174- opname [op ] = name
175- hasname .append (op )
176-
177- def jrel_op (name , op ):
178- opname [op ] = name
179- hasjrel .append (op )
180-
181- def jabs_op (name , op ):
182- opname [op ] = name
183- hasjabs .append (op )
184-
185- # Instruction opcodes for compiled code
186-
187- def_op ('STOP_CODE' , 0 )
188- def_op ('POP_TOP' , 1 )
189- def_op ('ROT_TWO' , 2 )
190- def_op ('ROT_THREE' , 3 )
191- def_op ('DUP_TOP' , 4 )
192- def_op ('ROT_FOUR' , 5 )
193-
194- def_op ('UNARY_POSITIVE' , 10 )
195- def_op ('UNARY_NEGATIVE' , 11 )
196- def_op ('UNARY_NOT' , 12 )
197- def_op ('UNARY_CONVERT' , 13 )
198-
199- def_op ('UNARY_INVERT' , 15 )
200-
201- def_op ('BINARY_POWER' , 19 )
202-
203- def_op ('BINARY_MULTIPLY' , 20 )
204- def_op ('BINARY_DIVIDE' , 21 )
205- def_op ('BINARY_MODULO' , 22 )
206- def_op ('BINARY_ADD' , 23 )
207- def_op ('BINARY_SUBTRACT' , 24 )
208- def_op ('BINARY_SUBSCR' , 25 )
209- def_op ('BINARY_FLOOR_DIVIDE' , 26 )
210- def_op ('BINARY_TRUE_DIVIDE' , 27 )
211- def_op ('INPLACE_FLOOR_DIVIDE' , 28 )
212- def_op ('INPLACE_TRUE_DIVIDE' , 29 )
213-
214- def_op ('SLICE+0' , 30 )
215- def_op ('SLICE+1' , 31 )
216- def_op ('SLICE+2' , 32 )
217- def_op ('SLICE+3' , 33 )
218-
219- def_op ('STORE_SLICE+0' , 40 )
220- def_op ('STORE_SLICE+1' , 41 )
221- def_op ('STORE_SLICE+2' , 42 )
222- def_op ('STORE_SLICE+3' , 43 )
223-
224- def_op ('DELETE_SLICE+0' , 50 )
225- def_op ('DELETE_SLICE+1' , 51 )
226- def_op ('DELETE_SLICE+2' , 52 )
227- def_op ('DELETE_SLICE+3' , 53 )
228-
229- def_op ('INPLACE_ADD' , 55 )
230- def_op ('INPLACE_SUBTRACT' , 56 )
231- def_op ('INPLACE_MULTIPLY' , 57 )
232- def_op ('INPLACE_DIVIDE' , 58 )
233- def_op ('INPLACE_MODULO' , 59 )
234- def_op ('STORE_SUBSCR' , 60 )
235- def_op ('DELETE_SUBSCR' , 61 )
236-
237- def_op ('BINARY_LSHIFT' , 62 )
238- def_op ('BINARY_RSHIFT' , 63 )
239- def_op ('BINARY_AND' , 64 )
240- def_op ('BINARY_XOR' , 65 )
241- def_op ('BINARY_OR' , 66 )
242- def_op ('INPLACE_POWER' , 67 )
243- def_op ('GET_ITER' , 68 )
244-
245- def_op ('PRINT_EXPR' , 70 )
246- def_op ('PRINT_ITEM' , 71 )
247- def_op ('PRINT_NEWLINE' , 72 )
248- def_op ('PRINT_ITEM_TO' , 73 )
249- def_op ('PRINT_NEWLINE_TO' , 74 )
250- def_op ('INPLACE_LSHIFT' , 75 )
251- def_op ('INPLACE_RSHIFT' , 76 )
252- def_op ('INPLACE_AND' , 77 )
253- def_op ('INPLACE_XOR' , 78 )
254- def_op ('INPLACE_OR' , 79 )
255- def_op ('BREAK_LOOP' , 80 )
256-
257- def_op ('LOAD_LOCALS' , 82 )
258- def_op ('RETURN_VALUE' , 83 )
259- def_op ('IMPORT_STAR' , 84 )
260- def_op ('EXEC_STMT' , 85 )
261- def_op ('YIELD_VALUE' , 86 )
262-
263- def_op ('POP_BLOCK' , 87 )
264- def_op ('END_FINALLY' , 88 )
265- def_op ('BUILD_CLASS' , 89 )
266-
267- HAVE_ARGUMENT = 90 # Opcodes from here have an argument:
268-
269- name_op ('STORE_NAME' , 90 ) # Index in name list
270- name_op ('DELETE_NAME' , 91 ) # ""
271- def_op ('UNPACK_SEQUENCE' , 92 ) # Number of tuple items
272- jrel_op ('FOR_ITER' , 93 )
273-
274- name_op ('STORE_ATTR' , 95 ) # Index in name list
275- name_op ('DELETE_ATTR' , 96 ) # ""
276- name_op ('STORE_GLOBAL' , 97 ) # ""
277- name_op ('DELETE_GLOBAL' , 98 ) # ""
278- def_op ('DUP_TOPX' , 99 ) # number of items to duplicate
279- def_op ('LOAD_CONST' , 100 ) # Index in const list
280- hasconst .append (100 )
281- name_op ('LOAD_NAME' , 101 ) # Index in name list
282- def_op ('BUILD_TUPLE' , 102 ) # Number of tuple items
283- def_op ('BUILD_LIST' , 103 ) # Number of list items
284- def_op ('BUILD_MAP' , 104 ) # Always zero for now
285- name_op ('LOAD_ATTR' , 105 ) # Index in name list
286- def_op ('COMPARE_OP' , 106 ) # Comparison operator
287- hascompare .append (106 )
288- name_op ('IMPORT_NAME' , 107 ) # Index in name list
289- name_op ('IMPORT_FROM' , 108 ) # Index in name list
290-
291- jrel_op ('JUMP_FORWARD' , 110 ) # Number of bytes to skip
292- jrel_op ('JUMP_IF_FALSE' , 111 ) # ""
293- jrel_op ('JUMP_IF_TRUE' , 112 ) # ""
294- jabs_op ('JUMP_ABSOLUTE' , 113 ) # Target byte offset from beginning of code
295-
296- name_op ('LOAD_GLOBAL' , 116 ) # Index in name list
297-
298- jabs_op ('CONTINUE_LOOP' , 119 ) # Target address
299- jrel_op ('SETUP_LOOP' , 120 ) # Distance to target address
300- jrel_op ('SETUP_EXCEPT' , 121 ) # ""
301- jrel_op ('SETUP_FINALLY' , 122 ) # ""
302-
303- def_op ('LOAD_FAST' , 124 ) # Local variable number
304- haslocal .append (124 )
305- def_op ('STORE_FAST' , 125 ) # Local variable number
306- haslocal .append (125 )
307- def_op ('DELETE_FAST' , 126 ) # Local variable number
308- haslocal .append (126 )
309-
310- def_op ('RAISE_VARARGS' , 130 ) # Number of raise arguments (1, 2, or 3)
311- def_op ('CALL_FUNCTION' , 131 ) # #args + (#kwargs << 8)
312- def_op ('MAKE_FUNCTION' , 132 ) # Number of args with default values
313- def_op ('BUILD_SLICE' , 133 ) # Number of items
314-
315- def_op ('MAKE_CLOSURE' , 134 )
316- def_op ('LOAD_CLOSURE' , 135 )
317- hasfree .append (135 )
318- def_op ('LOAD_DEREF' , 136 )
319- hasfree .append (136 )
320- def_op ('STORE_DEREF' , 137 )
321- hasfree .append (137 )
322-
323- def_op ('CALL_FUNCTION_VAR' , 140 ) # #args + (#kwargs << 8)
324- def_op ('CALL_FUNCTION_KW' , 141 ) # #args + (#kwargs << 8)
325- def_op ('CALL_FUNCTION_VAR_KW' , 142 ) # #args + (#kwargs << 8)
326-
327- def_op ('EXTENDED_ARG' , 143 )
328- EXTENDED_ARG = 143
329202
330203def _test ():
331204 """Simple test program to disassemble a file."""
0 commit comments