Skip to content

Commit

Permalink
pythongh-125008: Fix tokenize.untokenize roundtrip for \n{{ (pyth…
Browse files Browse the repository at this point in the history
…onGH-125013)

(cherry picked from commit db23b8b)

Co-authored-by: Tomas R. <[email protected]>
  • Loading branch information
tomasr8 authored and miss-islington committed Oct 6, 2024
1 parent b3e2c02 commit acc2968
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
20 changes: 20 additions & 0 deletions Lib/test/test_tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -1916,6 +1916,26 @@ def test_roundtrip(self):
self.check_roundtrip(r"f'\\\\N{{'")
self.check_roundtrip(r"f'\\\\\\N{{'")
self.check_roundtrip(r"f'\\\\\\\\N{{'")

self.check_roundtrip(r"f'\n{{foo}}'")
self.check_roundtrip(r"f'\\n{{foo}}'")
self.check_roundtrip(r"f'\\\n{{foo}}'")
self.check_roundtrip(r"f'\\\\n{{foo}}'")

self.check_roundtrip(r"f'\t{{foo}}'")
self.check_roundtrip(r"f'\\t{{foo}}'")
self.check_roundtrip(r"f'\\\t{{foo}}'")
self.check_roundtrip(r"f'\\\\t{{foo}}'")

self.check_roundtrip(r"rf'\t{{foo}}'")
self.check_roundtrip(r"rf'\\t{{foo}}'")
self.check_roundtrip(r"rf'\\\t{{foo}}'")
self.check_roundtrip(r"rf'\\\\t{{foo}}'")

self.check_roundtrip(r"rf'\{{foo}}'")
self.check_roundtrip(r"f'\\{{foo}}'")
self.check_roundtrip(r"rf'\\\{{foo}}'")
self.check_roundtrip(r"f'\\\\{{foo}}'")
cases = [
"""
if 1:
Expand Down
2 changes: 1 addition & 1 deletion Lib/tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def escape_brackets(self, token):
characters[-2::-1]
)
)
if n_backslashes % 2 == 0:
if n_backslashes % 2 == 0 or characters[-1] != "N":
characters.append(character)
else:
consume_until_next_bracket = True
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :func:`tokenize.untokenize` producing invalid syntax for
double braces preceded by certain escape characters.

0 comments on commit acc2968

Please sign in to comment.