-
-
Notifications
You must be signed in to change notification settings - Fork 33.5k
gh-112343: pdb: Use tokenize to replace convenience variables #112380
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
016f241
bb53b6c
e8ebbe6
f7ece90
7072c14
808dbbd
49a0c10
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,6 +76,7 @@ | |
| import dis | ||
| import code | ||
| import glob | ||
| import token | ||
| import codeop | ||
| import pprint | ||
| import signal | ||
|
|
@@ -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 | ||
gaogaotiantian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| dollar_start = None | ||
| dollar_end = None | ||
iritkatriel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 | ||
iritkatriel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| else: | ||
| if (last_token_is_dollar and | ||
| token_type == token.NAME and | ||
| start == dollar_end): | ||
iritkatriel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # line is a one line command so we only care about column | ||
gaogaotiantian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| replace_variables.append((dollar_start[1], end[1], token_string)) | ||
| last_token_is_dollar = False | ||
iritkatriel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 | ||
|
||
|
|
||
| def precmd(self, line): | ||
| """Handle alias expansion and ';;' separator.""" | ||
| if not line.strip(): | ||
|
|
@@ -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 | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Improve handling of pdb convenience variables to avoid replacing string contents. |
Uh oh!
There was an error while loading. Please reload this page.