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 [])`.
This commit is contained in:
Kodi Arfer 2018-04-16 19:34:50 -07:00
parent 80bece497a
commit 7bf2901364
4 changed files with 21 additions and 21 deletions

View File

@ -23,6 +23,7 @@ Other Breaking Changes
* `hy-repr` uses registered functions instead of methods * `hy-repr` uses registered functions instead of methods
* `HyKeyword` no longer inherits from the string type and has been * `HyKeyword` no longer inherits from the string type and has been
made into its own object type. made into its own object type.
* `(except)` is no longer allowed. Use `(except [])` instead.
New Features New Features
------------------------------ ------------------------------

View File

@ -908,7 +908,9 @@ class HyASTCompiler(object):
def _compile_catch_expression(self, expr, var): def _compile_catch_expression(self, expr, var):
catch = expr.pop(0) # catch 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: # exceptions catch should be either:
# [[list of exceptions]] # [[list of exceptions]]

View File

@ -125,11 +125,11 @@ 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 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))")
can_compile("(try 1 (except [x]) (except [y]) (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))") 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 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 []))")
cant_compile("(try 1 (finally 1) (except))") cant_compile("(try 1 (finally 1) (except []))")
cant_compile("(try 1 (except) (finally 1) (else 1))") cant_compile("(try 1 (except []) (finally 1) (else 1))")
def test_ast_good_except(): def test_ast_good_except():
"Make sure AST can compile valid except" "Make sure AST can compile valid except"
can_compile("(try 1 (except))")
can_compile("(try 1 (except []))") can_compile("(try 1 (except []))")
can_compile("(try 1 (except [Foobar]))") can_compile("(try 1 (except [Foobar]))")
can_compile("(try 1 (except [[]]))") can_compile("(try 1 (except [[]]))")
@ -161,8 +160,10 @@ def test_ast_good_except():
def test_ast_bad_except(): def test_ast_bad_except():
"Make sure AST can't compile invalid except" "Make sure AST can't compile invalid except"
cant_compile("(except 1)") cant_compile("(except 1)")
cant_compile("(try 1 (except))")
cant_compile("(try 1 (except 1))") cant_compile("(try 1 (except 1))")
cant_compile("(try 1 (except [1 3]))") cant_compile("(try 1 (except [1 3]))")
cant_compile("(try 1 (except [(f) [IOError ValueError]]))")
cant_compile("(try 1 (except [x [FooBar] BarBar]))") cant_compile("(try 1 (except [x [FooBar] BarBar]))")

View File

@ -565,13 +565,13 @@
(defn test-exceptions [] (defn test-exceptions []
"NATIVE: 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 ; test that multiple expressions in a try get evaluated
(setv value 0) (setv value 0)
(try (+= value 1) (+= value 2) (except [IOError]) (except)) (try (+= value 1) (+= value 2) (except [IOError]) (except []))
(assert (= value 3)) (assert (= value 3))
;; Test correct (raise) ;; Test correct (raise)
@ -606,7 +606,7 @@
(setv passed False) (setv passed False)
(try (try
(raise Exception) (raise Exception)
(except) (except [])
(finally (setv passed True))) (finally (setv passed True)))
(assert passed) (assert passed)
@ -616,7 +616,7 @@
not-elsed True) not-elsed True)
(try (try
(raise Exception) (raise Exception)
(except) (except [])
(else (setv not-elsed False)) (else (setv not-elsed False))
(finally (setv passed True))) (finally (setv passed True)))
(assert passed) (assert passed)
@ -662,10 +662,6 @@
(get [1] 3) (get [1] 3)
(except [[IndexError NameError]] (do))) (except [[IndexError NameError]] (do)))
(try
(print foobar42ofthebaz)
(except))
(try (try
(print foobar42ofthebaz) (print foobar42ofthebaz)
(except [])) (except []))
@ -682,7 +678,7 @@
(setv passed False) (setv passed False)
(try (try
(try (do) (except) (else (bla))) (try (do) (except []) (else (bla)))
(except [NameError] (setv passed True))) (except [NameError] (setv passed True)))
(assert passed) (assert passed)
@ -709,7 +705,7 @@
(except [IOError] (except [IOError]
(setv x 45)) (setv x 45))
(else (setv x 44))) (else (setv x 44)))
(except)) (except []))
(assert (= x 0)) (assert (= x 0))
; test that [except ...] and ("except" ...) aren't treated like (except ...), ; test that [except ...] and ("except" ...) aren't treated like (except ...),
@ -718,7 +714,7 @@
(try (try
(+= x 1) (+= x 1)
("except" [IOError] (+= x 1)) ("except" [IOError] (+= x 1))
(except)) (except []))
(assert (= x 2)) (assert (= x 2))
@ -726,7 +722,7 @@
(try (try
(+= x 1) (+= x 1)
[except [IOError] (+= x 1)] [except [IOError] (+= x 1)]
(except)) (except []))
(assert (= x 2))) (assert (= x 2)))