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

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

Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2025-02-03 01:38:44 +01:00 committed by GitHub
parent d75e9a0954
commit b13b76f98a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 15 additions and 7 deletions

View file

@ -1275,9 +1275,14 @@ class _Unparser(NodeVisitor):
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:

View file

@ -513,11 +513,13 @@ class CosmeticTestCase(ASTTestCase):
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 = (

View file

@ -0,0 +1 @@
Avoid reusing quote types in :func:`ast.unparse` if not needed.