Ejemplo n.º 1
0
    def _body_optional_variable(self,
                                tmp_variable,
                                variable,
                                collect_var,
                                check_type=False):
        """
        Responsible for collecting value and managing error and create the body
        of optional arguments in format
                if (pyobject == Py_None){
                    collect Null
                }else if(Type Check == False){
                    Print TypeError Wrong Type
                    return Null
                }else{
                    assign pyobject value to tmp variable
                    collect the adress of the tmp variable
                }
        Parameters:
        ----------
        tmp_variable : Variable
            The temporary variable  to hold result
        Variable : Variable
            The optional variable
        collect_var : variable
            the pyobject type variable  holder of value
        check_type : Boolean
            True if the type is needed

        Returns
        -------
        body : list
            A list of statements
        """
        body = [(PyccelEq(VariableAddress(collect_var),
                          VariableAddress(Py_None)),
                 [Assign(VariableAddress(variable), Nil())])]
        if check_type:  # Type check
            check = PyccelNot(
                PyccelOr(NumpyType_Check(variable, collect_var),
                         PythonType_Check(variable, collect_var)))
            error = PyErr_SetString(
                'PyExc_TypeError',
                '"{} must be {}"'.format(variable, variable.dtype))
            body += [(check, [error, Return([Nil()])])]
        body += [(LiteralTrue(), [
            self._create_collecting_value_body(variable, collect_var,
                                               tmp_variable),
            Assign(VariableAddress(variable), VariableAddress(tmp_variable))
        ])]
        body = [If(*body)]

        return body
Ejemplo n.º 2
0
    def _get_check_type_statement(self, variable, collect_var):

        if variable.rank > 0 :
            numpy_dtype = self.find_in_numpy_dtype_registry(variable)
            check = PyccelEq(FunctionCall(numpy_get_type, [collect_var]), numpy_dtype)

        else :
            python_check = PythonType_Check(variable, collect_var)
            numpy_check = NumpyType_Check(variable, collect_var)
            if variable.precision == default_precision[str_dtype(variable.dtype)] :
                check = PyccelOr(python_check, numpy_check)
            else :
                check = PyccelAssociativeParenthesis(PyccelAnd(PyccelNot(python_check), numpy_check))

        if isinstance(variable, ValuedVariable):
            default = PyccelNot(VariableAddress(collect_var)) if variable.rank > 0 else PyccelEq(VariableAddress(collect_var), VariableAddress(Py_None))
            check = PyccelAssociativeParenthesis(PyccelOr(default, check))

        return check