Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a675ef1

Browse files
committed
Merged revisions 61825 via svnmerge from
svn+ssh://[email protected]/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://[email protected]/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' ........ ................
1 parent fe337bf commit a675ef1

6 files changed

Lines changed: 62 additions & 23 deletions

File tree

Lib/lib2to3/fixes/fix_import.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@
77
And this import:
88
import spam
99
Becomes:
10-
import .spam
10+
from . import spam
1111
"""
1212

1313
# Local imports
1414
from . import basefix
1515
from os.path import dirname, join, exists, pathsep
16+
from .util import FromImport
1617

1718
class FixImport(basefix.BaseFix):
1819

1920
PATTERN = """
20-
import_from< 'from' imp=any 'import' any >
21+
import_from< type='from' imp=any 'import' any >
2122
|
22-
import_name< 'import' imp=any >
23+
import_name< type='import' imp=any >
2324
"""
2425

2526
def transform(self, node, results):
@@ -33,15 +34,19 @@ def transform(self, node, results):
3334
# I guess this is a global import -- skip it!
3435
return
3536

36-
# Some imps are top-level (eg: 'import ham')
37-
# some are first level (eg: 'import ham.eggs')
38-
# some are third level (eg: 'import ham.eggs as spam')
39-
# Hence, the loop
40-
while not hasattr(imp, 'value'):
41-
imp = imp.children[0]
42-
43-
imp.value = "." + imp.value
44-
node.changed()
37+
if results['type'].value == 'from':
38+
# Some imps are top-level (eg: 'import ham')
39+
# some are first level (eg: 'import ham.eggs')
40+
# some are third level (eg: 'import ham.eggs as spam')
41+
# Hence, the loop
42+
while not hasattr(imp, 'value'):
43+
imp = imp.children[0]
44+
imp.value = "." + imp.value
45+
node.changed()
46+
else:
47+
new = FromImport('.', getattr(imp, 'content', None) or [imp])
48+
new.prefix = node.get_prefix()
49+
node = new
4550
return node
4651

4752
def probably_a_local_import(imp_name, file_path):

Lib/lib2to3/fixes/fix_itertools_imports.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ def transform(self, node, results):
1717
# Handle 'import ... as ...'
1818
continue
1919
if child.value in ('imap', 'izip', 'ifilter'):
20+
# The value must be set to none in case child == import,
21+
# so that the test for empty imports will work out
22+
child.value = None
2023
child.remove()
2124
elif child.value == 'ifilterfalse':
2225
node.changed()
@@ -34,10 +37,9 @@ def transform(self, node, results):
3437
if str(children[-1]) == ',':
3538
children[-1].remove()
3639

37-
# If there is nothing left, return a blank line
40+
# If there are no imports left, just get rid of the entire statement
3841
if not (imports.children or getattr(imports, 'value', None)):
39-
new = BlankLine()
40-
new.prefix = node.get_prefix()
41-
else:
42-
new = node
43-
return new
42+
p = node.get_prefix()
43+
node = BlankLine()
44+
node.prefix = p
45+
return node

Lib/lib2to3/fixes/util.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,26 @@ def ListComp(xp, fp, it, test=None):
108108
inner,
109109
Leaf(token.RBRACE, "]")])
110110

111+
def FromImport(package_name, name_leafs):
112+
""" Return an import statement in the form:
113+
from package import name_leafs"""
114+
# XXX: May not handle dotted imports properly (eg, package_name='foo.bar')
115+
assert package_name == '.' or '.' not in package.name, "FromImport has "\
116+
"not been tested with dotted package names -- use at your own "\
117+
"peril!"
118+
119+
for leaf in name_leafs:
120+
# Pull the leaves out of their old tree
121+
leaf.remove()
122+
123+
children = [Leaf(token.NAME, 'from'),
124+
Leaf(token.NAME, package_name, prefix=" "),
125+
Leaf(token.NAME, 'import', prefix=" "),
126+
Node(syms.import_as_names, name_leafs)]
127+
imp = Node(syms.import_from, children)
128+
return imp
129+
130+
111131
###########################################################
112132
### Determine whether a node represents a given literal
113133
###########################################################

Lib/lib2to3/tests/benchmark.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from time import time
1414

1515
# Test imports
16-
from support import adjust_path
16+
from .support import adjust_path
1717
adjust_path()
1818

1919
# Local imports

Lib/lib2to3/tests/pytree_idempotency.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
__author__ = "Guido van Rossum <[email protected]>"
88

99
# Support imports (need to be imported first)
10-
import support
10+
from . import support
1111

1212
# Python imports
1313
import os

Lib/lib2to3/tests/test_fixers.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3036,6 +3036,10 @@ def test_none(self):
30363036
a = ""
30373037
self.check(b, a)
30383038

3039+
b = "from itertools import izip"
3040+
a = ""
3041+
self.check(b, a)
3042+
30393043
def test_import_as(self):
30403044
b = "from itertools import izip, bar as bang, imap"
30413045
a = "from itertools import bar as bang"
@@ -3105,6 +3109,10 @@ def p(path):
31053109
self.failUnlessEqual(set(self.files_checked), expected_checks)
31063110

31073111
def test_from(self):
3112+
b = "from foo import bar, baz"
3113+
a = "from .foo import bar, baz"
3114+
self.check_both(b, a)
3115+
31083116
b = "from foo import bar"
31093117
a = "from .foo import bar"
31103118
self.check_both(b, a)
@@ -3121,17 +3129,21 @@ def test_from_as(self):
31213129

31223130
def test_import(self):
31233131
b = "import foo"
3124-
a = "import .foo"
3132+
a = "from . import foo"
3133+
self.check_both(b, a)
3134+
3135+
b = "import foo, bar"
3136+
a = "from . import foo, bar"
31253137
self.check_both(b, a)
31263138

31273139
def test_dotted_import(self):
31283140
b = "import foo.bar"
3129-
a = "import .foo.bar"
3141+
a = "from . import foo.bar"
31303142
self.check_both(b, a)
31313143

31323144
def test_dotted_import_as(self):
31333145
b = "import foo.bar as bang"
3134-
a = "import .foo.bar as bang"
3146+
a = "from . import foo.bar as bang"
31353147
self.check_both(b, a)
31363148

31373149

0 commit comments

Comments
 (0)