Minor cleanup for raise and try

This commit is contained in:
Kodi Arfer 2018-06-21 10:53:21 -07:00
parent bd675a5db6
commit 88f33453dc

View File

@ -575,6 +575,7 @@ class HyASTCompiler(object):
@special("raise", [maybe(FORM), maybe(sym(":from") + FORM)]) @special("raise", [maybe(FORM), maybe(sym(":from") + FORM)])
def compile_raise_expression(self, expr, root, exc, cause): def compile_raise_expression(self, expr, root, exc, cause):
ret = Result() ret = Result()
if exc is not None: if exc is not None:
exc = self.compile(exc) exc = self.compile(exc)
ret += exc ret += exc
@ -587,12 +588,10 @@ class HyASTCompiler(object):
ret += cause ret += cause
cause = cause.force_expr cause = cause.force_expr
ret += asty.Raise( return ret + asty.Raise(
expr, type=ret.expr, exc=exc, expr, type=ret.expr, exc=exc,
inst=None, tback=None, cause=cause) inst=None, tback=None, cause=cause)
return ret
@special("try", @special("try",
[many(notpexpr("except", "else", "finally")), [many(notpexpr("except", "else", "finally")),
many(pexpr(sym("except"), many(pexpr(sym("except"),
@ -685,8 +684,6 @@ class HyASTCompiler(object):
# or # or
# [] # []
# [variable [list of exceptions]]
# let's pop variable and use it as name
name = None name = None
if len(exceptions) == 2: if len(exceptions) == 2:
name = exceptions[0] name = exceptions[0]
@ -697,21 +694,20 @@ class HyASTCompiler(object):
if isinstance(exceptions_list, HyList): if isinstance(exceptions_list, HyList):
if len(exceptions_list): if len(exceptions_list):
# [FooBar BarFoo] → catch Foobar and BarFoo exceptions # [FooBar BarFoo] → catch Foobar and BarFoo exceptions
elts, _type, _ = self._compile_collect(exceptions_list) elts, types, _ = self._compile_collect(exceptions_list)
_type += asty.Tuple(exceptions_list, elts=elts, ctx=ast.Load()) types += asty.Tuple(exceptions_list, elts=elts, ctx=ast.Load())
else: else:
# [] → all exceptions caught # [] → all exceptions caught
_type = Result() types = Result()
else: else:
_type = self.compile(exceptions_list) types = self.compile(exceptions_list)
body = self._compile_branch(body) body = self._compile_branch(body)
body += asty.Assign(expr, targets=[var], value=body.force_expr) body += asty.Assign(expr, targets=[var], value=body.force_expr)
body += body.expr_as_stmt() body += body.expr_as_stmt()
# use _type.expr to get a literal `None` return types + asty.ExceptHandler(
return _type + asty.ExceptHandler( expr, type=types.expr, name=name,
expr, type=_type.expr, name=name,
body=body.stmts or [asty.Pass(expr)]) body=body.stmts or [asty.Pass(expr)])
@special("if*", [FORM, FORM, maybe(FORM)]) @special("if*", [FORM, FORM, maybe(FORM)])