8000 [3.13] gh-127975: Avoid reusing quote types in ast.unparse if not nee… · python/cpython@b13b76f · GitHub
[go: up one dir, main page]

Skip to content

Commit b13b76f

Browse files
[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 8df5193) Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
1 parent d75e9a0 commit b13b76f

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

Lib/ast.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,9 +1275,14 @@ def visit_JoinedStr(self, node):
12751275
fallback_to_repr = True
12761276
break
12771277
quote_types = new_quote_types
1278-
elif "\n" in value:
1279-
quote_types = [q for q in quote_types if q in _MULTI_QUOTES]
1280-
assert quote_types
1278+
else:
1279+
if "\n" in value:
1280+
quote_types = [q for q in quote_types if q in _MULTI_QUOTES]
1281+
assert quote_types
1282+
1283+
new_quote_types = [q for q in quote_types if q not in value]
1284+
if new_quote_types:
1285+
quote_types = new_quote_types
12811286
new_fstring_parts.append(value)
12821287

12831288
if fallback_to_repr:

Lib/test/test_unparse.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,11 +513,13 @@ def test_class_bases_and_keywords(self):
513513
self.check_src_roundtrip("class X(*args, **kwargs):\n pass")
514514

515515
def test_fstrings(self):
516-
self.check_src_roundtrip("f'-{f'*{f'+{f'.{x}.'}+'}*'}-'")
517-
self.check_src_roundtrip("f'\\u2028{'x'}'")
516+
self.check_src_roundtrip('''f\'\'\'-{f"""*{f"+{f'.{x}.'}+"}*"""}-\'\'\'''')
517+
self 8000 .check_src_roundtrip('''f\'-{f\'\'\'*{f"""+{f".{f'{x}'}."}+"""}*\'\'\'}-\'''')
518+
self.check_src_roundtrip('''f\'-{f\'*{f\'\'\'+{f""".{f"{f'{x}'}"}."""}+\'\'\'}*\'}-\'''')
519+
self.check_src_roundtrip('''f"\\u2028{'x'}"''')
518520
self.check_src_roundtrip(r"f'{x}\n'")
519-
self.check_src_roundtrip("f'{'\\n'}\\n'")
520-
self.check_src_roundtrip("f'{f'{x}\\n'}\\n'")
521+
self.check_src_roundtrip('''f"{'\\n'}\\n"''')
522+
self.check_src_roundtrip('''f"{f'{x}\\n'}\\n"''')
521523

522524
def test_docstrings(self):
523525
docstrings = (
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Avoid reusing quote types in :func:`ast.unparse` if not needed.

0 commit comments

Comments
 (0)
0