Skip to content

Commit 492e0ad

Browse files
committed
Ensure triple-quoted strings keep working as before
Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
1 parent 2c2c980 commit 492e0ad

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed

Lib/test/test_tokenize.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -577,13 +577,30 @@ def test_string(self):
577577
NL '\\n' (1, 7) (1, 8)
578578
NAME 'x' (2, 4) (2, 5)
579579
OP ':' (2, 5) (2, 6)
580-
FSTRING_MIDDLE 'a' (2, 6) (2, 7)
581-
NL '\\n' (2, 7) (2, 8)
580+
FSTRING_MIDDLE 'a\\n' (2, 6) (3, 0)
582581
OP '}' (3, 0) (3, 1)
583582
FSTRING_MIDDLE '__' (3, 1) (3, 3)
584583
FSTRING_END "'''" (3, 3) (3, 6)
585584
""")
586585
self.check_tokenize("""\
586+
f'''__{
587+
x:a
588+
b
589+
c
590+
d
591+
}__'''""", """\
592+
FSTRING_START "f'''" (1, 0) (1, 4)
593+
FSTRING_MIDDLE '__' (1, 4) (1, 6)
594+
OP '{' (1, 6) (1, 7)
595+
NL '\\n' (1, 7) (1, 8)
596+
NAME 'x' (2, 4) (2, 5)
597+
OP ':' (2, 5) (2, 6)
598+
FSTRING_MIDDLE 'a\\n b\\n c\\n d\\n' (2, 6) (6, 0)
599+
OP '}' (6, 0) (6, 1)
600+
FSTRING_MIDDLE '__' (6, 1) (6, 3)
601+
FSTRING_END "'''" (6, 3) (6, 6)
602+
""")
603+
self.check_tokenize("""\
587604
f'__{
588605
x:d
589606
}__'""", """\
@@ -2320,11 +2337,30 @@ def test_string(self):
23202337
LBRACE '{' (1, 6) (1, 7)
23212338
NAME 'x' (2, 4) (2, 5)
23222339
COLON ':' (2, 5) (2, 6)
2323-
FSTRING_MIDDLE 'a' (2, 6) (2, 7)
2340+
FSTRING_MIDDLE 'a\\n' (2, 6) (3, 0)
23242341
RBRACE '}' (3, 0) (3, 1)
23252342
FSTRING_MIDDLE '__' (3, 1) (3, 3)
23262343
FSTRING_END "'''" (3, 3) (3, 6)
23272344
""")
2345+
2346+
self.check_tokenize("""\
2347+
f'''__{
2348+
x:a
2349+
b
2350+
c
2351+
d
2352+
}__'''""", """\
2353+
FSTRING_START "f'''" (1, 0) (1, 4)
2354+
FSTRING_MIDDLE '__' (1, 4) (1, 6)
2355+
LBRACE '{' (1, 6) (1, 7)
2356+
NAME 'x' (2, 4) (2, 5)
2357+
COLON ':' (2, 5) (2, 6)
2358+
FSTRING_MIDDLE 'a\\n b\\n c\\n d\\n' (2, 6) (6, 0)
2359+
RBRACE '}' (6, 0) (6, 1)
2360+
FSTRING_MIDDLE '__' (6, 1) (6, 3)
2361+
FSTRING_END "'''" (6, 3) (6, 6)
2362+
""")
2363+
23282364
self.check_tokenize("""\
23292365
f'__{
23302366
x:d

Parser/tokenizer.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2696,21 +2696,22 @@ tok_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct
26962696
INSIDE_FSTRING_EXPR(current_tok)
26972697
);
26982698

2699-
// If we are in a format spec and we found a newline,
2700-
// it means that the format spec ends here and we should
2701-
// return to the regular mode.
2702-
if (in_format_spec && c == '\n') {
2703-
tok_backup(tok, c);
2704-
TOK_GET_MODE(tok)->kind = TOK_REGULAR_MODE;
2705-
p_start = tok->start;
2706-
p_end = tok->cur;
2707-
return MAKE_TOKEN(FSTRING_MIDDLE);
2708-
}
2709-
if (c == EOF || (current_tok->f_string_quote_size == 1 && c == '\n')) {
2699+
if (c == EOF || (current_tok->f_string_quote_size == 1 && c == '\n')) {
27102700
if (tok->decoding_erred) {
27112701
return MAKE_TOKEN(ERRORTOKEN);
27122702
}
27132703

2704+
// If we are in a format spec and we found a newline,
2705+
// it means that the format spec ends here and we should
2706+
// return to the regular mode.
2707+
if (in_format_spec && c == '\n') {
2708+
tok_backup(tok, c);
2709+
TOK_GET_MODE(tok)->kind = TOK_REGULAR_MODE;
2710+
p_start = tok->start;
2711+
p_end = tok->cur;
2712+
return MAKE_TOKEN(FSTRING_MIDDLE);
2713+
}
2714+
27142715
assert(tok->multi_line_start != NULL);
27152716
// shift the tok_state's location into
27162717
// the start of string, and report the error

0 commit comments

Comments
 (0)