diff --git a/NEWS.rst b/NEWS.rst index c1a673b..cabc57b 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -50,6 +50,7 @@ Bug Fixes * Fixed bugs that caused `defclass` to drop statements or crash * Fixed a REPL crash caused by illegle unicode escape string inputs * `NaN` can no longer create an infinite loop during macro-expansion +* Fixed a bug that caused `try` to drop expressions Misc. Improvements ---------------------------- diff --git a/hy/compiler.py b/hy/compiler.py index 91344ba..e567d8d 100755 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -790,13 +790,14 @@ class HyASTCompiler(object): expr.pop(0) # try # (try something somethingelse…) - body = Result() + body = [] # Check against HyExpression and HySymbol to avoid incorrectly # matching [except ...] or ("except" ...) while expr and not (isinstance(expr[0], HyExpression) and isinstance(expr[0][0], HySymbol) and expr[0][0] in ("except", "else", "finally")): - body += self.compile(expr.pop(0)) + body.append(expr.pop(0)) + body = self._compile_branch(body) var = self.get_anon_var() name = asty.Name(expr, id=ast_str(var), ctx=ast.Store()) @@ -805,8 +806,6 @@ class HyASTCompiler(object): returnable = Result(expr=expr_name, temp_variables=[expr_name, name], contains_yield=body.contains_yield) - if not all(expr): - raise HyTypeError(expr, "Empty list not allowed in `try'") handler_results = Result() handlers = [] while expr and expr[0][0] == HySymbol("except"): @@ -846,7 +845,6 @@ class HyASTCompiler(object): body += body.expr_as_stmt() if orelse else asty.Assign( expr, targets=[name], value=body.force_expr) - body = body.stmts or [asty.Pass(expr)] if PY3: diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index d742c91..ad1f00f 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -584,18 +584,27 @@ (do)) -(defn test-exceptions [] - "NATIVE: test Exceptions" +(defn test-try [] (try (do) (except [])) (try (do) (except [IOError]) (except [])) - ; test that multiple expressions in a try get evaluated + ; test that multiple statements in a try get evaluated (setv value 0) (try (+= value 1) (+= value 2) (except [IOError]) (except [])) (assert (= value 3)) + ; test that multiple expressions in a try get evaluated + ; https://github.com/hylang/hy/issues/1584 + (setv l []) + (defn f [] (.append l 1)) + (try (f) (f) (f) (except [IOError])) + (assert (= l [1 1 1])) + (setv l []) + (try (f) (f) (f) (except [IOError]) (else (f))) + (assert (= l [1 1 1 1])) + ;; Test correct (raise) (setv passed False) (try