try: accept a simpler form

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2013-04-08 00:36:08 +02:00
parent a73d460beb
commit 0d3500f6e5
3 changed files with 25 additions and 1 deletions

View File

@ -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=[])

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

View File

@ -165,6 +165,12 @@
(try)
(try (pass))
(try (pass) (except))
(try (pass) (except [IOError]) (except))
(try
(raise (KeyError))
(catch [[IOError]] (assert false))