[Python-3000-checkins] r67182 - in python/branches/py3k: Lib/lib2to3 Lib/lib2to3/fixes/fix_import.py Lib/lib2to3/fixes/fix_imports.py Lib/lib2to3/fixes/fix_metaclass.py Lib/lib2to3/refactor.py Lib/lib2to3/tests/test_fixers.py

benjamin.peterson python-3000-checkins at python.org
Mon Nov 10 23:21:33 CET 2008


Author: benjamin.peterson
Date: Mon Nov 10 23:21:33 2008
New Revision: 67182

Log:
Merged revisions 67180 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/trunk

................
  r67180 | benjamin.peterson | 2008-11-10 16:11:12 -0600 (Mon, 10 Nov 2008) | 29 lines
  
  Merged revisions 66985,67170,67173,67177-67179 via svnmerge from 
  svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
  
  ........
    r66985 | benjamin.peterson | 2008-10-20 16:43:46 -0500 (Mon, 20 Oct 2008) | 1 line
    
    no need to use nested try, except, finally
  ........
    r67170 | benjamin.peterson | 2008-11-08 12:28:31 -0600 (Sat, 08 Nov 2008) | 1 line
    
    fix #4271: fix_imports didn't recognize imports with parenthesis (ie from x import (a, b))
  ........
    r67173 | benjamin.peterson | 2008-11-08 17:42:08 -0600 (Sat, 08 Nov 2008) | 1 line
    
    consolidate test
  ........
    r67177 | benjamin.peterson | 2008-11-09 21:52:52 -0600 (Sun, 09 Nov 2008) | 1 line
    
    let the metclass fixer handle complex assignments in the class body gracefully
  ........
    r67178 | benjamin.peterson | 2008-11-10 15:26:43 -0600 (Mon, 10 Nov 2008) | 1 line
    
    the metaclass fixers shouldn't die when bases are not a simple name
  ........
    r67179 | benjamin.peterson | 2008-11-10 15:29:58 -0600 (Mon, 10 Nov 2008) | 1 line
    
    allow the fix_import pattern to catch from imports with parenthesis
  ........
................


Modified:
   python/branches/py3k/   (props changed)
   python/branches/py3k/Lib/lib2to3/   (props changed)
   python/branches/py3k/Lib/lib2to3/fixes/fix_import.py
   python/branches/py3k/Lib/lib2to3/fixes/fix_imports.py
   python/branches/py3k/Lib/lib2to3/fixes/fix_metaclass.py
   python/branches/py3k/Lib/lib2to3/refactor.py
   python/branches/py3k/Lib/lib2to3/tests/test_fixers.py

Modified: python/branches/py3k/Lib/lib2to3/fixes/fix_import.py
==============================================================================
--- python/branches/py3k/Lib/lib2to3/fixes/fix_import.py	(original)
+++ python/branches/py3k/Lib/lib2to3/fixes/fix_import.py	Mon Nov 10 23:21:33 2008
@@ -18,7 +18,7 @@
 class FixImport(fixer_base.BaseFix):
 
     PATTERN = """
-    import_from< type='from' imp=any 'import' any >
+    import_from< type='from' imp=any 'import' ['('] any [')'] >
     |
     import_name< type='import' imp=any >
     """

Modified: python/branches/py3k/Lib/lib2to3/fixes/fix_imports.py
==============================================================================
--- python/branches/py3k/Lib/lib2to3/fixes/fix_imports.py	(original)
+++ python/branches/py3k/Lib/lib2to3/fixes/fix_imports.py	Mon Nov 10 23:21:33 2008
@@ -66,9 +66,9 @@
     yield """import_name< 'import' ((%s)
                           | dotted_as_names< any* (%s) any* >) >
           """ % (mod_list, mod_list)
-    yield """import_from< 'from' (%s) 'import'
+    yield """import_from< 'from' (%s) 'import' ['(']
               ( any | import_as_name< any 'as' any > |
-                import_as_names< any* >) >
+                import_as_names< any* >)  [')'] >
           """ % mod_name_list
     yield """import_name< 'import'
                           dotted_as_name< (%s) 'as' any > >

Modified: python/branches/py3k/Lib/lib2to3/fixes/fix_metaclass.py
==============================================================================
--- python/branches/py3k/Lib/lib2to3/fixes/fix_metaclass.py	(original)
+++ python/branches/py3k/Lib/lib2to3/fixes/fix_metaclass.py	Mon Nov 10 23:21:33 2008
@@ -35,8 +35,9 @@
         elif node.type == syms.simple_stmt and node.children:
             expr_node = node.children[0]
             if expr_node.type == syms.expr_stmt and expr_node.children:
-                leaf_node = expr_node.children[0]
-                if leaf_node.value == '__metaclass__':
+                left_side = expr_node.children[0]
+                if isinstance(left_side, Leaf) and \
+                        left_side.value == '__metaclass__':
                     return True
     return False
 
@@ -165,12 +166,10 @@
             if node.children[3].type == syms.arglist:
                 arglist = node.children[3]
             # Node(classdef, ['class', 'name', '(', 'Parent', ')', ':', suite])
-            elif isinstance(node.children[3], Leaf):
+            else:
                 parent = node.children[3].clone()
                 arglist = Node(syms.arglist, [parent])
                 node.set_child(3, arglist)
-            else:
-                raise ValueError("Unexpected class inheritance arglist")
         elif len(node.children) == 6:
             # Node(classdef, ['class', 'name', '(',  ')', ':', suite])
             #                 0        1       2     3    4    5

Modified: python/branches/py3k/Lib/lib2to3/refactor.py
==============================================================================
--- python/branches/py3k/Lib/lib2to3/refactor.py	(original)
+++ python/branches/py3k/Lib/lib2to3/refactor.py	Mon Nov 10 23:21:33 2008
@@ -363,10 +363,9 @@
             self.log_error("Can't create %s: %s", filename, err)
             return
         try:
-            try:
-                f.write(new_text)
-            except os.error as err:
-                self.log_error("Can't write %s: %s", filename, err)
+            f.write(new_text)
+        except os.error as err:
+            self.log_error("Can't write %s: %s", filename, err)
         finally:
             f.close()
         self.log_debug("Wrote changes to %s", filename)

Modified: python/branches/py3k/Lib/lib2to3/tests/test_fixers.py
==============================================================================
--- python/branches/py3k/Lib/lib2to3/tests/test_fixers.py	(original)
+++ python/branches/py3k/Lib/lib2to3/tests/test_fixers.py	Mon Nov 10 23:21:33 2008
@@ -1450,6 +1450,10 @@
             a = "from %s import foo, bar" % new
             self.check(b, a)
 
+            b = "from %s import (yes, no)" % old
+            a = "from %s import (yes, no)" % new
+            self.check(b, a)
+
     def test_import_module_as(self):
         for old, new in self.modules.items():
             b = "import %s as foo_bar" % old
@@ -3345,6 +3349,10 @@
         a = "from .foo import bar"
         self.check_both(b, a)
 
+        b = "from foo import (bar, baz)"
+        a = "from .foo import (bar, baz)"
+        self.check_both(b, a)
+
     def test_dotted_from(self):
         b = "from green.eggs import ham"
         a = "from .green.eggs import ham"
@@ -3624,6 +3632,12 @@
         """
         self.unchanged(s)
 
+        s = """
+        class X:
+            a[23] = 74
+        """
+        self.unchanged(s)
+
     def test_comments(self):
         b = """
         class X:
@@ -3732,6 +3746,26 @@
         a = """class m(a, arg=23, metaclass=Meta): pass"""
         self.check(b, a)
 
+        b = """
+        class X(expression(2 + 4)):
+            __metaclass__ = Meta
+        """
+        a = """
+        class X(expression(2 + 4), metaclass=Meta):
+            pass
+        """
+        self.check(b, a)
+
+        b = """
+        class X(expression(2 + 4), x**4):
+            __metaclass__ = Meta
+        """
+        a = """
+        class X(expression(2 + 4), x**4, metaclass=Meta):
+            pass
+        """
+        self.check(b, a)
+
 
 class Test_getcwdu(FixerTestCase):
 


More information about the Python-3000-checkins mailing list