Merge pull request #92 from jd/jd/raise-0-arg

Implements (raise)
This commit is contained in:
Julien Danjou 2013-04-09 08:23:09 -07:00
commit 4ba6ada77b
3 changed files with 26 additions and 4 deletions

View File

@ -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,

View File

@ -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():

View File

@ -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))