| 1 |
|
| 2 |
"""Various functions used to find fixes to SyntaxErrors""" |
| 3 |
|
| 4 |
import warnings |
| 5 |
|
| 6 |
from .. import debug_helper, token_utils |
| 7 |
|
| 8 |
|
| 9 |
def replace_token(tokens, original_token, new_token_string=" "): |
| 10 |
"""Replaces a single token string to produce a new source. |
| 11 |
More specifically, given:: |
| 12 |
|
| 13 |
* tokens: a list of tokens |
| 14 |
* original_token: single token to be replaced |
| 15 |
* replace: new string to replace original_token.string |
| 16 |
|
| 17 |
It creates a new list of tokens with the replacement having been done, |
| 18 |
untokenize it to produce a modified source which is stripped of |
| 19 |
leading and ending spaces so that it could be inserted in a |
| 20 |
code sample at the beginning of a line with no indentation. |
| 21 |
""" |
| 22 |
if not tokens: |
| 23 |
debug_helper.log("Problem in fixers.replace_token().") |
| 24 |
debug_helper.log("Empty token list was received") |
| 25 |
debug_helper.log_error() |
| 26 |
return "?" |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
if not original_token.string.strip(): |
| 32 |
return "?" |
| 33 |
|
| 34 |
try: |
| 35 |
new_tokens = [] |
| 36 |
for tok in tokens: |
| 37 |
if tok is original_token: |
| 38 |
new_token = original_token.copy() |
| 39 |
new_token.string = new_token_string |
| 40 |
new_tokens.append(new_token) |
| 41 |
else: |
| 42 |
new_tokens.append(tok) |
| 43 |
source = token_utils.untokenize(new_tokens) |
| 44 |
return source.strip() |
| 45 |
except Exception as e: |
| 46 |
debug_helper.log("Problem in fixers._modify_source().") |
| 47 |
debug_helper.log_error(e) |
| 48 |
return token_utils.untokenize(tokens) |
| 49 |
|
| 50 |
|
| 51 |
def replace_two_tokens( |
| 52 |
tokens, first_token, first_string="", second_token=None, second_string="" |
| 53 |
): |
| 54 |
"""Replace two tokens at once by their new string value""" |
| 55 |
if second_token is None: |
| 56 |
debug_helper.log("Problem in fixers.replace_two_tokens()") |
| 57 |
debug_helper.log("second_token should not be None") |
| 58 |
return token_utils.untokenize(tokens) |
| 59 |
try: |
| 60 |
new_tokens = [] |
| 61 |
for tok in tokens: |
| 62 |
if tok is first_token: |
| 63 |
new_token = first_token.copy() |
| 64 |
new_token.string = first_string |
| 65 |
new_tokens.append(new_token) |
| 66 |
elif tok is second_token: |
| 67 |
new_token = second_token.copy() |
| 68 |
new_token.string = second_string |
| 69 |
new_tokens.append(new_token) |
| 70 |
else: |
| 71 |
new_tokens.append(tok) |
| 72 |
|
| 73 |
source = token_utils.untokenize(new_tokens) |
| 74 |
return source.strip() |
| 75 |
except Exception as e: |
| 76 |
debug_helper.log("Problem in fixers.replace_two_tokens().") |
| 77 |
debug_helper.log_error(e) |
| 78 |
return token_utils.untokenize(tokens) |
| 79 |
|
| 80 |
|
| 81 |
def check_statement(statement): |
| 82 |
"""Given a single line of code expected to be a valid 'statement', |
| 83 |
that is a line which could be part of a larger source of code and |
| 84 |
contains no unmatched brackets and would not end with the continuation |
| 85 |
character, it is potentially modified to include some required |
| 86 |
'environment' and compiled to see if it raises any SyntaxErrors. |
| 87 |
|
| 88 |
Returns True if no SyntaxError is raised, False otherwise. |
| 89 |
""" |
| 90 |
if not statement: |
| 91 |
return False |
| 92 |
|
| 93 |
statement = token_utils.strip_comment(statement) |
| 94 |
try: |
| 95 |
|
| 96 |
|
| 97 |
statement = statement.lstrip() |
| 98 |
if statement.rstrip().endswith(":"): |
| 99 |
statement += " pass" |
| 100 |
|
| 101 |
if statement.startswith("elif") or statement.startswith("else"): |
| 102 |
statement = if_block % statement |
| 103 |
elif statement.startswith("return") or statement.startswith("yield"): |
| 104 |
statement = def_block % statement |
| 105 |
elif statement.startswith("except") or statement.startswith("finally"): |
| 106 |
statement = try_block % statement |
| 107 |
|
| 108 |
with warnings.catch_warnings(): |
| 109 |
warnings.simplefilter("error") |
| 110 |
try: |
| 111 |
compile(statement, "fake-file", "exec") |
| 112 |
return True |
| 113 |
except SyntaxError: |
| 114 |
return False |
| 115 |
|
| 116 |
except Exception as e: |
| 117 |
debug_helper.log("Problem in token_utils.check_statement().") |
| 118 |
debug_helper.log_error(e) |
| 119 |
return False |
| 120 |
|
| 121 |
|
| 122 |
if_block = """ |
| 123 |
if True: |
| 124 |
pass |
| 125 |
%s |
| 126 |
""" |
| 127 |
|
| 128 |
def_block = """ |
| 129 |
def test(): |
| 130 |
%s |
| 131 |
""" |
| 132 |
|
| 133 |
try_block = """ |
| 134 |
try: |
| 135 |
pass |
| 136 |
%s |
| 137 |
""" |