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

This commit is contained in:
Miss Islington (bot) 2024-10-06 15:39:47 +02:00 committed by GitHub
parent b87aea6b0d
commit b30da225cf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 23 additions and 1 deletions

View file

@ -1919,6 +1919,26 @@ class TestRoundtrip(TestCase):
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:

View file

@ -200,7 +200,7 @@ class Untokenizer:
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

View file

@ -0,0 +1,2 @@
Fix :func:`tokenize.untokenize` producing invalid syntax for
double braces preceded by certain escape characters.