1616import re
1717import sys
1818
19+ lazy from _colorize import decolor , get_theme
20+
1921__all__ = ['dis' , 'genops' , 'optimize' ]
2022
2123bytes_types = pickle .bytes_types
@@ -2443,13 +2445,16 @@ def dis(pickle, out=None, memo=None, indentlevel=4, annotate=0):
24432445 indentchunk = ' ' * indentlevel
24442446 errormsg = None
24452447 annocol = annotate # column hint for annotations
2448+ t = get_theme (tty_file = out if out is not None else sys .stdout ).pickletools
24462449 for opcode , arg , pos in genops (pickle ):
24472450 if pos is not None :
2448- print ("%5d:" % pos , end = ' ' , file = out )
2451+ print (f" { t . position } { pos :5d } : { t . reset } " , end = ' ' , file = out )
24492452
2450- line = "%-4s %s%s" % (repr (opcode .code )[1 :- 1 ],
2451- indentchunk * len (markstack ),
2452- opcode .name )
2453+ line = (
2454+ f"{ t .opcode_code } { repr (opcode .code )[1 :- 1 ]:<4} { t .reset } "
2455+ f"{ indentchunk * len (markstack )} "
2456+ f"{ t .opcode_name } { opcode .name } { t .reset } "
2457+ )
24532458
24542459 maxproto = max (maxproto , opcode .proto )
24552460 before = opcode .stack_before # don't mutate
@@ -2510,18 +2515,26 @@ def dis(pickle, out=None, memo=None, indentlevel=4, annotate=0):
25102515 line += ' ' * (10 - len (opcode .name ))
25112516 if arg is not None :
25122517 if opcode .name in ("STRING" , "BINSTRING" , "SHORT_BINSTRING" ):
2513- line += ' ' + ascii (arg )
2518+ arg_text = ascii (arg )
25142519 else :
2515- line += ' ' + repr (arg )
2520+ arg_text = repr (arg )
2521+ arg_color = (
2522+ t .arg_number
2523+ if isinstance (arg , (int , float ))
2524+ else t .arg_string
2525+ )
2526+ line += f" { arg_color } { arg_text } { t .reset } "
25162527 if markmsg :
2517- line += ' ' + markmsg
2528+ line += f" { t . mark } { markmsg } { t . reset } "
25182529 if annotate :
2519- line += ' ' * (annocol - len (line ))
2530+ visible_len = len (decolor (line ))
2531+ line += ' ' * (annocol - visible_len )
25202532 # make a mild effort to align annotations
2521- annocol = len ( line )
2533+ annocol = max ( visible_len , annocol )
25222534 if annocol > 50 :
25232535 annocol = annotate
2524- line += ' ' + opcode .doc .split ('\n ' , 1 )[0 ]
2536+ doc = opcode .doc .split ('\n ' , 1 )[0 ]
2537+ line += f" { t .annotation } { doc } { t .reset } "
25252538 print (line , file = out )
25262539
25272540 if errormsg :
@@ -2541,7 +2554,10 @@ def dis(pickle, out=None, memo=None, indentlevel=4, annotate=0):
25412554
25422555 stack .extend (after )
25432556
2544- print ("highest protocol among opcodes =" , maxproto , file = out )
2557+ print (
2558+ f"highest protocol among opcodes = { t .proto } { maxproto } { t .reset } " ,
2559+ file = out ,
2560+ )
25452561 if stack :
25462562 raise ValueError ("stack not empty after STOP: %r" % stack )
25472563
@@ -2841,10 +2857,7 @@ def __init__(self, value):
28412857
28422858def _main (args = None ):
28432859 import argparse
2844- parser = argparse .ArgumentParser (
2845- description = 'disassemble one or more pickle files' ,
2846- color = True ,
2847- )
2860+ parser = argparse .ArgumentParser (description = 'disassemble one or more pickle files' )
28482861 parser .add_argument (
28492862 'pickle_file' ,
28502863 nargs = '+' , help = 'the pickle file' )
0 commit comments