From 7bf290136483dd4634189edf638cf3fd058fa270 Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Mon, 16 Apr 2018 19:34:50 -0700 Subject: [PATCH] Ban `(except)` It's rarely useful, because it catches all exceptions, but it doesn't let you do anything other than return `None`. You can still get the same effect with `(except [])`. --- NEWS.rst | 1 + hy/compiler.py | 4 +++- tests/compilers/test_ast.py | 15 ++++++++------- tests/native_tests/language.hy | 22 +++++++++------------- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/NEWS.rst b/NEWS.rst index a52a2ae..e6f3a9a 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -23,6 +23,7 @@ Other Breaking Changes * `hy-repr` uses registered functions instead of methods * `HyKeyword` no longer inherits from the string type and has been made into its own object type. +* `(except)` is no longer allowed. Use `(except [])` instead. New Features ------------------------------ diff --git a/hy/compiler.py b/hy/compiler.py index d94e94b..4c23cb2 100755 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -908,7 +908,9 @@ class HyASTCompiler(object): def _compile_catch_expression(self, expr, var): catch = expr.pop(0) # catch - exceptions = expr.pop(0) if expr else HyList() + if not expr: + raise HyTypeError(expr, "`%s' missing exceptions list" % catch) + exceptions = expr.pop(0) # exceptions catch should be either: # [[list of exceptions]] diff --git a/tests/compilers/test_ast.py b/tests/compilers/test_ast.py index dbf5858..e8d5af8 100644 --- a/tests/compilers/test_ast.py +++ b/tests/compilers/test_ast.py @@ -125,11 +125,11 @@ def test_ast_bad_raise(): def test_ast_good_try(): "Make sure AST can compile valid try" - can_compile("(try 1 (except) (else 1))") + can_compile("(try 1 (except []) (else 1))") can_compile("(try 1 (finally 1))") - can_compile("(try 1 (except) (finally 1))") + can_compile("(try 1 (except []) (finally 1))") can_compile("(try 1 (except [x]) (except [y]) (finally 1))") - can_compile("(try 1 (except) (else 1) (finally 1))") + can_compile("(try 1 (except []) (else 1) (finally 1))") can_compile("(try 1 (except [x]) (except [y]) (else 1) (finally 1))") @@ -142,14 +142,13 @@ def test_ast_bad_try(): 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))") - cant_compile("(try 1 (finally 1) (except))") - cant_compile("(try 1 (except) (finally 1) (else 1))") + cant_compile("(try 1 (else 1) (except []))") + cant_compile("(try 1 (finally 1) (except []))") + cant_compile("(try 1 (except []) (finally 1) (else 1))") def test_ast_good_except(): "Make sure AST can compile valid except" - can_compile("(try 1 (except))") can_compile("(try 1 (except []))") can_compile("(try 1 (except [Foobar]))") can_compile("(try 1 (except [[]]))") @@ -161,8 +160,10 @@ def test_ast_good_except(): def test_ast_bad_except(): "Make sure AST can't compile invalid except" cant_compile("(except 1)") + cant_compile("(try 1 (except))") cant_compile("(try 1 (except 1))") cant_compile("(try 1 (except [1 3]))") + cant_compile("(try 1 (except [(f) [IOError ValueError]]))") cant_compile("(try 1 (except [x [FooBar] BarBar]))") diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index 34d8561..ff61db9 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -565,13 +565,13 @@ (defn test-exceptions [] "NATIVE: test Exceptions" - (try (do) (except)) + (try (do) (except [])) - (try (do) (except [IOError]) (except)) + (try (do) (except [IOError]) (except [])) ; test that multiple expressions in a try get evaluated (setv value 0) - (try (+= value 1) (+= value 2) (except [IOError]) (except)) + (try (+= value 1) (+= value 2) (except [IOError]) (except [])) (assert (= value 3)) ;; Test correct (raise) @@ -606,7 +606,7 @@ (setv passed False) (try (raise Exception) - (except) + (except []) (finally (setv passed True))) (assert passed) @@ -616,7 +616,7 @@ not-elsed True) (try (raise Exception) - (except) + (except []) (else (setv not-elsed False)) (finally (setv passed True))) (assert passed) @@ -662,10 +662,6 @@ (get [1] 3) (except [[IndexError NameError]] (do))) - (try - (print foobar42ofthebaz) - (except)) - (try (print foobar42ofthebaz) (except [])) @@ -682,7 +678,7 @@ (setv passed False) (try - (try (do) (except) (else (bla))) + (try (do) (except []) (else (bla))) (except [NameError] (setv passed True))) (assert passed) @@ -709,7 +705,7 @@ (except [IOError] (setv x 45)) (else (setv x 44))) - (except)) + (except [])) (assert (= x 0)) ; test that [except ...] and ("except" ...) aren't treated like (except ...), @@ -718,7 +714,7 @@ (try (+= x 1) ("except" [IOError] (+= x 1)) - (except)) + (except [])) (assert (= x 2)) @@ -726,7 +722,7 @@ (try (+= x 1) [except [IOError] (+= x 1)] - (except)) + (except [])) (assert (= x 2)))