Skip to content

Commit

Permalink
Merge pull request #519 from Ionaru/fix_input_multiple_runs
Browse files Browse the repository at this point in the history
Add check for input import before running FixInput
  • Loading branch information
jmadler authored Oct 24, 2019
2 parents e783561 + d7ff41c commit 615ee1e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/libfuturize/fixes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
'lib2to3.fixes.fix_getcwdu',
# 'lib2to3.fixes.fix_imports', # called by libfuturize.fixes.fix_future_standard_library
# 'lib2to3.fixes.fix_imports2', # we don't handle this yet (dbm)
'lib2to3.fixes.fix_input',
# 'lib2to3.fixes.fix_input', # Called conditionally by libfuturize.fixes.fix_input
'lib2to3.fixes.fix_itertools',
'lib2to3.fixes.fix_itertools_imports',
'lib2to3.fixes.fix_filter',
Expand Down Expand Up @@ -86,6 +86,7 @@
'libfuturize.fixes.fix_future_builtins',
'libfuturize.fixes.fix_future_standard_library',
'libfuturize.fixes.fix_future_standard_library_urllib',
'libfuturize.fixes.fix_input',
'libfuturize.fixes.fix_metaclass',
'libpasteurize.fixes.fix_newstyle',
'libfuturize.fixes.fix_object',
Expand Down
32 changes: 32 additions & 0 deletions src/libfuturize/fixes/fix_input.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
Fixer for input.
Does a check for `from builtins import input` before running the lib2to3 fixer.
The fixer will not run when the input is already present.
this:
a = input()
becomes:
from builtins import input
a = eval(input())
and this:
from builtins import input
a = input()
becomes (no change):
from builtins import input
a = input()
"""

import lib2to3.fixes.fix_input
from lib2to3.fixer_util import does_tree_import


class FixInput(lib2to3.fixes.fix_input.FixInput):
def transform(self, node, results):

if does_tree_import('builtins', 'input', node):
return

return super(FixInput, self).transform(node, results)
21 changes: 21 additions & 0 deletions tests/test_future/test_futurize.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,27 @@ def test_import_builtins(self):
"""
self.convert_check(before, after, ignore_imports=False, run=False)

def test_input_without_import(self):
before = """
a = input()
"""
after = """
from builtins import input
a = eval(input())
"""
self.convert_check(before, after, ignore_imports=False, run=False)

def test_input_with_import(self):
before = """
from builtins import input
a = input()
"""
after = """
from builtins import input
a = input()
"""
self.convert_check(before, after, ignore_imports=False, run=False)

def test_xrange(self):
"""
The ``from builtins import range`` line was being added to the
Expand Down

0 comments on commit 615ee1e

Please sign in to comment.