Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(45)

Side by Side Diff: Lib/dis.py

Issue 20103: http://bugs.python.org/issue2459 -- speed up loops SVN Base: http://svn.python.org/view/*checkout*/python/trunk/
Patch Set: Created 9 months, 1 week ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 """Disassembler of Python byte code into mnemonics.""" 1 """Disassembler of Python byte code into mnemonics."""
2 2
3 import sys 3 import sys
4 import types 4 import types
5 5
6 from opcode import * 6 from opcode import *
7 from opcode import __all__ as _opcodes_all 7 from opcode import __all__ as _opcodes_all
8 8
9 __all__ = ["dis", "disassemble", "distb", "disco", 9 __all__ = ["dis", "disassemble", "distb", "disco",
10 "findlinestarts", "findlabels"] + _opcodes_all 10 "findlinestarts", "findlabels"] + _opcodes_all
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 labels.append(label) 175 labels.append(label)
176 return labels 176 return labels
177 177
178 def findlinestarts(code): 178 def findlinestarts(code):
179 """Find the offsets in a byte code which are start of lines in the source. 179 """Find the offsets in a byte code which are start of lines in the source.
180 180
181 Generate pairs (offset, lineno) as described in Python/compile.c. 181 Generate pairs (offset, lineno) as described in Python/compile.c.
182 182
183 """ 183 """
184 byte_increments = [ord(c) for c in code.co_lnotab[0::2]] 184 byte_increments = [ord(c) for c in code.co_lnotab[0::2]]
185 line_increments = [ord(c) for c in code.co_lnotab[1::2]] 185 line_increments = [c if c < 128 else c - 256
186 for c in map(ord, code.co_lnotab[1::2])]
186 187
187 lastlineno = None 188 lastlineno = None
188 lineno = code.co_firstlineno 189 lineno = code.co_firstlineno
189 addr = 0 190 addr = 0
190 for byte_incr, line_incr in zip(byte_increments, line_increments): 191 for byte_incr, line_incr in zip(byte_increments, line_increments):
191 if byte_incr: 192 if byte_incr:
192 if lineno != lastlineno: 193 if lineno != lastlineno:
193 yield (addr, lineno) 194 yield (addr, lineno)
194 lastlineno = lineno 195 lastlineno = lineno
195 addr += byte_incr 196 addr += byte_incr
(...skipping 19 matching lines...) Expand all
215 source = f.read() 216 source = f.read()
216 if fn is not None: 217 if fn is not None:
217 f.close() 218 f.close()
218 else: 219 else:
219 fn = "<stdin>" 220 fn = "<stdin>"
220 code = compile(source, fn, "exec") 221 code = compile(source, fn, "exec")
221 dis(code) 222 dis(code)
222 223
223 if __name__ == "__main__": 224 if __name__ == "__main__":
224 _test() 225 _test()
OLDNEW

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld r497