From cd9c213a62e2e15711a5f6e3ecccbfea6fbee276 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Mon, 6 Apr 2020 23:34:27 +0200 Subject: [PATCH] Fix fstring issues when error leaves are involved --- parso/python/diff.py | 7 +++---- parso/python/tokenize.py | 2 +- test/test_diff_parser.py | 21 +++++++++++++++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/parso/python/diff.py b/parso/python/diff.py index 9bc236b..ba1e494 100644 --- a/parso/python/diff.py +++ b/parso/python/diff.py @@ -12,10 +12,9 @@ import logging from parso.utils import split_lines from parso.python.parser import Parser -from parso.python.tree import EndMarker, PythonErrorLeaf +from parso.python.tree import EndMarker from parso.python.tokenize import PythonToken from parso.python.token import PythonTokenTypes -from parso.tree import search_ancestor LOG = logging.getLogger(__name__) DEBUG_DIFF_PARSER = False @@ -432,9 +431,9 @@ class DiffParser(object): ) stack = self._active_parser.stack self._replace_tos_indent = None - #print('start', line_offset + 1, indents) + # print('start', line_offset + 1, indents) for token in tokens: - #print(token, indents) + # print(token, indents) typ = token.type if typ == DEDENT: if len(indents) < initial_indentation_count: diff --git a/parso/python/tokenize.py b/parso/python/tokenize.py index 4c5b34b..dec4363 100644 --- a/parso/python/tokenize.py +++ b/parso/python/tokenize.py @@ -532,7 +532,7 @@ def tokenize_lines(lines, version_info, start_pos=(1, 0), indents=None, is_first if not pseudomatch: # scan for tokens match = whitespace.match(line, pos) - if pos == 0 and paren_level == 0: + if pos == 0 and paren_level == 0 and not fstring_stack: for t in dedent_if_necessary(match.end()): yield t pos = match.end() diff --git a/test/test_diff_parser.py b/test/test_diff_parser.py index 8ec3a0f..3878dc8 100644 --- a/test/test_diff_parser.py +++ b/test/test_diff_parser.py @@ -1609,3 +1609,24 @@ def test_backslash_insertion(differ): differ.initialize(code1) differ.parse(code2, parsers=2, copies=1, expect_error_leaves=True) differ.parse(code1, parsers=2, copies=1) + + +def test_fstring_with_error_leaf(differ): + code1 = dedent("""\ + def f(): + x + def g(): + y + """) + code2 = dedent("""\ + def f(): + x + F''' + def g(): + y + {a + \x01 + """) + + differ.initialize(code1) + differ.parse(code2, parsers=1, copies=1, expect_error_leaves=True)