remove catch in favor of except
This commit is contained in:
parent
e8d26f1067
commit
66c1f38fcc
@ -1300,20 +1300,20 @@ or no arguments to re-raise the last ``Exception``.
|
|||||||
try
|
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:
|
used as follows:
|
||||||
|
|
||||||
.. code-block:: clj
|
.. code-block:: clj
|
||||||
|
|
||||||
(try
|
(try
|
||||||
(error-prone-function)
|
(error-prone-function)
|
||||||
(catch [e ZeroDivisionError] (print "Division by zero"))
|
(except [e ZeroDivisionError] (print "Division by zero"))
|
||||||
(else (print "no errors"))
|
(else (print "no errors"))
|
||||||
(finally (print "all done")))
|
(finally (print "all done")))
|
||||||
|
|
||||||
``try`` must contain at least one ``catch`` block, and may optionally include
|
``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 catch
|
an ``else`` or ``finally`` block. If an error is raised with a matching except
|
||||||
block during the execution of ``error-prone-function``, that ``catch`` block
|
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
|
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
|
``finally`` block will be executed last regardless of whether or not an error
|
||||||
was raised.
|
was raised.
|
||||||
|
@ -1072,7 +1072,7 @@ if *from-file* ends before a complete expression can be parsed.
|
|||||||
... (do
|
... (do
|
||||||
... (print "OHY" exp)
|
... (print "OHY" exp)
|
||||||
... (eval exp))))
|
... (eval exp))))
|
||||||
... (catch [e EOFError]
|
... (except [e EOFError]
|
||||||
... (print "EOF!"))))
|
... (print "EOF!"))))
|
||||||
OHY ('print' 'hello')
|
OHY ('print' 'hello')
|
||||||
hello
|
hello
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
(try
|
(try
|
||||||
(import [urllib.request [urlopen]])
|
(import [urllib.request [urlopen]])
|
||||||
(catch [ImportError]
|
(except [ImportError]
|
||||||
(import [urllib2 [urlopen]])))
|
(import [urllib2 [urlopen]])))
|
||||||
|
|
||||||
(defn get-rss-feed-name [tumblr]
|
(defn get-rss-feed-name [tumblr]
|
||||||
|
@ -827,7 +827,7 @@ class HyASTCompiler(object):
|
|||||||
if not len(e):
|
if not len(e):
|
||||||
raise HyTypeError(e, "Empty list not allowed in `try'")
|
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)
|
handler_results += self._compile_catch_expression(e, name)
|
||||||
handlers.append(handler_results.stmts.pop())
|
handlers.append(handler_results.stmts.pop())
|
||||||
elif e[0] == HySymbol("else"):
|
elif e[0] == HySymbol("else"):
|
||||||
@ -905,7 +905,6 @@ class HyASTCompiler(object):
|
|||||||
return accumulated
|
return accumulated
|
||||||
|
|
||||||
@builds("except")
|
@builds("except")
|
||||||
@builds("catch")
|
|
||||||
def magic_internal_form(self, expr):
|
def magic_internal_form(self, expr):
|
||||||
raise HyTypeError(expr,
|
raise HyTypeError(expr,
|
||||||
"Error: `%s' can't be used like that." % (expr[0]))
|
"Error: `%s' can't be used like that." % (expr[0]))
|
||||||
|
@ -29,5 +29,5 @@
|
|||||||
(let [[r (requests.get
|
(let [[r (requests.get
|
||||||
"https://raw.githubusercontent.com/hylang/hy/master/AUTHORS")]]
|
"https://raw.githubusercontent.com/hylang/hy/master/AUTHORS")]]
|
||||||
(repeat r.text)))
|
(repeat r.text)))
|
||||||
(catch [e ImportError]
|
(except [e ImportError]
|
||||||
(repeat "Botsbuildbots requires `requests' to function."))))
|
(repeat "Botsbuildbots requires `requests' to function."))))
|
||||||
|
@ -222,8 +222,8 @@
|
|||||||
"Return True if char `x` parses as an integer"
|
"Return True if char `x` parses as an integer"
|
||||||
(try
|
(try
|
||||||
(integer? (int x))
|
(integer? (int x))
|
||||||
(catch [e ValueError] False)
|
(except [e ValueError] False)
|
||||||
(catch [e TypeError] False)))
|
(except [e TypeError] False)))
|
||||||
|
|
||||||
(defn interleave [&rest seqs]
|
(defn interleave [&rest seqs]
|
||||||
"Return an iterable of the first item in each of seqs, then the second etc."
|
"Return an iterable of the first item in each of seqs, then the second etc."
|
||||||
@ -414,7 +414,7 @@
|
|||||||
(HyKeyword (+ ":" (hyify value)))
|
(HyKeyword (+ ":" (hyify value)))
|
||||||
(try
|
(try
|
||||||
(hyify (.__name__ value))
|
(hyify (.__name__ value))
|
||||||
(catch [] (HyKeyword (+ ":" (string value))))))))
|
(except [] (HyKeyword (+ ":" (string value))))))))
|
||||||
|
|
||||||
(defn name [value]
|
(defn name [value]
|
||||||
"Convert the given value to a string. Keyword special character will be stripped.
|
"Convert the given value to a string. Keyword special character will be stripped.
|
||||||
@ -425,7 +425,7 @@
|
|||||||
(hyify value)
|
(hyify value)
|
||||||
(try
|
(try
|
||||||
(hyify (. value __name__))
|
(hyify (. value __name__))
|
||||||
(catch [] (string value))))))
|
(except [] (string value))))))
|
||||||
|
|
||||||
(def *exports*
|
(def *exports*
|
||||||
'[butlast calling-module-name coll? cons cons? cycle dec distinct disassemble
|
'[butlast calling-module-name coll? cons cons? cycle dec distinct disassemble
|
||||||
|
@ -189,7 +189,7 @@
|
|||||||
(try (if (isinstance ~g!iter types.GeneratorType)
|
(try (if (isinstance ~g!iter types.GeneratorType)
|
||||||
(setv ~g!message (yield (.send ~g!iter ~g!message)))
|
(setv ~g!message (yield (.send ~g!iter ~g!message)))
|
||||||
(setv ~g!message (yield (next ~g!iter))))
|
(setv ~g!message (yield (next ~g!iter))))
|
||||||
(catch [~g!e StopIteration]
|
(except [~g!e StopIteration]
|
||||||
(do (setv ~g!return (if (hasattr ~g!e "value")
|
(do (setv ~g!return (if (hasattr ~g!e "value")
|
||||||
(. ~g!e value)
|
(. ~g!e value)
|
||||||
nil))
|
nil))
|
||||||
|
@ -149,26 +149,6 @@ def test_ast_bad_try():
|
|||||||
cant_compile("(try 1 (else 1))")
|
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():
|
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))")
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
;; non-tco-sum should fail
|
;; non-tco-sum should fail
|
||||||
(try
|
(try
|
||||||
(setv n (non-tco-sum 100 10000))
|
(setv n (non-tco-sum 100 10000))
|
||||||
(catch [e RuntimeError]
|
(except [e RuntimeError]
|
||||||
(assert true))
|
(assert true))
|
||||||
(else
|
(else
|
||||||
(assert false)))
|
(assert false)))
|
||||||
@ -26,7 +26,7 @@
|
|||||||
;; tco-sum should not fail
|
;; tco-sum should not fail
|
||||||
(try
|
(try
|
||||||
(setv n (tco-sum 100 10000))
|
(setv n (tco-sum 100 10000))
|
||||||
(catch [e RuntimeError]
|
(except [e RuntimeError]
|
||||||
(assert false))
|
(assert false))
|
||||||
(else
|
(else
|
||||||
(assert (= n 10100)))))
|
(assert (= n 10100)))))
|
||||||
@ -40,7 +40,7 @@
|
|||||||
|
|
||||||
(try
|
(try
|
||||||
(bad-recur 3)
|
(bad-recur 3)
|
||||||
(catch [e TypeError]
|
(except [e TypeError]
|
||||||
(assert true))
|
(assert true))
|
||||||
(else
|
(else
|
||||||
(assert false))))
|
(assert false))))
|
||||||
|
@ -67,11 +67,11 @@
|
|||||||
(assert-equal -1 (dec 0))
|
(assert-equal -1 (dec 0))
|
||||||
(assert-equal 0 (dec (dec 2)))
|
(assert-equal 0 (dec (dec 2)))
|
||||||
(try (do (dec "foo") (assert False))
|
(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))
|
(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))
|
(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 []
|
(defn test-setv []
|
||||||
"NATIVE: testing setv mutation"
|
"NATIVE: testing setv mutation"
|
||||||
@ -85,9 +85,9 @@
|
|||||||
(assert-equal (x y) 9)
|
(assert-equal (x y) 9)
|
||||||
(assert-equal (y x) 9)
|
(assert-equal (y x) 9)
|
||||||
(try (do (setv a.b 1) (assert False))
|
(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))
|
(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)
|
(import itertools)
|
||||||
(setv foopermutations (fn [x] (itertools.permutations x)))
|
(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)]))
|
(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])))
|
(setv res (list (drop 0 [1 2 3 4 5])))
|
||||||
(assert-equal res [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))
|
(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]))))
|
(setv res (list (drop 6 (iter [1 2 3 4 5]))))
|
||||||
(assert-equal res [])
|
(assert-equal res [])
|
||||||
(setv res (list (take 5 (drop 2 (iterate inc 0)))))
|
(setv res (list (take 5 (drop 2 (iterate inc 0)))))
|
||||||
@ -174,11 +174,11 @@
|
|||||||
(assert-false (even? 1))
|
(assert-false (even? 1))
|
||||||
(assert-true (even? 0))
|
(assert-true (even? 0))
|
||||||
(try (even? "foo")
|
(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? [])
|
(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)
|
(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? []
|
(defn test-every? []
|
||||||
"NATIVE: testing the every? function"
|
"NATIVE: testing the every? function"
|
||||||
@ -221,9 +221,9 @@
|
|||||||
(setv res (flatten (, 1 (, None 3))))
|
(setv res (flatten (, 1 (, None 3))))
|
||||||
(assert-equal res [1 None 3])
|
(assert-equal res [1 None 3])
|
||||||
(try (flatten "foo")
|
(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)
|
(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? []
|
(defn test-float? []
|
||||||
"NATIVE: testing the float? function"
|
"NATIVE: testing the float? function"
|
||||||
@ -264,11 +264,11 @@
|
|||||||
(assert-equal 3 (inc 2))
|
(assert-equal 3 (inc 2))
|
||||||
(assert-equal 0 (inc -1))
|
(assert-equal 0 (inc -1))
|
||||||
(try (do (inc "foo") (assert False))
|
(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))
|
(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))
|
(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 []
|
(defn test-instance []
|
||||||
"NATIVE: testing instance? function"
|
"NATIVE: testing instance? function"
|
||||||
@ -395,11 +395,11 @@
|
|||||||
(assert-false (neg? 1))
|
(assert-false (neg? 1))
|
||||||
(assert-false (neg? 0))
|
(assert-false (neg? 0))
|
||||||
(try (do (neg? "foo") (assert False))
|
(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))
|
(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))
|
(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 []
|
(defn test-zero []
|
||||||
"NATIVE: testing the zero? function"
|
"NATIVE: testing the zero? function"
|
||||||
@ -407,11 +407,11 @@
|
|||||||
(assert-false (zero? 1))
|
(assert-false (zero? 1))
|
||||||
(assert-true (zero? 0))
|
(assert-true (zero? 0))
|
||||||
(try (do (zero? "foo") (assert False))
|
(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))
|
(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))
|
(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 []
|
(defn test-none []
|
||||||
"NATIVE: testing for `is None`"
|
"NATIVE: testing for `is None`"
|
||||||
@ -438,7 +438,7 @@
|
|||||||
(assert-equal (nth [1 2 4 7] 5 "some default value")
|
(assert-equal (nth [1 2 4 7] 5 "some default value")
|
||||||
"some default value") ; with default specified
|
"some default value") ; with default specified
|
||||||
(try (do (nth [1 2 4 7] -1) (assert False))
|
(try (do (nth [1 2 4 7] -1) (assert False))
|
||||||
(catch [e [ValueError]] nil))
|
(except [e [ValueError]] nil))
|
||||||
;; now for iterators
|
;; now for iterators
|
||||||
(assert-equal 2 (nth (iter [1 2 4 7]) 1))
|
(assert-equal 2 (nth (iter [1 2 4 7]) 1))
|
||||||
(assert-equal 7 (nth (iter [1 2 4 7]) 3))
|
(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")
|
(assert-equal (nth (iter [1 2 4 7]) 5 "some default value")
|
||||||
"some default value") ; with default specified
|
"some default value") ; with default specified
|
||||||
(try (do (nth (iter [1 2 4 7]) -1) (assert False))
|
(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)))
|
(assert-equal 5 (nth (take 3 (drop 2 [1 2 3 4 5 6])) 2)))
|
||||||
|
|
||||||
(defn test-numeric? []
|
(defn test-numeric? []
|
||||||
@ -464,11 +464,11 @@
|
|||||||
(assert-true (odd? 1))
|
(assert-true (odd? 1))
|
||||||
(assert-false (odd? 0))
|
(assert-false (odd? 0))
|
||||||
(try (do (odd? "foo") (assert False))
|
(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))
|
(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))
|
(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 []
|
(defn test-partition []
|
||||||
"NATIVE: testing the partition function"
|
"NATIVE: testing the partition function"
|
||||||
@ -488,11 +488,11 @@
|
|||||||
(assert-false (pos? -1))
|
(assert-false (pos? -1))
|
||||||
(assert-false (pos? 0))
|
(assert-false (pos? 0))
|
||||||
(try (do (pos? "foo") (assert False))
|
(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))
|
(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))
|
(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 []
|
(defn test-remove []
|
||||||
"NATIVE: testing the remove function"
|
"NATIVE: testing the remove function"
|
||||||
@ -555,7 +555,7 @@
|
|||||||
(setv res (list (take 0 (repeat "s"))))
|
(setv res (list (take 0 (repeat "s"))))
|
||||||
(assert-equal res [])
|
(assert-equal res [])
|
||||||
(try (do (list (take -1 (repeat "s"))) (assert False))
|
(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])))
|
(setv res (list (take 6 [1 2 None 4])))
|
||||||
(assert-equal res [1 2 None 4]))
|
(assert-equal res [1 2 None 4]))
|
||||||
|
|
||||||
@ -582,7 +582,7 @@
|
|||||||
(let [[passed false]]
|
(let [[passed false]]
|
||||||
(try
|
(try
|
||||||
(setv res (list (take-nth 0 [1 2 3 4 5 6 7])))
|
(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)))
|
(assert passed)))
|
||||||
|
|
||||||
(defn test-take-while []
|
(defn test-take-while []
|
||||||
|
@ -43,25 +43,25 @@
|
|||||||
(defn test-setv-builtin []
|
(defn test-setv-builtin []
|
||||||
"NATIVE: test that setv doesn't work on builtins"
|
"NATIVE: test that setv doesn't work on builtins"
|
||||||
(try (eval '(setv False 1))
|
(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))
|
(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))
|
(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))
|
(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))
|
(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))
|
(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))
|
(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")))
|
(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")))
|
(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")))
|
(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 []
|
(defn test-setv-pairs []
|
||||||
@ -72,16 +72,16 @@
|
|||||||
(setv y 0 x 1 y x)
|
(setv y 0 x 1 y x)
|
||||||
(assert y)
|
(assert y)
|
||||||
(try (eval '(setv a 1 b))
|
(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 []
|
(defn test-fn-corner-cases []
|
||||||
"NATIVE: tests that fn/defn handles corner cases gracefully"
|
"NATIVE: tests that fn/defn handles corner cases gracefully"
|
||||||
(try (eval '(fn "foo"))
|
(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)))))
|
(str e)))))
|
||||||
(try (eval '(defn foo "foo"))
|
(try (eval '(defn foo "foo"))
|
||||||
(catch [e [Exception]]
|
(except [e [Exception]]
|
||||||
(assert (in "takes a parameter list as second" (str e))))))
|
(assert (in "takes a parameter list as second" (str e))))))
|
||||||
|
|
||||||
(defn test-for-loop []
|
(defn test-for-loop []
|
||||||
@ -365,22 +365,22 @@
|
|||||||
|
|
||||||
(try
|
(try
|
||||||
(raise (KeyError))
|
(raise (KeyError))
|
||||||
(catch [[IOError]] (assert false))
|
(except [[IOError]] (assert false))
|
||||||
(catch [e [KeyError]] (assert e)))
|
(except [e [KeyError]] (assert e)))
|
||||||
|
|
||||||
(try
|
(try
|
||||||
(raise (KeyError))
|
(raise (KeyError))
|
||||||
(except [[IOError]] (assert false))
|
(except [[IOError]] (assert false))
|
||||||
(catch [e [KeyError]] (assert e)))
|
(except [e [KeyError]] (assert e)))
|
||||||
|
|
||||||
(try
|
(try
|
||||||
(get [1] 3)
|
(get [1] 3)
|
||||||
(catch [IndexError] (assert true))
|
(except [IndexError] (assert true))
|
||||||
(except [IndexError] (do)))
|
(except [IndexError] (do)))
|
||||||
|
|
||||||
(try
|
(try
|
||||||
(print foobar42ofthebaz)
|
(print foobar42ofthebaz)
|
||||||
(catch [IndexError] (assert false))
|
(except [IndexError] (assert false))
|
||||||
(except [NameError] (do)))
|
(except [NameError] (do)))
|
||||||
|
|
||||||
(try
|
(try
|
||||||
@ -389,7 +389,7 @@
|
|||||||
|
|
||||||
(try
|
(try
|
||||||
(get [1] 3)
|
(get [1] 3)
|
||||||
(catch [e [IndexError NameError]] (assert (isinstance e IndexError))))
|
(except [e [IndexError NameError]] (assert (isinstance e IndexError))))
|
||||||
|
|
||||||
(try
|
(try
|
||||||
(print foobar42ofthebaz)
|
(print foobar42ofthebaz)
|
||||||
@ -397,15 +397,15 @@
|
|||||||
|
|
||||||
(try
|
(try
|
||||||
(print foobar42)
|
(print foobar42)
|
||||||
(catch [[IndexError NameError]] (do)))
|
(except [[IndexError NameError]] (do)))
|
||||||
|
|
||||||
(try
|
(try
|
||||||
(get [1] 3)
|
(get [1] 3)
|
||||||
(catch [[IndexError NameError]] (do)))
|
(except [[IndexError NameError]] (do)))
|
||||||
|
|
||||||
(try
|
(try
|
||||||
(print foobar42ofthebaz)
|
(print foobar42ofthebaz)
|
||||||
(catch))
|
(except))
|
||||||
|
|
||||||
(try
|
(try
|
||||||
(print foobar42ofthebaz)
|
(print foobar42ofthebaz)
|
||||||
@ -417,7 +417,7 @@
|
|||||||
|
|
||||||
(try
|
(try
|
||||||
(print foobar42ofthebaz)
|
(print foobar42ofthebaz)
|
||||||
(catch []
|
(except []
|
||||||
(setv foobar42ofthebaz 42)
|
(setv foobar42ofthebaz 42)
|
||||||
(assert (= foobar42ofthebaz 42))))
|
(assert (= foobar42ofthebaz 42))))
|
||||||
|
|
||||||
@ -754,7 +754,7 @@
|
|||||||
6))
|
6))
|
||||||
(try
|
(try
|
||||||
(assert (= x 42)) ; This ain't true
|
(assert (= x 42)) ; This ain't true
|
||||||
(catch [e [NameError]] (assert e)))
|
(except [e [NameError]] (assert e)))
|
||||||
(assert (= y 123)))
|
(assert (= y 123)))
|
||||||
|
|
||||||
|
|
||||||
@ -872,7 +872,7 @@
|
|||||||
(do
|
(do
|
||||||
(eval ~@body)
|
(eval ~@body)
|
||||||
(assert False "we shouldn't have arrived here"))
|
(assert False "we shouldn't have arrived here"))
|
||||||
(catch [e Exception]
|
(except [e Exception]
|
||||||
(assert (instance? ~exc-type e)
|
(assert (instance? ~exc-type e)
|
||||||
(.format "Expected exception of type {}, got {}: {}"
|
(.format "Expected exception of type {}, got {}: {}"
|
||||||
(. ~exc-type --name--)
|
(. ~exc-type --name--)
|
||||||
@ -891,7 +891,7 @@
|
|||||||
; this should fail with a name error
|
; this should fail with a name error
|
||||||
(eval (quote x) d2)
|
(eval (quote x) d2)
|
||||||
(assert False "We shouldn't have arrived here"))
|
(assert False "We shouldn't have arrived here"))
|
||||||
(catch [e Exception]
|
(except [e Exception]
|
||||||
(assert (isinstance e NameError))))))
|
(assert (isinstance e NameError))))))
|
||||||
|
|
||||||
(defn test-eval-failure []
|
(defn test-eval-failure []
|
||||||
@ -991,7 +991,7 @@
|
|||||||
"NATIVE: test requiring macros from python code"
|
"NATIVE: test requiring macros from python code"
|
||||||
(try
|
(try
|
||||||
(assert (= "this won't happen" (qplah 1 2 3 4)))
|
(assert (= "this won't happen" (qplah 1 2 3 4)))
|
||||||
(catch [NameError]))
|
(except [NameError]))
|
||||||
(require tests.resources.tlib)
|
(require tests.resources.tlib)
|
||||||
(assert (= [1 2 3] (qplah 1 2 3))))
|
(assert (= [1 2 3] (qplah 1 2 3))))
|
||||||
|
|
||||||
@ -1159,7 +1159,7 @@
|
|||||||
"NATIVE: test lambda lists are only parsed in defn"
|
"NATIVE: test lambda lists are only parsed in defn"
|
||||||
(try
|
(try
|
||||||
(foo [&rest spam] 1)
|
(foo [&rest spam] 1)
|
||||||
(catch [NameError] True)
|
(except [NameError] True)
|
||||||
(else (raise AssertionError))))
|
(else (raise AssertionError))))
|
||||||
|
|
||||||
(defn test-read []
|
(defn test-read []
|
||||||
@ -1183,7 +1183,7 @@
|
|||||||
(read stdin-buffer)
|
(read stdin-buffer)
|
||||||
(try
|
(try
|
||||||
(read stdin-buffer)
|
(read stdin-buffer)
|
||||||
(catch [e Exception]
|
(except [e Exception]
|
||||||
(assert (isinstance e EOFError)))))
|
(assert (isinstance e EOFError)))))
|
||||||
|
|
||||||
(defn test-read-str []
|
(defn test-read-str []
|
||||||
|
@ -180,14 +180,14 @@
|
|||||||
product-of-test-matrices))
|
product-of-test-matrices))
|
||||||
;; Python <= 3.4
|
;; Python <= 3.4
|
||||||
(let [[matmul-attempt (try (@ first-test-matrix second-test-matrix)
|
(let [[matmul-attempt (try (@ first-test-matrix second-test-matrix)
|
||||||
(catch [e [Exception]] e))]]
|
(except [e [Exception]] e))]]
|
||||||
(assert (isinstance matmul-attempt NameError)))))
|
(assert (isinstance matmul-attempt NameError)))))
|
||||||
|
|
||||||
(defn test-augassign-matmul []
|
(defn test-augassign-matmul []
|
||||||
"NATIVE: test augmented-assignment matrix multiplication"
|
"NATIVE: test augmented-assignment matrix multiplication"
|
||||||
(let [[matrix first-test-matrix]
|
(let [[matrix first-test-matrix]
|
||||||
[matmul-attempt (try (@= matrix second-test-matrix)
|
[matmul-attempt (try (@= matrix second-test-matrix)
|
||||||
(catch [e [Exception]] e))]]
|
(except [e [Exception]] e))]]
|
||||||
(if PY35
|
(if PY35
|
||||||
(assert (= product-of-test-matrices matrix))
|
(assert (= product-of-test-matrices matrix))
|
||||||
(assert (isinstance matmul-attempt NameError)))))
|
(assert (isinstance matmul-attempt NameError)))))
|
||||||
|
@ -253,7 +253,7 @@
|
|||||||
(yield i))
|
(yield i))
|
||||||
(try
|
(try
|
||||||
(yield-from (yield-from-subgenerator-test))
|
(yield-from (yield-from-subgenerator-test))
|
||||||
(catch [e AssertionError]
|
(except [e AssertionError]
|
||||||
(yield 4))))
|
(yield 4))))
|
||||||
(assert (= (list (yield-from-test)) [0 1 2 1 2 3 4])))
|
(assert (= (list (yield-from-test)) [0 1 2 1 2 3 4])))
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@
|
|||||||
(let [[kwonly-foo-no-default (fn [&kwonly foo] foo)]
|
(let [[kwonly-foo-no-default (fn [&kwonly foo] foo)]
|
||||||
[attempt-to-omit-default (try
|
[attempt-to-omit-default (try
|
||||||
(kwonly-foo-no-default)
|
(kwonly-foo-no-default)
|
||||||
(catch [e [Exception]] e))]]
|
(except [e [Exception]] e))]]
|
||||||
;; works
|
;; works
|
||||||
(assert (= (apply kwonly-foo-no-default [] {"foo" "quux"}) "quux"))
|
(assert (= (apply kwonly-foo-no-default [] {"foo" "quux"}) "quux"))
|
||||||
;; raises TypeError with appropriate message if not supplied
|
;; raises TypeError with appropriate message if not supplied
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
(let [[x +]]
|
(let [[x +]]
|
||||||
(assert (try
|
(assert (try
|
||||||
(x)
|
(x)
|
||||||
(catch [TypeError] True)
|
(except [TypeError] True)
|
||||||
(else (raise AssertionError))))
|
(else (raise AssertionError))))
|
||||||
(assert (= (x 1 2 3 4) 10))
|
(assert (= (x 1 2 3 4) 10))
|
||||||
(assert (= (x 1 2 3 4 5) 15))
|
(assert (= (x 1 2 3 4 5) 15))
|
||||||
@ -24,7 +24,7 @@
|
|||||||
(let [[x -]]
|
(let [[x -]]
|
||||||
(assert (try
|
(assert (try
|
||||||
(x)
|
(x)
|
||||||
(catch [TypeError] True)
|
(except [TypeError] True)
|
||||||
(else (raise AssertionError))))
|
(else (raise AssertionError))))
|
||||||
(assert (= (x 1) -1))
|
(assert (= (x 1) -1))
|
||||||
(assert (= (x 2 1) 1))
|
(assert (= (x 2 1) 1))
|
||||||
@ -44,7 +44,7 @@
|
|||||||
(let [[x /]]
|
(let [[x /]]
|
||||||
(assert (try
|
(assert (try
|
||||||
(x)
|
(x)
|
||||||
(catch [TypeError] True)
|
(except [TypeError] True)
|
||||||
(else (raise AssertionError))))
|
(else (raise AssertionError))))
|
||||||
(assert (= (x 1) 1))
|
(assert (= (x 1) 1))
|
||||||
(assert (= (x 8 2) 4))
|
(assert (= (x 8 2) 4))
|
||||||
@ -57,11 +57,11 @@
|
|||||||
(for [x [< <= = != >= >]]
|
(for [x [< <= = != >= >]]
|
||||||
(assert (try
|
(assert (try
|
||||||
(x)
|
(x)
|
||||||
(catch [TypeError] True)
|
(except [TypeError] True)
|
||||||
(else (raise AssertionError))))
|
(else (raise AssertionError))))
|
||||||
(assert (try
|
(assert (try
|
||||||
(x 1)
|
(x 1)
|
||||||
(catch [TypeError] True)
|
(except [TypeError] True)
|
||||||
(else (raise AssertionError)))))
|
(else (raise AssertionError)))))
|
||||||
(for [(, x y) [[< >=]
|
(for [(, x y) [[< >=]
|
||||||
[<= >]
|
[<= >]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user