From 5bf9ecfc5a40821eb15e2ac7f218ae276f81fa60 Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Thu, 25 May 2017 18:43:31 -0700 Subject: [PATCH] Forbid (try) and (try BODY) --- NEWS | 1 + hy/compiler.py | 17 ++++++----------- tests/compilers/test_ast.py | 5 +++-- tests/native_tests/language.hy | 11 ----------- 4 files changed, 10 insertions(+), 24 deletions(-) diff --git a/NEWS b/NEWS index c606eb1..e5b6a63 100644 --- a/NEWS +++ b/NEWS @@ -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 diff --git a/hy/compiler.py b/hy/compiler.py index 7bb165a..157cdcb 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -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)] diff --git a/tests/compilers/test_ast.py b/tests/compilers/test_ast.py index bc22967..2e88046 100644 --- a/tests/compilers/test_ast.py +++ b/tests/compilers/test_ast.py @@ -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))") diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index 8e2dced..1baaedd 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -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))