Output try-except-finally statements where appropriate.
authorMark Dickinson <dickinsm@gmail.com>
Wed, 30 Jun 2010 08:46:53 +0000 (08:46 +0000)
committerMark Dickinson <dickinsm@gmail.com>
Wed, 30 Jun 2010 08:46:53 +0000 (08:46 +0000)
Demo/parser/test_unparse.py
Demo/parser/unparse.py

index 7d85f16cfda079ef1fa89697dcc9e9c92add59df..3a795e9974428e1e6b60cc105e0c286210e01c90 100644 (file)
@@ -80,7 +80,18 @@ elif cond2:
     suite2
 """
 
-
+try_except_finally = """\
+try:
+    suite1
+except ex1:
+    suite2
+except ex2:
+    suite3
+else:
+    suite4
+finally:
+    suite5
+"""
 
 class ASTTestCase(unittest.TestCase):
     def assertASTEqual(self, ast1, ast2):
@@ -181,6 +192,9 @@ class UnparseTestCase(ASTTestCase):
         self.check_roundtrip(elif1)
         self.check_roundtrip(elif2)
 
+    def test_try_except_finally(self):
+        self.check_roundtrip(try_except_finally)
+
 class DirectoryTestCase(ASTTestCase):
     """Test roundtrip behaviour on all files in Lib and Lib/test."""
 
index 03dd9e14e1d16049086e4c0f5f03ed54cba5f7a7..fa8f43441b618724355b17dc5a355930e3fb144e 100644 (file)
@@ -169,10 +169,14 @@ class Unparser:
             self.leave()
 
     def _TryFinally(self, t):
-        self.fill("try")
-        self.enter()
-        self.dispatch(t.body)
-        self.leave()
+        if len(t.body) == 1 and isinstance(t.body[0], ast.TryExcept):
+            # try-except-finally
+            self.dispatch(t.body)
+        else:
+            self.fill("try")
+            self.enter()
+            self.dispatch(t.body)
+            self.leave()
 
         self.fill("finally")
         self.enter()