diff --git a/parso/python/diff.py b/parso/python/diff.py index 79f78ab..6495557 100644 --- a/parso/python/diff.py +++ b/parso/python/diff.py @@ -135,6 +135,15 @@ def _get_last_line(node_or_leaf): if _ends_with_newline(last_leaf): return last_leaf.start_pos[0] else: + n = last_leaf.get_next_leaf() + if n.type == 'endmarker' and '\n' in n.prefix: + # This is a very special case and has to do with error recovery in + # Parso. The problem is basically that there's no newline leaf at + # the end sometimes (it's required in the grammar, but not needed + # actually before endmarker, CPython just adds a newline to make + # source code pass the parser, to account for that Parso error + # recovery allows small_stmt instead of simple_stmt). + return last_leaf.end_pos[0] + 1 return last_leaf.end_pos[0] diff --git a/test/test_diff_parser.py b/test/test_diff_parser.py index b962532..ea9632b 100644 --- a/test/test_diff_parser.py +++ b/test/test_diff_parser.py @@ -1692,3 +1692,17 @@ def test_backslash_before_def(differ): differ.initialize(code1) differ.parse(code2, parsers=3, copies=1, expect_error_leaves=True) + + +def test_backslash_with_imports(differ): + code1 = dedent('''\ + from x import y, \\ + ''') + code2 = dedent('''\ + from x import y, \\ + z + ''') + + differ.initialize(code1) + differ.parse(code2, parsers=1) + differ.parse(code1, parsers=1)