Skip to content

Commit db23b8b

Browse files
authored
gh-125008: Fix tokenize.untokenize roundtrip for \n{{ (#125013)
1 parent 39c859f commit db23b8b

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
@@ -1919,6 +1919,26 @@ def test_roundtrip(self):
19191919
self.check_roundtrip(r"f'\\\\N{{'")
19201920
self.check_roundtrip(r"f'\\\\\\N{{'")
19211921
self.check_roundtrip(r"f'\\\\\\\\N{{'")
1922+
1923+
self.check_roundtrip(r"f'\n{{foo}}'")
1924+
self.check_roundtrip(r"f'\\n{{foo}}'")
1925+
self.check_roundtrip(r"f'\\\n{{foo}}'")
1926+
self.check_roundtrip(r"f'\\\\n{{foo}}'")
1927+
1928+
self.check_roundtrip(r"f'\t{{foo}}'")
1929+
self.check_roundtrip(r"f'\\t{{foo}}'")
1930+
self.check_roundtrip(r"f'\\\t{{foo}}'")
1931+
self.check_roundtrip(r"f'\\\\t{{foo}}'")
1932+
1933+
self.check_roundtrip(r"rf'\t{{foo}}'")
1934+
self.check_roundtrip(r"rf'\\t{{foo}}'")
1935+
self.check_roundtrip(r"rf'\\\t{{foo}}'")
1936+
self.check_roundtrip(r"rf'\\\\t{{foo}}'")
1937+
1938+
self.check_roundtrip(r"rf'\{{foo}}'")
1939+
self.check_roundtrip(r"f'\\{{foo}}'")
1940+
self.check_roundtrip(r"rf'\\\{{foo}}'")
1941+
self.check_roundtrip(r"f'\\\\{{foo}}'")
19221942
cases = [
19231943
"""
19241944
if 1:

Lib/tokenize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def escape_brackets(self, token):
200200
characters[-2::-1]
201201
)
202202
)
203-
if n_backslashes % 2 == 0:
203+
if n_backslashes % 2 == 0 or characters[-1] != "N":
204204
characters.append(character)
205205
else:
206206
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)