[Python-checkins] r67386 - in sandbox/trunk/2to3/lib2to3: fixes/fix_imports.py tests/test_fixers.py

benjamin.peterson python-checkins at python.org
Tue Nov 25 23:44:52 CET 2008


Author: benjamin.peterson
Date: Tue Nov 25 23:44:52 2008
New Revision: 67386

Log:
#4423 fix_imports was still replacing usage of a module if attributes were being used

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	Tue Nov 25 23:44:52 2008
@@ -63,6 +63,8 @@
 def build_pattern(mapping=MAPPING):
     mod_list = ' | '.join(["module='" + key + "'" for key in mapping.keys()])
     mod_name_list = ' | '.join(["module_name='" + key + "'" for key in mapping.keys()])
+    mod_attribute_list = ' | '.join(["mod_with_attribute='" + key + "'"
+                                     for key in mapping.keys()])
     yield """import_name< 'import' ((%s)
                           | dotted_as_names< any* (%s) any* >) >
           """ % (mod_list, mod_list)
@@ -76,8 +78,8 @@
     # Find usages of module members in code e.g. urllib.foo(bar)
     yield """power< (%s)
              trailer<'.' any > any* >
-          """ % mod_name_list
-    yield """bare_name=%s""" % alternates(mapping.keys())
+          """ % mod_attribute_list
+    yield "bare_name=(%s)" % alternates(mapping.keys())
 
 class FixImports(fixer_base.BaseFix):
     PATTERN = "|".join(build_pattern())
@@ -103,6 +105,7 @@
         import_mod = results.get("module")
         mod_name = results.get("module_name")
         bare_name = results.get("bare_name")
+        mod_with_attribute = results.get("mod_with_attribute")
 
         if import_mod or mod_name:
             new_name = self.mapping[(import_mod or mod_name).value]
@@ -112,6 +115,11 @@
             import_mod.replace(Name(new_name, prefix=import_mod.get_prefix()))
         elif mod_name:
             mod_name.replace(Name(new_name, prefix=mod_name.get_prefix()))
+        elif mod_with_attribute:
+            new_name = self.replace.get(mod_with_attribute.value)
+            if new_name:
+                mod_with_attribute.replace(Name(new_name,
+                                                prefix=mod_with_attribute.get_prefix()))
         elif bare_name:
             bare_name = bare_name[0]
             new_name = self.replace.get(bare_name.value)

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	Tue Nov 25 23:44:52 2008
@@ -1485,6 +1485,22 @@
                 """ % (new, new)
             self.check(b, a)
 
+            b = """
+                from %s import x
+                %s = 23
+                """ % (old, old)
+            a = """
+                from %s import x
+                %s = 23
+                """ % (new, old)
+            self.check(b, a)
+
+            s = """
+                def f():
+                    %s.method()
+                """ % (old,)
+            self.unchanged(s)
+
 
 class Test_imports2(Test_imports):
     fixer = "imports2"


More information about the Python-checkins mailing list