Guido van Rossum | 421c224 | 1997-11-18 15:47:55 +0000 | [diff] [blame] | 1 | """Disassembler of Python byte code into mnemonics.""" |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 2 | |
| 3 | import sys |
Guido van Rossum | 18aef3c | 1997-03-14 04:15:43 +0000 | [diff] [blame] | 4 | import types |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 5 | |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 6 | __all__ = ["dis","disassemble","distb","disco","opname","cmp_op", |
| 7 | "hasconst","hasname","hasjrel","hasjabs","haslocal", |
Jeremy Hylton | a39414b | 2001-01-25 20:08:47 +0000 | [diff] [blame] | 8 | "hascompare", "hasfree"] |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | bd30795 | 1997-01-17 20:05:04 +0000 | [diff] [blame] | 10 | def dis(x=None): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 11 | """Disassemble classes, methods, functions, or code. |
Guido van Rossum | 421c224 | 1997-11-18 15:47:55 +0000 | [diff] [blame] | 12 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 13 | With no argument, disassemble the last traceback. |
Guido van Rossum | 421c224 | 1997-11-18 15:47:55 +0000 | [diff] [blame] | 14 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 15 | """ |
| 16 | if not x: |
| 17 | distb() |
| 18 | return |
| 19 | if type(x) is types.InstanceType: |
| 20 | x = x.__class__ |
Guido van Rossum | fc53c13 | 2001-01-19 02:41:41 +0000 | [diff] [blame] | 21 | if hasattr(x, 'im_func'): |
| 22 | x = x.im_func |
| 23 | if hasattr(x, 'func_code'): |
| 24 | x = x.func_code |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 25 | if hasattr(x, '__dict__'): |
| 26 | items = x.__dict__.items() |
| 27 | items.sort() |
| 28 | for name, x1 in items: |
| 29 | if type(x1) in (types.MethodType, |
| 30 | types.FunctionType, |
| 31 | types.CodeType): |
| 32 | print "Disassembly of %s:" % name |
| 33 | try: |
| 34 | dis(x1) |
| 35 | except TypeError, msg: |
| 36 | print "Sorry:", msg |
| 37 | print |
Guido van Rossum | fc53c13 | 2001-01-19 02:41:41 +0000 | [diff] [blame] | 38 | elif hasattr(x, 'co_code'): |
| 39 | disassemble(x) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 40 | else: |
Guido van Rossum | fc53c13 | 2001-01-19 02:41:41 +0000 | [diff] [blame] | 41 | raise TypeError, \ |
| 42 | "don't know how to disassemble %s objects" % \ |
| 43 | type(x).__name__ |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | bd30795 | 1997-01-17 20:05:04 +0000 | [diff] [blame] | 45 | def distb(tb=None): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 46 | """Disassemble a traceback (default: last traceback).""" |
| 47 | if not tb: |
| 48 | try: |
| 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) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | bd30795 | 1997-01-17 20:05:04 +0000 | [diff] [blame] | 55 | def disassemble(co, lasti=-1): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 56 | """Disassemble a code object.""" |
| 57 | code = co.co_code |
| 58 | labels = findlabels(code) |
| 59 | n = len(code) |
| 60 | i = 0 |
| 61 | extended_arg = 0 |
Jeremy Hylton | a39414b | 2001-01-25 20:08:47 +0000 | [diff] [blame] | 62 | free = None |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 63 | while i < n: |
| 64 | c = code[i] |
| 65 | op = ord(c) |
| 66 | if op == SET_LINENO and i > 0: print # Extra blank line |
| 67 | if i == lasti: print '-->', |
| 68 | else: print ' ', |
| 69 | if i in labels: print '>>', |
| 70 | else: print ' ', |
Eric S. Raymond | 373c55e | 2001-02-09 08:25:29 +0000 | [diff] [blame] | 71 | print `i`.rjust(4), |
| 72 | print opname[op].ljust(20), |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 73 | i = i+1 |
| 74 | if op >= HAVE_ARGUMENT: |
| 75 | oparg = ord(code[i]) + ord(code[i+1])*256 + extended_arg |
| 76 | extended_arg = 0 |
| 77 | i = i+2 |
| 78 | if op == EXTENDED_ARG: |
| 79 | extended_arg = oparg*65536L |
Eric S. Raymond | 373c55e | 2001-02-09 08:25:29 +0000 | [diff] [blame] | 80 | print `oparg`.rjust(5), |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 81 | if op in hasconst: |
| 82 | print '(' + `co.co_consts[oparg]` + ')', |
| 83 | elif op in hasname: |
| 84 | print '(' + co.co_names[oparg] + ')', |
| 85 | elif op in hasjrel: |
| 86 | print '(to ' + `i + oparg` + ')', |
| 87 | elif op in haslocal: |
| 88 | print '(' + co.co_varnames[oparg] + ')', |
| 89 | elif op in hascompare: |
| 90 | print '(' + cmp_op[oparg] + ')', |
Jeremy Hylton | a39414b | 2001-01-25 20:08:47 +0000 | [diff] [blame] | 91 | elif op in hasfree: |
| 92 | if free is None: |
| 93 | free = co.co_cellvars + co.co_freevars |
| 94 | print '(' + free[oparg] + ')', |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 95 | print |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 96 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 97 | disco = disassemble # XXX For backwards compatibility |
Guido van Rossum | bd30795 | 1997-01-17 20:05:04 +0000 | [diff] [blame] | 98 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 99 | def findlabels(code): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 100 | """Detect all offsets in a byte code which are jump targets. |
Guido van Rossum | 421c224 | 1997-11-18 15:47:55 +0000 | [diff] [blame] | 101 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 102 | Return the list of offsets. |
Guido van Rossum | 421c224 | 1997-11-18 15:47:55 +0000 | [diff] [blame] | 103 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 104 | """ |
| 105 | labels = [] |
| 106 | n = len(code) |
| 107 | i = 0 |
| 108 | while i < n: |
| 109 | c = code[i] |
| 110 | op = ord(c) |
| 111 | i = i+1 |
| 112 | if op >= HAVE_ARGUMENT: |
| 113 | oparg = ord(code[i]) + ord(code[i+1])*256 |
| 114 | i = i+2 |
| 115 | label = -1 |
| 116 | if op in hasjrel: |
| 117 | label = i+oparg |
| 118 | elif op in hasjabs: |
| 119 | label = oparg |
| 120 | if label >= 0: |
| 121 | if label not in labels: |
| 122 | labels.append(label) |
| 123 | return labels |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 124 | |
Guido van Rossum | d0bc9cb | 1997-01-16 18:52:24 +0000 | [diff] [blame] | 125 | cmp_op = ('<', '<=', '==', '!=', '>', '>=', 'in', 'not in', 'is', |
| 126 | 'is not', 'exception match', 'BAD') |
| 127 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 128 | hasconst = [] |
| 129 | hasname = [] |
| 130 | hasjrel = [] |
| 131 | hasjabs = [] |
Guido van Rossum | 934a4ce | 1996-09-12 17:39:36 +0000 | [diff] [blame] | 132 | haslocal = [] |
Guido van Rossum | d0bc9cb | 1997-01-16 18:52:24 +0000 | [diff] [blame] | 133 | hascompare = [] |
Jeremy Hylton | a39414b | 2001-01-25 20:08:47 +0000 | [diff] [blame] | 134 | hasfree = [] |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 135 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 136 | opname = [''] * 256 |
| 137 | for op in range(256): opname[op] = '<' + `op` + '>' |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 138 | |
| 139 | def def_op(name, op): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 140 | opname[op] = name |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 141 | |
| 142 | def name_op(name, op): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 143 | opname[op] = name |
| 144 | hasname.append(op) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 145 | |
| 146 | def jrel_op(name, op): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 147 | opname[op] = name |
| 148 | hasjrel.append(op) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 149 | |
| 150 | def jabs_op(name, op): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 151 | opname[op] = name |
| 152 | hasjabs.append(op) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 153 | |
| 154 | # Instruction opcodes for compiled code |
| 155 | |
| 156 | def_op('STOP_CODE', 0) |
| 157 | def_op('POP_TOP', 1) |
| 158 | def_op('ROT_TWO', 2) |
| 159 | def_op('ROT_THREE', 3) |
| 160 | def_op('DUP_TOP', 4) |
Thomas Wouters | 6af3b37 | 2000-08-24 22:44:53 +0000 | [diff] [blame] | 161 | def_op('ROT_FOUR', 5) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 162 | |
| 163 | def_op('UNARY_POSITIVE', 10) |
| 164 | def_op('UNARY_NEGATIVE', 11) |
| 165 | def_op('UNARY_NOT', 12) |
| 166 | def_op('UNARY_CONVERT', 13) |
Guido van Rossum | d0f2372 | 1997-05-09 03:21:44 +0000 | [diff] [blame] | 167 | |
Guido van Rossum | 8379ed5 | 1993-03-30 19:13:03 +0000 | [diff] [blame] | 168 | def_op('UNARY_INVERT', 15) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 169 | |
Guido van Rossum | 6e21ceb | 1996-07-21 02:16:53 +0000 | [diff] [blame] | 170 | def_op('BINARY_POWER', 19) |
Guido van Rossum | d0f2372 | 1997-05-09 03:21:44 +0000 | [diff] [blame] | 171 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 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) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 178 | |
| 179 | def_op('SLICE+0', 30) |
| 180 | def_op('SLICE+1', 31) |
| 181 | def_op('SLICE+2', 32) |
| 182 | def_op('SLICE+3', 33) |
| 183 | |
| 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) |
| 188 | |
| 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) |
| 193 | |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 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) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 199 | def_op('STORE_SUBSCR', 60) |
| 200 | def_op('DELETE_SUBSCR', 61) |
Guido van Rossum | d0f2372 | 1997-05-09 03:21:44 +0000 | [diff] [blame] | 201 | |
Guido van Rossum | d0bc9cb | 1997-01-16 18:52:24 +0000 | [diff] [blame] | 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) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 207 | def_op('INPLACE_POWER', 67) |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame^] | 208 | def_op('GET_ITER', 68) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 209 | |
| 210 | def_op('PRINT_EXPR', 70) |
| 211 | def_op('PRINT_ITEM', 71) |
| 212 | def_op('PRINT_NEWLINE', 72) |
Barry Warsaw | 203da6d | 2000-08-21 17:18:40 +0000 | [diff] [blame] | 213 | def_op('PRINT_ITEM_TO', 73) |
| 214 | def_op('PRINT_NEWLINE_TO', 74) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 215 | def_op('INPLACE_LSHIFT', 75) |
| 216 | def_op('INPLACE_RSHIFT', 76) |
| 217 | def_op('INPLACE_AND', 77) |
| 218 | def_op('INPLACE_XOR', 78) |
| 219 | def_op('INPLACE_OR', 79) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 220 | def_op('BREAK_LOOP', 80) |
Guido van Rossum | d0f2372 | 1997-05-09 03:21:44 +0000 | [diff] [blame] | 221 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 222 | def_op('LOAD_LOCALS', 82) |
| 223 | def_op('RETURN_VALUE', 83) |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 224 | def_op('IMPORT_STAR', 84) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 225 | def_op('EXEC_STMT', 85) |
Guido van Rossum | d0f2372 | 1997-05-09 03:21:44 +0000 | [diff] [blame] | 226 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 227 | def_op('POP_BLOCK', 87) |
| 228 | def_op('END_FINALLY', 88) |
| 229 | def_op('BUILD_CLASS', 89) |
| 230 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 231 | HAVE_ARGUMENT = 90 # Opcodes from here have an argument: |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 232 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 233 | name_op('STORE_NAME', 90) # Index in name list |
| 234 | name_op('DELETE_NAME', 91) # "" |
| 235 | def_op('UNPACK_SEQUENCE', 92) # Number of tuple items |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame^] | 236 | def_op('FOR_ITER', 93) |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 237 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 238 | name_op('STORE_ATTR', 95) # Index in name list |
| 239 | name_op('DELETE_ATTR', 96) # "" |
| 240 | name_op('STORE_GLOBAL', 97) # "" |
| 241 | name_op('DELETE_GLOBAL', 98) # "" |
| 242 | def_op('DUP_TOPX', 99) # number of items to duplicate |
| 243 | def_op('LOAD_CONST', 100) # Index in const list |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 244 | hasconst.append(100) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 245 | name_op('LOAD_NAME', 101) # Index in name list |
| 246 | def_op('BUILD_TUPLE', 102) # Number of tuple items |
| 247 | def_op('BUILD_LIST', 103) # Number of list items |
| 248 | def_op('BUILD_MAP', 104) # Always zero for now |
| 249 | name_op('LOAD_ATTR', 105) # Index in name list |
| 250 | def_op('COMPARE_OP', 106) # Comparison operator |
Guido van Rossum | d0f2372 | 1997-05-09 03:21:44 +0000 | [diff] [blame] | 251 | hascompare.append(106) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 252 | name_op('IMPORT_NAME', 107) # Index in name list |
| 253 | name_op('IMPORT_FROM', 108) # Index in name list |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 254 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 255 | jrel_op('JUMP_FORWARD', 110) # Number of bytes to skip |
| 256 | jrel_op('JUMP_IF_FALSE', 111) # "" |
| 257 | jrel_op('JUMP_IF_TRUE', 112) # "" |
| 258 | jabs_op('JUMP_ABSOLUTE', 113) # Target byte offset from beginning of code |
| 259 | jrel_op('FOR_LOOP', 114) # Number of bytes to skip |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 260 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 261 | name_op('LOAD_GLOBAL', 116) # Index in name list |
Guido van Rossum | d0f2372 | 1997-05-09 03:21:44 +0000 | [diff] [blame] | 262 | |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 263 | jabs_op('CONTINUE_LOOP', 119) # Target address |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 264 | jrel_op('SETUP_LOOP', 120) # Distance to target address |
| 265 | jrel_op('SETUP_EXCEPT', 121) # "" |
| 266 | jrel_op('SETUP_FINALLY', 122) # "" |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 267 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 268 | def_op('LOAD_FAST', 124) # Local variable number |
Guido van Rossum | 934a4ce | 1996-09-12 17:39:36 +0000 | [diff] [blame] | 269 | haslocal.append(124) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 270 | def_op('STORE_FAST', 125) # Local variable number |
Guido van Rossum | 934a4ce | 1996-09-12 17:39:36 +0000 | [diff] [blame] | 271 | haslocal.append(125) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 272 | def_op('DELETE_FAST', 126) # Local variable number |
Guido van Rossum | 934a4ce | 1996-09-12 17:39:36 +0000 | [diff] [blame] | 273 | haslocal.append(126) |
Guido van Rossum | 8379ed5 | 1993-03-30 19:13:03 +0000 | [diff] [blame] | 274 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 275 | def_op('SET_LINENO', 127) # Current line number |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 276 | SET_LINENO = 127 |
Guido van Rossum | 6e21ceb | 1996-07-21 02:16:53 +0000 | [diff] [blame] | 277 | |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 278 | def_op('RAISE_VARARGS', 130) # Number of raise arguments (1, 2, or 3) |
| 279 | def_op('CALL_FUNCTION', 131) # #args + (#kwargs << 8) |
| 280 | def_op('MAKE_FUNCTION', 132) # Number of args with default values |
| 281 | def_op('BUILD_SLICE', 133) # Number of items |
| 282 | |
Jeremy Hylton | a39414b | 2001-01-25 20:08:47 +0000 | [diff] [blame] | 283 | def_op('MAKE_CLOSURE', 134) |
| 284 | def_op('LOAD_CLOSURE', 135) |
| 285 | hasfree.append(135) |
| 286 | def_op('LOAD_DEREF', 136) |
| 287 | hasfree.append(136) |
| 288 | def_op('STORE_DEREF', 137) |
| 289 | hasfree.append(137) |
| 290 | |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 291 | def_op('CALL_FUNCTION_VAR', 140) # #args + (#kwargs << 8) |
| 292 | def_op('CALL_FUNCTION_KW', 141) # #args + (#kwargs << 8) |
| 293 | def_op('CALL_FUNCTION_VAR_KW', 142) # #args + (#kwargs << 8) |
Guido van Rossum | 1fdae12 | 2000-02-04 17:47:55 +0000 | [diff] [blame] | 294 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 295 | def_op('EXTENDED_ARG', 143) |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 296 | EXTENDED_ARG = 143 |
Guido van Rossum | 1fdae12 | 2000-02-04 17:47:55 +0000 | [diff] [blame] | 297 | |
| 298 | def _test(): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 299 | """Simple test program to disassemble a file.""" |
| 300 | if sys.argv[1:]: |
| 301 | if sys.argv[2:]: |
| 302 | sys.stderr.write("usage: python dis.py [-|file]\n") |
| 303 | sys.exit(2) |
| 304 | fn = sys.argv[1] |
| 305 | if not fn or fn == "-": |
| 306 | fn = None |
| 307 | else: |
| 308 | fn = None |
| 309 | if not fn: |
| 310 | f = sys.stdin |
| 311 | else: |
| 312 | f = open(fn) |
| 313 | source = f.read() |
| 314 | if fn: |
| 315 | f.close() |
| 316 | else: |
| 317 | fn = "<stdin>" |
| 318 | code = compile(source, fn, "exec") |
| 319 | dis(code) |
Guido van Rossum | 1fdae12 | 2000-02-04 17:47:55 +0000 | [diff] [blame] | 320 | |
| 321 | if __name__ == "__main__": |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 322 | _test() |