@@ -84,7 +84,7 @@ def _try_compile(source, name):
8484 return compile (source , name , 'exec' )
8585
8686def dis (x = None , * , file = None , depth = None , show_caches = False , adaptive = False ,
87- show_offsets = False , show_positions = False , show_jit = False ):
87+ show_offsets = False , show_positions = False , show_jit = False , show_block_bg = False ):
8888 """Disassemble classes, methods, functions, and other compiled objects.
8989
9090 With no argument, disassemble the last traceback.
@@ -96,7 +96,7 @@ def dis(x=None, *, file=None, depth=None, show_caches=False, adaptive=False,
9696 if x is None :
9797 distb (file = file , show_caches = show_caches , adaptive = adaptive ,
9898 show_offsets = show_offsets , show_positions = show_positions ,
99- show_jit = show_jit )
99+ show_jit = show_jit , show_block_bg = show_block_bg )
100100 return
101101 # Extract functions from methods.
102102 if hasattr (x , '__func__' ):
@@ -118,30 +118,35 @@ def dis(x=None, *, file=None, depth=None, show_caches=False, adaptive=False,
118118 print ("Disassembly of %s:" % name , file = file )
119119 try :
120120 dis (x1 , file = file , depth = depth , show_caches = show_caches , adaptive = adaptive ,
121- show_offsets = show_offsets , show_positions = show_positions , show_jit = show_jit )
121+ show_offsets = show_offsets , show_positions = show_positions , show_jit = show_jit ,
122+ show_block_bg = show_block_bg )
122123 except TypeError as msg :
123124 print ("Sorry:" , msg , file = file )
124125 print (file = file )
125126 elif hasattr (x , 'co_code' ): # Code object
126127 _disassemble_recursive (x , file = file , depth = depth , show_caches = show_caches , adaptive = adaptive ,
127- show_offsets = show_offsets , show_positions = show_positions , show_jit = show_jit )
128+ show_offsets = show_offsets , show_positions = show_positions , show_jit = show_jit ,
129+ show_block_bg = show_block_bg )
128130 elif isinstance (x , (bytes , bytearray )): # Raw bytecode
129131 labels_map = _make_labels_map (x )
130132 label_width = 4 + len (str (len (labels_map )))
131133 formatter = Formatter (file = file ,
132134 offset_width = len (str (max (len (x ) - 2 , 9999 ))) if show_offsets else 0 ,
133135 label_width = label_width ,
134- show_caches = show_caches )
136+ show_caches = show_caches ,
137+ show_block_bg = show_block_bg )
135138 arg_resolver = ArgResolver (labels_map = labels_map )
136139 _disassemble_bytes (x , arg_resolver = arg_resolver , formatter = formatter )
137140 elif isinstance (x , str ): # Source code
138141 _disassemble_str (x , file = file , depth = depth , show_caches = show_caches , adaptive = adaptive ,
139- show_offsets = show_offsets , show_positions = show_positions , show_jit = show_jit )
142+ show_offsets = show_offsets , show_positions = show_positions , show_jit = show_jit ,
143+ show_block_bg = show_block_bg )
140144 else :
141145 raise TypeError ("don't know how to disassemble %s objects" %
142146 type (x ).__name__ )
143147
144- def distb (tb = None , * , file = None , show_caches = False , adaptive = False , show_offsets = False , show_positions = False , show_jit = False ):
148+ def distb (tb = None , * , file = None , show_caches = False , adaptive = False , show_offsets = False , show_positions = False , show_jit = False ,
149+ show_block_bg = False ):
145150 """Disassemble a traceback (default: last traceback)."""
146151 if tb is None :
147152 try :
@@ -153,7 +158,8 @@ def distb(tb=None, *, file=None, show_caches=False, adaptive=False, show_offsets
153158 raise RuntimeError ("no last traceback to disassemble" ) from None
154159 while tb .tb_next : tb = tb .tb_next
155160 disassemble (tb .tb_frame .f_code , tb .tb_lasti , file = file , show_caches = show_caches , adaptive = adaptive ,
156- show_offsets = show_offsets , show_positions = show_positions , show_jit = show_jit )
161+ show_offsets = show_offsets , show_positions = show_positions , show_jit = show_jit ,
162+ show_block_bg = show_block_bg )
157163
158164# The inspect module interrogates this dictionary to build its
159165# list of CO_* constants. It is also used by pretty_flags to
@@ -449,7 +455,8 @@ def _get_dis_theme():
449455class Formatter :
450456
451457 def __init__ (self , file = None , lineno_width = 0 , offset_width = 0 , label_width = 0 ,
452- line_offset = 0 , show_caches = False , * , show_positions = False ):
458+ line_offset = 0 , show_caches = False , * , show_positions = False ,
459+ show_block_bg = False ):
453460 """Create a Formatter
454461
455462 *file* where to write the output
@@ -468,6 +475,8 @@ def __init__(self, file=None, lineno_width=0, offset_width=0, label_width=0,
468475 self .label_width = label_width
469476 self .show_caches = show_caches
470477 self .show_positions = show_positions
478+ self .show_block_bg = show_block_bg
479+ self ._alt_block = False # toggle between first/second alt block color
471480
472481 def print_instruction (self , instr , mark_as_current = False ):
473482 self .print_instruction_line (instr , mark_as_current )
@@ -499,6 +508,8 @@ def print_instruction_line(self, instr, mark_as_current):
499508 instr .offset > 0 )
500509 if new_source_line :
501510 print (file = self .file )
511+ if self .show_block_bg :
512+ self ._alt_block = not self ._alt_block
502513
503514 fields = []
504515 # Column: Source code locations information
@@ -548,7 +559,14 @@ def print_instruction_line(self, instr, mark_as_current):
548559 # Column: Opcode argument details
549560 if instr .argrepr :
550561 fields .append (f'{ theme .argument_detail } (' + instr .argrepr + f'){ theme .reset } ' )
551- print (' ' .join (fields ).rstrip (), file = self .file )
562+
563+ line = ' ' .join (fields ).rstrip ()
564+
565+ if self .show_block_bg :
566+ bg = theme .alt_block_first_bg if self ._alt_block else theme .alt_block_second_bg
567+ line = bg + line .replace (theme .reset , theme .reset + bg ) + "\x1b [K" + theme .reset
568+
569+ print (line , file = self .file )
552570
553571 def print_exception_table (self , exception_entries ):
554572 file = self .file
@@ -837,7 +855,8 @@ def _get_instructions_bytes(code, linestarts=None, line_offset=0, co_positions=N
837855
838856
839857def disassemble (co , lasti = - 1 , * , file = None , show_caches = False , adaptive = False ,
840- show_offsets = False , show_positions = False , show_jit = False ):
858+ show_offsets = False , show_positions = False , show_jit = False ,
859+ show_block_bg = False ):
841860 """Disassemble a code object."""
842861 linestarts = dict (findlinestarts (co ))
843862 exception_entries = _parse_exception_table (co )
@@ -852,7 +871,8 @@ def disassemble(co, lasti=-1, *, file=None, show_caches=False, adaptive=False,
852871 offset_width = len (str (max (len (co .co_code ) - 2 , 9999 ))) if show_offsets else 0 ,
853872 label_width = label_width ,
854873 show_caches = show_caches ,
855- show_positions = show_positions )
874+ show_positions = show_positions ,
875+ show_block_bg = show_block_bg )
856876 arg_resolver = ArgResolver (co_consts = co .co_consts ,
857877 names = co .co_names ,
858878 varname_from_oparg = co ._varname_from_oparg ,
@@ -861,8 +881,9 @@ def disassemble(co, lasti=-1, *, file=None, show_caches=False, adaptive=False,
861881 exception_entries = exception_entries , co_positions = co .co_positions (),
862882 original_code = co .co_code , arg_resolver = arg_resolver , formatter = formatter )
863883
864- def _disassemble_recursive (co , * , file = None , depth = None , show_caches = False , adaptive = False , show_offsets = False , show_positions = False , show_jit = False ):
865- disassemble (co , file = file , show_caches = show_caches , adaptive = adaptive , show_offsets = show_offsets , show_positions = show_positions , show_jit = show_jit )
884+ def _disassemble_recursive (co , * , file = None , depth = None , show_caches = False , adaptive = False , show_offsets = False , show_positions = False , show_jit = False , show_block_bg = False ):
885+ disassemble (co , file = file , show_caches = show_caches , adaptive = adaptive , show_offsets = show_offsets , show_positions = show_positions , show_jit = show_jit ,
886+ show_block_bg = show_block_bg )
866887 if depth is None or depth > 0 :
867888 if depth is not None :
868889 depth = depth - 1
@@ -874,7 +895,8 @@ def _disassemble_recursive(co, *, file=None, depth=None, show_caches=False, adap
874895 _disassemble_recursive (
875896 x , file = file , depth = depth , show_caches = show_caches ,
876897 adaptive = adaptive , show_offsets = show_offsets ,
877- show_positions = show_positions , show_jit = show_jit
898+ show_positions = show_positions , show_jit = show_jit ,
899+ show_block_bg = show_block_bg
878900 )
879901
880902
@@ -1175,6 +1197,8 @@ def main(args=None):
11751197 help = 'show instruction positions' )
11761198 parser .add_argument ('-S' , '--specialized' , action = 'store_true' ,
11771199 help = 'show specialized bytecode' )
1200+ parser .add_argument ('-B' , '--block-bg' , action = 'store_true' ,
1201+ help = 'alternate background per source-line block' )
11781202 parser .add_argument ('infile' , nargs = '?' , default = '-' )
11791203 args = parser .parse_args (args = args )
11801204 if args .infile == '-' :
@@ -1186,7 +1210,8 @@ def main(args=None):
11861210 source = infile .read ()
11871211 code = compile (source , name , "exec" )
11881212 dis (code , show_caches = args .show_caches , adaptive = args .specialized ,
1189- show_offsets = args .show_offsets , show_positions = args .show_positions )
1213+ show_offsets = args .show_offsets , show_positions = args .show_positions ,
1214+ show_block_bg = args .block_bg )
11901215
11911216if __name__ == "__main__" :
11921217 main ()
0 commit comments