diff --git a/parso/python/diff.py b/parso/python/diff.py index 2255b5c..5be6acb 100644 --- a/parso/python/diff.py +++ b/parso/python/diff.py @@ -17,6 +17,25 @@ from parso.python.tokenize import PythonToken from parso.python.token import PythonTokenTypes LOG = logging.getLogger(__name__) +DEBUG_DIFF_PARSER = False + + +def _assert_valid_graph(node): + """ + Checks if the parent/children relationship is correct. + """ + try: + children = node.children + except AttributeError: + previous_leaf = node.get_previous_leaf() + if previous_leaf is not None: + assert previous_leaf.end_pos <= node.start_pos, \ + (previous_leaf, node) + return + + for child in children: + assert child.parent == node + _assert_valid_graph(child) def _get_last_line(node_or_leaf): @@ -171,7 +190,12 @@ class DiffParser(object): % (last_pos, line_length, parso.__version__, ''.join(diff)) ) - #assert self._module.get_code() == ''.join(new_lines) + if DEBUG_DIFF_PARSER: + # If there is reasonable suspicion that the diff parser is not + # behaving well, this should be enabled. + assert self._module.get_code() == ''.join(new_lines) + _assert_valid_graph(self._module) + LOG.debug('diff parser end') return self._module diff --git a/test/test_diff_parser.py b/test/test_diff_parser.py index eaf748c..8df6402 100644 --- a/test/test_diff_parser.py +++ b/test/test_diff_parser.py @@ -6,7 +6,7 @@ import pytest from parso.utils import split_lines from parso import cache from parso import load_grammar -from parso.python.diff import DiffParser +from parso.python.diff import DiffParser, _assert_valid_graph from parso import parse @@ -34,24 +34,6 @@ def _check_error_leaves_nodes(node): return False -def _assert_valid_graph(node): - """ - Checks if the parent/children relationship is correct. - """ - try: - children = node.children - except AttributeError: - previous_leaf = node.get_previous_leaf() - if previous_leaf is not None: - assert previous_leaf.end_pos <= node.start_pos, \ - (previous_leaf, node) - return - - for child in children: - assert child.parent == node - _assert_valid_graph(child) - - class Differ(object): grammar = load_grammar()