[Python-checkins] r53944 - in sandbox/trunk/2to3: example.py fixer_tests.py fixes/fix_input.py fixes/fix_raw_input.py

guido.van.rossum python-checkins at python.org
Mon Feb 26 17:44:07 CET 2007


Author: guido.van.rossum
Date: Mon Feb 26 17:44:06 2007
New Revision: 53944

Added:
   sandbox/trunk/2to3/fixes/fix_input.py   (contents, props changed)
   sandbox/trunk/2to3/fixes/fix_raw_input.py   (contents, props changed)
Modified:
   sandbox/trunk/2to3/example.py
   sandbox/trunk/2to3/fixer_tests.py
Log:
Fixers for input() and raw_input() by Andre Roberge, with unittests.
Unittest for xrange() -> range() fixer, by Andre Roberge.


Modified: sandbox/trunk/2to3/example.py
==============================================================================
--- sandbox/trunk/2to3/example.py	(original)
+++ sandbox/trunk/2to3/example.py	Mon Feb 26 17:44:06 2007
@@ -319,4 +319,12 @@
     for i in xrange(0, 100): print i
     for i in xrange(0, 100, 10): print i
 
+def input_examples():
+    a = input()
+    b = input(str(a))
+
+def raw_input_examples():
+    a = raw_input()
+    b = raw_input(a.rstrip())
+
 # This is the last line.

Modified: sandbox/trunk/2to3/fixer_tests.py
==============================================================================
--- sandbox/trunk/2to3/fixer_tests.py	(original)
+++ sandbox/trunk/2to3/fixer_tests.py	Mon Feb 26 17:44:06 2007
@@ -1057,6 +1057,66 @@
         a = "for x in list(h.keys())[0]: print x"
         self.check(b, a)
 
+class Test_xrange(FixerTestCase):
+    fixer = "xrange"
+    
+    def test_1(self):
+        b = """x = xrange(10)"""
+        a = """x = range(10)"""
+        self.check(b, a)
+    
+    def test_2(self):
+        b = """x = xrange(1, 10)"""
+        a = """x = range(1, 10)"""
+        self.check(b, a)
+    
+    def test_3(self):
+        b = """x = xrange(0, 10, 2)"""
+        a = """x = range(0, 10, 2)"""
+        self.check(b, a)
+    
+    def test_4(self):
+        b = """for i in xrange(10):\n    j=i"""
+        a = """for i in range(10):\n    j=i"""
+        self.check(b, a)
+
+
+class Test_raw_input(FixerTestCase):
+    fixer = "raw_input"
+    
+    def test_1(self):
+        b = """x = raw_input()"""
+        a = """x = input()"""
+        self.check(b, a)
+    
+    def test_2(self):
+        b = """x = raw_input('')"""
+        a = """x = input('')"""
+        self.check(b, a)
+    
+    def test_3(self):
+        b = """x = raw_input('prompt')"""
+        a = """x = input('prompt')"""
+        self.check(b, a)
+
+
+class Test_input(FixerTestCase):
+    fixer = "input"
+    
+    def test_1(self):
+        b = """x = input()"""
+        a = """x = eval(input())"""
+        self.check(b, a)
+    
+    def test_2(self):
+        b = """x = input('')"""
+        a = """x = eval(input(''))"""
+        self.check(b, a)
+    
+    def test_3(self):
+        b = """x = input('prompt')"""
+        a = """x = eval(input('prompt'))"""
+        self.check(b, a)
 
 
 if __name__ == "__main__":

Added: sandbox/trunk/2to3/fixes/fix_input.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/2to3/fixes/fix_input.py	Mon Feb 26 17:44:06 2007
@@ -0,0 +1,25 @@
+"""Fixer that changes input(...) into eval(input(...))."""
+# Author: Andre Roberge
+
+# Local imports
+import pytree
+import patcomp
+from pgen2 import token
+from fixes import basefix
+from fixes import macros
+
+class FixInput(basefix.BaseFix):
+
+    PATTERN = """
+    power<
+        'input'
+        args=trailer< '(' [any] ')' >
+    >
+    """
+
+    def transform(self, node):
+        new = node.clone()
+        new.set_prefix("")
+        new = macros.Call(macros.Name("eval"), [new])
+        new.set_prefix(node.get_prefix())
+        return new

Added: sandbox/trunk/2to3/fixes/fix_raw_input.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/2to3/fixes/fix_raw_input.py	Mon Feb 26 17:44:06 2007
@@ -0,0 +1,25 @@
+"""Fixer that changes raw_input(...) into input(...)."""
+# Author: Andre Roberge
+
+# Local imports
+import pytree
+from pgen2 import token
+from fixes import basefix
+from fixes import macros
+
+class FixRawInput(basefix.BaseFix):
+
+    PATTERN = """
+    power<
+        'raw_input'
+        args=trailer< '(' [any] ')' >
+    >
+    """
+
+    def transform(self, node):
+        results = self.match(node)
+        args = results["args"]
+        new = pytree.Node(self.syms.power,
+                          [macros.Name("input"), args.clone()])
+        new.set_prefix(node.get_prefix())
+        return new


More information about the Python-checkins mailing list