Fix an f-string tokenizer issue

This commit is contained in:
Dave Halter
2019-02-13 00:17:37 +01:00
parent f1ee7614c9
commit 3f6fc8a5ad
2 changed files with 14 additions and 8 deletions
+2 -2
View File
@@ -419,8 +419,6 @@ def tokenize_lines(lines, version_info, start_pos=(1, 0)):
tos = fstring_stack[-1] tos = fstring_stack[-1]
if not tos.is_in_expr(): if not tos.is_in_expr():
string, pos = _find_fstring_string(endpats, fstring_stack, line, lnum, pos) string, pos = _find_fstring_string(endpats, fstring_stack, line, lnum, pos)
if pos == max:
break
if string: if string:
yield PythonToken( yield PythonToken(
FSTRING_STRING, string, FSTRING_STRING, string,
@@ -431,6 +429,8 @@ def tokenize_lines(lines, version_info, start_pos=(1, 0)):
) )
tos.previous_lines = '' tos.previous_lines = ''
continue continue
if pos == max:
break
rest = line[pos:] rest = line[pos:]
fstring_end_token, additional_prefix, quote_length = _close_fstring_if_necessary( fstring_end_token, additional_prefix, quote_length = _close_fstring_if_necessary(
+12 -6
View File
@@ -79,11 +79,17 @@ def test_tokenize_start_pos(code, positions):
assert positions == [p.start_pos for p in tokens] assert positions == [p.start_pos for p in tokens]
def test_roundtrip(grammar): @pytest.mark.parametrize(
code = dedent("""\ 'code', [
f'''s{ dedent("""\
str.uppe f'''s{
''' str.uppe
""") '''
"""),
'f"foo',
'f"""foo',
]
)
def test_roundtrip(grammar, code):
tree = grammar.parse(code) tree = grammar.parse(code)
assert tree.get_code() == code assert tree.get_code() == code