from numba.nodes import *
import numba.nodes

class CheckErrorNode(ExprNode):
    """
    Check for an exception.

        badval: if this value is returned, propagate an error
        goodval: if this value is not returned, propagate an error

    If exc_type, exc_msg and optionally exc_args are given, an error is
    raised instead of propagating it.
    """

    _fields = ['return_value', 'badval', 'raise_node']

    def __init__(self, return_value, badval=None, goodval=None,
                 exc_type=None, exc_msg=None, exc_args=None,
                 **kwargs):
        super(CheckErrorNode, self).__init__(**kwargs)
        self.return_value = return_value

        if badval is not None and not isinstance(badval, ast.AST):
            badval = ConstNode(badval, return_value.type)
        if goodval is not None and not isinstance(goodval, ast.AST):
            goodval = ConstNode(goodval, return_value.type)

        self.badval = badval
        self.goodval = goodval

        self.raise_node = RaiseNode(exc_type, exc_msg, exc_args)

class RaiseNode(ExprNode):

    _fields = ['exc_type', 'exc_msg', 'exc_args']

    def __init__(self, exc_type, exc_msg, exc_args=None, print_on_trap=True,
                 **kwargs):
        super(RaiseNode, self).__init__(**kwargs)
        self.exception_type = None
        if isinstance(exc_type, type) and issubclass(exc_type, BaseException):
            self.exception_type = exc_type
            exc_type = const(exc_type, object_)
        if isinstance(exc_msg, (str, unicode)):
            exc_msg = const(exc_msg, c_string_type)

        self.exc_type = exc_type
        self.exc_msg = exc_msg
        self.exc_args = exc_args

        self.print_on_trap = print_on_trap

class PropagateNode(ExprNode):
    """
    Propagate an exception (jump to the error label).
    """

@@ -347,16 +347,60 @@
 ting it.
+%0A%0A    See RaiseNode for the exc_* arguments.
 %0A    %22%22%22
@@ -1115,16 +1115,505 @@
 prNode):
+%0A    %22%22%22%0A    Raise an exception.%0A%0A        exception_type: The Python exception type%0A%0A        exc_type: The Python exception as an AST node%0A            May be passed in as a Python exception type%0A%0A        exc_msg: The message to print as an AST node%0A            May be passed in as a string%0A%0A        exc_args: If given, must be an list of AST nodes representing the%0A                  arguments to PyErr_Format (matching the format specifiers%0A                  at runtime in exc_msg)%0A    %22%22%22
 %0A%0A    _f
@@ -2295,24 +2295,24 @@
 e):%0A    %22%22%22%0A
-
     Propagat
@@ -2352,16 +2352,97 @@
  label).
+ This is resolved%0A    at code generation time and can be generated at any moment.
 %0A    %22%22%22

Document AST error nodes