mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Merged revisions 61825 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ................ r61825 | martin.v.loewis | 2008-03-24 01:46:53 +0100 (Mo, 24 Mär 2008) | 17 lines 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' ........ ................
This commit is contained in:
parent
fe337bfd0d
commit
a675ef1141
6 changed files with 62 additions and 23 deletions
|
@ -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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue