Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 81ad8cc

Browse files
committed
Output try-except-finally statements where appropriate.
1 parent 8d6d760 commit 81ad8cc

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

Demo/parser/test_unparse.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,18 @@ class Foo: pass
8080
suite2
8181
"""
8282

83-
83+
try_except_finally = """\
84+
try:
85+
suite1
86+
except ex1:
87+
suite2
88+
except ex2:
89+
suite3
90+
else:
91+
suite4
92+
finally:
93+
suite5
94+
"""
8495

8596
class ASTTestCase(unittest.TestCase):
8697
def assertASTEqual(self, ast1, ast2):
@@ -181,6 +192,9 @@ def test_elifs(self):
181192
self.check_roundtrip(elif1)
182193
self.check_roundtrip(elif2)
183194

195+
def test_try_except_finally(self):
196+
self.check_roundtrip(try_except_finally)
197+
184198
class DirectoryTestCase(ASTTestCase):
185199
"""Test roundtrip behaviour on all files in Lib and Lib/test."""
186200

Demo/parser/unparse.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,14 @@ def _TryExcept(self, t):
169169
self.leave()
170170

171171
def _TryFinally(self, t):
172-
self.fill("try")
173-
self.enter()
174-
self.dispatch(t.body)
175-
self.leave()
172+
if len(t.body) == 1 and isinstance(t.body[0], ast.TryExcept):
173+
# try-except-finally
174+
self.dispatch(t.body)
175+
else:
176+
self.fill("try")
177+
self.enter()
178+
self.dispatch(t.body)
179+
self.leave()
176180

177181
self.fill("finally")
178182
self.enter()

0 commit comments

Comments
 (0)