mirror of
https://github.com/python/cpython.git
synced 2025-11-01 02:38:53 +00:00
Merged revisions 61596-61597 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r61596 | martin.v.loewis | 2008-03-18 23:43:46 -0500 (Di, 18 Mär 2008) | 2 lines Import lib2to3. ........ r61597 | martin.v.loewis | 2008-03-18 23:58:04 -0500 (Di, 18 Mär 2008) | 3 lines Initialized merge tracking via "svnmerge" with revisions "1-61595" from svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3 ........
This commit is contained in:
parent
c42bcbb1f0
commit
ef04c44e29
67 changed files with 11990 additions and 0 deletions
89
Lib/lib2to3/fixes/fix_imports.py
Normal file
89
Lib/lib2to3/fixes/fix_imports.py
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
"""Fix incompatible imports and module references.
|
||||
|
||||
Fixes:
|
||||
* StringIO -> io
|
||||
* cStringIO -> io
|
||||
* md5 -> hashlib
|
||||
"""
|
||||
# Author: Collin Winter
|
||||
|
||||
# Local imports
|
||||
from . import basefix
|
||||
from .util import Name, attr_chain, any, set
|
||||
import __builtin__
|
||||
builtin_names = [name for name in dir(__builtin__)
|
||||
if name not in ("__name__", "__doc__")]
|
||||
|
||||
MAPPING = {"StringIO": ("io", ["StringIO"]),
|
||||
"cStringIO": ("io", ["StringIO"]),
|
||||
"__builtin__" : ("builtins", builtin_names),
|
||||
}
|
||||
|
||||
|
||||
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 FixImports(basefix.BaseFix):
|
||||
PATTERN = "|".join(build_pattern())
|
||||
|
||||
order = "pre" # Pre-order tree traversal
|
||||
|
||||
# Don't match the node if it's within another match
|
||||
def match(self, node):
|
||||
match = super(FixImports, 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(FixImports, self).start_tree(tree, filename)
|
||||
self.replace = {}
|
||||
|
||||
def transform(self, node, results):
|
||||
import_mod = results.get("module")
|
||||
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:
|
||||
self.replace[import_mod.value] = new_name
|
||||
import_mod.replace(Name(new_name, prefix=import_mod.get_prefix()))
|
||||
elif mod_name:
|
||||
if star:
|
||||
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]
|
||||
new_name = self.replace.get(bare_name.value)
|
||||
if new_name:
|
||||
bare_name.replace(Name(new_name, prefix=bare_name.get_prefix()))
|
||||
Loading…
Add table
Add a link
Reference in a new issue