Replace the convenience variables in 'line' with their values. e.g. $foo is replaced by __pdb_convenience_variables["foo"]. Note: such pattern in string literals will be skipped
(self, line)
| 986 | self._error_exc() |
| 987 | |
| 988 | def _replace_convenience_variables(self, line): |
| 989 | """Replace the convenience variables in 'line' with their values. |
| 990 | e.g. $foo is replaced by __pdb_convenience_variables["foo"]. |
| 991 | Note: such pattern in string literals will be skipped""" |
| 992 | |
| 993 | if "$" not in line: |
| 994 | return line |
| 995 | |
| 996 | dollar_start = dollar_end = (-1, -1) |
| 997 | replace_variables = [] |
| 998 | try: |
| 999 | for t in tokenize.generate_tokens(io.StringIO(line).readline): |
| 1000 | token_type, token_string, start, end, _ = t |
| 1001 | if token_type == token.OP and token_string == '$': |
| 1002 | dollar_start, dollar_end = start, end |
| 1003 | elif start == dollar_end and token_type == token.NAME: |
| 1004 | # line is a one-line command so we only care about column |
| 1005 | replace_variables.append((dollar_start[1], end[1], token_string)) |
| 1006 | except tokenize.TokenError: |
| 1007 | return line |
| 1008 | |
| 1009 | if not replace_variables: |
| 1010 | return line |
| 1011 | |
| 1012 | last_end = 0 |
| 1013 | line_pieces = [] |
| 1014 | for start, end, name in replace_variables: |
| 1015 | line_pieces.append(line[last_end:start] + f'__pdb_convenience_variables["{name}"]') |
| 1016 | last_end = end |
| 1017 | line_pieces.append(line[last_end:]) |
| 1018 | |
| 1019 | return ''.join(line_pieces) |
| 1020 | |
| 1021 | def precmd(self, line): |
| 1022 | """Handle alias expansion and ';;' separator.""" |