Forbid (try) and (try BODY)

This commit is contained in:
Kodi Arfer 2017-05-25 18:43:31 -07:00 committed by Ryan Gonzalez
parent dffa2811e6
commit 5bf9ecfc5a
4 changed files with 10 additions and 24 deletions

1
NEWS
View File

@ -12,6 +12,7 @@ Changes from 0.12.1
follow all `except`s, and `finally` must come last). This is only a
syntactic change; the elements were already run in Python order even when
defined out of order.
* `try` now requires an `except` or `finally` clause, as in Python
* Importing or executing a Hy file automatically byte-compiles it, or loads
a byte-compiled version if it exists and is up to date. This brings big
speed boosts, even for one-liners, because Hy no longer needs to recompile

View File

@ -819,17 +819,11 @@ class HyASTCompiler(object):
raise HyTypeError(
expr,
"`try' cannot have `else' without `except'")
# (try) or (try BODY)
# Generate a default handler for Python >= 3.3 and pypy
if not handlers and not finalbody and not orelse:
handlers = [ast.ExceptHandler(
lineno=expr.start_line,
col_offset=expr.start_column,
type=None,
name=None,
body=[ast.Raise(lineno=expr.start_line,
col_offset=expr.start_column)])]
# Likewise a bare (try) or (try BODY).
if not (handlers or finalbody):
raise HyTypeError(
expr,
"`try' must have an `except' or `finally' clause")
ret = handler_results
@ -838,6 +832,7 @@ class HyASTCompiler(object):
value=body.force_expr,
lineno=expr.start_line,
col_offset=expr.start_column)
body = body.stmts or [ast.Pass(lineno=expr.start_line,
col_offset=expr.start_column)]

View File

@ -113,8 +113,6 @@ def test_ast_bad_raise():
def test_ast_good_try():
"Make sure AST can compile valid try"
can_compile("(try)")
can_compile("(try 1)")
can_compile("(try 1 (except) (else 1))")
can_compile("(try 1 (finally 1))")
can_compile("(try 1 (except) (finally 1))")
@ -125,8 +123,11 @@ def test_ast_good_try():
def test_ast_bad_try():
"Make sure AST can't compile invalid try"
cant_compile("(try)")
cant_compile("(try 1)")
cant_compile("(try 1 bla)")
cant_compile("(try 1 bla bla)")
cant_compile("(try (do bla bla))")
cant_compile("(try (do) (else 1) (else 2))")
cant_compile("(try 1 (else 1))")
cant_compile("(try 1 (else 1) (except))")

View File

@ -421,21 +421,10 @@
"NATIVE: test do"
(do))
(defn test-bare-try [] (try
(try (raise ValueError))
(except [ValueError])
(else (assert False))))
(defn test-exceptions []
"NATIVE: test Exceptions"
(try)
(try (do))
(try (do))
(try (do) (except))
(try (do) (except [IOError]) (except))