Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
gh-103693: Add convenience variable feature topdb
#103694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
7db9d37
f7a11af
a202abf
1415210
ac23148
150250b
f626d36
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -263,6 +263,21 @@ the commands; the input is split at the first ``;;`` pair, even if it is in the | ||
middle of a quoted string. A workaround for strings with double semicolons | ||
is to use implicit string concatenation ``';'';'`` or ``";"";"``. | ||
To set a temporary global variable, use a *convenience variable*. A *convenience | ||
variable* is a variable whose name starts with ``$``. For example, ``$foo = 1`` | ||
sets a global variable ``$foo`` which you can use in the debugger session. The | ||
*convenience variables* are cleared when the program resumes execution so it's | ||
less likely to interfere with your program compared to using normal variables | ||
iritkatriel marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
like ``foo = 1``. | ||
There are three preset *convenience variables*: | ||
* ``$_frame``: the current frame you are debugging | ||
* ``$_retval``: the return value if the frame is returning | ||
* ``$_exception``: the exception if the frame is raising an exception | ||
iritkatriel marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
.. versionadded:: 3.12 | ||
.. index:: | ||
pair: .pdbrc; file | ||
triple: debugger; configuration; file | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -270,6 +270,8 @@ def forget(self): | ||
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 @@ def setup(self, f, tb): | ||
# 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 @@ def user_return(self, frame, return_value): | ||
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 @@ def user_exception(self, frame, exc_info): | ||
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 @@ def _cmdloop(self): | ||
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 @@ def precmd(self, line): | ||
next = line[marker+2:].lstrip() | ||
self.cmdqueue.append(next) | ||
line = line[:marker].rstrip() | ||
# Replace all the convenience variables | ||
iritkatriel marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
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 @@ def message(self, msg): | ||
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: | ||
iritkatriel marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
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 @@ def _select_frame(self, number): | ||
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 | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add convenience variable feature to :mod:`pdb` | ||
iritkatriel marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. |