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 | 19c6ba3 | 2003-02-27 21:29:27 +0000 | [diff] [blame] | 6 | from opcode import * |
| 7 | from opcode import __all__ as _opcodes_all |
| 8 | |
Nick Coghlan | 7646f7e | 2010-09-10 12:24:24 +0000 | [diff] [blame] | 9 | __all__ = ["code_info", "dis", "disassemble", "distb", "disco", |
Nick Coghlan | e8814fb | 2010-09-10 14:08:04 +0000 | [diff] [blame] | 10 | "findlinestarts", "findlabels", "show_code"] + _opcodes_all |
Skip Montanaro | 19c6ba3 | 2003-02-27 21:29:27 +0000 | [diff] [blame] | 11 | del _opcodes_all |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 12 | |
Benjamin Peterson | 6ef9a84 | 2010-04-04 23:26:50 +0000 | [diff] [blame] | 13 | _have_code = (types.MethodType, types.FunctionType, types.CodeType, type) |
| 14 | |
Nick Coghlan | 5c8b54e | 2010-07-03 07:36:51 +0000 | [diff] [blame] | 15 | def _try_compile(source, name): |
| 16 | """Attempts to compile the given source, first as an expression and |
| 17 | then as a statement if the first approach fails. |
| 18 | |
| 19 | Utility function to accept strings in functions that otherwise |
| 20 | expect code objects |
| 21 | """ |
Nick Coghlan | 5c8b54e | 2010-07-03 07:36:51 +0000 | [diff] [blame] | 22 | try: |
| 23 | c = compile(source, name, 'eval') |
| 24 | except SyntaxError: |
| 25 | c = compile(source, name, 'exec') |
| 26 | return c |
| 27 | |
Guido van Rossum | bd30795 | 1997-01-17 20:05:04 +0000 | [diff] [blame] | 28 | def dis(x=None): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 29 | """Disassemble classes, methods, functions, or code. |
Guido van Rossum | 421c224 | 1997-11-18 15:47:55 +0000 | [diff] [blame] | 30 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 31 | With no argument, disassemble the last traceback. |
Guido van Rossum | 421c224 | 1997-11-18 15:47:55 +0000 | [diff] [blame] | 32 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 33 | """ |
Raymond Hettinger | 0f4940c | 2002-06-01 00:57:55 +0000 | [diff] [blame] | 34 | if x is None: |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 35 | distb() |
| 36 | return |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 37 | if hasattr(x, '__func__'): # Method |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 38 | x = x.__func__ |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 39 | if hasattr(x, '__code__'): # Function |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 40 | x = x.__code__ |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 41 | if hasattr(x, '__dict__'): # Class or module |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 42 | items = sorted(x.__dict__.items()) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 43 | for name, x1 in items: |
Benjamin Peterson | 6ef9a84 | 2010-04-04 23:26:50 +0000 | [diff] [blame] | 44 | if isinstance(x1, _have_code): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 45 | print("Disassembly of %s:" % name) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 46 | try: |
| 47 | dis(x1) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 48 | except TypeError as msg: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 49 | print("Sorry:", msg) |
| 50 | print() |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 51 | elif hasattr(x, 'co_code'): # Code object |
Guido van Rossum | fc53c13 | 2001-01-19 02:41:41 +0000 | [diff] [blame] | 52 | disassemble(x) |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 53 | elif isinstance(x, (bytes, bytearray)): # Raw bytecode |
Nick Coghlan | 5c8b54e | 2010-07-03 07:36:51 +0000 | [diff] [blame] | 54 | _disassemble_bytes(x) |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 55 | elif isinstance(x, str): # Source code |
Nick Coghlan | 5c8b54e | 2010-07-03 07:36:51 +0000 | [diff] [blame] | 56 | _disassemble_str(x) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 57 | else: |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 58 | raise TypeError("don't know how to disassemble %s objects" % |
| 59 | type(x).__name__) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | bd30795 | 1997-01-17 20:05:04 +0000 | [diff] [blame] | 61 | def distb(tb=None): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 62 | """Disassemble a traceback (default: last traceback).""" |
Raymond Hettinger | 0f4940c | 2002-06-01 00:57:55 +0000 | [diff] [blame] | 63 | if tb is None: |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 64 | try: |
| 65 | tb = sys.last_traceback |
| 66 | except AttributeError: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 67 | raise RuntimeError("no last traceback to disassemble") |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 68 | while tb.tb_next: tb = tb.tb_next |
| 69 | disassemble(tb.tb_frame.f_code, tb.tb_lasti) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 70 | |
Nick Coghlan | 09c8123 | 2010-08-17 10:18:16 +0000 | [diff] [blame] | 71 | # The inspect module interrogates this dictionary to build its |
| 72 | # list of CO_* constants. It is also used by pretty_flags to |
| 73 | # turn the co_flags field into a human readable list. |
| 74 | COMPILER_FLAG_NAMES = { |
Guido van Rossum | 3e1b85e | 2007-05-30 02:07:00 +0000 | [diff] [blame] | 75 | 1: "OPTIMIZED", |
| 76 | 2: "NEWLOCALS", |
| 77 | 4: "VARARGS", |
| 78 | 8: "VARKEYWORDS", |
| 79 | 16: "NESTED", |
| 80 | 32: "GENERATOR", |
| 81 | 64: "NOFREE", |
| 82 | } |
| 83 | |
| 84 | def pretty_flags(flags): |
| 85 | """Return pretty representation of code flags.""" |
| 86 | names = [] |
| 87 | for i in range(32): |
| 88 | flag = 1<<i |
| 89 | if flags & flag: |
Nick Coghlan | 09c8123 | 2010-08-17 10:18:16 +0000 | [diff] [blame] | 90 | names.append(COMPILER_FLAG_NAMES.get(flag, hex(flag))) |
Guido van Rossum | 3e1b85e | 2007-05-30 02:07:00 +0000 | [diff] [blame] | 91 | flags ^= flag |
| 92 | if not flags: |
| 93 | break |
| 94 | else: |
| 95 | names.append(hex(flags)) |
| 96 | return ", ".join(names) |
| 97 | |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 98 | def code_info(x): |
| 99 | """Formatted details of methods, functions, or code.""" |
| 100 | if hasattr(x, '__func__'): # Method |
| 101 | x = x.__func__ |
| 102 | if hasattr(x, '__code__'): # Function |
| 103 | x = x.__code__ |
| 104 | if isinstance(x, str): # Source code |
| 105 | x = _try_compile(x, "<code_info>") |
| 106 | if hasattr(x, 'co_code'): # Code object |
| 107 | return _format_code_info(x) |
| 108 | else: |
| 109 | raise TypeError("don't know how to disassemble %s objects" % |
| 110 | type(x).__name__) |
| 111 | |
| 112 | def _format_code_info(co): |
| 113 | lines = [] |
| 114 | lines.append("Name: %s" % co.co_name) |
| 115 | lines.append("Filename: %s" % co.co_filename) |
| 116 | lines.append("Argument count: %s" % co.co_argcount) |
| 117 | lines.append("Kw-only arguments: %s" % co.co_kwonlyargcount) |
| 118 | lines.append("Number of locals: %s" % co.co_nlocals) |
| 119 | lines.append("Stack size: %s" % co.co_stacksize) |
| 120 | lines.append("Flags: %s" % pretty_flags(co.co_flags)) |
| 121 | if co.co_consts: |
| 122 | lines.append("Constants:") |
| 123 | for i_c in enumerate(co.co_consts): |
| 124 | lines.append("%4d: %r" % i_c) |
| 125 | if co.co_names: |
| 126 | lines.append("Names:") |
| 127 | for i_n in enumerate(co.co_names): |
| 128 | lines.append("%4d: %s" % i_n) |
| 129 | if co.co_varnames: |
| 130 | lines.append("Variable names:") |
| 131 | for i_n in enumerate(co.co_varnames): |
| 132 | lines.append("%4d: %s" % i_n) |
| 133 | if co.co_freevars: |
| 134 | lines.append("Free variables:") |
| 135 | for i_n in enumerate(co.co_freevars): |
| 136 | lines.append("%4d: %s" % i_n) |
| 137 | if co.co_cellvars: |
| 138 | lines.append("Cell variables:") |
| 139 | for i_n in enumerate(co.co_cellvars): |
| 140 | lines.append("%4d: %s" % i_n) |
| 141 | return "\n".join(lines) |
| 142 | |
Guido van Rossum | 3e1b85e | 2007-05-30 02:07:00 +0000 | [diff] [blame] | 143 | def show_code(co): |
Nick Coghlan | e8814fb | 2010-09-10 14:08:04 +0000 | [diff] [blame] | 144 | """Print details of methods, functions, or code to stdout.""" |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 145 | print(code_info(co)) |
Guido van Rossum | 3e1b85e | 2007-05-30 02:07:00 +0000 | [diff] [blame] | 146 | |
Guido van Rossum | bd30795 | 1997-01-17 20:05:04 +0000 | [diff] [blame] | 147 | def disassemble(co, lasti=-1): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 148 | """Disassemble a code object.""" |
| 149 | code = co.co_code |
| 150 | labels = findlabels(code) |
Armin Rigo | 9c8f7ea | 2003-10-28 12:17:25 +0000 | [diff] [blame] | 151 | linestarts = dict(findlinestarts(co)) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 152 | n = len(code) |
| 153 | i = 0 |
| 154 | extended_arg = 0 |
Jeremy Hylton | a39414b | 2001-01-25 20:08:47 +0000 | [diff] [blame] | 155 | free = None |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 156 | while i < n: |
Guido van Rossum | 75a902d | 2007-10-19 22:06:24 +0000 | [diff] [blame] | 157 | op = code[i] |
Armin Rigo | 9c8f7ea | 2003-10-28 12:17:25 +0000 | [diff] [blame] | 158 | if i in linestarts: |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 159 | if i > 0: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 160 | print() |
| 161 | print("%3d" % linestarts[i], end=' ') |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 162 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 163 | print(' ', end=' ') |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 164 | |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 165 | if i == lasti: print('-->', end=' ') |
| 166 | else: print(' ', end=' ') |
| 167 | if i in labels: print('>>', end=' ') |
| 168 | else: print(' ', end=' ') |
| 169 | print(repr(i).rjust(4), end=' ') |
| 170 | print(opname[op].ljust(20), end=' ') |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 171 | i = i+1 |
| 172 | if op >= HAVE_ARGUMENT: |
Guido van Rossum | 75a902d | 2007-10-19 22:06:24 +0000 | [diff] [blame] | 173 | oparg = code[i] + code[i+1]*256 + extended_arg |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 174 | extended_arg = 0 |
| 175 | i = i+2 |
| 176 | if op == EXTENDED_ARG: |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 177 | extended_arg = oparg*65536 |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 178 | print(repr(oparg).rjust(5), end=' ') |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 179 | if op in hasconst: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 180 | print('(' + repr(co.co_consts[oparg]) + ')', end=' ') |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 181 | elif op in hasname: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 182 | print('(' + co.co_names[oparg] + ')', end=' ') |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 183 | elif op in hasjrel: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 184 | print('(to ' + repr(i + oparg) + ')', end=' ') |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 185 | elif op in haslocal: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 186 | print('(' + co.co_varnames[oparg] + ')', end=' ') |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 187 | elif op in hascompare: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 188 | print('(' + cmp_op[oparg] + ')', end=' ') |
Jeremy Hylton | a39414b | 2001-01-25 20:08:47 +0000 | [diff] [blame] | 189 | elif op in hasfree: |
| 190 | if free is None: |
| 191 | free = co.co_cellvars + co.co_freevars |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 192 | print('(' + free[oparg] + ')', end=' ') |
| 193 | print() |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 194 | |
Nick Coghlan | 5c8b54e | 2010-07-03 07:36:51 +0000 | [diff] [blame] | 195 | def _disassemble_bytes(code, lasti=-1, varnames=None, names=None, |
Skip Montanaro | 19c6ba3 | 2003-02-27 21:29:27 +0000 | [diff] [blame] | 196 | constants=None): |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 197 | labels = findlabels(code) |
| 198 | n = len(code) |
| 199 | i = 0 |
| 200 | while i < n: |
Guido van Rossum | 75a902d | 2007-10-19 22:06:24 +0000 | [diff] [blame] | 201 | op = code[i] |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 202 | if i == lasti: print('-->', end=' ') |
| 203 | else: print(' ', end=' ') |
| 204 | if i in labels: print('>>', end=' ') |
| 205 | else: print(' ', end=' ') |
| 206 | print(repr(i).rjust(4), end=' ') |
| 207 | print(opname[op].ljust(15), end=' ') |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 208 | i = i+1 |
| 209 | if op >= HAVE_ARGUMENT: |
Guido van Rossum | 75a902d | 2007-10-19 22:06:24 +0000 | [diff] [blame] | 210 | oparg = code[i] + code[i+1]*256 |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 211 | i = i+2 |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 212 | print(repr(oparg).rjust(5), end=' ') |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 213 | if op in hasconst: |
| 214 | if constants: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 215 | print('(' + repr(constants[oparg]) + ')', end=' ') |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 216 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 217 | print('(%d)'%oparg, end=' ') |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 218 | elif op in hasname: |
| 219 | if names is not None: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 220 | print('(' + names[oparg] + ')', end=' ') |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 221 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 222 | print('(%d)'%oparg, end=' ') |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 223 | elif op in hasjrel: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 224 | print('(to ' + repr(i + oparg) + ')', end=' ') |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 225 | elif op in haslocal: |
| 226 | if varnames: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 227 | print('(' + varnames[oparg] + ')', end=' ') |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 228 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 229 | print('(%d)' % oparg, end=' ') |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 230 | elif op in hascompare: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 231 | print('(' + cmp_op[oparg] + ')', end=' ') |
| 232 | print() |
Skip Montanaro | 19c6ba3 | 2003-02-27 21:29:27 +0000 | [diff] [blame] | 233 | |
Nick Coghlan | 5c8b54e | 2010-07-03 07:36:51 +0000 | [diff] [blame] | 234 | def _disassemble_str(source): |
| 235 | """Compile the source string, then disassemble the code object.""" |
| 236 | disassemble(_try_compile(source, '<dis>')) |
| 237 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 238 | disco = disassemble # XXX For backwards compatibility |
Guido van Rossum | bd30795 | 1997-01-17 20:05:04 +0000 | [diff] [blame] | 239 | |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 240 | def findlabels(code): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 241 | """Detect all offsets in a byte code which are jump targets. |
Guido van Rossum | 421c224 | 1997-11-18 15:47:55 +0000 | [diff] [blame] | 242 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 243 | Return the list of offsets. |
Guido van Rossum | 421c224 | 1997-11-18 15:47:55 +0000 | [diff] [blame] | 244 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 245 | """ |
| 246 | labels = [] |
| 247 | n = len(code) |
| 248 | i = 0 |
| 249 | while i < n: |
Guido van Rossum | 75a902d | 2007-10-19 22:06:24 +0000 | [diff] [blame] | 250 | op = code[i] |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 251 | i = i+1 |
| 252 | if op >= HAVE_ARGUMENT: |
Guido van Rossum | 75a902d | 2007-10-19 22:06:24 +0000 | [diff] [blame] | 253 | oparg = code[i] + code[i+1]*256 |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 254 | i = i+2 |
| 255 | label = -1 |
| 256 | if op in hasjrel: |
| 257 | label = i+oparg |
| 258 | elif op in hasjabs: |
| 259 | label = oparg |
| 260 | if label >= 0: |
| 261 | if label not in labels: |
| 262 | labels.append(label) |
| 263 | return labels |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 264 | |
Armin Rigo | 9c8f7ea | 2003-10-28 12:17:25 +0000 | [diff] [blame] | 265 | def findlinestarts(code): |
| 266 | """Find the offsets in a byte code which are start of lines in the source. |
| 267 | |
| 268 | Generate pairs (offset, lineno) as described in Python/compile.c. |
| 269 | |
| 270 | """ |
Guido van Rossum | 75a902d | 2007-10-19 22:06:24 +0000 | [diff] [blame] | 271 | byte_increments = list(code.co_lnotab[0::2]) |
| 272 | line_increments = list(code.co_lnotab[1::2]) |
Armin Rigo | 9c8f7ea | 2003-10-28 12:17:25 +0000 | [diff] [blame] | 273 | |
| 274 | lastlineno = None |
| 275 | lineno = code.co_firstlineno |
| 276 | addr = 0 |
| 277 | for byte_incr, line_incr in zip(byte_increments, line_increments): |
| 278 | if byte_incr: |
| 279 | if lineno != lastlineno: |
| 280 | yield (addr, lineno) |
| 281 | lastlineno = lineno |
| 282 | addr += byte_incr |
| 283 | lineno += line_incr |
| 284 | if lineno != lastlineno: |
| 285 | yield (addr, lineno) |
Guido van Rossum | 1fdae12 | 2000-02-04 17:47:55 +0000 | [diff] [blame] | 286 | |
| 287 | def _test(): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 288 | """Simple test program to disassemble a file.""" |
| 289 | if sys.argv[1:]: |
| 290 | if sys.argv[2:]: |
| 291 | sys.stderr.write("usage: python dis.py [-|file]\n") |
| 292 | sys.exit(2) |
| 293 | fn = sys.argv[1] |
| 294 | if not fn or fn == "-": |
| 295 | fn = None |
| 296 | else: |
| 297 | fn = None |
Raymond Hettinger | 0f4940c | 2002-06-01 00:57:55 +0000 | [diff] [blame] | 298 | if fn is None: |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 299 | f = sys.stdin |
| 300 | else: |
| 301 | f = open(fn) |
| 302 | source = f.read() |
Raymond Hettinger | 0f4940c | 2002-06-01 00:57:55 +0000 | [diff] [blame] | 303 | if fn is not None: |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 304 | f.close() |
| 305 | else: |
| 306 | fn = "<stdin>" |
| 307 | code = compile(source, fn, "exec") |
| 308 | dis(code) |
Guido van Rossum | 1fdae12 | 2000-02-04 17:47:55 +0000 | [diff] [blame] | 309 | |
| 310 | if __name__ == "__main__": |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 311 | _test() |