Skip to content

Commit 0db3d08

Browse files
pablogsalmiss-islington
authored andcommitted
gh-137314: Fix incorrect treatment of format specs in raw fstrings (GH-137328)
(cherry picked from commit 0153d82) Co-authored-by: Pablo Galindo Salgado <[email protected]>
1 parent 7adea43 commit 0db3d08

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

Lib/test/test_fstring.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1821,6 +1821,34 @@ def test_newlines_in_format_specifiers(self):
18211821
for case in valid_cases:
18221822
compile(case, "<string>", "exec")
18231823

1824+
def test_raw_fstring_format_spec(self):
1825+
# Test raw f-string format spec behavior (Issue #137314).
1826+
#
1827+
# Raw f-strings should preserve literal backslashes in format specifications,
1828+
# not interpret them as escape sequences.
1829+
class UnchangedFormat:
1830+
"""Test helper that returns the format spec unchanged."""
1831+
def __format__(self, format):
1832+
return format
1833+
1834+
# Test basic escape sequences
1835+
self.assertEqual(f"{UnchangedFormat():\xFF}", 'ÿ')
1836+
self.assertEqual(rf"{UnchangedFormat():\xFF}", '\\xFF')
1837+
1838+
# Test nested expressions with raw/non-raw combinations
1839+
self.assertEqual(rf"{UnchangedFormat():{'\xFF'}}", 'ÿ')
1840+
self.assertEqual(f"{UnchangedFormat():{r'\xFF'}}", '\\xFF')
1841+
self.assertEqual(rf"{UnchangedFormat():{r'\xFF'}}", '\\xFF')
1842+
1843+
# Test continuation character in format specs
1844+
self.assertEqual(f"""{UnchangedFormat():{'a'\
1845+
'b'}}""", 'ab')
1846+
self.assertEqual(rf"""{UnchangedFormat():{'a'\
1847+
'b'}}""", 'ab')
1848+
1849+
# Test multiple format specs in same raw f-string
1850+
self.assertEqual(rf"{UnchangedFormat():\xFF} {UnchangedFormat():\n}", '\\xFF \\n')
1851+
18241852

18251853
if __name__ == '__main__':
18261854
unittest.main()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fixed a regression where raw f-strings incorrectly interpreted
2+
escape sequences in format specifications. Raw f-strings now properly preserve
3+
literal backslashes in format specs, matching the behavior from Python 3.11.
4+
For example, ``rf"{obj:\xFF}"`` now correctly produces ``'\\xFF'`` instead of
5+
``'ÿ'``. Patch by Pablo Galindo.

Parser/action_helpers.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1369,7 +1369,15 @@ expr_ty _PyPegen_decoded_constant_from_token(Parser* p, Token* tok) {
13691369
if (PyBytes_AsStringAndSize(tok->bytes, &bstr, &bsize) == -1) {
13701370
return NULL;
13711371
}
1372-
PyObject* str = _PyPegen_decode_string(p, 0, bstr, bsize, tok);
1372+
1373+
// Check if we're inside a raw f-string for format spec decoding
1374+
int is_raw = 0;
1375+
if (INSIDE_FSTRING(p->tok)) {
1376+
tokenizer_mode *mode = TOK_GET_MODE(p->tok);
1377+
is_raw = mode->raw;
1378+
}
1379+
1380+
PyObject* str = _PyPegen_decode_string(p, is_raw, bstr, bsize, tok);
13731381
if (str == NULL) {
13741382
return NULL;
13751383
}

0 commit comments

Comments
 (0)