Skip to content

Commit

Permalink
Fix trailing newlines in f-strings
Browse files Browse the repository at this point in the history
If an f-string was ended with a newline character (more precisely, if
the last element of a JoinedStr node was just a newline character), its
round-trip source code representation would have an extra newline on a
wrong place, resulting in invalid syntax.

The bug affects only python>=3.6 (f-strings weren't supported before)

This commit fixes the aforementioned bug.

Fixes berkerpeksag#89.
  • Loading branch information
radomirbosak committed May 5, 2018
1 parent b47718f commit bc28b5a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions astor/code_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,9 @@ def recurse(node):

index = len(result)
recurse(node)

# Flush trailing newlines (so that they are part of mystr)
self.write('')
mystr = ''.join(result[index:])
del result[index:]
self.colinfo = res_index, str_index # Put it back like we found it
Expand Down
6 changes: 6 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ New features

.. _`Issue 86`: https://github.com/berkerpeksag/astor/issues/86

Bug fixes
~~~~~~~~~

* Fix function, class and module docstrings for Python 3.7

Starting Python 3.7.0 the docstrings `are stored <https://bugs.python.org/issue29463>`_ in a new ``docstring`` attribute of the AST node instead of the first expression in code body. This change wasn't taken into account in ``astor.code_gen``, which resulted in ``astor.code_gen.to_source`` omitting the docstring in Python 3.7+.

0.6.2 - 2017-11-11
------------------
Expand Down
9 changes: 9 additions & 0 deletions tests/test_code_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,15 @@ def test_non_string_leakage(self):
'''
self.assertAstRoundtrips(source)

def test_fstring_trailing_newline(self):
"""
Check that trailing newlines in f-strings are printed correctly
"""
source = '''
x = f"""{host}\n\t{port}\n"""
'''
self.assertSrcRoundtripsGtVer(source, (3, 6))


if __name__ == '__main__':
unittest.main()

0 comments on commit bc28b5a

Please sign in to comment.