remove catch in favor of except

This commit is contained in:
gilch 2015-08-09 00:41:11 -06:00
parent e8d26f1067
commit 66c1f38fcc
15 changed files with 85 additions and 106 deletions

View File

@ -1300,20 +1300,20 @@ or no arguments to re-raise the last ``Exception``.
try
---
The ``try`` form is used to start a ``try`` / ``catch`` block. The form is
The ``try`` form is used to start a ``try`` / ``except`` block. The form is
used as follows:
.. code-block:: clj
(try
(error-prone-function)
(catch [e ZeroDivisionError] (print "Division by zero"))
(except [e ZeroDivisionError] (print "Division by zero"))
(else (print "no errors"))
(finally (print "all done")))
``try`` must contain at least one ``catch`` block, and may optionally include
an ``else`` or ``finally`` block. If an error is raised with a matching catch
block during the execution of ``error-prone-function``, that ``catch`` block
``try`` must contain at least one ``except`` block, and may optionally include
an ``else`` or ``finally`` block. If an error is raised with a matching except
block during the execution of ``error-prone-function``, that ``except`` block
will be executed. If no errors are raised, the ``else`` block is executed. The
``finally`` block will be executed last regardless of whether or not an error
was raised.

View File

@ -1072,7 +1072,7 @@ if *from-file* ends before a complete expression can be parsed.
... (do
... (print "OHY" exp)
... (eval exp))))
... (catch [e EOFError]
... (except [e EOFError]
... (print "EOF!"))))
OHY ('print' 'hello')
hello

View File

@ -7,7 +7,7 @@
(try
(import [urllib.request [urlopen]])
(catch [ImportError]
(except [ImportError]
(import [urllib2 [urlopen]])))
(defn get-rss-feed-name [tumblr]

View File

@ -827,7 +827,7 @@ class HyASTCompiler(object):
if not len(e):
raise HyTypeError(e, "Empty list not allowed in `try'")
if e[0] in (HySymbol("except"), HySymbol("catch")):
if e[0] == HySymbol("except"):
handler_results += self._compile_catch_expression(e, name)
handlers.append(handler_results.stmts.pop())
elif e[0] == HySymbol("else"):
@ -905,7 +905,6 @@ class HyASTCompiler(object):
return accumulated
@builds("except")
@builds("catch")
def magic_internal_form(self, expr):
raise HyTypeError(expr,
"Error: `%s' can't be used like that." % (expr[0]))

View File

@ -29,5 +29,5 @@
(let [[r (requests.get
"https://raw.githubusercontent.com/hylang/hy/master/AUTHORS")]]
(repeat r.text)))
(catch [e ImportError]
(except [e ImportError]
(repeat "Botsbuildbots requires `requests' to function."))))

View File

@ -222,8 +222,8 @@
"Return True if char `x` parses as an integer"
(try
(integer? (int x))
(catch [e ValueError] False)
(catch [e TypeError] False)))
(except [e ValueError] False)
(except [e TypeError] False)))
(defn interleave [&rest seqs]
"Return an iterable of the first item in each of seqs, then the second etc."
@ -414,7 +414,7 @@
(HyKeyword (+ ":" (hyify value)))
(try
(hyify (.__name__ value))
(catch [] (HyKeyword (+ ":" (string value))))))))
(except [] (HyKeyword (+ ":" (string value))))))))
(defn name [value]
"Convert the given value to a string. Keyword special character will be stripped.
@ -425,7 +425,7 @@
(hyify value)
(try
(hyify (. value __name__))
(catch [] (string value))))))
(except [] (string value))))))
(def *exports*
'[butlast calling-module-name coll? cons cons? cycle dec distinct disassemble

View File

@ -189,7 +189,7 @@
(try (if (isinstance ~g!iter types.GeneratorType)
(setv ~g!message (yield (.send ~g!iter ~g!message)))
(setv ~g!message (yield (next ~g!iter))))
(catch [~g!e StopIteration]
(except [~g!e StopIteration]
(do (setv ~g!return (if (hasattr ~g!e "value")
(. ~g!e value)
nil))

View File

@ -149,26 +149,6 @@ def test_ast_bad_try():
cant_compile("(try 1 (else 1))")
def test_ast_good_catch():
"Make sure AST can compile valid catch"
can_compile("(try 1 (catch))")
can_compile("(try 1 (catch []))")
can_compile("(try 1 (catch [Foobar]))")
can_compile("(try 1 (catch [[]]))")
can_compile("(try 1 (catch [x FooBar]))")
can_compile("(try 1 (catch [x [FooBar BarFoo]]))")
can_compile("(try 1 (catch [x [FooBar BarFoo]]))")
def test_ast_bad_catch():
"Make sure AST can't compile invalid catch"
cant_compile("(catch 22)") # heh
cant_compile("(try (catch 1))")
cant_compile("(try (catch \"A\"))")
cant_compile("(try (catch [1 3]))")
cant_compile("(try (catch [x [FooBar] BarBar]))")
def test_ast_good_except():
"Make sure AST can compile valid except"
can_compile("(try 1 (except))")

View File

@ -18,7 +18,7 @@
;; non-tco-sum should fail
(try
(setv n (non-tco-sum 100 10000))
(catch [e RuntimeError]
(except [e RuntimeError]
(assert true))
(else
(assert false)))
@ -26,7 +26,7 @@
;; tco-sum should not fail
(try
(setv n (tco-sum 100 10000))
(catch [e RuntimeError]
(except [e RuntimeError]
(assert false))
(else
(assert (= n 10100)))))
@ -40,7 +40,7 @@
(try
(bad-recur 3)
(catch [e TypeError]
(except [e TypeError]
(assert true))
(else
(assert false))))

View File

@ -67,11 +67,11 @@
(assert-equal -1 (dec 0))
(assert-equal 0 (dec (dec 2)))
(try (do (dec "foo") (assert False))
(catch [e [TypeError]] (assert (in "not a number" (str e)))))
(except [e [TypeError]] (assert (in "not a number" (str e)))))
(try (do (dec []) (assert False))
(catch [e [TypeError]] (assert (in "not a number" (str e)))))
(except [e [TypeError]] (assert (in "not a number" (str e)))))
(try (do (dec None) (assert False))
(catch [e [TypeError]] (assert (in "not a number" (str e))))))
(except [e [TypeError]] (assert (in "not a number" (str e))))))
(defn test-setv []
"NATIVE: testing setv mutation"
@ -85,9 +85,9 @@
(assert-equal (x y) 9)
(assert-equal (y x) 9)
(try (do (setv a.b 1) (assert False))
(catch [e [NameError]] (assert (in "name 'a' is not defined" (str e)))))
(except [e [NameError]] (assert (in "name 'a' is not defined" (str e)))))
(try (do (setv b.a (fn [x] x)) (assert False))
(catch [e [NameError]] (assert (in "name 'b' is not defined" (str e)))))
(except [e [NameError]] (assert (in "name 'b' is not defined" (str e)))))
(import itertools)
(setv foopermutations (fn [x] (itertools.permutations x)))
(setv p (set [(, 1 3 2) (, 3 2 1) (, 2 1 3) (, 3 1 2) (, 1 2 3) (, 2 3 1)]))
@ -127,7 +127,7 @@
(setv res (list (drop 0 [1 2 3 4 5])))
(assert-equal res [1 2 3 4 5])
(try (do (list (drop -1 [1 2 3 4 5])) (assert False))
(catch [e [ValueError]] nil))
(except [e [ValueError]] nil))
(setv res (list (drop 6 (iter [1 2 3 4 5]))))
(assert-equal res [])
(setv res (list (take 5 (drop 2 (iterate inc 0)))))
@ -174,11 +174,11 @@
(assert-false (even? 1))
(assert-true (even? 0))
(try (even? "foo")
(catch [e [TypeError]] (assert (in "not a number" (str e)))))
(except [e [TypeError]] (assert (in "not a number" (str e)))))
(try (even? [])
(catch [e [TypeError]] (assert (in "not a number" (str e)))))
(except [e [TypeError]] (assert (in "not a number" (str e)))))
(try (even? None)
(catch [e [TypeError]] (assert (in "not a number" (str e))))))
(except [e [TypeError]] (assert (in "not a number" (str e))))))
(defn test-every? []
"NATIVE: testing the every? function"
@ -221,9 +221,9 @@
(setv res (flatten (, 1 (, None 3))))
(assert-equal res [1 None 3])
(try (flatten "foo")
(catch [e [TypeError]] (assert (in "not a collection" (str e)))))
(except [e [TypeError]] (assert (in "not a collection" (str e)))))
(try (flatten 12.34)
(catch [e [TypeError]] (assert (in "not a collection" (str e))))))
(except [e [TypeError]] (assert (in "not a collection" (str e))))))
(defn test-float? []
"NATIVE: testing the float? function"
@ -264,11 +264,11 @@
(assert-equal 3 (inc 2))
(assert-equal 0 (inc -1))
(try (do (inc "foo") (assert False))
(catch [e [TypeError]] (assert (in "not a number" (str e)))))
(except [e [TypeError]] (assert (in "not a number" (str e)))))
(try (do (inc []) (assert False))
(catch [e [TypeError]] (assert (in "not a number" (str e)))))
(except [e [TypeError]] (assert (in "not a number" (str e)))))
(try (do (inc None) (assert False))
(catch [e [TypeError]] (assert (in "not a number" (str e))))))
(except [e [TypeError]] (assert (in "not a number" (str e))))))
(defn test-instance []
"NATIVE: testing instance? function"
@ -395,11 +395,11 @@
(assert-false (neg? 1))
(assert-false (neg? 0))
(try (do (neg? "foo") (assert False))
(catch [e [TypeError]] (assert (in "not a number" (str e)))))
(except [e [TypeError]] (assert (in "not a number" (str e)))))
(try (do (neg? []) (assert False))
(catch [e [TypeError]] (assert (in "not a number" (str e)))))
(except [e [TypeError]] (assert (in "not a number" (str e)))))
(try (do (neg? None) (assert False))
(catch [e [TypeError]] (assert (in "not a number" (str e))))))
(except [e [TypeError]] (assert (in "not a number" (str e))))))
(defn test-zero []
"NATIVE: testing the zero? function"
@ -407,11 +407,11 @@
(assert-false (zero? 1))
(assert-true (zero? 0))
(try (do (zero? "foo") (assert False))
(catch [e [TypeError]] (assert (in "not a number" (str e)))))
(except [e [TypeError]] (assert (in "not a number" (str e)))))
(try (do (zero? []) (assert False))
(catch [e [TypeError]] (assert (in "not a number" (str e)))))
(except [e [TypeError]] (assert (in "not a number" (str e)))))
(try (do (zero? None) (assert False))
(catch [e [TypeError]] (assert (in "not a number" (str e))))))
(except [e [TypeError]] (assert (in "not a number" (str e))))))
(defn test-none []
"NATIVE: testing for `is None`"
@ -438,7 +438,7 @@
(assert-equal (nth [1 2 4 7] 5 "some default value")
"some default value") ; with default specified
(try (do (nth [1 2 4 7] -1) (assert False))
(catch [e [ValueError]] nil))
(except [e [ValueError]] nil))
;; now for iterators
(assert-equal 2 (nth (iter [1 2 4 7]) 1))
(assert-equal 7 (nth (iter [1 2 4 7]) 3))
@ -446,7 +446,7 @@
(assert-equal (nth (iter [1 2 4 7]) 5 "some default value")
"some default value") ; with default specified
(try (do (nth (iter [1 2 4 7]) -1) (assert False))
(catch [e [ValueError]] nil))
(except [e [ValueError]] nil))
(assert-equal 5 (nth (take 3 (drop 2 [1 2 3 4 5 6])) 2)))
(defn test-numeric? []
@ -464,11 +464,11 @@
(assert-true (odd? 1))
(assert-false (odd? 0))
(try (do (odd? "foo") (assert False))
(catch [e [TypeError]] (assert (in "not a number" (str e)))))
(except [e [TypeError]] (assert (in "not a number" (str e)))))
(try (do (odd? []) (assert False))
(catch [e [TypeError]] (assert (in "not a number" (str e)))))
(except [e [TypeError]] (assert (in "not a number" (str e)))))
(try (do (odd? None) (assert False))
(catch [e [TypeError]] (assert (in "not a number" (str e))))))
(except [e [TypeError]] (assert (in "not a number" (str e))))))
(defn test-partition []
"NATIVE: testing the partition function"
@ -488,11 +488,11 @@
(assert-false (pos? -1))
(assert-false (pos? 0))
(try (do (pos? "foo") (assert False))
(catch [e [TypeError]] (assert (in "not a number" (str e)))))
(except [e [TypeError]] (assert (in "not a number" (str e)))))
(try (do (pos? []) (assert False))
(catch [e [TypeError]] (assert (in "not a number" (str e)))))
(except [e [TypeError]] (assert (in "not a number" (str e)))))
(try (do (pos? None) (assert False))
(catch [e [TypeError]] (assert (in "not a number" (str e))))))
(except [e [TypeError]] (assert (in "not a number" (str e))))))
(defn test-remove []
"NATIVE: testing the remove function"
@ -555,7 +555,7 @@
(setv res (list (take 0 (repeat "s"))))
(assert-equal res [])
(try (do (list (take -1 (repeat "s"))) (assert False))
(catch [e [ValueError]] nil))
(except [e [ValueError]] nil))
(setv res (list (take 6 [1 2 None 4])))
(assert-equal res [1 2 None 4]))
@ -582,7 +582,7 @@
(let [[passed false]]
(try
(setv res (list (take-nth 0 [1 2 3 4 5 6 7])))
(catch [ValueError] (setv passed true)))
(except [ValueError] (setv passed true)))
(assert passed)))
(defn test-take-while []

View File

@ -43,25 +43,25 @@
(defn test-setv-builtin []
"NATIVE: test that setv doesn't work on builtins"
(try (eval '(setv False 1))
(catch [e [TypeError]] (assert (in "Can't assign to a builtin" (str e)))))
(except [e [TypeError]] (assert (in "Can't assign to a builtin" (str e)))))
(try (eval '(setv True 0))
(catch [e [TypeError]] (assert (in "Can't assign to a builtin" (str e)))))
(except [e [TypeError]] (assert (in "Can't assign to a builtin" (str e)))))
(try (eval '(setv None 1))
(catch [e [TypeError]] (assert (in "Can't assign to a builtin" (str e)))))
(except [e [TypeError]] (assert (in "Can't assign to a builtin" (str e)))))
(try (eval '(setv false 1))
(catch [e [TypeError]] (assert (in "Can't assign to a builtin" (str e)))))
(except [e [TypeError]] (assert (in "Can't assign to a builtin" (str e)))))
(try (eval '(setv true 0))
(catch [e [TypeError]] (assert (in "Can't assign to a builtin" (str e)))))
(except [e [TypeError]] (assert (in "Can't assign to a builtin" (str e)))))
(try (eval '(setv nil 1))
(catch [e [TypeError]] (assert (in "Can't assign to a builtin" (str e)))))
(except [e [TypeError]] (assert (in "Can't assign to a builtin" (str e)))))
(try (eval '(setv null 1))
(catch [e [TypeError]] (assert (in "Can't assign to a builtin" (str e)))))
(except [e [TypeError]] (assert (in "Can't assign to a builtin" (str e)))))
(try (eval '(defn defclass [] (print "hello")))
(catch [e [TypeError]] (assert (in "Can't assign to a builtin" (str e)))))
(except [e [TypeError]] (assert (in "Can't assign to a builtin" (str e)))))
(try (eval '(defn get [] (print "hello")))
(catch [e [TypeError]] (assert (in "Can't assign to a builtin" (str e)))))
(except [e [TypeError]] (assert (in "Can't assign to a builtin" (str e)))))
(try (eval '(defn lambda [] (print "hello")))
(catch [e [TypeError]] (assert (in "Can't assign to a builtin" (str e))))))
(except [e [TypeError]] (assert (in "Can't assign to a builtin" (str e))))))
(defn test-setv-pairs []
@ -72,16 +72,16 @@
(setv y 0 x 1 y x)
(assert y)
(try (eval '(setv a 1 b))
(catch [e [TypeError]] (assert (in "setv needs an even number of arguments" (str e))))))
(except [e [TypeError]] (assert (in "setv needs an even number of arguments" (str e))))))
(defn test-fn-corner-cases []
"NATIVE: tests that fn/defn handles corner cases gracefully"
(try (eval '(fn "foo"))
(catch [e [Exception]] (assert (in "to (fn) must be a list"
(except [e [Exception]] (assert (in "to (fn) must be a list"
(str e)))))
(try (eval '(defn foo "foo"))
(catch [e [Exception]]
(except [e [Exception]]
(assert (in "takes a parameter list as second" (str e))))))
(defn test-for-loop []
@ -365,22 +365,22 @@
(try
(raise (KeyError))
(catch [[IOError]] (assert false))
(catch [e [KeyError]] (assert e)))
(except [[IOError]] (assert false))
(except [e [KeyError]] (assert e)))
(try
(raise (KeyError))
(except [[IOError]] (assert false))
(catch [e [KeyError]] (assert e)))
(except [e [KeyError]] (assert e)))
(try
(get [1] 3)
(catch [IndexError] (assert true))
(except [IndexError] (assert true))
(except [IndexError] (do)))
(try
(print foobar42ofthebaz)
(catch [IndexError] (assert false))
(except [IndexError] (assert false))
(except [NameError] (do)))
(try
@ -389,7 +389,7 @@
(try
(get [1] 3)
(catch [e [IndexError NameError]] (assert (isinstance e IndexError))))
(except [e [IndexError NameError]] (assert (isinstance e IndexError))))
(try
(print foobar42ofthebaz)
@ -397,15 +397,15 @@
(try
(print foobar42)
(catch [[IndexError NameError]] (do)))
(except [[IndexError NameError]] (do)))
(try
(get [1] 3)
(catch [[IndexError NameError]] (do)))
(except [[IndexError NameError]] (do)))
(try
(print foobar42ofthebaz)
(catch))
(except))
(try
(print foobar42ofthebaz)
@ -417,7 +417,7 @@
(try
(print foobar42ofthebaz)
(catch []
(except []
(setv foobar42ofthebaz 42)
(assert (= foobar42ofthebaz 42))))
@ -754,7 +754,7 @@
6))
(try
(assert (= x 42)) ; This ain't true
(catch [e [NameError]] (assert e)))
(except [e [NameError]] (assert e)))
(assert (= y 123)))
@ -872,7 +872,7 @@
(do
(eval ~@body)
(assert False "we shouldn't have arrived here"))
(catch [e Exception]
(except [e Exception]
(assert (instance? ~exc-type e)
(.format "Expected exception of type {}, got {}: {}"
(. ~exc-type --name--)
@ -891,7 +891,7 @@
; this should fail with a name error
(eval (quote x) d2)
(assert False "We shouldn't have arrived here"))
(catch [e Exception]
(except [e Exception]
(assert (isinstance e NameError))))))
(defn test-eval-failure []
@ -991,7 +991,7 @@
"NATIVE: test requiring macros from python code"
(try
(assert (= "this won't happen" (qplah 1 2 3 4)))
(catch [NameError]))
(except [NameError]))
(require tests.resources.tlib)
(assert (= [1 2 3] (qplah 1 2 3))))
@ -1159,7 +1159,7 @@
"NATIVE: test lambda lists are only parsed in defn"
(try
(foo [&rest spam] 1)
(catch [NameError] True)
(except [NameError] True)
(else (raise AssertionError))))
(defn test-read []
@ -1183,7 +1183,7 @@
(read stdin-buffer)
(try
(read stdin-buffer)
(catch [e Exception]
(except [e Exception]
(assert (isinstance e EOFError)))))
(defn test-read-str []

View File

@ -180,14 +180,14 @@
product-of-test-matrices))
;; Python <= 3.4
(let [[matmul-attempt (try (@ first-test-matrix second-test-matrix)
(catch [e [Exception]] e))]]
(except [e [Exception]] e))]]
(assert (isinstance matmul-attempt NameError)))))
(defn test-augassign-matmul []
"NATIVE: test augmented-assignment matrix multiplication"
(let [[matrix first-test-matrix]
[matmul-attempt (try (@= matrix second-test-matrix)
(catch [e [Exception]] e))]]
(except [e [Exception]] e))]]
(if PY35
(assert (= product-of-test-matrices matrix))
(assert (isinstance matmul-attempt NameError)))))

View File

@ -253,7 +253,7 @@
(yield i))
(try
(yield-from (yield-from-subgenerator-test))
(catch [e AssertionError]
(except [e AssertionError]
(yield 4))))
(assert (= (list (yield-from-test)) [0 1 2 1 2 3 4])))

View File

@ -22,7 +22,7 @@
(let [[kwonly-foo-no-default (fn [&kwonly foo] foo)]
[attempt-to-omit-default (try
(kwonly-foo-no-default)
(catch [e [Exception]] e))]]
(except [e [Exception]] e))]]
;; works
(assert (= (apply kwonly-foo-no-default [] {"foo" "quux"}) "quux"))
;; raises TypeError with appropriate message if not supplied

View File

@ -3,7 +3,7 @@
(let [[x +]]
(assert (try
(x)
(catch [TypeError] True)
(except [TypeError] True)
(else (raise AssertionError))))
(assert (= (x 1 2 3 4) 10))
(assert (= (x 1 2 3 4 5) 15))
@ -24,7 +24,7 @@
(let [[x -]]
(assert (try
(x)
(catch [TypeError] True)
(except [TypeError] True)
(else (raise AssertionError))))
(assert (= (x 1) -1))
(assert (= (x 2 1) 1))
@ -44,7 +44,7 @@
(let [[x /]]
(assert (try
(x)
(catch [TypeError] True)
(except [TypeError] True)
(else (raise AssertionError))))
(assert (= (x 1) 1))
(assert (= (x 8 2) 4))
@ -57,11 +57,11 @@
(for [x [< <= = != >= >]]
(assert (try
(x)
(catch [TypeError] True)
(except [TypeError] True)
(else (raise AssertionError))))
(assert (try
(x 1)
(catch [TypeError] True)
(except [TypeError] True)
(else (raise AssertionError)))))
(for [(, x y) [[< >=]
[<= >]