try: accept a simpler form
Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
parent
a73d460beb
commit
0d3500f6e5
@ -186,16 +186,31 @@ class HyASTCompiler(object):
|
||||
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=body,
|
||||
handlers=[self.compile(s) for s in expr],
|
||||
handlers=handlers,
|
||||
finalbody=[],
|
||||
orelse=[])
|
||||
|
||||
|
@ -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"),
|
||||
@ -118,6 +119,8 @@ def test_ast_good_try():
|
||||
"Make sure AST can compile valid try"
|
||||
hy_compile(tokenize("(try)"))
|
||||
hy_compile(tokenize("(try 1)"))
|
||||
hy_compile(tokenize("(try 1 bla)"))
|
||||
hy_compile(tokenize("(try 1 bla bla)"))
|
||||
|
||||
|
||||
def test_ast_good_catch():
|
||||
|
@ -165,6 +165,12 @@
|
||||
|
||||
(try)
|
||||
|
||||
(try (pass))
|
||||
|
||||
(try (pass) (except))
|
||||
|
||||
(try (pass) (except [IOError]) (except))
|
||||
|
||||
(try
|
||||
(raise (KeyError))
|
||||
(catch [[IOError]] (assert false))
|
||||
|
Loading…
Reference in New Issue
Block a user