Skip to content

Commit e5d1e29

Browse files
committed
Merge pull request #64 from brettcannon/fix_input
Add a fixer for input() and raw_input()
2 parents 508d8cd + fac3602 commit e5d1e29

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

libmodernize/fixes/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
'libmodernize.fixes.fix_basestring',
3232
'libmodernize.fixes.fix_dict',
3333
'libmodernize.fixes.fix_filter',
34+
'libmodernize.fixes.fix_input_six',
3435
'libmodernize.fixes.fix_map',
3536
'libmodernize.fixes.fix_metaclass',
3637
'libmodernize.fixes.fix_raise_six',

libmodernize/fixes/fix_input_six.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# This is a derived work of Lib/lib2to3/fixes/fix_input.py and
2+
# Lib/lib2to3/fixes/fix_raw_input.py under the
3+
# copyright of the Python Software Foundation, licensed under the Python
4+
# Software Foundation License 2.
5+
#
6+
# Copyright notice:
7+
#
8+
# Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
9+
# 2011, 2012, 2013, 2014 Python Software Foundation. All rights reserved.
10+
#
11+
# Full license text: http://docs.python.org/3.4/license.html
12+
13+
from lib2to3 import fixer_base
14+
from lib2to3.fixer_util import Call, Name, touch_import
15+
16+
17+
class FixInputSix(fixer_base.ConditionalFix):
18+
19+
BM_compatible = True
20+
order = 'pre'
21+
skip_on = 'six.moves.input'
22+
23+
PATTERN = """
24+
power< (name='input' | name='raw_input')
25+
trailer< '(' [any] ')' > any* >
26+
"""
27+
28+
def transform(self, node, results):
29+
if self.should_skip(node):
30+
return
31+
32+
touch_import(u'six.moves', u'input', node)
33+
name = results['name']
34+
if name.value == 'raw_input':
35+
name.replace(Name('input', prefix=name.prefix))
36+
else:
37+
new_node = node.clone()
38+
new_node.prefix = ''
39+
return Call(Name('eval'), [new_node], prefix=node.prefix)

tests/test_fix_input_six.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from utils import check_on_input
2+
3+
4+
INPUT = ("""\
5+
input()
6+
""", """\
7+
from six.moves import input
8+
eval(input())
9+
""")
10+
11+
INPUT_ARGS = ("""\
12+
input('hello')
13+
""", """\
14+
from six.moves import input
15+
eval(input('hello'))
16+
""")
17+
18+
RAW_INPUT = ("""\
19+
raw_input()
20+
""", """\
21+
from six.moves import input
22+
input()
23+
""")
24+
25+
RAW_INPUT_TRAILER = ("""\
26+
raw_input()[0]
27+
""", """\
28+
from six.moves import input
29+
input()[0]
30+
""")
31+
32+
RAW_INPUT_INPUT = ("""\
33+
raw_input()
34+
input()
35+
""", """\
36+
from six.moves import input
37+
input()
38+
eval(input())
39+
""")
40+
41+
42+
def test_input():
43+
check_on_input(*INPUT)
44+
45+
def test_input_args():
46+
check_on_input(*INPUT_ARGS)
47+
48+
def test_raw_input():
49+
check_on_input(*RAW_INPUT)
50+
51+
def test_raw_input_trailer():
52+
check_on_input(*RAW_INPUT_TRAILER)
53+
54+
def test_raw_input_input():
55+
check_on_input(*RAW_INPUT_INPUT)

0 commit comments

Comments
 (0)