Skip to content

Commit db4b382

Browse files
[3.12] gh-125008: Fix tokenize.untokenize roundtrip for \n{{ (GH-125013) (#125021)
1 parent b3e2c02 commit db4b382

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

Lib/test/test_tokenize.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,6 +1916,26 @@ def test_roundtrip(self):
19161916
self.check_roundtrip(r"f'\\\\N{{'")
19171917
self.check_roundtrip(r"f'\\\\\\N{{'")
19181918
self.check_roundtrip(r"f'\\\\\\\\N{{'")
1919+
1920+
self.check_roundtrip(r"f'\n{{foo}}'")
1921+
self.check_roundtrip(r"f'\\n{{foo}}'")
1922+
self.check_roundtrip(r"f'\\\n{{foo}}'")
1923+
self.check_roundtrip(r"f'\\\\n{{foo}}'")
1924+
1925+
self.check_roundtrip(r"f'\t{{foo}}'")
1926+
self.check_roundtrip(r"f'\\t{{foo}}'")
1927+
self.check_roundtrip(r"f'\\\t{{foo}}'")
1928+
self.check_roundtrip(r"f'\\\\t{{foo}}'")
1929+
1930+
self.check_roundtrip(r"rf'\t{{foo}}'")
1931+
self.check_roundtrip(r"rf'\\t{{foo}}'")
1932+
self.check_roundtrip(r"rf'\\\t{{foo}}'")
1933+
self.check_roundtrip(r"rf'\\\\t{{foo}}'")
1934+
1935+
self.check_roundtrip(r"rf'\{{foo}}'")
1936+
self.check_roundtrip(r"f'\\{{foo}}'")
1937+
self.check_roundtrip(r"rf'\\\{{foo}}'")
1938+
self.check_roundtrip(r"f'\\\\{{foo}}'")
19191939
cases = [
19201940
"""
19211941
if 1:

Lib/tokenize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def escape_brackets(self, token):
202202
characters[-2::-1]
203203
)
204204
)
205-
if n_backslashes % 2 == 0:
205+
if n_backslashes % 2 == 0 or characters[-1] != "N":
206206
characters.append(character)
207207
else:
208208
consume_until_next_bracket = True
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :func:`tokenize.untokenize` producing invalid syntax for
2+
double braces preceded by certain escape characters.

0 commit comments

Comments
 (0)