summaryrefslogtreecommitdiffstats
path: root/Lib/lib2to3/fixes
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2008-03-24 00:46:53 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2008-03-24 00:46:53 (GMT)
commit966d0e0930e9d73456a47ff459ea04ea8dc1cf9c (patch)
treedb8f036732aa019cfd6769c537e197d9fc9711f6 /Lib/lib2to3/fixes
parent440ca772f33b7c2eae1beb2e26a643d3054066f9 (diff)
downloadcpython-966d0e0930e9d73456a47ff459ea04ea8dc1cf9c.zip
cpython-966d0e0930e9d73456a47ff459ea04ea8dc1cf9c.tar.gz
cpython-966d0e0930e9d73456a47ff459ea04ea8dc1cf9c.tar.bz2
Merged revisions 61724-61824 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3 ........ r61730 | martin.v.loewis | 2008-03-22 02:20:58 +0100 (Sa, 22 Mär 2008) | 2 lines More explicit relative imports. ........ r61755 | david.wolever | 2008-03-22 21:33:52 +0100 (Sa, 22 Mär 2008) | 1 line Fixing #2446 -- 2to3 now translates 'import foo' to 'from . import foo' ........ r61824 | david.wolever | 2008-03-24 01:30:24 +0100 (Mo, 24 Mär 2008) | 3 lines Fixed a bug where 'from itertools import izip' would return 'from itertools import' ........
Diffstat (limited to 'Lib/lib2to3/fixes')
-rw-r--r--Lib/lib2to3/fixes/fix_import.py29
-rw-r--r--Lib/lib2to3/fixes/fix_itertools_imports.py14
-rw-r--r--Lib/lib2to3/fixes/util.py20
3 files changed, 45 insertions, 18 deletions
diff --git a/Lib/lib2to3/fixes/fix_import.py b/Lib/lib2to3/fixes/fix_import.py
index 6617054..91cbcf7 100644
--- a/Lib/lib2to3/fixes/fix_import.py
+++ b/Lib/lib2to3/fixes/fix_import.py
@@ -7,19 +7,20 @@ Becomes:
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 @@ class FixImport(basefix.BaseFix):
# 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):
diff --git a/Lib/lib2to3/fixes/fix_itertools_imports.py b/Lib/lib2to3/fixes/fix_itertools_imports.py
index 0faa4ea..d5876a5 100644
--- a/Lib/lib2to3/fixes/fix_itertools_imports.py
+++ b/Lib/lib2to3/fixes/fix_itertools_imports.py
@@ -17,6 +17,9 @@ class FixItertoolsImports(basefix.BaseFix):
# Handle 'import ... as ...'
continue
if child.value in ('imap', 'izip', 'ifilter'):
+ # The value must be set to none in case child == import,
+ # so that the test for empty imports will work out
+ child.value = None
child.remove()
elif child.value == 'ifilterfalse':
node.changed()
@@ -34,10 +37,9 @@ class FixItertoolsImports(basefix.BaseFix):
if unicode(children[-1]) == ',':
children[-1].remove()
- # If there is nothing left, return a blank line
+ # If there are no imports left, just get rid of the entire statement
if not (imports.children or getattr(imports, 'value', None)):
- new = BlankLine()
- new.prefix = node.get_prefix()
- else:
- new = node
- return new
+ p = node.get_prefix()
+ node = BlankLine()
+ node.prefix = p
+ return node
diff --git a/Lib/lib2to3/fixes/util.py b/Lib/lib2to3/fixes/util.py
index ef809af..82dfba1 100644
--- a/Lib/lib2to3/fixes/util.py
+++ b/Lib/lib2to3/fixes/util.py
@@ -108,6 +108,26 @@ def ListComp(xp, fp, it, test=None):
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
###########################################################