diff options
author | Batuhan Taskaya <batuhanosmantaskaya@gmail.com> | 2020-05-03 17:11:51 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-03 17:11:51 (GMT) |
commit | 493bf1cc316b0b5bd90779ecd1132878c881669e (patch) | |
tree | b9e2d706bd6124f163d78b663208e16cccd802cd /Lib/ast.py | |
parent | 3dd2157febae5087ca3333d24f69b6de9cbd13cd (diff) | |
download | cpython-493bf1cc316b0b5bd90779ecd1132878c881669e.zip cpython-493bf1cc316b0b5bd90779ecd1132878c881669e.tar.gz cpython-493bf1cc316b0b5bd90779ecd1132878c881669e.tar.bz2 |
bpo-38870: Don't start generated output with newlines in ast.unparse (GH-19636)
Diffstat (limited to 'Lib/ast.py')
-rw-r--r-- | Lib/ast.py | 14 |
1 files changed, 10 insertions, 4 deletions
@@ -669,10 +669,16 @@ class _Unparser(NodeVisitor): else: self.interleave(lambda: self.write(", "), traverser, items) + def maybe_newline(self): + """Adds a newline if it isn't the start of generated source""" + if self._source: + self.write("\n") + def fill(self, text=""): """Indent a piece of text and append it, according to the current indentation level""" - self.write("\n" + " " * self._indent + text) + self.maybe_newline() + self.write(" " * self._indent + text) def write(self, text): """Append a piece of text""" @@ -916,7 +922,7 @@ class _Unparser(NodeVisitor): self.traverse(node.body) def visit_ClassDef(self, node): - self.write("\n") + self.maybe_newline() for deco in node.decorator_list: self.fill("@") self.traverse(deco) @@ -946,7 +952,7 @@ class _Unparser(NodeVisitor): self._function_helper(node, "async def") def _function_helper(self, node, fill_suffix): - self.write("\n") + self.maybe_newline() for deco in node.decorator_list: self.fill("@") self.traverse(deco) @@ -1043,7 +1049,7 @@ class _Unparser(NodeVisitor): write("{") unparser = type(self)() unparser.set_precedence(_Precedence.TEST.next(), node.value) - expr = unparser.visit(node.value).rstrip("\n") + expr = unparser.visit(node.value) if expr.startswith("{"): write(" ") # Separate pair of opening brackets as "{ {" write(expr) |