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

collin.winter python-checkins at python.org
Thu Aug 2 22:13:28 CEST 2007


Author: collin.winter
Date: Thu Aug  2 22:13:27 2007
New Revision: 56676

Added:
   sandbox/trunk/2to3/fixes/fix_imports.py
      - copied, changed from r56432, sandbox/trunk/2to3/fixes/fix_stringio.py
Removed:
   sandbox/trunk/2to3/fixes/fix_stringio.py
Modified:
   sandbox/trunk/2to3/   (props changed)
   sandbox/trunk/2to3/tests/test_fixers.py
Log:
Turn fix_stringio into a more general import fixer (fix_imports).


Copied: sandbox/trunk/2to3/fixes/fix_imports.py (from r56432, sandbox/trunk/2to3/fixes/fix_stringio.py)
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_stringio.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_imports.py	Thu Aug  2 22:13:27 2007
@@ -13,32 +13,47 @@
 
 # Local imports
 from fixes import basefix
-from fixes.util import Name, attr_chain, any
+from fixes.util import Name, attr_chain, any, set
 
-MODULE = "('StringIO' | 'cStringIO')"
+MAPPING = {"StringIO":  ("io", ["StringIO"]),
+           "cStringIO": ("io", ["StringIO"]),
+           "md5":       ("hashlib", ["md5"])}
+
+
+def alternates(members):
+    return "(" + "|".join(map(repr, members)) + ")"
+
+
+def build_pattern():
+    bare = set()
+    for old_module, (new_module, members) in MAPPING.items():
+        bare.add(old_module)
+        bare.update(members)
+        members = alternates(members)
+        yield """import_name< 'import' (module=%r
+                              | dotted_as_names< any* module=%r any* >) >
+              """ % (old_module, old_module)
+        yield """import_from< 'from' module_name=%r 'import'
+                   ( %s | import_as_name< %s 'as' any >) >
+              """ % (old_module, members, members)
+        yield """import_from< 'from' module_name=%r 'import' star='*' >
+              """ % old_module
+        yield """import_name< 'import'
+                              dotted_as_name< module_name=%r 'as' any > >
+              """ % old_module
+        yield """power< module_name=%r trailer< '.' %s > any* >
+              """ % (old_module, members)
+    yield """bare_name=%s""" % alternates(bare)
 
-class FixStringio(basefix.BaseFix):
-    PATTERN = """
-    import_name< 'import' (module=%s
-                           | dotted_as_names< any* module=%s any* >) >
-    |
-    import_from< 'from' module_name=%s 'import'
-                 ( 'StringIO' | import_as_name< 'StringIO' 'as' any >) >
-    |
-    import_from< 'from' module_name=%s 'import' star='*' >
-    |
-    import_name< 'import' dotted_as_name< module_name=%s 'as' any > >
-    |
-    power< module_name=%s trailer< '.' 'StringIO' > any* >
-    |
-    bare_name=%s
-    """ % ((MODULE,) * 7)
+
+class FixImports(basefix.BaseFix):
+    PATTERN = "|".join(build_pattern())
 
     order = "pre" # Pre-order tree traversal
 
-    # Don't match 'StringIO' if it's within another match
+    # Don't match the node if it's within another match
     def match(self, node):
-        match = super(FixStringio, self).match
+        match = super(FixImports, self).match
         results = match(node)
         if results:
             if any([match(obj) for obj in attr_chain(node, "parent")]):
@@ -47,24 +62,28 @@
         return False
 
     def start_tree(self, tree, filename):
-        super(FixStringio, self).start_tree(tree, filename)
-        self.module_import = False
+        super(FixImports, self).start_tree(tree, filename)
+        self.replace = {}
 
     def transform(self, node, results):
         import_mod = results.get("module")
-        module_name = results.get("module_name")
+        mod_name = results.get("module_name")
         bare_name = results.get("bare_name")
         star = results.get("star")
 
+        if import_mod or mod_name:
+            new_name, members = MAPPING[(import_mod or mod_name).value]
+
         if import_mod:
-            import_mod = import_mod[0]
-            self.module_import = True
-            import_mod.replace(Name("io", prefix=import_mod.get_prefix()))
-        elif module_name:
-            module_name = module_name[0]
-            module_name.replace(Name("io", prefix=module_name.get_prefix()))
+            self.replace[import_mod.value] = new_name
+            import_mod.replace(Name(new_name, prefix=import_mod.get_prefix()))
+        elif mod_name:
             if star:
-                star.replace(Name("StringIO", prefix=star.get_prefix()))
-        elif bare_name and self.module_import:
+                self.cannot_convert(node, "Cannot handle star imports.")
+            else:
+                mod_name.replace(Name(new_name, prefix=mod_name.get_prefix()))
+        elif bare_name:
             bare_name = bare_name[0]
-            bare_name.replace(Name("io", prefix=bare_name.get_prefix()))
+            new_name = self.replace.get(bare_name.value)
+            if new_name:
+              bare_name.replace(Name(new_name, prefix=bare_name.get_prefix()))

Deleted: /sandbox/trunk/2to3/fixes/fix_stringio.py
==============================================================================
--- /sandbox/trunk/2to3/fixes/fix_stringio.py	Thu Aug  2 22:13:27 2007
+++ (empty file)
@@ -1,70 +0,0 @@
-"""StringIO.StringIO -> io.StringIO (imports, too).
-
-Imports this fixer picks up on:
-* "import StringIO" -> "import io"
-* "from StringIO import StringIO" -> "from io import StringIO"
-* "import StringIO as foo" -> "import io as foo"
-
-If the fixer finds "import StringIO", all "StringIO.StringIO" attribute
-lookups will be translated to "io.StringIO" and all "StringIO" names
-will be translated to "io".
-"""
-# Author: Collin Winter
-
-# Local imports
-from fixes import basefix
-from fixes.util import Name, attr_chain, any
-
-MODULE = "('StringIO' | 'cStringIO')"
-
-class FixStringio(basefix.BaseFix):
-    PATTERN = """
-    import_name< 'import' (module=%s
-                           | dotted_as_names< any* module=%s any* >) >
-    |
-    import_from< 'from' module_name=%s 'import'
-                 ( 'StringIO' | import_as_name< 'StringIO' 'as' any >) >
-    |
-    import_from< 'from' module_name=%s 'import' star='*' >
-    |
-    import_name< 'import' dotted_as_name< module_name=%s 'as' any > >
-    |
-    power< module_name=%s trailer< '.' 'StringIO' > any* >
-    |
-    bare_name=%s
-    """ % ((MODULE,) * 7)
-
-    order = "pre" # Pre-order tree traversal
-
-    # Don't match 'StringIO' if it's within another match
-    def match(self, node):
-        match = super(FixStringio, self).match
-        results = match(node)
-        if results:
-            if any([match(obj) for obj in attr_chain(node, "parent")]):
-                return False
-            return results
-        return False
-
-    def start_tree(self, tree, filename):
-        super(FixStringio, self).start_tree(tree, filename)
-        self.module_import = False
-
-    def transform(self, node, results):
-        import_mod = results.get("module")
-        module_name = results.get("module_name")
-        bare_name = results.get("bare_name")
-        star = results.get("star")
-
-        if import_mod:
-            import_mod = import_mod[0]
-            self.module_import = True
-            import_mod.replace(Name("io", prefix=import_mod.get_prefix()))
-        elif module_name:
-            module_name = module_name[0]
-            module_name.replace(Name("io", prefix=module_name.get_prefix()))
-            if star:
-                star.replace(Name("StringIO", prefix=star.get_prefix()))
-        elif bare_name and self.module_import:
-            bare_name = bare_name[0]
-            bare_name.replace(Name("io", prefix=bare_name.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	Thu Aug  2 22:13:27 2007
@@ -1277,73 +1277,80 @@
         self.unchanged(s)
 
 
-class Test_stringio(FixerTestCase):
-    fixer = "stringio"
+class Test_imports(FixerTestCase):
+    fixer = "imports"
 
-    modules = ["StringIO", "cStringIO"]
+    modules = {"StringIO":  ("io", ["StringIO"]),
+               "cStringIO": ("io", ["StringIO"]),
+               "md5":       ("hashlib", ["md5"])}
 
     def test_import_module(self):
-        for module in self.modules:
-            b = "import %s" % module
-            a = "import io"
+        for old, (new, members) in self.modules.items():
+            b = "import %s" % old
+            a = "import %s" % new
             self.check(b, a)
 
-            b = "import foo, %s, bar" % module
-            a = "import foo, io, bar"
+            b = "import foo, %s, bar" % old
+            a = "import foo, %s, bar" % new
             self.check(b, a)
 
     def test_import_from(self):
-        for module in self.modules:
-            b = "from %s import StringIO" % module
-            a = "from io import StringIO"
-            self.check(b, a)
-
-            b = "from %s import *" % module
-            a = "from io import StringIO"
-            self.check(b, a)
+        for old, (new, members) in self.modules.items():
+            for member in members:
+                b = "from %s import %s" % (old, ", ".join(members))
+                a = "from %s import %s" % (new, ", ".join(members))
+                self.check(b, a)
 
-            s = "from foo import StringIO"
-            self.unchanged(s)
+                s = "from foo import %s" % ", ".join(members)
+                self.unchanged(s)
 
     def test_import_module_as(self):
-        for module in self.modules:
-            b = "import %s as foo_bar" % module
-            a = "import io as foo_bar"
+        for old, (new, members) in self.modules.items():
+            b = "import %s as foo_bar" % old
+            a = "import %s as foo_bar" % new
             self.check(b, a)
 
-            b = "import %s as foo_bar" % module
-            a = "import io as foo_bar"
+            b = "import %s as foo_bar" % old
+            a = "import %s as foo_bar" % new
             self.check(b, a)
 
     def test_import_from_as(self):
-        for module in self.modules:
-            b = "from %s import StringIO as foo_bar" % module
-            a = "from io import StringIO as foo_bar"
-            self.check(b, a)
+        for old, (new, members) in self.modules.items():
+            for member in members:
+                b = "from %s import %s as foo_bar" % (old, ", ".join(members))
+                a = "from %s import %s as foo_bar" % (new, ", ".join(members))
+                self.check(b, a)
+
+    def test_star(self):
+        for old in self.modules:
+            s = "from %s import *" % old
+            self.warns_unchanged(s, "Cannot handle star imports")
 
     def test_import_module_usage(self):
-        for module in self.modules:
-            b = """
-                import %s
-                foo(%s, %s.StringIO)
-                """ % (module, module, module)
-            a = """
-                import io
-                foo(io, io.StringIO)
-                """
-            self.check(b, a)
+        for old, (new, members) in self.modules.items():
+            for member in members:
+                b = """
+                    import %s
+                    foo(%s, %s.%s)
+                    """ % (old, old, old, member)
+                a = """
+                    import %s
+                    foo(%s, %s.%s)
+                    """ % (new, new, new, member)
+                self.check(b, a)
 
     def test_from_import_usage(self):
-        for module in self.modules:
-            b = """
-                from %s import StringIO
-                foo(StringIO, StringIO())
-                """ % module
-            a = """
-                from io import StringIO
-                foo(StringIO, StringIO())
-                """
-            self.check(b, a)
+        for old, (new, members) in self.modules.items():
+            for member in members:
+                b = """
+                    from %s import %s
+                    foo(%s, %s())
+                    """ % (old, member, member, member)
+                a = """
+                    from %s import %s
+                    foo(%s, %s())
+                    """ % (new, member, member, member)
+                self.check(b, a)
 
 
 class Test_input(FixerTestCase):


More information about the Python-checkins mailing list