mirror of
https://github.com/python/cpython.git
synced 2025-10-31 21:51:50 +00:00
gh-137314: Fix incorrect treatment of format specs in raw fstrings (#137328)
This commit is contained in:
parent
676748d4da
commit
0153d82a5a
3 changed files with 42 additions and 1 deletions
|
|
@ -1831,6 +1831,34 @@ def test_newlines_in_format_specifiers(self):
|
|||
for case in valid_cases:
|
||||
compile(case, "<string>", "exec")
|
||||
|
||||
def test_raw_fstring_format_spec(self):
|
||||
# Test raw f-string format spec behavior (Issue #137314).
|
||||
#
|
||||
# Raw f-strings should preserve literal backslashes in format specifications,
|
||||
# not interpret them as escape sequences.
|
||||
class UnchangedFormat:
|
||||
"""Test helper that returns the format spec unchanged."""
|
||||
def __format__(self, format):
|
||||
return format
|
||||
|
||||
# Test basic escape sequences
|
||||
self.assertEqual(f"{UnchangedFormat():\xFF}", 'ÿ')
|
||||
self.assertEqual(rf"{UnchangedFormat():\xFF}", '\\xFF')
|
||||
|
||||
# Test nested expressions with raw/non-raw combinations
|
||||
self.assertEqual(rf"{UnchangedFormat():{'\xFF'}}", 'ÿ')
|
||||
self.assertEqual(f"{UnchangedFormat():{r'\xFF'}}", '\\xFF')
|
||||
self.assertEqual(rf"{UnchangedFormat():{r'\xFF'}}", '\\xFF')
|
||||
|
||||
# Test continuation character in format specs
|
||||
self.assertEqual(f"""{UnchangedFormat():{'a'\
|
||||
'b'}}""", 'ab')
|
||||
self.assertEqual(rf"""{UnchangedFormat():{'a'\
|
||||
'b'}}""", 'ab')
|
||||
|
||||
# Test multiple format specs in same raw f-string
|
||||
self.assertEqual(rf"{UnchangedFormat():\xFF} {UnchangedFormat():\n}", '\\xFF \\n')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue