[Python-checkins] r82384 - in python/branches/py3k/Demo/parser: test_unparse.py unparse.py

mark.dickinson python-checkins at python.org
Wed Jun 30 10:46:53 CEST 2010


Author: mark.dickinson
Date: Wed Jun 30 10:46:53 2010
New Revision: 82384

Log:
Output try-except-finally statements where appropriate.

Modified:
   python/branches/py3k/Demo/parser/test_unparse.py
   python/branches/py3k/Demo/parser/unparse.py

Modified: python/branches/py3k/Demo/parser/test_unparse.py
==============================================================================
--- python/branches/py3k/Demo/parser/test_unparse.py	(original)
+++ python/branches/py3k/Demo/parser/test_unparse.py	Wed Jun 30 10:46:53 2010
@@ -80,7 +80,18 @@
     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 @@
         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."""
 

Modified: python/branches/py3k/Demo/parser/unparse.py
==============================================================================
--- python/branches/py3k/Demo/parser/unparse.py	(original)
+++ python/branches/py3k/Demo/parser/unparse.py	Wed Jun 30 10:46:53 2010
@@ -169,10 +169,14 @@
             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()


More information about the Python-checkins mailing list