[Python-checkins] r61755 - in sandbox/trunk/2to3/lib2to3: fixes/fix_import.py fixes/util.py tests/test_fixers.py

david.wolever python-checkins at python.org
Sat Mar 22 21:33:52 CET 2008


Author: david.wolever
Date: Sat Mar 22 21:33:52 2008
New Revision: 61755

Modified:
   sandbox/trunk/2to3/lib2to3/fixes/fix_import.py
   sandbox/trunk/2to3/lib2to3/fixes/util.py
   sandbox/trunk/2to3/lib2to3/tests/test_fixers.py
Log:
Fixing #2446 -- 2to3 now translates 'import foo' to 'from . import foo'

Modified: sandbox/trunk/2to3/lib2to3/fixes/fix_import.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/fixes/fix_import.py	(original)
+++ sandbox/trunk/2to3/lib2to3/fixes/fix_import.py	Sat Mar 22 21:33:52 2008
@@ -7,19 +7,20 @@
 And this import:
     import spam
 Becomes:
-    import .spam
+    from . import spam
 """
 
 # Local imports
 from . import basefix
 from os.path import dirname, join, exists, pathsep
+from .util import FromImport
 
 class FixImport(basefix.BaseFix):
 
     PATTERN = """
-    import_from< 'from' imp=any 'import' any >
+    import_from< type='from' imp=any 'import' any >
     |
-    import_name< 'import' imp=any >
+    import_name< type='import' imp=any >
     """
 
     def transform(self, node, results):
@@ -33,15 +34,19 @@
             # I guess this is a global import -- skip it!
             return
 
-        # Some imps are top-level (eg: 'import ham')
-        # some are first level (eg: 'import ham.eggs')
-        # some are third level (eg: 'import ham.eggs as spam')
-        # Hence, the loop
-        while not hasattr(imp, 'value'):
-            imp = imp.children[0]
-
-        imp.value = "." + imp.value
-        node.changed()
+        if results['type'].value == 'from':
+            # Some imps are top-level (eg: 'import ham')
+            # some are first level (eg: 'import ham.eggs')
+            # some are third level (eg: 'import ham.eggs as spam')
+            # Hence, the loop
+            while not hasattr(imp, 'value'):
+                imp = imp.children[0]
+            imp.value = "." + imp.value
+            node.changed()
+        else:
+            new = FromImport('.', getattr(imp, 'content', None) or [imp])
+            new.prefix = node.get_prefix()
+            node = new
         return node
 
 def probably_a_local_import(imp_name, file_path):

Modified: sandbox/trunk/2to3/lib2to3/fixes/util.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/fixes/util.py	(original)
+++ sandbox/trunk/2to3/lib2to3/fixes/util.py	Sat Mar 22 21:33:52 2008
@@ -108,6 +108,26 @@
                         inner,
                         Leaf(token.RBRACE, "]")])
 
+def FromImport(package_name, name_leafs):
+    """ Return an import statement in the form:
+        from package import name_leafs"""
+    # XXX: May not handle dotted imports properly (eg, package_name='foo.bar')
+    assert package_name == '.' or '.' not in package.name, "FromImport has "\
+           "not been tested with dotted package names -- use at your own "\
+           "peril!"
+
+    for leaf in name_leafs:
+        # Pull the leaves out of their old tree
+        leaf.remove()
+
+    children = [Leaf(token.NAME, 'from'),
+                Leaf(token.NAME, package_name, prefix=" "),
+                Leaf(token.NAME, 'import', prefix=" "),
+                Node(syms.import_as_names, name_leafs)]
+    imp = Node(syms.import_from, children)
+    return imp
+
+
 ###########################################################
 ### Determine whether a node represents a given literal
 ###########################################################

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	Sat Mar 22 21:33:52 2008
@@ -3105,6 +3105,10 @@
             self.failUnlessEqual(set(self.files_checked), expected_checks)
 
     def test_from(self):
+        b = "from foo import bar, baz"
+        a = "from .foo import bar, baz"
+        self.check_both(b, a)
+
         b = "from foo import bar"
         a = "from .foo import bar"
         self.check_both(b, a)
@@ -3121,17 +3125,21 @@
 
     def test_import(self):
         b = "import foo"
-        a = "import .foo"
+        a = "from . import foo"
+        self.check_both(b, a)
+
+        b = "import foo, bar"
+        a = "from . import foo, bar"
         self.check_both(b, a)
 
     def test_dotted_import(self):
         b = "import foo.bar"
-        a = "import .foo.bar"
+        a = "from . import foo.bar"
         self.check_both(b, a)
 
     def test_dotted_import_as(self):
         b = "import foo.bar as bang"
-        a = "import .foo.bar as bang"
+        a = "from . import foo.bar as bang"
         self.check_both(b, a)
 
 


More information about the Python-checkins mailing list