gh-125008: Fix `tokenize.untokenize` roundtrip for `\n{{` (#125013)

This commit is contained in:
Tomas R. 2024-10-06 15:16:41 +02:00 committed by GitHub
parent 39c859f6ff
commit db23b8bb13
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.