Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
import dis
import code
import glob
import token
import codeop
import pprint
import signal
Expand Down Expand Up @@ -590,6 +591,45 @@ def default(self, line):
except:
self._error_exc()

def _replace_convenience_variables(self, line):
"""Replace the convenience variables in line"""

if "$" not in line:
return line

last_token_is_dollar = False
dollar_start = None
dollar_end = None
replace_variables = []
try:
for t in tokenize.generate_tokens(io.StringIO(line).readline):
token_type, token_string, start, end, _ = t
if token_type == token.OP and token_string == '$':
last_token_is_dollar = True
dollar_end = end
dollar_start = start
else:
if (last_token_is_dollar and
token_type == token.NAME and
start == dollar_end):
# line is a one line command so we only care about column
replace_variables.append((dollar_start[1], end[1], token_string))
last_token_is_dollar = False
except tokenize.TokenError:
return line

if not replace_variables:
return line

last_end = 0
new_line = ''
for start, end, name in replace_variables:
new_line += line[last_end:start] + f'__pdb_convenience_variables["{name}"]'
last_end = end
new_line += line[last_end:]

return new_line
Copy link
Contributor

@salty-horse salty-horse Nov 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of repeatedly creating strings with +=, it can be cleaner to use an array for the new_line parts, and join them when returning.
And maybe if replace_variables is empty, the original line can be returned to avoid creating a copy.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add extra breaks in more common cases to avoid unnecessary computation like you mentioned in the other comments. I'll fix those soon. However, I don't think concatenating strings is less clean than joining an array - the code is almost exactly the same, except for an extra line of join at the end.

Joining a list is often preferred because it's faster and more memory friendly than repeatedly concatenating strings, but in the actual use case, it would be very rare to have multiple convenience variables to replace. So the gain for using lists instead of string concatenation is not significant. String concatenation is clearer semantically in my opinion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String concatenation is clearer semantically in my opinion.

Im not sure, string concatenation in a loop jumps out as a code smell.


def precmd(self, line):
"""Handle alias expansion and ';;' separator."""
if not line.strip():
Expand Down Expand Up @@ -624,7 +664,7 @@ def precmd(self, line):
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)
line = self._replace_convenience_variables(line)

return line

Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,9 +794,12 @@ def test_convenience_variables():

>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
... '$_frame.f_lineno', # Check frame convenience variable
... '$ _frame', # This should be a syntax error
... '$a = 10', # Set a convenience variable
... '$a', # Print its value
... 'p "$a"', # Print the string $a
... 'p $a + 2', # Do some calculation
... 'p f"$a = {$a}"', # Make sure $ in string is not converted and f-string works
... 'u', # Switch frame
... '$_frame.f_lineno', # Make sure the frame changed
... '$a', # Make sure the value persists
Expand All @@ -816,11 +819,17 @@ def test_convenience_variables():
-> try:
(Pdb) $_frame.f_lineno
3
(Pdb) $ _frame
*** SyntaxError: invalid syntax
(Pdb) $a = 10
(Pdb) $a
10
(Pdb) p "$a"
'$a'
(Pdb) p $a + 2
12
(Pdb) p f"$a = {$a}"
'$a = 10'
(Pdb) u
> <doctest test.test_pdb.test_convenience_variables[1]>(2)test_function()
-> util_function()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve handling of pdb convenience variables to avoid replacing string contents.