[Python-checkins] r67774 - in sandbox/trunk/2to3/lib2to3: fixes/fix_imports.py tests/test_fixers.py

benjamin.peterson python-checkins at python.org
Sun Dec 14 22:55:38 CET 2008


Author: benjamin.peterson
Date: Sun Dec 14 22:55:38 2008
New Revision: 67774

Log:
employ an evil hack to fix multiple names in the same import statement

Modified:
   sandbox/trunk/2to3/lib2to3/fixes/fix_imports.py
   sandbox/trunk/2to3/lib2to3/tests/test_fixers.py

Modified: sandbox/trunk/2to3/lib2to3/fixes/fix_imports.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/fixes/fix_imports.py	(original)
+++ sandbox/trunk/2to3/lib2to3/fixes/fix_imports.py	Sun Dec 14 22:55:38 2008
@@ -118,11 +118,22 @@
         import_mod = results.get("module_name")
         if import_mod:
             new_name = self.mapping[(import_mod or mod_name).value]
+            import_mod.replace(Name(new_name, prefix=import_mod.get_prefix()))
             if "name_import" in results:
                 # If it's not a "from x import x, y" or "import x as y" import,
                 # marked its usage to be replaced.
                 self.replace[import_mod.value] = new_name
-            import_mod.replace(Name(new_name, prefix=import_mod.get_prefix()))
+
+                # This is a nasty hack to fix multiple imports on a
+                # line. (ie. "import StringIO, urlparse") The problem is that I
+                # can't figure out an easy way to make a pattern recognize the
+                # keys of MAPPING randomly sprinkled in an import statement.
+                while True:
+                    results = self.match(node)
+                    if results:
+                        self.transform(node, results)
+                    else:
+                        break
         else:
             # Replace usage of the module.
             bare_name = results["bare_with_attr"][0]

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	Sun Dec 14 22:55:38 2008
@@ -1467,6 +1467,11 @@
     fixer = "imports"
     from ..fixes.fix_imports import MAPPING as modules
 
+    def test_several_on_a_line(self):
+        b = """import urlparse, cStringIO"""
+        a = """import urllib.parse, io"""
+        self.check(b, a)
+
     def test_import_module(self):
         for old, new in self.modules.items():
             b = "import %s" % old


More information about the Python-checkins mailing list