From ebc69545c767014e7a8fc228192d613023bc0c92 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Sun, 5 Apr 2020 00:13:55 +0200 Subject: [PATCH] Fix error recovery for multi line strings at the end of the file --- parso/python/parser.py | 4 ++-- test/test_diff_parser.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/parso/python/parser.py b/parso/python/parser.py index d89f30f..ada60fb 100644 --- a/parso/python/parser.py +++ b/parso/python/parser.py @@ -126,8 +126,8 @@ class Parser(BaseParser): if self._start_nonterminal == 'file_input' and \ (token.type == PythonTokenTypes.ENDMARKER - or token.type == DEDENT and '\n' not in last_leaf.value - and '\r' not in last_leaf.value): + or token.type == DEDENT and not last_leaf.value.endswith('\n') + and not last_leaf.value.endswith('\r')): # In Python statements need to end with a newline. But since it's # possible (and valid in Python) that there's no newline at the # end of a file, we have to recover even if the user doesn't want diff --git a/test/test_diff_parser.py b/test/test_diff_parser.py index 5c33d7b..21e49b0 100644 --- a/test/test_diff_parser.py +++ b/test/test_diff_parser.py @@ -1509,3 +1509,18 @@ def test_async_func2(differ): ''') differ.initialize(code1) differ.parse(code2, parsers=2, copies=1, expect_error_leaves=True) + + +def test_weird_ending(differ): + code1 = dedent('''\ + def foo(): + a + return + ''') + code2 = dedent('''\ + def foo(): + a + nonlocal xF""" + y"""''') + differ.initialize(code1) + differ.parse(code2, parsers=1, copies=1, expect_error_leaves=True)