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:
parent
4e61ae59fd
commit
b13cc60075
@ -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,
|
||||
|
@ -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():
|
||||
|
@ -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))
|
||||
|
Loading…
Reference in New Issue
Block a user