mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
gh-103693: Add convenience variable feature to pdb
(#103694)
This commit is contained in:
parent
524a7f77fd
commit
0fc58c66ba
5 changed files with 119 additions and 0 deletions
17
Lib/pdb.py
17
Lib/pdb.py
|
@ -270,6 +270,8 @@ class Pdb(bdb.Bdb, cmd.Cmd):
|
|||
self.lineno = None
|
||||
self.stack = []
|
||||
self.curindex = 0
|
||||
if hasattr(self, 'curframe') and self.curframe:
|
||||
self.curframe.f_globals.pop('__pdb_convenience_variables', None)
|
||||
self.curframe = None
|
||||
self.tb_lineno.clear()
|
||||
|
||||
|
@ -288,6 +290,7 @@ class Pdb(bdb.Bdb, cmd.Cmd):
|
|||
# locals whenever the .f_locals accessor is called, so we
|
||||
# cache it here to ensure that modifications are not overwritten.
|
||||
self.curframe_locals = self.curframe.f_locals
|
||||
self.set_convenience_variable(self.curframe, '_frame', self.curframe)
|
||||
return self.execRcLines()
|
||||
|
||||
# Can be executed earlier than 'setup' if desired
|
||||
|
@ -359,6 +362,7 @@ class Pdb(bdb.Bdb, cmd.Cmd):
|
|||
if self._wait_for_mainpyfile:
|
||||
return
|
||||
frame.f_locals['__return__'] = return_value
|
||||
self.set_convenience_variable(frame, '_retval', return_value)
|
||||
self.message('--Return--')
|
||||
self.interaction(frame, None)
|
||||
|
||||
|
@ -369,6 +373,7 @@ class Pdb(bdb.Bdb, cmd.Cmd):
|
|||
return
|
||||
exc_type, exc_value, exc_traceback = exc_info
|
||||
frame.f_locals['__exception__'] = exc_type, exc_value
|
||||
self.set_convenience_variable(frame, '_exception', exc_value)
|
||||
|
||||
# An 'Internal StopIteration' exception is an exception debug event
|
||||
# issued by the interpreter when handling a subgenerator run with
|
||||
|
@ -394,6 +399,7 @@ class Pdb(bdb.Bdb, cmd.Cmd):
|
|||
self.message('--KeyboardInterrupt--')
|
||||
|
||||
# Called before loop, handles display expressions
|
||||
# Set up convenience variable containers
|
||||
def preloop(self):
|
||||
displaying = self.displaying.get(self.curframe)
|
||||
if displaying:
|
||||
|
@ -477,6 +483,9 @@ class Pdb(bdb.Bdb, cmd.Cmd):
|
|||
next = line[marker+2:].lstrip()
|
||||
self.cmdqueue.append(next)
|
||||
line = line[:marker].rstrip()
|
||||
|
||||
# Replace all the convenience variables
|
||||
line = re.sub(r'\$([a-zA-Z_][a-zA-Z0-9_]*)', r'__pdb_convenience_variables["\1"]', line)
|
||||
return line
|
||||
|
||||
def onecmd(self, line):
|
||||
|
@ -527,6 +536,13 @@ class Pdb(bdb.Bdb, cmd.Cmd):
|
|||
def error(self, msg):
|
||||
print('***', msg, file=self.stdout)
|
||||
|
||||
# convenience variables
|
||||
|
||||
def set_convenience_variable(self, frame, name, value):
|
||||
if '__pdb_convenience_variables' not in frame.f_globals:
|
||||
frame.f_globals['__pdb_convenience_variables'] = {}
|
||||
frame.f_globals['__pdb_convenience_variables'][name] = value
|
||||
|
||||
# Generic completion functions. Individual complete_foo methods can be
|
||||
# assigned below to one of these functions.
|
||||
|
||||
|
@ -1018,6 +1034,7 @@ class Pdb(bdb.Bdb, cmd.Cmd):
|
|||
self.curindex = number
|
||||
self.curframe = self.stack[self.curindex][0]
|
||||
self.curframe_locals = self.curframe.f_locals
|
||||
self.set_convenience_variable(self.curframe, '_frame', self.curframe)
|
||||
self.print_stack_entry(self.stack[self.curindex])
|
||||
self.lineno = None
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue