| 1 |
|
| 2 |
"""analyze_syntax.py |
| 3 |
|
| 4 |
Collection of functions useful attempting to determine the |
| 5 |
cause of a SyntaxError and providing a somewhat detailed explanation. |
| 6 |
|
| 7 |
In an ideal world, one would write a custom parser for Python, extending |
| 8 |
the existing one with enhanced error messages about where in the parsing |
| 9 |
process a SyntaxError occurred, what kind of token was expected, etc., |
| 10 |
and use that information to give feedback to users. |
| 11 |
|
| 12 |
Unfortunately, we do not live in such a world. |
| 13 |
|
| 14 |
Friendly-traceback uses some ad-hoc heuristics to analyze the information |
| 15 |
given by Python or the code itself and makes an attempt at guessing |
| 16 |
as often as possible what went wrong while trying to avoid giving |
| 17 |
incorrect information. |
| 18 |
""" |
| 19 |
|
| 20 |
from friendly_traceback.ft_gettext import current_lang, internal_error, unknown_case |
| 21 |
|
| 22 |
from .. import debug_helper |
| 23 |
from . import message_analyzer, statement_analyzer |
| 24 |
|
| 25 |
_ = current_lang.translate |
| 26 |
|
| 27 |
|
| 28 |
def unknown_cause(): |
| 29 |
return _( |
| 30 |
"Currently, I cannot guess the likely cause of this error.\n" |
| 31 |
"Try to examine closely the line indicated as well as the line\n" |
| 32 |
"immediately above to see if you can identify some misspelled\n" |
| 33 |
"word, or missing symbols, like (, ), [, ], :, etc.\n" |
| 34 |
"\n" |
| 35 |
"Unless your code uses type annotations, which are beyond our scope,\n" |
| 36 |
"if you think that this is something which should be handled\n" |
| 37 |
"by friendly, please report this case to\n" |
| 38 |
"https://github.com/friendly-traceback/friendly-traceback/issues\n" |
| 39 |
"\n" |
| 40 |
) |
| 41 |
|
| 42 |
|
| 43 |
def set_cause_syntax(value, tb_data): |
| 44 |
"""Gets the likely cause of a given exception based on some information |
| 45 |
specific to a given exception. |
| 46 |
""" |
| 47 |
try: |
| 48 |
return find_syntax_error_cause(value, tb_data) |
| 49 |
except Exception as e: |
| 50 |
debug_helper.log_error(e) |
| 51 |
return {"cause": internal_error(e)} |
| 52 |
|
| 53 |
|
| 54 |
def find_syntax_error_cause(value, tb_data): |
| 55 |
"""Attempts to find the cause of a SyntaxError""" |
| 56 |
message = value.msg |
| 57 |
statement = tb_data.statement |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
if "invalid syntax" not in message: |
| 66 |
cause = message_analyzer.analyze_message(message, statement) |
| 67 |
if cause: |
| 68 |
return cause |
| 69 |
|
| 70 |
cause = statement_analyzer.analyze_statement(statement) |
| 71 |
if message == "expected ':'": |
| 72 |
new_cause = _( |
| 73 |
"Python told us that it expected a colon at the position indicated.\n" |
| 74 |
"However, adding a colon or replacing something else by a colon\n" |
| 75 |
"would not fix the problem.\n" |
| 76 |
) |
| 77 |
if cause: |
| 78 |
cause["cause"] = new_cause + cause["cause"] |
| 79 |
else: |
| 80 |
cause = {"cause": new_cause} |
| 81 |
return cause |
| 82 |
|
| 83 |
if not cause: |
| 84 |
return {"cause": unknown_cause(), "suggest": unknown_case()} |
| 85 |
|
| 86 |
notice = _( |
| 87 |
"Python gave us the following informative message\n" |
| 88 |
"about the possible cause of the error:\n\n" |
| 89 |
" {message}\n\n" |
| 90 |
"However, I do not recognize this information and I have\n" |
| 91 |
"to guess what caused the problem, but I might be wrong.\n\n" |
| 92 |
).format(message=message) |
| 93 |
debug_helper.log("This message is not known: " + message) |
| 94 |
filename = statement.filename |
| 95 |
if "\\tests\\" in filename: |
| 96 |
filename = "tests" + filename.split("tests")[1] |
| 97 |
debug_helper.log(f" file: {filename}") |
| 98 |
cause["cause"] = notice + cause["cause"] |
| 99 |
return cause |
| 100 |
|
| 101 |
cause = statement_analyzer.analyze_statement(statement) |
| 102 |
if cause: |
| 103 |
return cause |
| 104 |
|
| 105 |
return {"cause": unknown_cause(), "suggest": unknown_case()} |