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``
|
``loop`` establishes a recursion point. With ``loop``, ``recur``
|
||||||
rebinds the variables set in the recursion point and sends code
|
rebinds the variables set in the recursion point and sends code
|
||||||
execution back to that recursion point. If ``recur`` is used in a
|
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)`
|
Usage: `(loop bindings &rest body)`
|
||||||
|
|
||||||
|
@ -63,10 +63,10 @@ Compiles down to:
|
|||||||
which to do the attribute dereference. It uses bare symbols as attributes
|
which to do the attribute dereference. It uses bare symbols as attributes
|
||||||
to access (in the example, *bar*, *baz*, *frob*), and compiles the contents
|
to access (in the example, *bar*, *baz*, *frob*), and compiles the contents
|
||||||
of lists (in the example, ``[(+ 1 2)]``) for indexation. Other arguments
|
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
|
Access to unknown attributes raises an :exc:`AttributeError`. Access to
|
||||||
unknown keys throws an :exc:`IndexError` (on lists and tuples) or a
|
unknown keys raises an :exc:`IndexError` (on lists and tuples) or a
|
||||||
:exc:`KeyError` (on dictionaries).
|
: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
|
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``
|
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
|
.. code-block:: clj
|
||||||
|
|
||||||
@ -1275,25 +1275,25 @@ counted starting from the end of the list. Some example usage:
|
|||||||
[6, 7]
|
[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:
|
runtime. Example usage:
|
||||||
|
|
||||||
.. code-block:: clj
|
.. code-block:: clj
|
||||||
|
|
||||||
(throw)
|
(raise)
|
||||||
; re-rase the last exception
|
; re-rase the last exception
|
||||||
|
|
||||||
(throw IOError)
|
(raise IOError)
|
||||||
; Throw an IOError
|
; raise an IOError
|
||||||
|
|
||||||
(throw (IOError "foobar"))
|
(raise (IOError "foobar"))
|
||||||
; Throw an 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``.
|
or no arguments to re-raise the last ``Exception``.
|
||||||
|
|
||||||
|
|
||||||
|
@ -753,10 +753,9 @@ class HyASTCompiler(object):
|
|||||||
expression.pop(0)
|
expression.pop(0)
|
||||||
return self._compile_branch(expression)
|
return self._compile_branch(expression)
|
||||||
|
|
||||||
@builds("throw")
|
|
||||||
@builds("raise")
|
@builds("raise")
|
||||||
@checkargs(multiple=[0, 1, 3])
|
@checkargs(multiple=[0, 1, 3])
|
||||||
def compile_throw_expression(self, expr):
|
def compile_raise_expression(self, expr):
|
||||||
expr.pop(0)
|
expr.pop(0)
|
||||||
ret = Result()
|
ret = Result()
|
||||||
if expr:
|
if expr:
|
||||||
|
@ -381,7 +381,7 @@
|
|||||||
(while true
|
(while true
|
||||||
(def inn (str (.read from-file 1)))
|
(def inn (str (.read from-file 1)))
|
||||||
(if (= inn eof)
|
(if (= inn eof)
|
||||||
(throw (EOFError "Reached end of file" )))
|
(raise (EOFError "Reached end of file" )))
|
||||||
(setv buff (+ buff inn))
|
(setv buff (+ buff inn))
|
||||||
(try
|
(try
|
||||||
(def parsed (first (tokenize buff)))
|
(def parsed (first (tokenize buff)))
|
||||||
|
@ -116,6 +116,6 @@ class HyMacroExpansionError(HyTypeError):
|
|||||||
class HyIOError(HyError, IOError):
|
class HyIOError(HyError, IOError):
|
||||||
"""
|
"""
|
||||||
Trivial subclass of IOError and HyError, to distinguish between
|
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
|
pass
|
||||||
|
@ -111,17 +111,6 @@ def test_ast_good_do():
|
|||||||
can_compile("(do 1)")
|
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():
|
def test_ast_good_raise():
|
||||||
"Make sure AST can compile valid raise"
|
"Make sure AST can compile valid raise"
|
||||||
can_compile("(raise)")
|
can_compile("(raise)")
|
||||||
|
@ -115,21 +115,21 @@
|
|||||||
(for [x (range 2)
|
(for [x (range 2)
|
||||||
y (range 2)]
|
y (range 2)]
|
||||||
(break)
|
(break)
|
||||||
(else (throw Exception)))
|
(else (raise Exception)))
|
||||||
|
|
||||||
;; OK. This next test will ensure that the else is hooked up to the
|
;; OK. This next test will ensure that the else is hooked up to the
|
||||||
;; "inner" iteration
|
;; "inner" iteration
|
||||||
(for [x (range 2)
|
(for [x (range 2)
|
||||||
y (range 2)]
|
y (range 2)]
|
||||||
(if (= y 1) (break))
|
(if (= y 1) (break))
|
||||||
(else (throw Exception)))
|
(else (raise Exception)))
|
||||||
|
|
||||||
;; OK. This next test will ensure that the else is hooked up to the
|
;; OK. This next test will ensure that the else is hooked up to the
|
||||||
;; "outer" iteration
|
;; "outer" iteration
|
||||||
(for [x (range 2)
|
(for [x (range 2)
|
||||||
y (range 2)]
|
y (range 2)]
|
||||||
(if (= x 1) (break))
|
(if (= x 1) (break))
|
||||||
(else (throw Exception)))
|
(else (raise Exception)))
|
||||||
|
|
||||||
;; OK. This next test will ensure that we call the else branch exactly
|
;; OK. This next test will ensure that we call the else branch exactly
|
||||||
;; once.
|
;; once.
|
||||||
@ -369,7 +369,7 @@
|
|||||||
(catch [e [KeyError]] (assert e)))
|
(catch [e [KeyError]] (assert e)))
|
||||||
|
|
||||||
(try
|
(try
|
||||||
(throw (KeyError))
|
(raise (KeyError))
|
||||||
(except [[IOError]] (assert false))
|
(except [[IOError]] (assert false))
|
||||||
(catch [e [KeyError]] (assert e)))
|
(catch [e [KeyError]] (assert e)))
|
||||||
|
|
||||||
@ -867,7 +867,7 @@
|
|||||||
(assert (= None (eval (quote (print ""))))))
|
(assert (= None (eval (quote (print ""))))))
|
||||||
|
|
||||||
|
|
||||||
(defmacro assert-throw [exc-type &rest body]
|
(defmacro assert-raise [exc-type &rest body]
|
||||||
`(try
|
`(try
|
||||||
(do
|
(do
|
||||||
(eval ~@body)
|
(eval ~@body)
|
||||||
@ -897,10 +897,10 @@
|
|||||||
(defn test-eval-failure []
|
(defn test-eval-failure []
|
||||||
"NATIVE: test eval failure modes"
|
"NATIVE: test eval failure modes"
|
||||||
(import [hy.errors [HyTypeError]])
|
(import [hy.errors [HyTypeError]])
|
||||||
(assert-throw HyTypeError '(eval))
|
(assert-raise HyTypeError '(eval))
|
||||||
(assert-throw HyTypeError '(eval "snafu"))
|
(assert-raise HyTypeError '(eval "snafu"))
|
||||||
(assert-throw HyTypeError '(eval 'false []))
|
(assert-raise HyTypeError '(eval 'false []))
|
||||||
(assert-throw HyTypeError '(eval 'false {} 1)))
|
(assert-raise HyTypeError '(eval 'false {} 1)))
|
||||||
|
|
||||||
|
|
||||||
(defn test-import-syntax []
|
(defn test-import-syntax []
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
(assert (try
|
(assert (try
|
||||||
(x)
|
(x)
|
||||||
(catch [TypeError] True)
|
(catch [TypeError] True)
|
||||||
(else (throw 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))
|
||||||
; with strings
|
; with strings
|
||||||
@ -25,7 +25,7 @@
|
|||||||
(assert (try
|
(assert (try
|
||||||
(x)
|
(x)
|
||||||
(catch [TypeError] True)
|
(catch [TypeError] True)
|
||||||
(else (throw AssertionError))))
|
(else (raise AssertionError))))
|
||||||
(assert (= (x 1) -1))
|
(assert (= (x 1) -1))
|
||||||
(assert (= (x 2 1) 1))
|
(assert (= (x 2 1) 1))
|
||||||
(assert (= (x 2 1 1) 0))))
|
(assert (= (x 2 1 1) 0))))
|
||||||
@ -45,7 +45,7 @@
|
|||||||
(assert (try
|
(assert (try
|
||||||
(x)
|
(x)
|
||||||
(catch [TypeError] True)
|
(catch [TypeError] True)
|
||||||
(else (throw AssertionError))))
|
(else (raise AssertionError))))
|
||||||
(assert (= (x 1) 1))
|
(assert (= (x 1) 1))
|
||||||
(assert (= (x 8 2) 4))
|
(assert (= (x 8 2) 4))
|
||||||
(assert (= (x 8 2 2) 2))
|
(assert (= (x 8 2 2) 2))
|
||||||
@ -58,11 +58,11 @@
|
|||||||
(assert (try
|
(assert (try
|
||||||
(x)
|
(x)
|
||||||
(catch [TypeError] True)
|
(catch [TypeError] True)
|
||||||
(else (throw AssertionError))))
|
(else (raise AssertionError))))
|
||||||
(assert (try
|
(assert (try
|
||||||
(x 1)
|
(x 1)
|
||||||
(catch [TypeError] True)
|
(catch [TypeError] True)
|
||||||
(else (throw AssertionError)))))
|
(else (raise AssertionError)))))
|
||||||
(for [(, x y) [[< >=]
|
(for [(, x y) [[< >=]
|
||||||
[<= >]
|
[<= >]
|
||||||
[= !=]]]
|
[= !=]]]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user