mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-15 17:07:13 +08:00
Make parso errors even more informative
This commit is contained in:
@@ -64,6 +64,18 @@ def _assert_valid_graph(node):
|
|||||||
_assert_valid_graph(child)
|
_assert_valid_graph(child)
|
||||||
|
|
||||||
|
|
||||||
|
def _get_debug_error_message(module, old_lines, new_lines):
|
||||||
|
current_lines = split_lines(module.get_code(), keepends=True)
|
||||||
|
current_diff = difflib.unified_diff(current_lines, new_lines)
|
||||||
|
old_new_diff = difflib.unified_diff(old_lines, new_lines)
|
||||||
|
import parso
|
||||||
|
return (
|
||||||
|
"There's an issue with the diff parser. Please "
|
||||||
|
"report (parso v%s) - Old/New:\n%s\nActual Diff (May be empty):\n%s"
|
||||||
|
% (parso.__version__, ''.join(old_new_diff), ''.join(current_diff))
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def _get_last_line(node_or_leaf):
|
def _get_last_line(node_or_leaf):
|
||||||
last_leaf = node_or_leaf.get_last_leaf()
|
last_leaf = node_or_leaf.get_last_leaf()
|
||||||
if _ends_with_newline(last_leaf):
|
if _ends_with_newline(last_leaf):
|
||||||
@@ -207,20 +219,20 @@ class DiffParser(object):
|
|||||||
|
|
||||||
last_pos = self._module.end_pos[0]
|
last_pos = self._module.end_pos[0]
|
||||||
if last_pos != line_length:
|
if last_pos != line_length:
|
||||||
current_lines = split_lines(self._module.get_code(), keepends=True)
|
|
||||||
diff = difflib.unified_diff(current_lines, new_lines)
|
|
||||||
import parso
|
|
||||||
raise Exception(
|
raise Exception(
|
||||||
"There's an issue (%s != %s) with the diff parser. Please "
|
('(%s != %s) ' % (last_pos, line_length))
|
||||||
"report (parso v%s):\n%s"
|
+ _get_debug_error_message(self._module, old_lines, new_lines)
|
||||||
% (last_pos, line_length, parso.__version__, ''.join(diff))
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if DEBUG_DIFF_PARSER:
|
if DEBUG_DIFF_PARSER:
|
||||||
# If there is reasonable suspicion that the diff parser is not
|
# If there is reasonable suspicion that the diff parser is not
|
||||||
# behaving well, this should be enabled.
|
# behaving well, this should be enabled.
|
||||||
assert self._module.get_code() == ''.join(new_lines)
|
try:
|
||||||
_assert_valid_graph(self._module)
|
assert self._module.get_code() == ''.join(new_lines)
|
||||||
|
_assert_valid_graph(self._module)
|
||||||
|
except AssertionError:
|
||||||
|
print(_get_debug_error_message(self._module, old_lines, new_lines))
|
||||||
|
raise
|
||||||
|
|
||||||
LOG.debug('diff parser end')
|
LOG.debug('diff parser end')
|
||||||
return self._module
|
return self._module
|
||||||
|
|||||||
Reference in New Issue
Block a user