Commits

Joe Amenta committed 39e00c5

Bringing the alternate version of fix_imports up to speed with the main version

Comments (0)

Files changed (1)

lib3to2/fixes/imports_fix_alt_formatting.py

            "tkinter.turtle": "turtle",
            "urllib.robotparser": "robotparser",
            "xmlrpc.client": "xmlrpclib",
+           "builtins": "__builtin__",
 }
 
 # generic strings to help build patterns
 # helps match 'from http.client import HTTPConnection'
 from_import_match = "from_import=import_from< 'from' %s 'import' imported=any >"
 # helps match 'from http import client'
-from_import_submod_match = "from_import_submod=import_from< 'from' %s 'import' %s >"
+from_import_submod_match = "from_import_submod=import_from< 'from' %s 'import' (%s | import_as_name< %s 'as' renamed=any > | import_as_names< any* (%s | import_as_name< %s 'as' renamed=any >) any* > ) >"
 # helps match 'import urllib.request'
 name_import_match = "name_import=import_name< 'import' %s > | name_import=import_name< 'import' dotted_as_name< %s 'as' renamed=any > >"
 # helps match 'import http.client, winreg'
         simple_attr = subname_match % (attr)
         dotted_name = dotted_name_match % (simple_name, simple_attr)
         i_from = from_import_match % (dotted_name)
-        i_from_submod = from_import_submod_match % (simple_name, simple_attr)
+        i_from_submod = from_import_submod_match % (simple_name, simple_attr, simple_attr, simple_attr, simple_attr)
         i_name = name_import_match % (dotted_name, dotted_name)
         u_name = power_twoname_match % (simple_name, simple_attr)
         u_subname = power_subname_match % (simple_attr)
             parent.append_child(orig_stripped)
 
 
-    def get_dotted_import_replacement(self, name_node, attr_node, mapping=MAPPING):
+    def get_dotted_import_replacement(self, name_node, attr_node, mapping=MAPPING, renamed=None):
         """
         For (http, client) given and httplib being the correct replacement,
         returns (httplib as client, None)
         replacement = mapping[full_name]
         if '.' in replacement:
             new_name, new_attr = replacement.split('.')
-            return Name(new_name, prefix=name_node.prefix), Node(syms.dotted_as_name, [Name(new_attr, prefix=attr_node.prefix), Name('as', prefix=" "), attr_node.clone()])
+            if renamed is None:
+                return Name(new_name, prefix=name_node.prefix), Node(syms.dotted_as_name, [Name(new_attr, prefix=attr_node.prefix), Name('as', prefix=" "), attr_node.clone()])
+            else:
+                return Name(new_name, prefix=name_node.prefix), Name(new_attr, prefix=attr_node.prefix)
         else:
             return Node(syms.dotted_as_name, [Name(replacement, prefix=name_node.prefix), Name('as', prefix=' '), Name(attr_node.value, prefix=attr_node.prefix)]), None
     
                 elif name.type == syms.dotted_name:
                     self.fix_dotted_name(name)
         elif from_import_submod:
-            new_name, new_attr = self.get_dotted_import_replacement(name, attr)
+            renamed = results.get("renamed")
+            new_name, new_attr = self.get_dotted_import_replacement(name, attr, renamed=renamed)
             if new_attr is not None:
                 name.replace(new_name)
                 attr.replace(new_attr)