[Python-checkins] r61825 - in python/trunk/Lib/lib2to3: fixes/fix_import.py fixes/fix_itertools_imports.py fixes/util.py tests/benchmark.py tests/pytree_idempotency.py tests/test_fixers.py

martin.v.loewis python-checkins at python.org
Mon Mar 24 01:46:54 CET 2008


Author: martin.v.loewis
Date: Mon Mar 24 01:46:53 2008
New Revision: 61825
Modified:
 python/trunk/Lib/lib2to3/ (props changed)
 python/trunk/Lib/lib2to3/fixes/fix_import.py
 python/trunk/Lib/lib2to3/fixes/fix_itertools_imports.py
 python/trunk/Lib/lib2to3/fixes/util.py
 python/trunk/Lib/lib2to3/tests/benchmark.py
 python/trunk/Lib/lib2to3/tests/pytree_idempotency.py
 python/trunk/Lib/lib2to3/tests/test_fixers.py
Log:
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'
........
Modified: python/trunk/Lib/lib2to3/fixes/fix_import.py
==============================================================================
--- python/trunk/Lib/lib2to3/fixes/fix_import.py	(original)
+++ python/trunk/Lib/lib2to3/fixes/fix_import.py	Mon Mar 24 01:46:53 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: python/trunk/Lib/lib2to3/fixes/fix_itertools_imports.py
==============================================================================
--- python/trunk/Lib/lib2to3/fixes/fix_itertools_imports.py	(original)
+++ python/trunk/Lib/lib2to3/fixes/fix_itertools_imports.py	Mon Mar 24 01:46:53 2008
@@ -17,6 +17,9 @@
 # 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 @@
 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
Modified: python/trunk/Lib/lib2to3/fixes/util.py
==============================================================================
--- python/trunk/Lib/lib2to3/fixes/util.py	(original)
+++ python/trunk/Lib/lib2to3/fixes/util.py	Mon Mar 24 01:46:53 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: python/trunk/Lib/lib2to3/tests/benchmark.py
==============================================================================
--- python/trunk/Lib/lib2to3/tests/benchmark.py	(original)
+++ python/trunk/Lib/lib2to3/tests/benchmark.py	Mon Mar 24 01:46:53 2008
@@ -13,7 +13,7 @@
 from time import time
 
 # Test imports
-from support import adjust_path
+from .support import adjust_path
 adjust_path()
 
 # Local imports
Modified: python/trunk/Lib/lib2to3/tests/pytree_idempotency.py
==============================================================================
--- python/trunk/Lib/lib2to3/tests/pytree_idempotency.py	(original)
+++ python/trunk/Lib/lib2to3/tests/pytree_idempotency.py	Mon Mar 24 01:46:53 2008
@@ -7,7 +7,7 @@
 __author__ = "Guido van Rossum <guido at python.org>"
 
 # Support imports (need to be imported first)
-import support
+from . import support
 
 # Python imports
 import os
Modified: python/trunk/Lib/lib2to3/tests/test_fixers.py
==============================================================================
--- python/trunk/Lib/lib2to3/tests/test_fixers.py	(original)
+++ python/trunk/Lib/lib2to3/tests/test_fixers.py	Mon Mar 24 01:46:53 2008
@@ -3036,6 +3036,10 @@
 a = ""
 self.check(b, a)
 
+ b = "from itertools import izip"
+ a = ""
+ self.check(b, a)
+
 def test_import_as(self):
 b = "from itertools import izip, bar as bang, imap"
 a = "from itertools import bar as bang"
@@ -3105,6 +3109,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 +3129,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

AltStyle によって変換されたページ (->オリジナル) /