diff --git a/hy/compiler.py b/hy/compiler.py index 76b266a..3856616 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -167,10 +167,10 @@ class HyASTCompiler(object): @builds("throw") @builds("raise") - @checkargs(min=1) + @checkargs(max=1) def compile_throw_expression(self, expr): expr.pop(0) - exc = self.compile(expr.pop(0)) + exc = self.compile(expr.pop(0)) if expr else None return ast.Raise( lineno=expr.start_line, col_offset=expr.start_column, diff --git a/tests/compilers/test_ast.py b/tests/compilers/test_ast.py index f839feb..c10b822 100644 --- a/tests/compilers/test_ast.py +++ b/tests/compilers/test_ast.py @@ -94,22 +94,24 @@ def test_ast_good_do(): def test_ast_good_throw(): "Make sure AST can compile valid throw" + hy_compile(tokenize("(throw)")) hy_compile(tokenize("(throw 1)")) def test_ast_bad_throw(): "Make sure AST can't compile invalid throw" - cant_compile("(throw)") + cant_compile("(raise 1 2 3)") def test_ast_good_raise(): "Make sure AST can compile valid raise" + hy_compile(tokenize("(raise)")) hy_compile(tokenize("(raise 1)")) def test_ast_bad_raise(): "Make sure AST can't compile invalid raise" - cant_compile("(raise)") + cant_compile("(raise 1 2 3)") def test_ast_good_try(): diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index ef2de95..a47a525 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -178,6 +178,26 @@ (try (pass) (except [IOError]) (except)) + ;; Test correct (raise) + (let [[passed false]] + (try + (try + (raise IndexError) + (except [IndexError] (raise))) + (except [IndexError] + (setv passed true))) + (assert passed)) + + ;; Test incorrect (raise) + (let [[passed false]] + (try + (raise) + ;; Python 2 raises TypeError + ;; Python 3 raises RuntimeError + (except [[TypeError RuntimeError]] + (setv passed true))) + (assert passed)) + (try (raise (KeyError)) (catch [[IOError]] (assert false))