Skip to content

[3.13] gh-129958: Properly disallow newlines in format specs in single-quoted f-strings (GH-130063) #132692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Lib/test/test_fstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -1784,6 +1784,31 @@ def test_gh129093(self):
self.assertEqual(f'{f'{1!=2=}'=}', "f'{1!=2=}'='1!=2=True'")
self.assertEqual(f'{f'{1 != 2=}'=}', "f'{1 != 2=}'='1 != 2=True'")

def test_newlines_in_format_specifiers(self):
cases = [
"""f'{1:d\n}'""",
"""f'__{
1:d
}__'""",
'''f"{value:.
{'2f'}}"''',
'''f"{value:
{'.2f'}f}"''',
'''f"{value:
#{'x'}}"''',
]
self.assertAllRaise(SyntaxError, "f-string: newlines are not allowed in format specifiers", cases)

valid_cases = [
"""f'''__{
1:d
}__'''""",
"""f'''{1:d\n}'''""",
]

for case in valid_cases:
compile(case, "<string>", "exec")


if __name__ == '__main__':
unittest.main()
35 changes: 4 additions & 31 deletions Lib/test/test_tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,22 +603,6 @@ def test_string(self):
OP '}' (6, 0) (6, 1)
FSTRING_MIDDLE '__' (6, 1) (6, 3)
FSTRING_END "'''" (6, 3) (6, 6)
""")
self.check_tokenize("""\
f'__{
x:d
}__'""", """\
FSTRING_START "f'" (1, 0) (1, 2)
FSTRING_MIDDLE '__' (1, 2) (1, 4)
OP '{' (1, 4) (1, 5)
NL '\\n' (1, 5) (1, 6)
NAME 'x' (2, 4) (2, 5)
OP ':' (2, 5) (2, 6)
FSTRING_MIDDLE 'd' (2, 6) (2, 7)
NL '\\n' (2, 7) (2, 8)
OP '}' (3, 0) (3, 1)
FSTRING_MIDDLE '__' (3, 1) (3, 3)
FSTRING_END "'" (3, 3) (3, 4)
""")

self.check_tokenize("""\
Expand Down Expand Up @@ -2468,21 +2452,6 @@ def test_string(self):
RBRACE '}' (6, 0) (6, 1)
FSTRING_MIDDLE '__' (6, 1) (6, 3)
FSTRING_END "'''" (6, 3) (6, 6)
""")

self.check_tokenize("""\
f'__{
x:d
}__'""", """\
FSTRING_START "f'" (1, 0) (1, 2)
FSTRING_MIDDLE '__' (1, 2) (1, 4)
LBRACE '{' (1, 4) (1, 5)
NAME 'x' (2, 4) (2, 5)
COLON ':' (2, 5) (2, 6)
FSTRING_MIDDLE 'd' (2, 6) (2, 7)
RBRACE '}' (3, 0) (3, 1)
FSTRING_MIDDLE '__' (3, 1) (3, 3)
FSTRING_END "'" (3, 3) (3, 4)
""")

def test_function(self):
Expand Down Expand Up @@ -3038,6 +3007,10 @@ def get_tokens(string):
"'''sdfsdf''",
"("*1000+"a"+")"*1000,
"]",
"""\
f'__{
x:d
}__'""",
]:
with self.subTest(case=case):
self.assertRaises(tokenize.TokenError, get_tokens, case)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a bug that was allowing newlines inconsitently in format specifiers for
single-quoted f-strings. Patch by Pablo Galindo.
8 changes: 8 additions & 0 deletions Parser/lexer/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,14 @@ tok_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct
// it means that the format spec ends here and we should
// return to the regular mode.
if (in_format_spec && c == '\n') {
if (current_tok->f_string_quote_size == 1) {
return MAKE_TOKEN(
_PyTokenizer_syntaxerror(
tok,
"f-string: newlines are not allowed in format specifiers for single quoted f-strings"
)
);
}
tok_backup(tok, c);
TOK_GET_MODE(tok)->kind = TOK_REGULAR_MODE;
current_tok->in_format_spec = 0;
Expand Down
Loading