]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-125008: Fix `tokenize.untokenize` roundtrip for `\n{{` (GH-125013) (#125021)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sun, 6 Oct 2024 13:34:24 +0000 (15:34 +0200)
committerGitHub <noreply@github.com>
Sun, 6 Oct 2024 13:34:24 +0000 (13:34 +0000)
Lib/test/test_tokenize.py
Lib/tokenize.py
Misc/NEWS.d/next/Core_and_Builtins/2024-10-05-23-53-06.gh-issue-125008.ETANpd.rst [new file with mode: 0644]

index 84741e308bffae971807c46a40bfec1d1f16c2ea..2c4e7b960f7273be64252e861edd6dec0a4470ce 100644 (file)
@@ -1916,6 +1916,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:
index 7af7a5cc1cd6809d9cb532c96c4f94367ce9296c..b2dff8e69670941f528e584ec7e1daa131fc6307 100644 (file)
@@ -202,7 +202,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
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-10-05-23-53-06.gh-issue-125008.ETANpd.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-10-05-23-53-06.gh-issue-125008.ETANpd.rst
new file mode 100644 (file)
index 0000000..8971e05
--- /dev/null
@@ -0,0 +1,2 @@
+Fix :func:`tokenize.untokenize` producing invalid syntax for
+double braces preceded by certain escape characters.