@@ -175,6 +175,9 @@ def show_code(co, *, file=None):
175
175
_Instruction .starts_line .__doc__ = "Line started by this opcode (if any), otherwise None"
176
176
_Instruction .is_jump_target .__doc__ = "True if other code jumps to here, otherwise False"
177
177
178
+ _OPNAME_WIDTH = 20
179
+ _OPARG_WIDTH = 5
180
+
178
181
class Instruction (_Instruction ):
179
182
"""Details for a bytecode operation
180
183
@@ -189,11 +192,12 @@ class Instruction(_Instruction):
189
192
is_jump_target - True if other code jumps to here, otherwise False
190
193
"""
191
194
192
- def _disassemble (self , lineno_width = 3 , mark_as_current = False ):
195
+ def _disassemble (self , lineno_width = 3 , mark_as_current = False , offset_width = 4 ):
193
196
"""Format instruction details for inclusion in disassembly output
194
197
195
198
*lineno_width* sets the width of the line number field (0 omits it)
196
199
*mark_as_current* inserts a '-->' marker arrow as part of the line
200
+ *offset_width* sets the width of the instruction offset field
197
201
"""
198
202
fields = []
199
203
# Column: Source code line number
@@ -214,12 +218,12 @@ def _disassemble(self, lineno_width=3, mark_as_current=False):
214
218
else :
215
219
fields .append (' ' )
216
220
# Column: Instruction offset from start of code sequence
217
- fields .append (repr (self .offset ).rjust (4 ))
221
+ fields .append (repr (self .offset ).rjust (offset_width ))
218
222
# Column: Opcode name
219
- fields .append (self .opname .ljust (20 ))
223
+ fields .append (self .opname .ljust (_OPNAME_WIDTH ))
220
224
# Column: Opcode argument
221
225
if self .arg is not None :
222
- fields .append (repr (self .arg ).rjust (5 ))
226
+ fields .append (repr (self .arg ).rjust (_OPARG_WIDTH ))
223
227
# Column: Opcode argument details
224
228
if self .argrepr :
225
229
fields .append ('(' + self .argrepr + ')' )
@@ -339,8 +343,19 @@ def _disassemble_bytes(code, lasti=-1, varnames=None, names=None,
339
343
* , file = None , line_offset = 0 ):
340
344
# Omit the line number column entirely if we have no line number info
341
345
show_lineno = linestarts is not None
342
- # TODO?: Adjust width upwards if max(linestarts.values()) >= 1000?
343
- lineno_width = 3 if show_lineno else 0
346
+ if show_lineno :
347
+ maxlineno = max (linestarts .values ()) + line_offset
348
+ if maxlineno >= 1000 :
349
+ lineno_width = len (str (maxlineno ))
350
+ else :
351
+ lineno_width = 3
352
+ else :
353
+ lineno_width = 0
354
+ maxoffset = len (code ) - 2
355
+ if maxoffset >= 10000 :
356
+ offset_width = len (str (maxoffset ))
357
+ else :
358
+ offset_width = 4
344
359
for instr in _get_instructions_bytes (code , varnames , names ,
345
360
constants , cells , linestarts ,
346
361
line_offset = line_offset ):
@@ -350,7 +365,8 @@ def _disassemble_bytes(code, lasti=-1, varnames=None, names=None,
350
365
if new_source_line :
351
366
print (file = file )
352
367
is_current_instr = instr .offset == lasti
353
- print (instr ._disassemble (lineno_width , is_current_instr ), file = file )
368
+ print (instr ._disassemble (lineno_width , is_current_instr , offset_width ),
369
+ file = file )
354
370
355
371
def _disassemble_str (source , * , file = None ):
356
372
"""Compile the source string, then disassemble the code object."""
0 commit comments