Skip to content

Commit 615ee1e

Browse files
authored
Merge pull request #519 from Ionaru/fix_input_multiple_runs
Add check for input import before running FixInput
2 parents e783561 + d7ff41c commit 615ee1e

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

src/libfuturize/fixes/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
'lib2to3.fixes.fix_getcwdu',
5151
# 'lib2to3.fixes.fix_imports', # called by libfuturize.fixes.fix_future_standard_library
5252
# 'lib2to3.fixes.fix_imports2', # we don't handle this yet (dbm)
53-
'lib2to3.fixes.fix_input',
53+
# 'lib2to3.fixes.fix_input', # Called conditionally by libfuturize.fixes.fix_input
5454
'lib2to3.fixes.fix_itertools',
5555
'lib2to3.fixes.fix_itertools_imports',
5656
'lib2to3.fixes.fix_filter',
@@ -86,6 +86,7 @@
8686
'libfuturize.fixes.fix_future_builtins',
8787
'libfuturize.fixes.fix_future_standard_library',
8888
'libfuturize.fixes.fix_future_standard_library_urllib',
89+
'libfuturize.fixes.fix_input',
8990
'libfuturize.fixes.fix_metaclass',
9091
'libpasteurize.fixes.fix_newstyle',
9192
'libfuturize.fixes.fix_object',

src/libfuturize/fixes/fix_input.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
Fixer for input.
3+
4+
Does a check for `from builtins import input` before running the lib2to3 fixer.
5+
The fixer will not run when the input is already present.
6+
7+
8+
this:
9+
a = input()
10+
becomes:
11+
from builtins import input
12+
a = eval(input())
13+
14+
and this:
15+
from builtins import input
16+
a = input()
17+
becomes (no change):
18+
from builtins import input
19+
a = input()
20+
"""
21+
22+
import lib2to3.fixes.fix_input
23+
from lib2to3.fixer_util import does_tree_import
24+
25+
26+
class FixInput(lib2to3.fixes.fix_input.FixInput):
27+
def transform(self, node, results):
28+
29+
if does_tree_import('builtins', 'input', node):
30+
return
31+
32+
return super(FixInput, self).transform(node, results)

tests/test_future/test_futurize.py

+21
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,27 @@ def test_import_builtins(self):
436436
"""
437437
self.convert_check(before, after, ignore_imports=False, run=False)
438438

439+
def test_input_without_import(self):
440+
before = """
441+
a = input()
442+
"""
443+
after = """
444+
from builtins import input
445+
a = eval(input())
446+
"""
447+
self.convert_check(before, after, ignore_imports=False, run=False)
448+
449+
def test_input_with_import(self):
450+
before = """
451+
from builtins import input
452+
a = input()
453+
"""
454+
after = """
455+
from builtins import input
456+
a = input()
457+
"""
458+
self.convert_check(before, after, ignore_imports=False, run=False)
459+
439460
def test_xrange(self):
440461
"""
441462
The ``from builtins import range`` line was being added to the

0 commit comments

Comments
 (0)