@@ -175,6 +175,9 @@ def show_code(co, *, file=None):
175175_Instruction .starts_line .__doc__ = "Line started by this opcode (if any), otherwise None"
176176_Instruction .is_jump_target .__doc__ = "True if other code jumps to here, otherwise False"
177177
178+ _OPNAME_WIDTH = 20
179+ _OPARG_WIDTH = 5
180+
178181class Instruction (_Instruction ):
179182 """Details for a bytecode operation
180183
@@ -189,11 +192,12 @@ class Instruction(_Instruction):
189192 is_jump_target - True if other code jumps to here, otherwise False
190193 """
191194
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 ):
193196 """Format instruction details for inclusion in disassembly output
194197
195198 *lineno_width* sets the width of the line number field (0 omits it)
196199 *mark_as_current* inserts a '-->' marker arrow as part of the line
200+ *offset_width* sets the width of the instruction offset field
197201 """
198202 fields = []
199203 # Column: Source code line number
@@ -214,12 +218,12 @@ def _disassemble(self, lineno_width=3, mark_as_current=False):
214218 else :
215219 fields .append (' ' )
216220 # 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 ))
218222 # Column: Opcode name
219- fields .append (self .opname .ljust (20 ))
223+ fields .append (self .opname .ljust (_OPNAME_WIDTH ))
220224 # Column: Opcode argument
221225 if self .arg is not None :
222- fields .append (repr (self .arg ).rjust (5 ))
226+ fields .append (repr (self .arg ).rjust (_OPARG_WIDTH ))
223227 # Column: Opcode argument details
224228 if self .argrepr :
225229 fields .append ('(' + self .argrepr + ')' )
@@ -339,8 +343,19 @@ def _disassemble_bytes(code, lasti=-1, varnames=None, names=None,
339343 * , file = None , line_offset = 0 ):
340344 # Omit the line number column entirely if we have no line number info
341345 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
344359 for instr in _get_instructions_bytes (code , varnames , names ,
345360 constants , cells , linestarts ,
346361 line_offset = line_offset ):
@@ -350,7 +365,8 @@ def _disassemble_bytes(code, lasti=-1, varnames=None, names=None,
350365 if new_source_line :
351366 print (file = file )
352367 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 )
354370
355371def _disassemble_str (source , * , file = None ):
356372 """Compile the source string, then disassemble the code object."""
0 commit comments