Merge branch 'jd/try' of git://github.com/jd/hy into pr-82

This commit is contained in:
Paul R. Tagliamonte 2013-04-07 18:51:08 -04:00
commit 2216a23438
3 changed files with 36 additions and 8 deletions

View File

@ -176,7 +176,6 @@ class HyASTCompiler(object):
tback=None)
@builds("try")
@checkargs(min=1)
def compile_try_expression(self, expr):
expr.pop(0) # try
@ -186,11 +185,32 @@ class HyASTCompiler(object):
else:
Try = ast.TryExcept
if len(expr) == 0:
# (try)
body = [ast.Pass(lineno=expr.start_line,
col_offset=expr.start_column)]
else:
# (try something…)
body = self._code_branch(self.compile(expr.pop(0)))
if len(expr) == 0:
# (try) or (try body)
handlers = [ast.ExceptHandler(
lineno=expr.start_line,
col_offset=expr.start_column,
type=None,
name=None,
body=[ast.Pass(lineno=expr.start_line,
col_offset=expr.start_column)])]
else:
# (try body except except…)
handlers = [self.compile(s) for s in expr]
return Try(
lineno=expr.start_line,
col_offset=expr.start_column,
body=self._code_branch(self.compile(expr.pop(0))),
handlers=[self.compile(s) for s in expr],
body=body,
handlers=handlers,
finalbody=[],
orelse=[])

View File

@ -1,4 +1,5 @@
# Copyright (c) 2013 Paul Tagliamonte <paultag@debian.org>
# Copyright (c) 2013 Julien Danjou <julien@danjou.info>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
@ -116,12 +117,10 @@ def test_ast_bad_raise():
def test_ast_good_try():
"Make sure AST can compile valid try"
hy_compile(tokenize("(try)"))
hy_compile(tokenize("(try 1)"))
def test_ast_bad_try():
"Make sure AST can't compile invalid try"
cant_compile("(try)")
hy_compile(tokenize("(try 1 bla)"))
hy_compile(tokenize("(try 1 bla bla)"))
def test_ast_good_catch():

View File

@ -162,6 +162,15 @@
(defn test-exceptions []
"NATIVE: test Exceptions"
(try)
(try (pass))
(try (pass) (except))
(try (pass) (except [IOError]) (except))
(try
(raise (KeyError))
(catch [[IOError]] (assert false))