[Python-checkins] cpython: add fixer for reload() -> imp.reload() (closes #11797)\n\nPatch by Laurie

benjamin.peterson python-checkins at python.org
Sat Dec 8 04:44:17 CET 2012


http://hg.python.org/cpython/rev/3576c0c6f860
changeset:   80742:3576c0c6f860
user:        Benjamin Peterson <benjamin at python.org>
date:        Fri Dec 07 22:44:10 2012 -0500
summary:
  add fixer for reload() -> imp.reload() (closes #11797)\n\nPatch by Laurie Clark-Michalek and Berker Peksag

files:
  Doc/library/2to3.rst             |   4 +
  Lib/lib2to3/fixer_util.py        |  23 +++++++
  Lib/lib2to3/fixes/fix_intern.py  |  21 +------
  Lib/lib2to3/fixes/fix_reload.py  |  28 +++++++++
  Lib/lib2to3/tests/test_fixers.py |  59 ++++++++++++++++++++
  Misc/ACKS                        |   1 +
  Misc/NEWS                        |   2 +
  7 files changed, 120 insertions(+), 18 deletions(-)


diff --git a/Doc/library/2to3.rst b/Doc/library/2to3.rst
--- a/Doc/library/2to3.rst
+++ b/Doc/library/2to3.rst
@@ -343,6 +343,10 @@
 
    Handles the move of :func:`reduce` to :func:`functools.reduce`.
 
+.. 2to3fixer:: reload
+
+   Converts :func:`reload` to :func:`imp.reload`.
+
 .. 2to3fixer:: renames
 
    Changes :data:`sys.maxint` to :data:`sys.maxsize`.
diff --git a/Lib/lib2to3/fixer_util.py b/Lib/lib2to3/fixer_util.py
--- a/Lib/lib2to3/fixer_util.py
+++ b/Lib/lib2to3/fixer_util.py
@@ -129,6 +129,29 @@
     imp = Node(syms.import_from, children)
     return imp
 
+def ImportAndCall(node, results, names):
+    """Returns an import statement and calls a method
+    of the module:
+
+    import module
+    module.name()"""
+    obj = results["obj"].clone()
+    if obj.type == syms.arglist:
+        newarglist = obj.clone()
+    else:
+        newarglist = Node(syms.arglist, [obj.clone()])
+    after = results["after"]
+    if after:
+        after = [n.clone() for n in after]
+    new = Node(syms.power,
+               Attr(Name(names[0]), Name(names[1])) +
+               [Node(syms.trailer,
+                     [results["lpar"].clone(),
+                      newarglist,
+                      results["rpar"].clone()])] + after)
+    new.prefix = node.prefix
+    return new
+
 
 ###########################################################
 ### Determine whether a node represents a given literal
diff --git a/Lib/lib2to3/fixes/fix_intern.py b/Lib/lib2to3/fixes/fix_intern.py
--- a/Lib/lib2to3/fixes/fix_intern.py
+++ b/Lib/lib2to3/fixes/fix_intern.py
@@ -6,9 +6,8 @@
 intern(s) -> sys.intern(s)"""
 
 # Local imports
-from .. import pytree
 from .. import fixer_base
-from ..fixer_util import Name, Attr, touch_import
+from ..fixer_util import ImportAndCall, touch_import
 
 
 class FixIntern(fixer_base.BaseFix):
@@ -26,21 +25,7 @@
     """
 
     def transform(self, node, results):
-        syms = self.syms
-        obj = results["obj"].clone()
-        if obj.type == syms.arglist:
-            newarglist = obj.clone()
-        else:
-            newarglist = pytree.Node(syms.arglist, [obj.clone()])
-        after = results["after"]
-        if after:
-            after = [n.clone() for n in after]
-        new = pytree.Node(syms.power,
-                          Attr(Name("sys"), Name("intern")) +
-                          [pytree.Node(syms.trailer,
-                                       [results["lpar"].clone(),
-                                        newarglist,
-                                        results["rpar"].clone()])] + after)
-        new.prefix = node.prefix
+        names = ('sys', 'intern')
+        new = ImportAndCall(node, results, names)
         touch_import(None, 'sys', node)
         return new
diff --git a/Lib/lib2to3/fixes/fix_reload.py b/Lib/lib2to3/fixes/fix_reload.py
new file mode 100644
--- /dev/null
+++ b/Lib/lib2to3/fixes/fix_reload.py
@@ -0,0 +1,28 @@
+"""Fixer for reload().
+
+reload(s) -> imp.reload(s)"""
+
+# Local imports
+from .. import fixer_base
+from ..fixer_util import ImportAndCall, touch_import
+
+
+class FixReload(fixer_base.BaseFix):
+    BM_compatible = True
+    order = "pre"
+
+    PATTERN = """
+    power< 'reload'
+           trailer< lpar='('
+                    ( not(arglist | argument<any '=' any>) obj=any
+                      | obj=arglist<(not argument<any '=' any>) any ','> )
+                    rpar=')' >
+           after=any*
+    >
+    """
+
+    def transform(self, node, results):
+        names = ('imp', 'reload')
+        new = ImportAndCall(node, results, names)
+        touch_import(None, 'imp', node)
+        return new
diff --git a/Lib/lib2to3/tests/test_fixers.py b/Lib/lib2to3/tests/test_fixers.py
--- a/Lib/lib2to3/tests/test_fixers.py
+++ b/Lib/lib2to3/tests/test_fixers.py
@@ -282,6 +282,65 @@
         b = """f(*args, **kwds)"""
         self.check(a, b)
 
+class Test_reload(FixerTestCase):
+    fixer = "reload"
+
+    def test(self):
+        b = """reload(a)"""
+        a = """import imp\nimp.reload(a)"""
+        self.check(b, a)
+
+    def test_comment(self):
+        b = """reload( a ) # comment"""
+        a = """import imp\nimp.reload( a ) # comment"""
+        self.check(b, a)
+
+        # PEP 8 comments
+        b = """reload( a )  # comment"""
+        a = """import imp\nimp.reload( a )  # comment"""
+        self.check(b, a)
+
+    def test_space(self):
+        b = """reload( a )"""
+        a = """import imp\nimp.reload( a )"""
+        self.check(b, a)
+
+        b = """reload( a)"""
+        a = """import imp\nimp.reload( a)"""
+        self.check(b, a)
+
+        b = """reload(a )"""
+        a = """import imp\nimp.reload(a )"""
+        self.check(b, a)
+
+    def test_unchanged(self):
+        s = """reload(a=1)"""
+        self.unchanged(s)
+
+        s = """reload(f, g)"""
+        self.unchanged(s)
+
+        s = """reload(f, *h)"""
+        self.unchanged(s)
+
+        s = """reload(f, *h, **i)"""
+        self.unchanged(s)
+
+        s = """reload(f, **i)"""
+        self.unchanged(s)
+
+        s = """reload(*h, **i)"""
+        self.unchanged(s)
+
+        s = """reload(*h)"""
+        self.unchanged(s)
+
+        s = """reload(**i)"""
+        self.unchanged(s)
+
+        s = """reload()"""
+        self.unchanged(s)
+
 class Test_intern(FixerTestCase):
     fixer = "intern"
 
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -214,6 +214,7 @@
 Craig Citro
 Gilles Civario
 Chris Clark
+Laurie Clark-Michalek
 Mike Clarkson
 Andrew Clegg
 Brad Clements
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -503,6 +503,8 @@
 Tools/Demos
 -----------
 
+- Issue #11797: Add a 2to3 fixer that maps reload() to imp.reload().
+
 - Issue #10966: Remove the concept of unexpected skipped tests.
 
 - Issue #9893: Removed the Misc/Vim directory.

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list