[Python-checkins] r57917 - in sandbox/trunk/2to3: fixes/fix_input.py tests/test_fixers.py

collin.winter python-checkins at python.org
Mon Sep 3 07:55:44 CEST 2007


Author: collin.winter
Date: Mon Sep  3 07:55:44 2007
New Revision: 57917

Modified:
   sandbox/trunk/2to3/fixes/fix_input.py
   sandbox/trunk/2to3/tests/test_fixers.py
Log:
Make fix_input idempotent.

Modified: sandbox/trunk/2to3/fixes/fix_input.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_input.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_input.py	Mon Sep  3 07:55:44 2007
@@ -4,17 +4,23 @@
 # Local imports
 from fixes import basefix
 from fixes.util import Call, Name
+import patcomp
+
+
+context = patcomp.compile_pattern("power< 'eval' trailer< '(' any ')' > >")
+
 
 class FixInput(basefix.BaseFix):
 
     PATTERN = """
-    power<
-        'input'
-        args=trailer< '(' [any] ')' >
-    >
-    """
+              power< 'input' args=trailer< '(' [any] ')' > >
+              """
 
     def transform(self, node, results):
+        # If we're already wrapped in a eval() call, we're done.
+        if context.match(node.parent.parent):
+            return
+
         new = node.clone()
         new.set_prefix("")
         return Call(Name("eval"), [new], prefix=node.get_prefix())

Modified: sandbox/trunk/2to3/tests/test_fixers.py
==============================================================================
--- sandbox/trunk/2to3/tests/test_fixers.py	(original)
+++ sandbox/trunk/2to3/tests/test_fixers.py	Mon Sep  3 07:55:44 2007
@@ -1377,6 +1377,16 @@
         a = """x = eval(input())  #  foo"""
         self.check(b, a)
 
+    def test_idempotency(self):
+        s = """x = eval(input())"""
+        self.unchanged(s)
+
+        s = """x = eval(input(''))"""
+        self.unchanged(s)
+
+        s = """x = eval(input(foo(5) + 9))"""
+        self.unchanged(s)
+
     def test_1(self):
         b = """x = input()"""
         a = """x = eval(input())"""


More information about the Python-checkins mailing list