Merge branch 'jd/try' of git://github.com/jd/hy into pr-82
This commit is contained in:
commit
2216a23438
@ -176,7 +176,6 @@ class HyASTCompiler(object):
|
|||||||
tback=None)
|
tback=None)
|
||||||
|
|
||||||
@builds("try")
|
@builds("try")
|
||||||
@checkargs(min=1)
|
|
||||||
def compile_try_expression(self, expr):
|
def compile_try_expression(self, expr):
|
||||||
expr.pop(0) # try
|
expr.pop(0) # try
|
||||||
|
|
||||||
@ -186,11 +185,32 @@ class HyASTCompiler(object):
|
|||||||
else:
|
else:
|
||||||
Try = ast.TryExcept
|
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(
|
return Try(
|
||||||
lineno=expr.start_line,
|
lineno=expr.start_line,
|
||||||
col_offset=expr.start_column,
|
col_offset=expr.start_column,
|
||||||
body=self._code_branch(self.compile(expr.pop(0))),
|
body=body,
|
||||||
handlers=[self.compile(s) for s in expr],
|
handlers=handlers,
|
||||||
finalbody=[],
|
finalbody=[],
|
||||||
orelse=[])
|
orelse=[])
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
# Copyright (c) 2013 Paul Tagliamonte <paultag@debian.org>
|
# 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
|
# Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
# copy of this software and associated documentation files (the "Software"),
|
# copy of this software and associated documentation files (the "Software"),
|
||||||
@ -116,12 +117,10 @@ def test_ast_bad_raise():
|
|||||||
|
|
||||||
def test_ast_good_try():
|
def test_ast_good_try():
|
||||||
"Make sure AST can compile valid try"
|
"Make sure AST can compile valid try"
|
||||||
|
hy_compile(tokenize("(try)"))
|
||||||
hy_compile(tokenize("(try 1)"))
|
hy_compile(tokenize("(try 1)"))
|
||||||
|
hy_compile(tokenize("(try 1 bla)"))
|
||||||
|
hy_compile(tokenize("(try 1 bla bla)"))
|
||||||
def test_ast_bad_try():
|
|
||||||
"Make sure AST can't compile invalid try"
|
|
||||||
cant_compile("(try)")
|
|
||||||
|
|
||||||
|
|
||||||
def test_ast_good_catch():
|
def test_ast_good_catch():
|
||||||
|
@ -162,6 +162,15 @@
|
|||||||
|
|
||||||
(defn test-exceptions []
|
(defn test-exceptions []
|
||||||
"NATIVE: test Exceptions"
|
"NATIVE: test Exceptions"
|
||||||
|
|
||||||
|
(try)
|
||||||
|
|
||||||
|
(try (pass))
|
||||||
|
|
||||||
|
(try (pass) (except))
|
||||||
|
|
||||||
|
(try (pass) (except [IOError]) (except))
|
||||||
|
|
||||||
(try
|
(try
|
||||||
(raise (KeyError))
|
(raise (KeyError))
|
||||||
(catch [[IOError]] (assert false))
|
(catch [[IOError]] (assert false))
|
||||||
|
Loading…
Reference in New Issue
Block a user