[Python-checkins] cpython: Issue 25180: Fix Tools/parser/unparse.py for f-strings. Patch by Martin Panter.
eric.smith
python-checkins at python.org
Sun Sep 20 21:09:30 CEST 2015
https://hg.python.org/cpython/rev/2fcd2540ab97
changeset: 98089:2fcd2540ab97
user: Eric V. Smith <eric at trueblade.com>
date: Sun Sep 20 15:09:15 2015 -0400
summary:
Issue 25180: Fix Tools/parser/unparse.py for f-strings. Patch by Martin Panter.
files:
Lib/test/test_tools/test_unparse.py | 11 +++-
Tools/parser/unparse.py | 39 +++++++++++++++++
2 files changed, 48 insertions(+), 2 deletions(-)
diff --git a/Lib/test/test_tools/test_unparse.py b/Lib/test/test_tools/test_unparse.py
--- a/Lib/test/test_tools/test_unparse.py
+++ b/Lib/test/test_tools/test_unparse.py
@@ -134,6 +134,15 @@
class UnparseTestCase(ASTTestCase):
# Tests for specific bugs found in earlier versions of unparse
+ def test_fstrings(self):
+ # See issue 25180
+ self.check_roundtrip(r"""f'{f"{0}"*3}'""")
+ self.check_roundtrip(r"""f'{f"{y}"*3}'""")
+ self.check_roundtrip(r"""f'{f"{\'x\'}"*3}'""")
+
+ self.check_roundtrip(r'''f"{r'x' f'{\"s\"}'}"''')
+ self.check_roundtrip(r'''f"{r'x'rf'{\"s\"}'}"''')
+
def test_del_statement(self):
self.check_roundtrip("del x, y, z")
@@ -264,8 +273,6 @@
for d in self.test_directories:
test_dir = os.path.join(basepath, d)
for n in os.listdir(test_dir):
- if n == 'test_fstring.py':
- continue
if n.endswith('.py') and not n.startswith('bad'):
names.append(os.path.join(test_dir, n))
diff --git a/Tools/parser/unparse.py b/Tools/parser/unparse.py
--- a/Tools/parser/unparse.py
+++ b/Tools/parser/unparse.py
@@ -322,6 +322,45 @@
def _Str(self, tree):
self.write(repr(tree.s))
+ def _JoinedStr(self, t):
+ self.write("f")
+ string = io.StringIO()
+ self._fstring_JoinedStr(t, string.write)
+ self.write(repr(string.getvalue()))
+
+ def _FormattedValue(self, t):
+ self.write("f")
+ string = io.StringIO()
+ self._fstring_FormattedValue(t, string.write)
+ self.write(repr(string.getvalue()))
+
+ def _fstring_JoinedStr(self, t, write):
+ for value in t.values:
+ meth = getattr(self, "_fstring_" + type(value).__name__)
+ meth(value, write)
+
+ def _fstring_Str(self, t, write):
+ value = t.s.replace("{", "{{").replace("}", "}}")
+ write(value)
+
+ def _fstring_FormattedValue(self, t, write):
+ write("{")
+ expr = io.StringIO()
+ Unparser(t.value, expr)
+ expr = expr.getvalue().rstrip("\n")
+ if expr.startswith("{"):
+ write(" ") # Separate pair of opening brackets as "{ {"
+ write(expr)
+ if t.conversion != -1:
+ conversion = chr(t.conversion)
+ assert conversion in "sra"
+ write(f"!{conversion}")
+ if t.format_spec:
+ write(":")
+ meth = getattr(self, "_fstring_" + type(t.format_spec).__name__)
+ meth(t.format_spec, write)
+ write("}")
+
def _Name(self, t):
self.write(t.id)
--
Repository URL: https://hg.python.org/cpython
More information about the Python-checkins
mailing list