]> granicus.if.org Git - python/commitdiff
Merged revisions 61825-61989 via svnmerge from
authorMartin v. Löwis <martin@v.loewis.de>
Fri, 28 Mar 2008 05:27:44 +0000 (05:27 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Fri, 28 Mar 2008 05:27:44 +0000 (05:27 +0000)
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3

........
  r61899 | collin.winter | 2008-03-25 17:53:41 +0100 (Di, 25 Mär 2008) | 1 line

  Add a missing explicit fixer to test_all_fixers.
........
  r61983 | collin.winter | 2008-03-28 03:19:46 +0100 (Fr, 28 Mär 2008) | 2 lines

  Fix http://bugs.python.org/issue2453: support empty excepts in fix_except.
........

Lib/lib2to3/fixes/fix_except.py
Lib/lib2to3/tests/test_all_fixers.py
Lib/lib2to3/tests/test_fixers.py

index 34281a02033bf938b9137c474b9debb6eb336a5f..5d6d153fe2833524baa7e77d25b1f1b70d33e416 100644 (file)
@@ -37,15 +37,18 @@ class FixExcept(basefix.BaseFix):
 
     PATTERN = """
     try_stmt< 'try' ':' suite
-                  cleanup=((except_clause ':' suite)+ ['else' ':' suite]
-                                                      ['finally' ':' suite]
-                               | 'finally' ':' suite) >
+                  cleanup=(except_clause ':' suite)+
+                  tail=(['except' ':' suite]
+                        ['else' ':' suite]
+                        ['finally' ':' suite]) >
     """
 
     def transform(self, node, results):
         syms = self.syms
 
-        try_cleanup = [ch.clone() for ch in results['cleanup']]
+        tail = [n.clone() for n in results["tail"]]
+
+        try_cleanup = [ch.clone() for ch in results["cleanup"]]
         for except_clause, e_suite in find_excepts(try_cleanup):
             if len(except_clause.children) == 4:
                 (E, comma, N) = except_clause.children[1:4]
@@ -85,5 +88,5 @@ class FixExcept(basefix.BaseFix):
                     N.set_prefix(" ")
 
         #TODO(cwinter) fix this when children becomes a smart list
-        children = [c.clone() for c in node.children[:3]] + try_cleanup
+        children = [c.clone() for c in node.children[:3]] + try_cleanup + tail
         return pytree.Node(node.type, children)
index e8df320c431f0608ec26859c816e5ad4e375eb74..c1da09ebafc4128eae023153fbf5f440128953b5 100644 (file)
@@ -27,7 +27,7 @@ class Options:
 
 class Test_all(support.TestCase):
     def setUp(self):
-        options = Options(fix=["all", "idioms", "ws_comma"],
+        options = Options(fix=["all", "idioms", "ws_comma", "buffer"],
                           print_function=False)
         self.refactor = refactor.RefactoringTool(options)
 
index 585d628a8f2fd81473d9e6a32f1061c6a417e890..c3308d68a3d3a96be449b61b20bedcab5866392a 100755 (executable)
@@ -681,6 +681,72 @@ class Test_except(FixerTestCase):
                 pass"""
         self.check(b, a)
 
+    def test_bare_except(self):
+        b = """
+            try:
+                pass
+            except Exception, a:
+                pass
+            except:
+                pass"""
+
+        a = """
+            try:
+                pass
+            except Exception as a:
+                pass
+            except:
+                pass"""
+        self.check(b, a)
+
+    def test_bare_except_and_else_finally(self):
+        b = """
+            try:
+                pass
+            except Exception, a:
+                pass
+            except:
+                pass
+            else:
+                pass
+            finally:
+                pass"""
+
+        a = """
+            try:
+                pass
+            except Exception as a:
+                pass
+            except:
+                pass
+            else:
+                pass
+            finally:
+                pass"""
+        self.check(b, a)
+
+    def test_multi_fixed_excepts_before_bare_except(self):
+        b = """
+            try:
+                pass
+            except TypeError, b:
+                pass
+            except Exception, a:
+                pass
+            except:
+                pass"""
+
+        a = """
+            try:
+                pass
+            except TypeError as b:
+                pass
+            except Exception as a:
+                pass
+            except:
+                pass"""
+        self.check(b, a)
+
     # These should not be touched:
 
     def test_unchanged_1(self):