diff --git a/hy/compiler.py b/hy/compiler.py index 4bfbd56..e990a41 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -162,6 +162,7 @@ class HyASTCompiler(object): return [self.compile(x) for x in expr[1:]] @builds("throw") + @builds("raise") @checkargs(min=1) def compile_throw_expression(self, expr): expr.pop(0) @@ -194,6 +195,7 @@ class HyASTCompiler(object): orelse=[]) @builds("catch") + @builds("except") def compile_catch_expression(self, expr): expr.pop(0) # catch diff --git a/tests/compilers/test_ast.py b/tests/compilers/test_ast.py index 0fc69da..533f836 100644 --- a/tests/compilers/test_ast.py +++ b/tests/compilers/test_ast.py @@ -104,6 +104,16 @@ def test_ast_bad_throw(): cant_compile("(throw)") +def test_ast_good_raise(): + "Make sure AST can compile valid raise" + hy_compile(tokenize("(raise 1)")) + + +def test_ast_bad_raise(): + "Make sure AST can't compile invalid raise" + cant_compile("(raise)") + + def test_ast_good_try(): "Make sure AST can compile valid try" hy_compile(tokenize("(try 1)")) @@ -132,6 +142,24 @@ def test_ast_bad_catch(): cant_compile("(catch [x [FooBar] BarBar]])") +def test_ast_good_except(): + "Make sure AST can compile valid except" + hy_compile(tokenize("(except)")) + hy_compile(tokenize("(except [])")) + hy_compile(tokenize("(except [Foobar])")) + hy_compile(tokenize("(except [[]])")) + hy_compile(tokenize("(except [x FooBar])")) + hy_compile(tokenize("(except [x [FooBar BarFoo]])")) + hy_compile(tokenize("(except [x [FooBar BarFoo]])")) + + +def test_ast_bad_except(): + "Make sure AST can't compile invalid except" + cant_compile("(except 1)") + cant_compile("(except [1 3])") + cant_compile("(except [x [FooBar] BarBar]])") + + def test_ast_good_assert(): "Make sure AST can compile valid assert" hy_compile(tokenize("(assert 1)")) diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index c137cc7..d41d20b 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -163,23 +163,29 @@ (defn test-exceptions [] "NATIVE: test Exceptions" (try - (throw (KeyError)) + (raise (KeyError)) (catch [[IOError]] (assert false)) (catch [e [KeyError]] (assert e))) + (try + (throw (KeyError)) + (except [[IOError]] (assert false)) + (catch [e [KeyError]] (assert e))) + + (try (get [1] 3) (catch [IndexError] (assert true)) - (catch [IndexError] (pass))) + (except [IndexError] (pass))) (try (print foobar42ofthebaz) (catch [IndexError] (assert false)) - (catch [NameError] (pass))) + (except [NameError] (pass))) (try (get [1] 3) - (catch [e IndexError] (assert (isinstance e IndexError)))) + (except [e IndexError] (assert (isinstance e IndexError)))) (try (get [1] 3) @@ -187,7 +193,7 @@ (try (print foobar42ofthebaz) - (catch [e [IndexError NameError]] (assert (isinstance e NameError)))) + (except [e [IndexError NameError]] (assert (isinstance e NameError)))) (try (print foobar42) @@ -203,11 +209,11 @@ (try (print foobar42ofthebaz) - (catch [])) + (except [])) (try (print foobar42ofthebaz) - (catch [] (pass))) + (except [] (pass))) (try (print foobar42ofthebaz)