Skip to content

Commit

Permalink
Raise exceptions when no handlers are provided.
Browse files Browse the repository at this point in the history
Also small DRYing in try handling.

Previously, writing a bare (try (foo)) would invoke Pokemon
exception catching (gotta catch 'em all) instead of the correct
behavior, which is to raise the exception if no handler is provided.

Note that this is a cute feature of Hy, as a `try` with no `except`
is a syntax error.  We avoid the syntax error here because we don't
use Python's compiler, which is the only thing that can throw
Syntax Errors.  :D

Fixes #555.
  • Loading branch information
akaptur authored and berkerpeksag committed Apr 28, 2014
1 parent 4f8ab5a commit 3f9ae91
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -45,3 +45,4 @@
* kirbyfan64 <kirbyfan64@users.noreply.github.com>
* Brendan Curran-Johnson <brendan@bcjbcj.ca>
* Ivan Kozik <ivan@ludios.org>
* Allison Kaptur <allison.kaptur@gmail.com>
36 changes: 16 additions & 20 deletions hy/compiler.py
Expand Up @@ -732,25 +732,10 @@ def compile_try_expression(self, expr):
handler_results += self._compile_catch_expression(e, name)
handlers.append(handler_results.stmts.pop())
elif e[0] == HySymbol("else"):
if orelse:
raise HyTypeError(
e,
"`try' cannot have more than one `else'")
else:
orelse = self._compile_branch(e[1:])
# XXX tempvar magic
orelse += orelse.expr_as_stmt()
orelse = orelse.stmts
orelse = self.try_except_helper(e, HySymbol("else"), orelse)
elif e[0] == HySymbol("finally"):
if finalbody:
raise HyTypeError(
e,
"`try' cannot have more than one `finally'")
else:
finalbody = self._compile_branch(e[1:])
# XXX tempvar magic
finalbody += finalbody.expr_as_stmt()
finalbody = finalbody.stmts
finalbody = self.try_except_helper(e, HySymbol("finally"),
finalbody)
else:
raise HyTypeError(e, "Unknown expression in `try'")

Expand All @@ -768,8 +753,8 @@ def compile_try_expression(self, expr):
col_offset=expr.start_column,
type=None,
name=None,
body=[ast.Pass(lineno=expr.start_line,
col_offset=expr.start_column)])]
body=[ast.Raise(lineno=expr.start_line,
col_offset=expr.start_column)])]

ret = handler_results

Expand Down Expand Up @@ -809,6 +794,17 @@ def compile_try_expression(self, expr):
body=body,
orelse=orelse) + returnable

def try_except_helper(self, hy_obj, symbol, accumulated):
if accumulated:
raise HyTypeError(
hy_obj,
"`try' cannot have more than one `%s'" % symbol)
else:
accumulated = self._compile_branch(hy_obj[1:])
accumulated += accumulated.expr_as_stmt()
accumulated = accumulated.stmts
return accumulated

@builds("except")
@builds("catch")
def magic_internal_form(self, expr):
Expand Down
5 changes: 5 additions & 0 deletions tests/native_tests/language.hy
Expand Up @@ -236,6 +236,11 @@
"NATIVE: test do"
(do))

(defn test-bare-try [] (try
(try (raise ValueError))
(except [ValueError])
(else (assert false))))


(defn test-exceptions []
"NATIVE: test Exceptions"
Expand Down

0 comments on commit 3f9ae91

Please sign in to comment.