diff options
author | Shantanu <12621235+hauntsaninja@users.noreply.github.com> | 2025-01-31 08:49:06 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-01-31 08:49:06 (GMT) |
commit | 8df5193d37f70a1478642c4b456dcc7d6df6c117 (patch) | |
tree | e81c499c63bab25018ac819ba7b67cbb123220a6 | |
parent | 95504f429eec04010d0b815345ebcc3af2402af0 (diff) | |
download | cpython-8df5193d37f70a1478642c4b456dcc7d6df6c117.zip cpython-8df5193d37f70a1478642c4b456dcc7d6df6c117.tar.gz cpython-8df5193d37f70a1478642c4b456dcc7d6df6c117.tar.bz2 |
gh-127975: Avoid reusing quote types in ast.unparse if not needed (#127980)
-rw-r--r-- | Lib/ast.py | 11 | ||||
-rw-r--r-- | Lib/test/test_unparse.py | 10 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2024-12-20-08-44-12.gh-issue-127975.8HJwu9.rst | 1 |
3 files changed, 15 insertions, 7 deletions
@@ -1196,9 +1196,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: diff --git a/Lib/test/test_unparse.py b/Lib/test/test_unparse.py index 3329195..f6c4f1f 100644 --- a/Lib/test/test_unparse.py +++ b/Lib/test/test_unparse.py @@ -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 = ( 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 0000000..597fa41 --- /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. |