From 3d95b65b21e22858b6871ccd1401a5e453219847 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Fri, 27 Mar 2020 11:17:31 +0100 Subject: [PATCH] Fix an issue with unfinished f string literals --- parso/python/tokenize.py | 11 +++++++++++ test/test_get_code.py | 13 +++++++++++++ 2 files changed, 24 insertions(+) diff --git a/parso/python/tokenize.py b/parso/python/tokenize.py index 70facf9..b4cef21 100644 --- a/parso/python/tokenize.py +++ b/parso/python/tokenize.py @@ -649,6 +649,17 @@ def tokenize_lines(lines, version_info, start_pos=(1, 0)): if contstr.endswith('\n') or contstr.endswith('\r'): new_line = True + if fstring_stack: + tos = fstring_stack[-1] + if tos.previous_lines: + yield PythonToken( + FSTRING_STRING, tos.previous_lines, + tos.last_string_start_pos, + # Never has a prefix because it can start anywhere and + # include whitespace. + prefix='' + ) + end_pos = lnum, max # As the last position we just take the maximally possible position. We # remove -1 for the last new line. diff --git a/test/test_get_code.py b/test/test_get_code.py index 2f2260d..d99d792 100644 --- a/test/test_get_code.py +++ b/test/test_get_code.py @@ -118,3 +118,16 @@ def test_carriage_return_at_end(code, types): assert tree.get_code() == code assert [c.type for c in tree.children] == types assert tree.end_pos == (len(code) + 1, 0) + + +@pytest.mark.parametrize('code', [ + ' ', + ' F"""', + ' F"""\n', + ' F""" \n', + ' F""" \n3', + ' f"""\n"""', + ' f"""\n"""\n', +]) +def test_full_code_round_trip(code): + assert parse(code).get_code() == code