93
93
import traceback
94
94
import linecache
95
95
import _colorize
96
+ import _pyrepl .utils
96
97
97
98
from contextlib import closing
98
99
from contextlib import contextmanager
@@ -339,7 +340,7 @@ class Pdb(bdb.Bdb, cmd.Cmd):
339
340
_last_pdb_instance = None
340
341
341
342
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 ):
343
344
bdb .Bdb .__init__ (self , skip = skip , backend = backend if backend else get_default_backend ())
344
345
cmd .Cmd .__init__ (self , completekey , stdin , stdout )
345
346
sys .audit ("pdb.Pdb" )
@@ -352,6 +353,7 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None,
352
353
self ._wait_for_mainpyfile = False
353
354
self .tb_lineno = {}
354
355
self .mode = mode
356
+ self .colorize = _colorize .can_colorize (file = stdout or sys .stdout ) and colorize
355
357
# Try to load readline if it exists
356
358
try :
357
359
import readline
@@ -1036,6 +1038,13 @@ def handle_command_def(self, line):
1036
1038
return True
1037
1039
return False
1038
1040
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
+
1039
1048
# interface abstraction functions
1040
1049
1041
1050
def message (self , msg , end = '\n ' ):
@@ -2166,6 +2175,8 @@ def _print_lines(self, lines, start, breaks=(), frame=None):
2166
2175
s += '->'
2167
2176
elif lineno == exc_lineno :
2168
2177
s += '>>'
2178
+ if self .colorize :
2179
+ line = self ._colorize_code (line )
2169
2180
self .message (s + '\t ' + line .rstrip ())
2170
2181
2171
2182
def do_whatis (self , arg ):
@@ -2365,8 +2376,14 @@ def print_stack_entry(self, frame_lineno, prompt_prefix=line_prefix):
2365
2376
prefix = '> '
2366
2377
else :
2367
2378
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 )
2370
2387
2371
2388
# Provide help
2372
2389
@@ -2604,7 +2621,7 @@ def set_trace(*, header=None, commands=None):
2604
2621
if Pdb ._last_pdb_instance is not None :
2605
2622
pdb = Pdb ._last_pdb_instance
2606
2623
else :
2607
- pdb = Pdb (mode = 'inline' , backend = 'monitoring' )
2624
+ pdb = Pdb (mode = 'inline' , backend = 'monitoring' , colorize = True )
2608
2625
if header is not None :
2609
2626
pdb .message (header )
2610
2627
pdb .set_trace (sys ._getframe ().f_back , commands = commands )
@@ -2619,7 +2636,7 @@ async def set_trace_async(*, header=None, commands=None):
2619
2636
if Pdb ._last_pdb_instance is not None :
2620
2637
pdb = Pdb ._last_pdb_instance
2621
2638
else :
2622
- pdb = Pdb (mode = 'inline' , backend = 'monitoring' )
2639
+ pdb = Pdb (mode = 'inline' , backend = 'monitoring' , colorize = True )
2623
2640
if header is not None :
2624
2641
pdb .message (header )
2625
2642
await pdb .set_trace_async (sys ._getframe ().f_back , commands = commands )
@@ -2633,7 +2650,7 @@ def __init__(self, sockfile, owns_sockfile=True, **kwargs):
2633
2650
self ._sockfile = sockfile
2634
2651
self ._command_name_cache = []
2635
2652
self ._write_failed = False
2636
- super ().__init__ (** kwargs )
2653
+ super ().__init__ (colorize = False , ** kwargs )
2637
2654
2638
2655
@staticmethod
2639
2656
def protocol_version ():
@@ -3345,7 +3362,7 @@ def main():
3345
3362
# modified by the script being debugged. It's a bad idea when it was
3346
3363
# changed by the user from the command line. There is a "restart" command
3347
3364
# which allows explicit specification of command line arguments.
3348
- pdb = Pdb (mode = 'cli' , backend = 'monitoring' )
3365
+ pdb = Pdb (mode = 'cli' , backend = 'monitoring' , colorize = True )
3349
3366
pdb .rcLines .extend (opts .commands )
3350
3367
while True :
3351
3368
try :
0 commit comments