[Python-checkins] r61654 - in sandbox/trunk/2to3/lib2to3: fixes/basefix.py fixes/fix_future.py fixes/fix_itertools.py fixes/fix_print.py refactor.py tests/test_fixers.py

david.wolever python-checkins at python.org
Thu Mar 20 01:09:57 CET 2008


Author: david.wolever
Date: Thu Mar 20 01:09:56 2008
New Revision: 61654

Modified:
   sandbox/trunk/2to3/lib2to3/fixes/basefix.py
   sandbox/trunk/2to3/lib2to3/fixes/fix_future.py
   sandbox/trunk/2to3/lib2to3/fixes/fix_itertools.py
   sandbox/trunk/2to3/lib2to3/fixes/fix_print.py
   sandbox/trunk/2to3/lib2to3/refactor.py
   sandbox/trunk/2to3/lib2to3/tests/test_fixers.py
Log:
Added an explicit sort order to fixers -- fixes problems like #2427

Modified: sandbox/trunk/2to3/lib2to3/fixes/basefix.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/fixes/basefix.py	(original)
+++ sandbox/trunk/2to3/lib2to3/fixes/basefix.py	Thu Mar 20 01:09:56 2008
@@ -37,6 +37,8 @@
     used_names = set() # A set of all used NAMEs
     order = "post" # Does the fixer prefer pre- or post-order traversal
     explicit = False # Is this ignored by refactor.py -f all?
+    run_order = 5   # Fixers will be sorted by run order before execution
+                    # Lower numbers will be run first.
 
     # Shortcut for access to Python grammar symbols
     syms = pygram.python_symbols

Modified: sandbox/trunk/2to3/lib2to3/fixes/fix_future.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/fixes/fix_future.py	(original)
+++ sandbox/trunk/2to3/lib2to3/fixes/fix_future.py	Thu Mar 20 01:09:56 2008
@@ -11,11 +11,8 @@
 class FixFuture(basefix.BaseFix):
     PATTERN = """import_from< 'from' module_name="__future__" 'import' any >"""
 
-    # While the order that the tree is traversed does not matter,
-    # the postorder traversal fixers are run after the preorder fixers.
-    # Forcing this fixer to run in postorder ensures that fixers which check
-    # for __future__ can run first.
-    order='post'
+    # This should be run last -- some things check for the import
+    run_order = 10
 
     def transform(self, node, results):
         return BlankLine()

Modified: sandbox/trunk/2to3/lib2to3/fixes/fix_itertools.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/fixes/fix_itertools.py	(original)
+++ sandbox/trunk/2to3/lib2to3/fixes/fix_itertools.py	Thu Mar 20 01:09:56 2008
@@ -21,6 +21,9 @@
               power< func=%(it_funcs)s trailer< '(' [any] ')' > >
               """ %(locals())
 
+    # Needs to be run after fix_(map|zip|filter)
+    run_order = 6
+
     def transform(self, node, results):
         prefix = None
         func = results['func'][0]

Modified: sandbox/trunk/2to3/lib2to3/fixes/fix_print.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/fixes/fix_print.py	(original)
+++ sandbox/trunk/2to3/lib2to3/fixes/fix_print.py	Thu Mar 20 01:09:56 2008
@@ -32,11 +32,6 @@
               simple_stmt< bare='print' any > | print_stmt
               """
 
-    # The traversal order does not matter, but the preorder fixers are run
-    # before the post-order fixers, and fix_future (which is defined to be
-    # postorder will remove the __future__ import (so we've got to be run
-    # first)
-    order = 'pre'
     skip_on = '__future__.print_function'
 
     def transform(self, node, results):

Modified: sandbox/trunk/2to3/lib2to3/refactor.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/refactor.py	(original)
+++ sandbox/trunk/2to3/lib2to3/refactor.py	Thu Mar 20 01:09:56 2008
@@ -161,6 +161,9 @@
                 post_order_fixers.append(fixer)
             else:
                 raise ValueError("Illegal fixer order: %r" % fixer.order)
+
+        pre_order_fixers.sort(key=lambda x: x.run_order)
+        post_order_fixers.sort(key=lambda x: x.run_order)
         return (pre_order_fixers, post_order_fixers)
 
     def log_error(self, msg, *args, **kwds):

Modified: sandbox/trunk/2to3/lib2to3/tests/test_fixers.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/tests/test_fixers.py	(original)
+++ sandbox/trunk/2to3/lib2to3/tests/test_fixers.py	Thu Mar 20 01:09:56 2008
@@ -62,6 +62,22 @@
         if not ignore_warnings:
             self.failUnlessEqual(self.fixer_log, [])
 
+    def assert_runs_after(self, *names):
+        fix = [self.fixer]
+        fix.extend(names)
+        options = Options(fix=fix, print_function=False)
+        r = refactor.RefactoringTool(options)
+        (pre, post) = r.get_fixers()
+        n = "fix_" + self.fixer
+        if post and post[-1].__class__.__module__.endswith(n):
+            # We're the last fixer to run
+            return
+        if pre and pre[-1].__class__.__module__.endswith(n) and not post:
+            # We're the last in pre and post is empty
+            return 
+        self.fail("Fixer run order (%s) is incorrect; %s should be last."\
+               %(", ".join([x.__class__.__module__ for x in (pre+post)]), n))
+
 class Test_ne(FixerTestCase):
     fixer = "ne"
 
@@ -2939,6 +2955,10 @@
         a = """"""
         self.check(b, a)
 
+    def test_run_order(self):
+        self.assert_runs_after('print')
+
+
 class Test_itertools(FixerTestCase):
     fixer = "itertools"
 
@@ -2984,6 +3004,9 @@
         a = """    itertools.filterfalse(a, b)"""
         self.check(b, a)
 
+    def test_run_order(self):
+        self.assert_runs_after('map', 'zip', 'filter')
+
 class Test_itertools_imports(FixerTestCase):
     fixer = 'itertools_imports'
 


More information about the Python-checkins mailing list