[Python-checkins] r67778 - in sandbox/trunk/2to3/lib2to3: fixes/fix_imports.py tests/test_fixers.py
alexandre.vassalotti
python-checkins at python.org
Mon Dec 15 00:48:21 CET 2008
Author: alexandre.vassalotti
Date: Mon Dec 15 00:48:20 2008
New Revision: 67778
Log:
Make fix_imports refactor multiple imports as.
Modified:
sandbox/trunk/2to3/lib2to3/fixes/fix_imports.py
sandbox/trunk/2to3/lib2to3/tests/test_fixers.py
Modified: sandbox/trunk/2to3/lib2to3/fixes/fix_imports.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/fixes/fix_imports.py (original)
+++ sandbox/trunk/2to3/lib2to3/fixes/fix_imports.py Mon Dec 15 00:48:20 2008
@@ -66,16 +66,17 @@
mod_list = ' | '.join(["module_name='%s'" % key for key in mapping])
bare_names = alternates(mapping.keys())
- yield """name_import=import_name< 'import' ((%s)
- | dotted_as_names< any* (%s) any* >) >
+ yield """name_import=import_name< 'import' ((%s) |
+ multiple_imports=dotted_as_names< any* (%s) any* >) >
""" % (mod_list, mod_list)
yield """import_from< 'from' (%s) 'import' ['(']
( any | import_as_name< any 'as' any > |
import_as_names< any* >) [')'] >
""" % mod_list
- yield """import_name< 'import'
- dotted_as_name< (%s) 'as' any > >
- """ % mod_list
+ yield """import_name< 'import' (dotted_as_name< (%s) 'as' any > |
+ multiple_imports=dotted_as_names<
+ any* dotted_as_name< (%s) 'as' any > any* >) >
+ """ % (mod_list, mod_list)
# Find usages of module members in code e.g. thread.foo(bar)
yield "power< bare_with_attr=(%s) trailer<'.' any > any* >" % bare_names
@@ -123,17 +124,16 @@
# If it's not a "from x import x, y" or "import x as y" import,
# marked its usage to be replaced.
self.replace[import_mod.value] = new_name
-
+ if "multiple_imports" in results:
# This is a nasty hack to fix multiple imports on a
- # line. (ie. "import StringIO, urlparse") The problem is that I
+ # line (e.g., "import StringIO, urlparse"). The problem is that I
# can't figure out an easy way to make a pattern recognize the
# keys of MAPPING randomly sprinkled in an import statement.
while True:
results = self.match(node)
- if results:
- self.transform(node, results)
- else:
+ if not results:
break
+ self.transform(node, results)
else:
# Replace usage of the module.
bare_name = results["bare_with_attr"][0]
Modified: sandbox/trunk/2to3/lib2to3/tests/test_fixers.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/tests/test_fixers.py (original)
+++ sandbox/trunk/2to3/lib2to3/tests/test_fixers.py Mon Dec 15 00:48:20 2008
@@ -1566,11 +1566,22 @@
fixer = "imports"
from ..fixes.fix_imports import MAPPING as modules
- def test_several_on_a_line(self):
+ def test_multiple_imports(self):
b = """import urlparse, cStringIO"""
a = """import urllib.parse, io"""
self.check(b, a)
+ def test_multiple_imports_as(self):
+ b = """
+ import copy_reg as bar, HTMLParser as foo, urlparse
+ s = urlparse.spam(bar.foo())
+ """
+ a = """
+ import copyreg as bar, html.parser as foo, urllib.parse
+ s = urllib.parse.spam(bar.foo())
+ """
+ self.check(b, a)
+
class Test_imports2(FixerTestCase, ImportsFixerTests):
fixer = "imports2"
More information about the Python-checkins
mailing list