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

(cherry picked from commit 2f8b08da47)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
This commit is contained in:
Łukasz Langa 2025-04-18 17:41:42 +02:00 committed by GitHub
parent ca2e1a1e1c
commit a37d719d37
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 39 additions and 31 deletions

View file

@ -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()