try: allow empty body

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2013-04-07 19:06:02 +02:00
parent 4debdf63ea
commit a73d460beb
3 changed files with 11 additions and 7 deletions

View File

@ -176,7 +176,6 @@ class HyASTCompiler(object):
tback=None) tback=None)
@builds("try") @builds("try")
@checkargs(min=1)
def compile_try_expression(self, expr): def compile_try_expression(self, expr):
expr.pop(0) # try expr.pop(0) # try
@ -186,10 +185,16 @@ class HyASTCompiler(object):
else: else:
Try = ast.TryExcept Try = ast.TryExcept
if len(expr) == 0:
body = [ast.Pass(lineno=expr.start_line,
col_offset=expr.start_column)]
else:
body = self._code_branch(self.compile(expr.pop(0)))
return Try( return Try(
lineno=expr.start_line, lineno=expr.start_line,
col_offset=expr.start_column, col_offset=expr.start_column,
body=self._code_branch(self.compile(expr.pop(0))), body=body,
handlers=[self.compile(s) for s in expr], handlers=[self.compile(s) for s in expr],
finalbody=[], finalbody=[],
orelse=[]) orelse=[])

View File

@ -116,14 +116,10 @@ def test_ast_bad_raise():
def test_ast_good_try(): def test_ast_good_try():
"Make sure AST can compile valid try" "Make sure AST can compile valid try"
hy_compile(tokenize("(try)"))
hy_compile(tokenize("(try 1)")) hy_compile(tokenize("(try 1)"))
def test_ast_bad_try():
"Make sure AST can't compile invalid try"
cant_compile("(try)")
def test_ast_good_catch(): def test_ast_good_catch():
"Make sure AST can compile valid catch" "Make sure AST can compile valid catch"
hy_compile(tokenize("(catch)")) hy_compile(tokenize("(catch)"))

View File

@ -162,6 +162,9 @@
(defn test-exceptions [] (defn test-exceptions []
"NATIVE: test Exceptions" "NATIVE: test Exceptions"
(try)
(try (try
(raise (KeyError)) (raise (KeyError))
(catch [[IOError]] (assert false)) (catch [[IOError]] (assert false))