Skip to content

Commit 1eee2cf

Browse files
committed
futurize: Prevent any encoding or shebang prefixes to print statements from appearing twice in the output
1 parent d5418a4 commit 1eee2cf

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/libfuturize/fixer_util.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def future_import(feature, node):
200200
shebang_encoding_idx = None
201201

202202
for idx, node in enumerate(root.children):
203-
# If it's a shebang or encoding line, attach the prefix to
203+
# Is it a shebang or encoding line?
204204
if is_shebang_comment(node) or is_encoding_comment(node):
205205
shebang_encoding_idx = idx
206206
if node.type == syms.simple_stmt and \
@@ -218,11 +218,13 @@ def future_import(feature, node):
218218
import_ = FromImport(u'__future__', [Leaf(token.NAME, feature, prefix=" ")])
219219
if shebang_encoding_idx == 0 and idx == 0:
220220
# If this __future__ import would go on the first line,
221-
# detach the shebang / encoding prefix from the current first line
221+
# detach the shebang / encoding prefix from the current first line.
222222
# and attach it to our new __future__ import node.
223-
import_.prefix = root.children[0].prefix
224-
root.children[0].prefix = u''
225-
children = [import_, Newline()]
223+
import_.set_prefix(root.children[0].get_prefix())
224+
root.children[0].set_prefix(u'')
225+
# End the __future__ import line with a newline and add a blank line
226+
# afterwards:
227+
children = [import_ , Newline()]
226228
root.insert_child(idx, Node(syms.simple_stmt, children))
227229

228230

src/libfuturize/fixes/fix_print_with_import.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
class FixPrintWithImport(FixPrint):
1515
run_order = 7
1616
def transform(self, node, results):
17-
n_stmt = super(FixPrintWithImport, self).transform(node, results)
17+
# Add the __future__ import first. (Otherwise any shebang or encoding
18+
# comment line attached as a prefix to the print statement will be
19+
# copied twice and appear twice.)
1820
future_import(u'print_function', node)
21+
n_stmt = super(FixPrintWithImport, self).transform(node, results)
1922
return n_stmt
2023

0 commit comments

Comments
 (0)