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 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 syntactic change; the elements were already run in Python order even when
defined out of order. 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 * 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 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 speed boosts, even for one-liners, because Hy no longer needs to recompile

View File

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

View File

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

View File

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