Implements (raise)

As in Python, this allows to re-raise the last raised exception.

This fixes #86

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2013-04-09 16:50:27 +02:00
parent 4e61ae59fd
commit b13cc60075
3 changed files with 26 additions and 4 deletions

View File

@ -167,10 +167,10 @@ class HyASTCompiler(object):
@builds("throw") @builds("throw")
@builds("raise") @builds("raise")
@checkargs(min=1) @checkargs(max=1)
def compile_throw_expression(self, expr): def compile_throw_expression(self, expr):
expr.pop(0) expr.pop(0)
exc = self.compile(expr.pop(0)) exc = self.compile(expr.pop(0)) if expr else None
return ast.Raise( return ast.Raise(
lineno=expr.start_line, lineno=expr.start_line,
col_offset=expr.start_column, col_offset=expr.start_column,

View File

@ -94,22 +94,24 @@ def test_ast_good_do():
def test_ast_good_throw(): def test_ast_good_throw():
"Make sure AST can compile valid throw" "Make sure AST can compile valid throw"
hy_compile(tokenize("(throw)"))
hy_compile(tokenize("(throw 1)")) hy_compile(tokenize("(throw 1)"))
def test_ast_bad_throw(): def test_ast_bad_throw():
"Make sure AST can't compile invalid throw" "Make sure AST can't compile invalid throw"
cant_compile("(throw)") cant_compile("(raise 1 2 3)")
def test_ast_good_raise(): def test_ast_good_raise():
"Make sure AST can compile valid raise" "Make sure AST can compile valid raise"
hy_compile(tokenize("(raise)"))
hy_compile(tokenize("(raise 1)")) hy_compile(tokenize("(raise 1)"))
def test_ast_bad_raise(): def test_ast_bad_raise():
"Make sure AST can't compile invalid raise" "Make sure AST can't compile invalid raise"
cant_compile("(raise)") cant_compile("(raise 1 2 3)")
def test_ast_good_try(): def test_ast_good_try():

View File

@ -178,6 +178,26 @@
(try (pass) (except [IOError]) (except)) (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 (try
(raise (KeyError)) (raise (KeyError))
(catch [[IOError]] (assert false)) (catch [[IOError]] (assert false))