remove throw in favor of raise
This commit is contained in:
parent
2963ce6121
commit
e8d26f1067
@ -37,7 +37,7 @@ loop
|
||||
``loop`` establishes a recursion point. With ``loop``, ``recur``
|
||||
rebinds the variables set in the recursion point and sends code
|
||||
execution back to that recursion point. If ``recur`` is used in a
|
||||
non-tail position, an exception is thrown.
|
||||
non-tail position, an exception is raised.
|
||||
|
||||
Usage: `(loop bindings &rest body)`
|
||||
|
||||
|
@ -63,10 +63,10 @@ Compiles down to:
|
||||
which to do the attribute dereference. It uses bare symbols as attributes
|
||||
to access (in the example, *bar*, *baz*, *frob*), and compiles the contents
|
||||
of lists (in the example, ``[(+ 1 2)]``) for indexation. Other arguments
|
||||
throw a compilation error.
|
||||
raise a compilation error.
|
||||
|
||||
Access to unknown attributes throws an :exc:`AttributeError`. Access to
|
||||
unknown keys throws an :exc:`IndexError` (on lists and tuples) or a
|
||||
Access to unknown attributes raises an :exc:`AttributeError`. Access to
|
||||
unknown keys raises an :exc:`IndexError` (on lists and tuples) or a
|
||||
:exc:`KeyError` (on dictionaries).
|
||||
|
||||
->
|
||||
@ -835,7 +835,7 @@ assign a value to a global symbol. Reading a global symbol does not require the
|
||||
|
||||
The following example shows how the global symbol ``a`` is assigned a value in a
|
||||
function and is later on printed in another function. Without the ``global``
|
||||
keyword, the second function would have thrown a ``NameError``.
|
||||
keyword, the second function would have raised a ``NameError``.
|
||||
|
||||
.. code-block:: clj
|
||||
|
||||
@ -1275,25 +1275,25 @@ counted starting from the end of the list. Some example usage:
|
||||
[6, 7]
|
||||
|
||||
|
||||
throw / raise
|
||||
raise
|
||||
-------------
|
||||
|
||||
The ``throw`` or ``raise`` forms can be used to raise an ``Exception`` at
|
||||
The or ``raise`` forms can be used to raise an ``Exception`` at
|
||||
runtime. Example usage:
|
||||
|
||||
.. code-block:: clj
|
||||
|
||||
(throw)
|
||||
(raise)
|
||||
; re-rase the last exception
|
||||
|
||||
(throw IOError)
|
||||
; Throw an IOError
|
||||
(raise IOError)
|
||||
; raise an IOError
|
||||
|
||||
(throw (IOError "foobar"))
|
||||
; Throw an IOError("foobar")
|
||||
(raise (IOError "foobar"))
|
||||
; raise an IOError("foobar")
|
||||
|
||||
|
||||
``throw`` can accept a single argument (an ``Exception`` class or instance)
|
||||
``raise`` can accept a single argument (an ``Exception`` class or instance)
|
||||
or no arguments to re-raise the last ``Exception``.
|
||||
|
||||
|
||||
|
@ -753,10 +753,9 @@ class HyASTCompiler(object):
|
||||
expression.pop(0)
|
||||
return self._compile_branch(expression)
|
||||
|
||||
@builds("throw")
|
||||
@builds("raise")
|
||||
@checkargs(multiple=[0, 1, 3])
|
||||
def compile_throw_expression(self, expr):
|
||||
def compile_raise_expression(self, expr):
|
||||
expr.pop(0)
|
||||
ret = Result()
|
||||
if expr:
|
||||
|
@ -381,7 +381,7 @@
|
||||
(while true
|
||||
(def inn (str (.read from-file 1)))
|
||||
(if (= inn eof)
|
||||
(throw (EOFError "Reached end of file" )))
|
||||
(raise (EOFError "Reached end of file" )))
|
||||
(setv buff (+ buff inn))
|
||||
(try
|
||||
(def parsed (first (tokenize buff)))
|
||||
|
@ -116,6 +116,6 @@ class HyMacroExpansionError(HyTypeError):
|
||||
class HyIOError(HyError, IOError):
|
||||
"""
|
||||
Trivial subclass of IOError and HyError, to distinguish between
|
||||
IOErrors thrown by Hy itself as opposed to Hy programs.
|
||||
IOErrors raised by Hy itself as opposed to Hy programs.
|
||||
"""
|
||||
pass
|
||||
|
@ -111,17 +111,6 @@ def test_ast_good_do():
|
||||
can_compile("(do 1)")
|
||||
|
||||
|
||||
def test_ast_good_throw():
|
||||
"Make sure AST can compile valid throw"
|
||||
can_compile("(throw)")
|
||||
can_compile("(throw Exception)")
|
||||
|
||||
|
||||
def test_ast_bad_throw():
|
||||
"Make sure AST can't compile invalid throw"
|
||||
cant_compile("(throw Exception Exception)")
|
||||
|
||||
|
||||
def test_ast_good_raise():
|
||||
"Make sure AST can compile valid raise"
|
||||
can_compile("(raise)")
|
||||
|
@ -115,21 +115,21 @@
|
||||
(for [x (range 2)
|
||||
y (range 2)]
|
||||
(break)
|
||||
(else (throw Exception)))
|
||||
(else (raise Exception)))
|
||||
|
||||
;; OK. This next test will ensure that the else is hooked up to the
|
||||
;; "inner" iteration
|
||||
(for [x (range 2)
|
||||
y (range 2)]
|
||||
(if (= y 1) (break))
|
||||
(else (throw Exception)))
|
||||
(else (raise Exception)))
|
||||
|
||||
;; OK. This next test will ensure that the else is hooked up to the
|
||||
;; "outer" iteration
|
||||
(for [x (range 2)
|
||||
y (range 2)]
|
||||
(if (= x 1) (break))
|
||||
(else (throw Exception)))
|
||||
(else (raise Exception)))
|
||||
|
||||
;; OK. This next test will ensure that we call the else branch exactly
|
||||
;; once.
|
||||
@ -369,7 +369,7 @@
|
||||
(catch [e [KeyError]] (assert e)))
|
||||
|
||||
(try
|
||||
(throw (KeyError))
|
||||
(raise (KeyError))
|
||||
(except [[IOError]] (assert false))
|
||||
(catch [e [KeyError]] (assert e)))
|
||||
|
||||
@ -867,7 +867,7 @@
|
||||
(assert (= None (eval (quote (print ""))))))
|
||||
|
||||
|
||||
(defmacro assert-throw [exc-type &rest body]
|
||||
(defmacro assert-raise [exc-type &rest body]
|
||||
`(try
|
||||
(do
|
||||
(eval ~@body)
|
||||
@ -897,10 +897,10 @@
|
||||
(defn test-eval-failure []
|
||||
"NATIVE: test eval failure modes"
|
||||
(import [hy.errors [HyTypeError]])
|
||||
(assert-throw HyTypeError '(eval))
|
||||
(assert-throw HyTypeError '(eval "snafu"))
|
||||
(assert-throw HyTypeError '(eval 'false []))
|
||||
(assert-throw HyTypeError '(eval 'false {} 1)))
|
||||
(assert-raise HyTypeError '(eval))
|
||||
(assert-raise HyTypeError '(eval "snafu"))
|
||||
(assert-raise HyTypeError '(eval 'false []))
|
||||
(assert-raise HyTypeError '(eval 'false {} 1)))
|
||||
|
||||
|
||||
(defn test-import-syntax []
|
||||
|
@ -4,7 +4,7 @@
|
||||
(assert (try
|
||||
(x)
|
||||
(catch [TypeError] True)
|
||||
(else (throw AssertionError))))
|
||||
(else (raise AssertionError))))
|
||||
(assert (= (x 1 2 3 4) 10))
|
||||
(assert (= (x 1 2 3 4 5) 15))
|
||||
; with strings
|
||||
@ -25,7 +25,7 @@
|
||||
(assert (try
|
||||
(x)
|
||||
(catch [TypeError] True)
|
||||
(else (throw AssertionError))))
|
||||
(else (raise AssertionError))))
|
||||
(assert (= (x 1) -1))
|
||||
(assert (= (x 2 1) 1))
|
||||
(assert (= (x 2 1 1) 0))))
|
||||
@ -45,7 +45,7 @@
|
||||
(assert (try
|
||||
(x)
|
||||
(catch [TypeError] True)
|
||||
(else (throw AssertionError))))
|
||||
(else (raise AssertionError))))
|
||||
(assert (= (x 1) 1))
|
||||
(assert (= (x 8 2) 4))
|
||||
(assert (= (x 8 2 2) 2))
|
||||
@ -58,11 +58,11 @@
|
||||
(assert (try
|
||||
(x)
|
||||
(catch [TypeError] True)
|
||||
(else (throw AssertionError))))
|
||||
(else (raise AssertionError))))
|
||||
(assert (try
|
||||
(x 1)
|
||||
(catch [TypeError] True)
|
||||
(else (throw AssertionError)))))
|
||||
(else (raise AssertionError)))))
|
||||
(for [(, x y) [[< >=]
|
||||
[<= >]
|
||||
[= !=]]]
|
||||
|
Loading…
Reference in New Issue
Block a user