https://github.com/python/cpython/commit/97097469c7782ad0e8d18cb2e0bb42b8bd24dd18
commit: 97097469c7782ad0e8d18cb2e0bb42b8bd24dd18
branch: 3.12
author: Miss Islington (bot) <[email protected]>
committer: hauntsaninja <[email protected]>
date: 2025-02-02T22:58:22Z
summary:

[3.12] gh-127975: Avoid reusing quote types in ast.unparse if not needed 
(GH-127980) (#129601)

gh-127975: Avoid reusing quote types in ast.unparse if not needed (GH-127980)
(cherry picked from commit 8df5193d37f70a1478642c4b456dcc7d6df6c117)

Co-authored-by: Shantanu <[email protected]>

files:
A Misc/NEWS.d/next/Library/2024-12-20-08-44-12.gh-issue-127975.8HJwu9.rst
M Lib/ast.py
M Lib/test/test_unparse.py

diff --git a/Lib/ast.py b/Lib/ast.py
index b0995fa7f10902..6d9785cc48e40c 100644
--- a/Lib/ast.py
+++ b/Lib/ast.py
@@ -1246,9 +1246,14 @@ def visit_JoinedStr(self, node):
                     fallback_to_repr = True
                     break
                 quote_types = new_quote_types
-            elif "\n" in value:
-                quote_types = [q for q in quote_types if q in _MULTI_QUOTES]
-                assert quote_types
+            else:
+                if "\n" in value:
+                    quote_types = [q for q in quote_types if q in 
_MULTI_QUOTES]
+                    assert quote_types
+
+                new_quote_types = [q for q in quote_types if q not in value]
+                if new_quote_types:
+                    quote_types = new_quote_types
             new_fstring_parts.append(value)
 
         if fallback_to_repr:
diff --git a/Lib/test/test_unparse.py b/Lib/test/test_unparse.py
index 106704ba8c9c2d..496ccce261aa3e 100644
--- a/Lib/test/test_unparse.py
+++ b/Lib/test/test_unparse.py
@@ -513,11 +513,13 @@ def test_class_bases_and_keywords(self):
         self.check_src_roundtrip("class X(*args, **kwargs):\n    pass")
 
     def test_fstrings(self):
-        self.check_src_roundtrip("f'-{f'*{f'+{f'.{x}.'}+'}*'}-'")
-        self.check_src_roundtrip("f'\\u2028{'x'}'")
+        
self.check_src_roundtrip('''f\'\'\'-{f"""*{f"+{f'.{x}.'}+"}*"""}-\'\'\'''')
+        
self.check_src_roundtrip('''f\'-{f\'\'\'*{f"""+{f".{f'{x}'}."}+"""}*\'\'\'}-\'''')
+        
self.check_src_roundtrip('''f\'-{f\'*{f\'\'\'+{f""".{f"{f'{x}'}"}."""}+\'\'\'}*\'}-\'''')
+        self.check_src_roundtrip('''f"\\u2028{'x'}"''')
         self.check_src_roundtrip(r"f'{x}\n'")
-        self.check_src_roundtrip("f'{'\\n'}\\n'")
-        self.check_src_roundtrip("f'{f'{x}\\n'}\\n'")
+        self.check_src_roundtrip('''f"{'\\n'}\\n"''')
+        self.check_src_roundtrip('''f"{f'{x}\\n'}\\n"''')
 
     def test_docstrings(self):
         docstrings = (
diff --git 
a/Misc/NEWS.d/next/Library/2024-12-20-08-44-12.gh-issue-127975.8HJwu9.rst 
b/Misc/NEWS.d/next/Library/2024-12-20-08-44-12.gh-issue-127975.8HJwu9.rst
new file mode 100644
index 00000000000000..597fa41deb811c
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-12-20-08-44-12.gh-issue-127975.8HJwu9.rst
@@ -0,0 +1 @@
+Avoid reusing quote types in :func:`ast.unparse` if not needed.

_______________________________________________
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]

Reply via email to