https://github.com/python/cpython/commit/b30da225cfe86f22d0d838af2c15908277191608
commit: b30da225cfe86f22d0d838af2c15908277191608
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: pablogsal <[email protected]>
date: 2024-10-06T13:39:47Z
summary:
[3.13] gh-125008: Fix `tokenize.untokenize` roundtrip for `\n{{` (GH-125013)
(#125020)
files:
A
Misc/NEWS.d/next/Core_and_Builtins/2024-10-05-23-53-06.gh-issue-125008.ETANpd.rst
M Lib/test/test_tokenize.py
M Lib/tokenize.py
diff --git a/Lib/test/test_tokenize.py b/Lib/test/test_tokenize.py
index de0e0b430a21bf..75710db7d05375 100644
--- a/Lib/test/test_tokenize.py
+++ b/Lib/test/test_tokenize.py
@@ -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:
diff --git a/Lib/tokenize.py b/Lib/tokenize.py
index 7f418bb7a1b37f..4b4c3cfe16999b 100644
--- a/Lib/tokenize.py
+++ b/Lib/tokenize.py
@@ -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
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
index 00000000000000..8971e052860225
--- /dev/null
+++
b/Misc/NEWS.d/next/Core_and_Builtins/2024-10-05-23-53-06.gh-issue-125008.ETANpd.rst
@@ -0,0 +1,2 @@
+Fix :func:`tokenize.untokenize` producing invalid syntax for
+double braces preceded by certain escape characters.
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]