Skip to content

Add check for input import before running FixInput #519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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