Try/Except: Ensure that we return properly

From both inside a try and in an exception handler.
This commit is contained in:
Paul Tagliamonte 2013-05-08 20:41:16 -04:00
parent 98134e7396
commit 1fa53f9255
2 changed files with 7 additions and 8 deletions

View File

@ -606,7 +606,7 @@ class HyASTCompiler(object):
raise HyTypeError(e, "Empty list not allowed in `try'")
if e[0] in (HySymbol("except"), HySymbol("catch")):
handler_results += self._compile_catch_expression(e, var)
handler_results += self._compile_catch_expression(e, name)
handlers.append(handler_results.stmts.pop())
elif e[0] == HySymbol("else"):
if orelse:
@ -694,11 +694,6 @@ class HyASTCompiler(object):
def _compile_catch_expression(self, expr, var):
catch = expr.pop(0) # catch
ret_name = ast.Name(id=ast_str(var), arg=ast_str(var),
ctx=ast.Store(),
lineno=expr.start_line,
col_offset=expr.start_column)
try:
exceptions = expr.pop(0)
except IndexError:
@ -762,7 +757,7 @@ class HyASTCompiler(object):
"`%s' needs a valid exception list" % catch)
body = self._compile_branch(expr)
body += ast.Assign(targets=[ret_name],
body += ast.Assign(targets=[var],
value=body.force_expr,
lineno=expr.start_line,
col_offset=expr.start_column)

View File

@ -684,4 +684,8 @@
(defn test-try-except-return []
"NATIVE: test we can return from in a try except"
(assert ((fn [] (try xxx (except [NameError] (+ 1 1)))))) 2)
(assert (= ((fn [] (try xxx (except [NameError] (+ 1 1))))) 2))
(setf foo (try xxx (except [NameError] (+ 1 1))))
(assert (= foo 2))
(setf foo (try (+ 2 2) (except [NameError] (+ 1 1))))
(assert (= foo 4)))