Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.13] gh-125008: Fix tokenize.untokenize roundtrip for \n{{ (GH-125013) #125020

Merged
merged 1 commit into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Lib/test/test_tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -1919,6 +1919,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 @@ -200,7 +200,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.
Loading