Esempio n. 1
0
def create_get_type_of_member(owner_var,
                              member_name,
                              lineno,
                              col_offset,
                              test_unreferenced=True):
    comment = create_src_comment(
        "Obtaining the member '{0}' of a type".format(member_name), lineno)
    localization = create_localization(lineno, col_offset)
    # TODO: Remove?
    # get_type_of_member_func = core_language_copy.create_Name('get_type_of_member')
    # get_type_of_member_call = functions_copy.create_call(get_type_of_member_func, [localization, owner_var,
    #                                                                           core_language_copy.create_str(
    #                                                                               member_name)])

    get_type_of_member_func = core_language_copy.create_attribute(
        owner_var, 'get_type_of_member')
    if not test_unreferenced:
        get_type_of_member_call = functions_copy.create_call(
            get_type_of_member_func, [
                localization,
                core_language_copy.create_str(member_name),
                core_language_copy.create_Name('False')
            ])
    else:
        get_type_of_member_call = functions_copy.create_call(
            get_type_of_member_func,
            [localization,
             core_language_copy.create_str(member_name)])

    member_stmts, member_var = create_temp_Assign(get_type_of_member_call,
                                                  lineno, col_offset)

    return flatten_lists(comment, member_stmts), member_var
Esempio n. 2
0
def create_get_type_of(var_name, lineno, col_offset, test_unreferenced=True):
    get_type_of_comment = create_src_comment(
        "Getting the type of '{0}'".format(var_name), lineno)
    get_type_of_method = core_language_copy.create_attribute(
        default_module_type_store_var_name,
        "get_type_of",
        line=lineno,
        column=col_offset)
    localization = create_localization(lineno, col_offset)
    if test_unreferenced:
        get_type_of_call = functions_copy.create_call(
            get_type_of_method,
            [localization,
             core_language_copy.create_str(var_name)])
    else:
        get_type_of_call = functions_copy.create_call(get_type_of_method, [
            localization,
            core_language_copy.create_str(var_name),
            core_language_copy.create_Name('False')
        ])

    assign_stmts, temp_assign = create_temp_Assign(get_type_of_call, lineno,
                                                   col_offset)

    return flatten_lists(get_type_of_comment, assign_stmts), temp_assign
Esempio n. 3
0
def create_unsupported_feature_call(localization, feature_name, feature_desc,
                                    lineno, col_offset):
    """
    Creates AST nodes to call to the unsupported_python_feature function
    """
    unsupported_feature_func = core_language_copy.create_Name(
        'unsupported_python_feature', line=lineno, column=col_offset)
    unsupported_feature = core_language_copy.create_str(feature_name)
    unsupported_description = core_language_copy.create_str(feature_desc)
    return functions_copy.create_call_expression(
        unsupported_feature_func,
        [localization, unsupported_feature, unsupported_description])
Esempio n. 4
0
def create_keyword_dict(keywords):
    dict_node = ast.Dict(ctx=ast.Load(), keys=[], values=[])

    if keywords is not None:
        for elem in keywords:
            dict_node.keys.append(core_language_copy.create_str(elem))
            dict_node.values.append(keywords[elem])

    return dict_node
Esempio n. 5
0
def create_set_type_of(var_name, new_value, lineno, col_offset):
    set_type_of_comment = create_src_comment("Type assignment", lineno)
    set_type_of_method = core_language_copy.create_attribute(
        default_module_type_store_var_name, "set_type_of")

    localization = create_localization(lineno, col_offset)

    set_type_of_call = functions_copy.create_call_expression(
        set_type_of_method, [
            localization,
            core_language_copy.create_str(var_name, lineno, col_offset),
            new_value
        ])

    return flatten_lists(set_type_of_comment, set_type_of_call)
Esempio n. 6
0
def create_stacktrace_push(func_name, declared_arguments):
    """
    Creates an AST Node that model the call to the localitazion.set_stack_trace method

    :param func_name: Name of the function that will do the push to the stack trace
    :param declared_arguments: Arguments of the call
    :return: An AST Expr node
    """
    # Code to push a new stack trace to handle errors.
    attribute = core_language_copy.create_attribute("localization", "set_stack_trace")
    arguments_var = core_language_copy.create_Name("arguments")
    stack_push_call = create_call(attribute, [core_language_copy.create_str(func_name), declared_arguments, arguments_var])
    stack_push = ast.Expr()
    stack_push.value = stack_push_call

    return stack_push
Esempio n. 7
0
def create_context_set(func_name, lineno, col_offset):
    """
    Creates an AST Node that model the call to the type_store.set_context method

    :param func_name: Name of the function that will do the push to the stack trace
    :param lineno: Line
    :param col_offset: Column
    :return: An AST Expr node
    """
    attribute = core_language_copy.create_attribute("type_store", "set_context")
    context_set_call = create_call(attribute, [core_language_copy.create_str(func_name),
                                               core_language_copy.create_num(lineno),
                                               core_language_copy.create_num(col_offset)])
    context_set = ast.Expr()
    context_set.value = context_set_call

    return context_set
Esempio n. 8
0
def create_unary_operator(operator_name, left_op, lineno, col_offset):
    """
    Creates AST Nodes to model an unary operator

    :param operator_name: Name of the operator
    :param left_op: operand (AST Node)
    :param lineno: Line
    :param col_offset: Column
    :return: List of instructions
    """
    operator_symbol = operator_name_to_symbol(operator_name)
    op_name = core_language_copy.create_str(operator_symbol)
    operation_comment = create_src_comment(
        "Applying the '{0}' unary operator".format(operator_symbol), lineno)
    operator_call, result_var = create_temp_Assign(
        operators_copy.create_unary_operator(op_name, left_op, lineno,
                                             col_offset), lineno, col_offset)

    return flatten_lists(operation_comment, operator_call), result_var
Esempio n. 9
0
def obtain_arg_list(args, ismethod=False, isstaticmethod=False):
    """
    Creates an AST List node with the names of the arguments passed to a function
    :param args: Arguments
    :param ismethod: Whether to count the first argument (self) or not
    :return: An AST List
    """
    arg_list = ast.List()

    arg_list.elts = []
    if ismethod and not isstaticmethod:
        arg_list_contents = args.args[1:]
    else:
        arg_list_contents = args.args

    for arg in arg_list_contents:
        arg_list.elts.append(core_language_copy.create_str(arg.id))

    return arg_list
Esempio n. 10
0
def create_set_type_of_member(owner_var, member_name, value, lineno,
                              col_offset):
    comment = create_src_comment(
        "Setting the type of the member '{0}' of a type".format(member_name),
        lineno)
    localization = create_localization(lineno, col_offset)
    # TODO: Remove?
    # set_type_of_member_func = core_language_copy.create_Name('set_type_of_member')
    # set_type_of_member_call = functions_copy.create_call_expression(set_type_of_member_func, [localization, onwer_var,
    #                                                                           core_language_copy.create_str(
    #                                                                               member_name), value])

    set_type_of_member_func = core_language_copy.create_attribute(
        owner_var, 'set_type_of_member')
    set_type_of_member_call = functions_copy.create_call_expression(
        set_type_of_member_func,
        [localization,
         core_language_copy.create_str(member_name), value])

    return flatten_lists(comment, set_type_of_member_call)
Esempio n. 11
0
def create_arg_number_test(function_def_node, context=[]):
    """
    Creates an AST Node that model the call to the process_argument_values method. This method is used to check
    the parameters passed to a function/method in a type inference program

    :param function_def_node: AST Node with the function definition
    :param context: Context passed to the call
    :return: List of AST nodes that perform the call to the mentioned function and make the necessary tests once it
    is called
    """
    args_test_resul = core_language_copy.create_Name('arguments', False)

    # Call to arg test function
    func = core_language_copy.create_Name('process_argument_values')
    # Fixed parameters
    localization_arg = core_language_copy.create_Name('localization')
    type_store_arg = core_language_copy.create_Name('type_store')

    # Declaration data arguments
    # Func name
    if is_method(context):
        function_name_arg = core_language_copy.create_str(context[-1].name + "." + function_def_node.name)
        type_of_self_arg = core_language_copy.create_Name('type_of_self')
    else:
        function_name_arg = core_language_copy.create_str(function_def_node.name)
        type_of_self_arg = core_language_copy.create_Name('None')

    # Declared param names list
    param_names_list_arg = obtain_arg_list(function_def_node.args, is_method(context),
                                           is_static_method(function_def_node))

    # Declared var args parameter name
    if function_def_node.args.vararg is None:
        declared_varargs = None
    else:
        declared_varargs = function_def_node.args.vararg
    varargs_param_name = core_language_copy.create_str(declared_varargs)
    # Declared kwargs parameter name
    if function_def_node.args.kwarg is None:
        declared_kwargs = None
    else:
        declared_kwargs = function_def_node.args.kwarg
    kwargs_param_name = core_language_copy.create_str(declared_kwargs)

    # Call data arguments
    # Declared defaults list name
    call_defaults = core_language_copy.create_Name('defaults')  # function_def_node.args.defaults

    # Call varargs
    call_varargs = core_language_copy.create_Name('varargs')
    # Call kwargs
    call_kwargs = core_language_copy.create_Name('kwargs')

    # Parameter number check call
    call = create_call(func,
                       [localization_arg, type_of_self_arg, type_store_arg, function_name_arg, param_names_list_arg,
                        varargs_param_name, kwargs_param_name, call_defaults, call_varargs, call_kwargs])

    assign = core_language_copy.create_Assign(args_test_resul, call)

    # After parameter number check call
    argument_errors = core_language_copy.create_Name('arguments')
    is_error_type = core_language_copy.create_Name('is_error_type')
    if_test = create_call(is_error_type, argument_errors)

    if is_constructor(function_def_node):
        argument_errors = None  # core_language.create_Name('None')

    body = [create_context_unset(), create_return(argument_errors)]
    if_ = conditional_statements_copy.create_if(if_test, body)

    return [assign, if_]