9393import traceback
9494import linecache
9595import _colorize
96+ import _pyrepl .utils
9697
9798from contextlib import closing
9899from contextlib import contextmanager
@@ -339,7 +340,7 @@ class Pdb(bdb.Bdb, cmd.Cmd):
339340 _last_pdb_instance = None
340341
341342 def __init__ (self , completekey = 'tab' , stdin = None , stdout = None , skip = None ,
342- nosigint = False , readrc = True , mode = None , backend = None ):
343+ nosigint = False , readrc = True , mode = None , backend = None , colorize = False ):
343344 bdb .Bdb .__init__ (self , skip = skip , backend = backend if backend else get_default_backend ())
344345 cmd .Cmd .__init__ (self , completekey , stdin , stdout )
345346 sys .audit ("pdb.Pdb" )
@@ -352,6 +353,7 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None,
352353 self ._wait_for_mainpyfile = False
353354 self .tb_lineno = {}
354355 self .mode = mode
356+ self .colorize = _colorize .can_colorize (file = stdout or sys .stdout ) and colorize
355357 # Try to load readline if it exists
356358 try :
357359 import readline
@@ -1036,6 +1038,13 @@ def handle_command_def(self, line):
10361038 return True
10371039 return False
10381040
1041+ def _colorize_code (self , code ):
1042+ if self .colorize :
1043+ colors = list (_pyrepl .utils .gen_colors (code ))
1044+ chars , _ = _pyrepl .utils .disp_str (code , colors = colors )
1045+ code = "" .join (chars )
1046+ return code
1047+
10391048 # interface abstraction functions
10401049
10411050 def message (self , msg , end = '\n ' ):
@@ -2166,6 +2175,8 @@ def _print_lines(self, lines, start, breaks=(), frame=None):
21662175 s += '->'
21672176 elif lineno == exc_lineno :
21682177 s += '>>'
2178+ if self .colorize :
2179+ line = self ._colorize_code (line )
21692180 self .message (s + '\t ' + line .rstrip ())
21702181
21712182 def do_whatis (self , arg ):
@@ -2365,8 +2376,14 @@ def print_stack_entry(self, frame_lineno, prompt_prefix=line_prefix):
23652376 prefix = '> '
23662377 else :
23672378 prefix = ' '
2368- self .message (prefix +
2369- self .format_stack_entry (frame_lineno , prompt_prefix ))
2379+ stack_entry = self .format_stack_entry (frame_lineno , prompt_prefix )
2380+ if self .colorize :
2381+ lines = stack_entry .split (prompt_prefix , 1 )
2382+ if len (lines ) > 1 :
2383+ # We have some code to display
2384+ lines [1 ] = self ._colorize_code (lines [1 ])
2385+ stack_entry = prompt_prefix .join (lines )
2386+ self .message (prefix + stack_entry )
23702387
23712388 # Provide help
23722389
@@ -2604,7 +2621,7 @@ def set_trace(*, header=None, commands=None):
26042621 if Pdb ._last_pdb_instance is not None :
26052622 pdb = Pdb ._last_pdb_instance
26062623 else :
2607- pdb = Pdb (mode = 'inline' , backend = 'monitoring' )
2624+ pdb = Pdb (mode = 'inline' , backend = 'monitoring' , colorize = True )
26082625 if header is not None :
26092626 pdb .message (header )
26102627 pdb .set_trace (sys ._getframe ().f_back , commands = commands )
@@ -2619,7 +2636,7 @@ async def set_trace_async(*, header=None, commands=None):
26192636 if Pdb ._last_pdb_instance is not None :
26202637 pdb = Pdb ._last_pdb_instance
26212638 else :
2622- pdb = Pdb (mode = 'inline' , backend = 'monitoring' )
2639+ pdb = Pdb (mode = 'inline' , backend = 'monitoring' , colorize = True )
26232640 if header is not None :
26242641 pdb .message (header )
26252642 await pdb .set_trace_async (sys ._getframe ().f_back , commands = commands )
@@ -2633,7 +2650,7 @@ def __init__(self, sockfile, owns_sockfile=True, **kwargs):
26332650 self ._sockfile = sockfile
26342651 self ._command_name_cache = []
26352652 self ._write_failed = False
2636- super ().__init__ (** kwargs )
2653+ super ().__init__ (colorize = False , ** kwargs )
26372654
26382655 @staticmethod
26392656 def protocol_version ():
@@ -3345,7 +3362,7 @@ def main():
33453362 # modified by the script being debugged. It's a bad idea when it was
33463363 # changed by the user from the command line. There is a "restart" command
33473364 # which allows explicit specification of command line arguments.
3348- pdb = Pdb (mode = 'cli' , backend = 'monitoring' )
3365+ pdb = Pdb (mode = 'cli' , backend = 'monitoring' , colorize = True )
33493366 pdb .rcLines .extend (opts .commands )
33503367 while True :
33513368 try :
0 commit comments