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

benjamin.peterson python-checkins at python.org
Sun Aug 8 01:55:28 CEST 2010


Author: benjamin.peterson
Date: Sun Aug  8 01:55:28 2010
New Revision: 83798

Log:
when splitting import statements, use correct indentation #9386

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

Modified: sandbox/trunk/2to3/lib2to3/fixes/fix_urllib.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/fixes/fix_urllib.py	(original)
+++ sandbox/trunk/2to3/lib2to3/fixes/fix_urllib.py	Sun Aug  8 01:55:28 2010
@@ -7,7 +7,8 @@
 # Local imports
 from .fix_imports import alternates, FixImports
 from .. import fixer_base
-from ..fixer_util import Name, Comma, FromImport, Newline, attr_chain
+from ..fixer_util import (Name, Comma, FromImport, Newline, attr_chain,
+                          find_indentation)
 
 MAPPING = {'urllib':  [
                 ('urllib.request',
@@ -133,13 +134,17 @@
                                 modules.append(change[0])
 
             new_nodes = []
-            for module in modules:
+            indentation = find_indentation(node)
+            for i, module in enumerate(modules):
                 elts = mod_dict[module]
                 names = []
                 for elt in elts[:-1]:
                     names.extend([Name(elt, prefix=pref), Comma()])
                 names.append(Name(elts[-1], prefix=pref))
-                new_nodes.append(FromImport(module, names))
+                new = FromImport(module, names)
+                if i > 0:
+                    new.prefix = indentation
+                new_nodes.append(new)
             if new_nodes:
                 nodes = []
                 for new_node in new_nodes[:-1]:

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 Aug  8 01:55:28 2010
@@ -1818,6 +1818,19 @@
             s = "from %s import *" % old
             self.warns_unchanged(s, "Cannot handle star imports")
 
+    def test_indented(self):
+        b = """
+def foo():
+    from urllib import urlencode, urlopen
+"""
+        a = """
+def foo():
+    from urllib.parse import urlencode
+    from urllib.request import urlopen
+"""
+        self.check(b, a)
+
+
     def test_import_module_usage(self):
         for old, changes in self.modules.items():
             for new, members in changes:


More information about the Python-checkins mailing list