diff --git a/src/libfuturize/fixer_util.py b/src/libfuturize/fixer_util.py index ce1e9753..a10184c7 100644 --- a/src/libfuturize/fixer_util.py +++ b/src/libfuturize/fixer_util.py @@ -243,6 +243,7 @@ def future_import(feature, node): # Is it a shebang or encoding line? if is_shebang_comment(node) or is_encoding_comment(node): shebang_encoding_idx = idx + continue if is_docstring(node): # skip over docstring continue @@ -443,7 +444,10 @@ def check_future_import(node): hasattr(node.children[1], 'value') and node.children[1].value == u'__future__'): return set() - node = node.children[3] + if node.children[3].type == token.LPAR: + node = node.children[4] + else: + node = node.children[3] # now node is the import_as_name[s] # print(python_grammar.number2symbol[node.type]) # breaks sometimes if node.type == syms.import_as_names: diff --git a/tests/test_future/test_futurize.py b/tests/test_future/test_futurize.py index f3fe0b84..d46f9bc4 100644 --- a/tests/test_future/test_futurize.py +++ b/tests/test_future/test_futurize.py @@ -123,6 +123,17 @@ def test_encoding_comments_kept_at_top(self): """ self.convert_check(before, after) + def test_multiline_future_import(self): + """ + Issue #113: don't crash if a future import has multiple lines + """ + text = """ + from __future__ import ( + division + ) + """ + self.convert(text) + def test_shebang_blank_with_future_division_import(self): """ Issue #43: Is shebang line preserved as the first