Skip to content

Commit dd4fc04

Browse files
committed
gh-127975: Avoid reusing quote types in ast.unparse if not needed
1 parent 8b3cccf commit dd4fc04

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

Lib/ast.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,9 +1196,14 @@ def visit_JoinedStr(self, node):
11961196
fallback_to_repr = True
11971197
break
11981198
quote_types = new_quote_types
1199-
elif "\n" in value:
1200-
quote_types = [q for q in quote_types if q in _MULTI_QUOTES]
1201-
assert quote_types
1199+
else:
1200+
if "\n" in value:
1201+
quote_types = [q for q in quote_types if q in _MULTI_QUOTES]
1202+
assert quote_types
1203+
1204+
new_quote_types = [q for q in quote_types if q not in value]
1205+
if new_quote_types:
1206+
quote_types = new_quote_types
12021207
new_fstring_parts.append(value)
12031208

12041209
if fallback_to_repr:

Lib/test/test_unparse.py

Lines changed: 7 additions & 5 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.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 = (
@@ -799,7 +801,7 @@ class DirectoryTestCase(ASTTestCase):
799801
"test_patma.py", "test_type_alias.py", "test_type_params.py",
800802
"test_tokenize.py"}
801803

802-
_files_to_test = None
804+
_files_to_test = []
803805

804806
@classmethod
805807
def files_to_test(cls):

0 commit comments

Comments
 (0)