mirror of
https://github.com/python/cpython.git
synced 2025-08-27 04:05:34 +00:00
gh-127975: Avoid reusing quote types in ast.unparse if not needed (#127980)
This commit is contained in:
parent
95504f429e
commit
8df5193d37
3 changed files with 15 additions and 7 deletions
11
Lib/ast.py
11
Lib/ast.py
|
@ -1196,9 +1196,14 @@ class _Unparser(NodeVisitor):
|
||||||
fallback_to_repr = True
|
fallback_to_repr = True
|
||||||
break
|
break
|
||||||
quote_types = new_quote_types
|
quote_types = new_quote_types
|
||||||
elif "\n" in value:
|
else:
|
||||||
quote_types = [q for q in quote_types if q in _MULTI_QUOTES]
|
if "\n" in value:
|
||||||
assert quote_types
|
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)
|
new_fstring_parts.append(value)
|
||||||
|
|
||||||
if fallback_to_repr:
|
if fallback_to_repr:
|
||||||
|
|
|
@ -513,11 +513,13 @@ class CosmeticTestCase(ASTTestCase):
|
||||||
self.check_src_roundtrip("class X(*args, **kwargs):\n pass")
|
self.check_src_roundtrip("class X(*args, **kwargs):\n pass")
|
||||||
|
|
||||||
def test_fstrings(self):
|
def test_fstrings(self):
|
||||||
self.check_src_roundtrip("f'-{f'*{f'+{f'.{x}.'}+'}*'}-'")
|
self.check_src_roundtrip('''f\'\'\'-{f"""*{f"+{f'.{x}.'}+"}*"""}-\'\'\'''')
|
||||||
self.check_src_roundtrip("f'\\u2028{'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(r"f'{x}\n'")
|
||||||
self.check_src_roundtrip("f'{'\\n'}\\n'")
|
self.check_src_roundtrip('''f"{'\\n'}\\n"''')
|
||||||
self.check_src_roundtrip("f'{f'{x}\\n'}\\n'")
|
self.check_src_roundtrip('''f"{f'{x}\\n'}\\n"''')
|
||||||
|
|
||||||
def test_docstrings(self):
|
def test_docstrings(self):
|
||||||
docstrings = (
|
docstrings = (
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Avoid reusing quote types in :func:`ast.unparse` if not needed.
|
Loading…
Add table
Add a link
Reference in a new issue