Example #1
0
def create_type_store(type_store_name=default_module_type_store_var_name):
    """
    Creates code to create a new type store
    :param type_store_name:
    :return:
    """
    call_arg = core_language.create_Name("None")
    call_arg2 = core_language.create_Name("__file__")
    call_func = core_language.create_Name("Context")
    call = functions.create_call(call_func, [call_arg, call_arg2])
    assign_target = core_language.create_Name(type_store_name, False)
    assign = core_language.create_Assign(assign_target, call)

    return assign
Example #2
0
def __remove_not_subtype_from_union_implementation(if_test, type_, lineno,
                                                   col_offset):
    """
    Implements the call to remove_not_subtype_from_union when the idiom is recognized
    :param if_test:
    :param type_:
    :param lineno:
    :param col_offset:
    :return:
    """
    param = __get_idiom_type_param(if_test)
    if type(param) is ast.Name:
        # obj_type, obj_var = stypy_functions.create_get_type_of(param.id, lineno, col_offset)
        remove_type_call = functions.create_call(
            core_language.create_Name("remove_not_subtype_from_union"),
            [type_, if_test.args[1]],
            line=lineno,
            column=col_offset)
        set_type = stypy_functions.create_set_type_of(param.id,
                                                      remove_type_call, lineno,
                                                      col_offset)

        return stypy_functions.flatten_lists(set_type)  # obj_type, set_type)
    if type(param) is ast.Attribute:
        # try:
        # Get the owner of the attribute
        if type(param.value) is ast.Name:
            obj_type_stmts, obj_var = stypy_functions.create_get_type_of(
                param.value.id, lineno, col_offset)
        else:
            obj_type_stmts, obj_var = stypy_functions.create_get_type_of_member(
                param.value, param.value.attr, lineno, col_offset)
        # except Exception as ex:
        #     print ex

        # Get the current type of the owner of the attribute
        att_type_stmts, att_var = stypy_functions.create_get_type_of_member(
            obj_var, param.attr, lineno, col_offset)
        remove_type_call = functions.create_call(
            core_language.create_Name("remove_not_subtype_from_union"),
            [type_, if_test.args[1]],
            line=lineno,
            column=col_offset)
        set_member = stypy_functions.create_set_type_of_member(
            obj_var, param.attr, remove_type_call, lineno, col_offset)
        return stypy_functions.flatten_lists(obj_type_stmts, att_type_stmts,
                                             set_member)

    return []
Example #3
0
def create_open_ssa_context(context_name):
    """
    Creates code to open a new SSA context
    :param context_name:
    :return:
    """
    ssa_context_class = core_language.create_Name("SSAContext")
    attribute = core_language.create_attribute(ssa_context_class,
                                               "create_ssa_context")
    clone_call = functions.create_call(
        attribute,
        core_language.create_Name(default_module_type_store_var_name),
        [core_language.create_str(context_name)])

    return create_temp_type_store_Assign(clone_call)
Example #4
0
def create_get_type_of(var_name, lineno, col_offset, test_unreferenced=True):
    """
    Creates code to get the type of a variable
    :param var_name:
    :param lineno:
    :param col_offset:
    :param test_unreferenced:
    :return:
    """
    get_type_of_comment = create_src_comment(
        "Getting the type of '{0}'".format(var_name), lineno)
    get_type_of_method = core_language.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.create_call(
            get_type_of_method,
            [localization, core_language.create_str(var_name)])
    else:
        get_type_of_call = functions.create_call(get_type_of_method, [
            localization,
            core_language.create_str(var_name),
            core_language.create_Name('False')
        ])

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

    return flatten_lists(get_type_of_comment, assign_stmts), temp_assign
def create_context_set(func_name, lineno, col_offset, access_parent=True):
    """
    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
    :param access_parent: Value of the "has access to its parent context" parameter
    :return: An AST Expr node
    """
    attribute = core_language.create_attribute(
        stypy_functions.default_module_type_store_var_name,
        "open_function_context")
    context_set_call = create_call(attribute, [
        core_language.create_str(func_name),
        core_language.create_num(lineno),
        core_language.create_num(col_offset),
        core_language.create_bool(access_parent)
    ])

    left_hand_side = core_language.create_Name(
        stypy_functions.default_module_type_store_var_name, False)
    assign_statement = ast.Assign([left_hand_side], context_set_call)

    return assign_statement
def create_function_def(name,
                        localization,
                        decorators,
                        context,
                        line=0,
                        column=0):
    """
    Creates a FunctionDef node, that represent a function declaration. This is used in type inference code, so every
    created function has the following parameters (type_of_self, localization, *varargs, **kwargs) for methods and
    (localization, *varargs, **kwargs) for functions.

    :param name: Name of the function
    :param localization: Localization parameter
    :param decorators: Decorators of the function, mainly the norecursion one
    :param context: Context passed to this method
    :param line: Line
    :param column: Column
    :return: An AST FunctionDef node
    """
    function_def_arguments = ast.arguments()
    function_def_arguments.args = [localization]

    isconstructor = is_constructor(name)
    ismethod = is_method(context, decorators)

    function_def = ast.FunctionDef()
    function_def.lineno = line
    function_def.col_offset = column
    if types.type_inspection.is_special_name_method(name):
        function_def.name = types.type_inspection.convert_special_name_method(
            name)
    else:
        function_def.name = name

    function_def.args = function_def_arguments

    function_def_arguments.args = []

    if isconstructor or (ismethod and not isconstructor):
        function_def_arguments.args.append(
            core_language.create_Name('type_of_self'))

    function_def_arguments.args.append(localization)

    function_def_arguments.kwarg = "kwargs"
    function_def_arguments.vararg = "varargs"
    function_def_arguments.defaults = []

    if data_structures.is_iterable(decorators):
        function_def.decorator_list = decorators
    else:
        function_def.decorator_list = [decorators]

    global_ts = ast.Global()
    global_ts.names = [stypy_functions.default_module_type_store_var_name]
    function_def.body = [global_ts]

    return function_def
Example #7
0
def create_default_return_variable():
    """
    Creates AST Nodes that adds the default return variable to a function. Functions of generated type inference
     programs only has a return clause
    """
    return_val_instr = create_set_type_of(default_function_ret_var_name,
                                          core_language.create_Name("None"), 0,
                                          0)
    return return_val_instr
Example #8
0
def create_print_var(variable):
    """
    Creates a node to print a variable
    """
    node = ast.Print()
    node.nl = True
    node.dest = None
    node.values = [core_language.create_Name(variable)]

    return node
Example #9
0
def new_temp_Name(right_hand_side=True,
                  descriptive_var_name="",
                  lineno=0,
                  col_offset=0):
    """
    Creates an AST Name node with a suitable name for a new temp variable. If descriptive_var_name has a value, then
    this value is added to the variable predefined name
    """
    return core_language.create_Name(__new_temp_str(descriptive_var_name),
                                     right_hand_side, lineno, col_offset)
Example #10
0
def create_temp_type_store_Assign(right_hand_side):
    """
    Creates a new type store variable assignment
    :param right_hand_side:
    :return:
    """
    left_hand_side = core_language.create_Name(
        default_module_type_store_var_name, False)
    assign_statement = ast.Assign([left_hand_side], right_hand_side)
    return assign_statement, left_hand_side
Example #11
0
def __create_src_comment(comment_txt):
    """
    Creates a new comment in the source with the provided text
    :param comment_txt:
    :return:
    """
    comment_node = core_language.create_Name(comment_txt)
    comment_expr = ast.Expr()
    comment_expr.value = comment_node

    return comment_expr
Example #12
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.create_Name(
        'unsupported_python_feature', line=lineno, column=col_offset)
    unsupported_feature = core_language.create_str(feature_name)
    unsupported_description = core_language.create_str(feature_desc)
    return functions.create_call_expression(
        unsupported_feature_func,
        [localization, unsupported_feature, unsupported_description])
Example #13
0
def create_original_code_comment(file_name, original_code):
    """
    Creates a Python block comment with the original source file code
    """
    if original_code is not None:
        # Remove block comments, as this code will be placed in a block comment
        original_code = original_code.replace("\"\"\"", "'''")

        numbered_original_code = ModuleLineNumbering.put_line_numbers_to_module_code(
            file_name, original_code)

        comment_txt = core_language.create_Name(
            "\"\"\"\nORIGINAL PROGRAM SOURCE CODE:\n" +
            numbered_original_code + "\n\"\"\"\n")
    else:
        comment_txt = core_language.create_Name("")

    initial_comment = ast.Expr()
    initial_comment.value = comment_txt

    return initial_comment
Example #14
0
def create_get_type_instance_of(type_name, lineno, col_offset):
    """
    Create code to get an instance of the passed type
    :param type_name:
    :param lineno:
    :param col_offset:
    :return:
    """
    get_func = core_language.create_Name("get_builtin_python_type_instance")
    param1 = core_language.create_str(type_name)
    localization = create_localization(lineno, col_offset)
    get_list_call = functions.create_call(get_func, [localization, param1])
    return create_temp_Assign(get_list_call, lineno, col_offset,
                              type_name.replace(".", "_"))
def create_unary_operator(op_name, op, line=0, column=0):
    """
    Creates a binary operator
    :param op_name:
    :param op:
    :param line:
    :param column:
    :return:
    """
    localization = stypy_functions.create_localization(line, column)

    unop_func = core_language.create_Name(stypy_functions.default_operator_call_name)
    unop = functions.create_call(unop_func, [localization, op_name, op])

    return unop
Example #16
0
def create_set_unreferenced_var_check(state):
    """
    Creates code to check for unreferenced variables
    :param state:
    :return:
    """
    if ENABLE_CODING_ADVICES:
        attribute = core_language.create_attribute(
            default_module_type_store_var_name, "set_check_unreferenced_vars")
        call_ = functions.create_call_expression(
            attribute, [core_language.create_Name(str(state))])

        return call_
    else:
        return []
Example #17
0
def create_localization(line, col):
    """
    Creates AST Nodes that creates a new Localization instance
    """
    linen = core_language.create_num(line)
    coln = core_language.create_num(col)
    file_namen = core_language.create_Name('__file__')

    module1 = core_language.create_attribute('stypy', 'reporting')
    module2 = core_language.create_attribute(module1, 'localization')
    loc_namen = core_language.create_attribute(module2, 'Localization')

    loc_call = functions.create_call(loc_namen, [file_namen, linen, coln])

    return loc_call
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.create_attribute("localization",
                                               "set_stack_trace")
    arguments_var = core_language.create_Name("arguments")
    stack_push_call = create_call(attribute, [
        core_language.create_str(func_name), declared_arguments, arguments_var
    ])
    stack_push = ast.Expr()
    stack_push.value = stack_push_call

    return stack_push
def create_type_for_lambda_function(function_name, lambda_call, lineno,
                                    col_offset):
    """
    Creates a variable to store a lambda function definition

    :param function_name: Name of the lambda function
    :param lambda_call: Lambda function
    :param lineno: Line
    :param col_offset: Column
    :return: Statements to create the lambda function type
    """

    call_arg = core_language.create_Name(lambda_call)

    set_type_stmts = stypy_functions.create_set_type_of(
        function_name, call_arg, lineno, col_offset)

    # return stypy_functions.flatten_lists(assign, set_type_stmts)
    return stypy_functions.flatten_lists(set_type_stmts)
Example #20
0
def create_add_stored_type(owner_var, index, value, lineno, col_offset):
    """
    Create code to add an element to a container
    :param owner_var:
    :param index:
    :param value:
    :param lineno:
    :param col_offset:
    :return:
    """
    comment = create_src_comment("Storing an element on a container", lineno)
    localization = create_localization(lineno, col_offset)

    add_type_func = core_language.create_Name('set_contained_elements_type')
    param_tuple = ast.Tuple()
    param_tuple.elts = [index, value]
    set_type_of_member_call = functions.create_call_expression(
        add_type_func, [localization, owner_var, param_tuple])

    return flatten_lists(comment, set_type_of_member_call)
def create_context_unset():
    """
    Creates an AST Node that model the call to the type_store.unset_context method

    :return: An AST Expr node
    """

    # Generate code for pop the function context.
    comment = stypy_functions.create_src_comment("Destroy the current context")

    # Code to pop a stack trace once the function finishes.
    attribute = core_language.create_attribute(
        stypy_functions.default_module_type_store_var_name,
        "close_function_context")
    context_unset_call = create_call(attribute, [])

    left_hand_side = core_language.create_Name(
        stypy_functions.default_module_type_store_var_name, False)
    assign_statement = ast.Assign([left_hand_side], context_unset_call)

    return stypy_functions.flatten_lists(comment, assign_statement)
Example #22
0
def create_get_type_of_member(owner_var,
                              member_name,
                              lineno,
                              col_offset,
                              test_unreferenced=True):
    """
    Creates code to get the type of an object member
    :param owner_var:
    :param member_name:
    :param lineno:
    :param col_offset:
    :param test_unreferenced:
    :return:
    """
    comment = create_src_comment(
        "Obtaining the member '{0}' of a type".format(member_name), lineno)
    localization = create_localization(lineno, col_offset)

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

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

    return flatten_lists(comment, member_stmts), member_var
def create_arg_number_test(function_def_node, decorators, 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 decorators: Decorators of the function
    :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.create_Name('arguments', False)

    # Call to arg test function
    func = core_language.create_Name('process_argument_values')
    # Fixed parameters
    localization_arg = core_language.create_Name('localization')
    type_store_arg = core_language.create_Name(
        stypy_functions.default_module_type_store_var_name)

    # Declaration data arguments
    # Func name
    if is_method(context, decorators):
        if types.type_inspection.is_special_name_method(
                function_def_node.name):
            function_name_arg = core_language.create_str(
                context[-1].name + "." +
                types.type_inspection.convert_special_name_method(
                    function_def_node.name))
        else:
            function_name_arg = core_language.create_str(
                context[-1].name + "." + function_def_node.name)
        type_of_self_arg = core_language.create_Name('type_of_self')
    else:
        if types.type_inspection.is_special_name_method(
                function_def_node.name):
            function_name_arg = core_language.create_str(
                types.type_inspection.convert_special_name_method(
                    function_def_node.name))
        else:
            function_name_arg = core_language.create_str(
                function_def_node.name)
        type_of_self_arg = core_language.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.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.create_str(declared_kwargs)

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

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

    # Store call information into the function object to recursion checks
    if is_method(context):
        f_name = function_def_node.name
        if types.type_inspection.is_special_name_method(
                function_def_node.name):
            f_name = types.type_inspection.convert_special_name_method(f_name)

        f_var_name = core_language.create_attribute(
            core_language.create_attribute(context[-1].name, f_name),
            '__dict__')
    else:
        if types.type_inspection.is_special_name_method(
                function_def_node.name):
            f_var_name = core_language.create_Name(
                types.type_inspection.convert_special_name_method(
                    function_def_node.name), False)
        else:
            f_var_name = core_language.create_Name(function_def_node.name,
                                                   False)

    info_storage_instr = []

    if function_def_node.name is not '__init__':
        if is_method(context):
            subscript_call = create_call_expression(
                core_language.create_attribute(f_var_name, '__setitem__'), [
                    core_language.create_str('stypy_localization'),
                    localization_arg
                ])

            info_storage_instr.append(subscript_call)

            subscript_call = create_call_expression(
                core_language.create_attribute(f_var_name, '__setitem__'), [
                    core_language.create_str('stypy_type_of_self'),
                    type_of_self_arg
                ])

            info_storage_instr.append(subscript_call)

            subscript_call = create_call_expression(
                core_language.create_attribute(f_var_name, '__setitem__'),
                [core_language.create_str('stypy_type_store'), type_store_arg])

            info_storage_instr.append(subscript_call)

            subscript_call = create_call_expression(
                core_language.create_attribute(f_var_name, '__setitem__'), [
                    core_language.create_str('stypy_function_name'),
                    function_name_arg
                ])

            info_storage_instr.append(subscript_call)

            subscript_call = create_call_expression(
                core_language.create_attribute(f_var_name, '__setitem__'), [
                    core_language.create_str('stypy_param_names_list'),
                    param_names_list_arg
                ])

            info_storage_instr.append(subscript_call)

            subscript_call = create_call_expression(
                core_language.create_attribute(f_var_name, '__setitem__'), [
                    core_language.create_str('stypy_varargs_param_name'),
                    varargs_param_name
                ])

            info_storage_instr.append(subscript_call)

            subscript_call = create_call_expression(
                core_language.create_attribute(f_var_name, '__setitem__'), [
                    core_language.create_str('stypy_kwargs_param_name'),
                    kwargs_param_name
                ])

            info_storage_instr.append(subscript_call)

            subscript_call = create_call_expression(
                core_language.create_attribute(f_var_name, '__setitem__'), [
                    core_language.create_str('stypy_call_defaults'),
                    call_defaults
                ])

            info_storage_instr.append(subscript_call)

            subscript_call = create_call_expression(
                core_language.create_attribute(f_var_name, '__setitem__'),
                [core_language.create_str('stypy_call_varargs'), call_varargs])

            info_storage_instr.append(subscript_call)

            subscript_call = create_call_expression(
                core_language.create_attribute(f_var_name, '__setitem__'),
                [core_language.create_str('stypy_call_kwargs'), call_kwargs])

            info_storage_instr.append(subscript_call)

            subscript_call = create_call_expression(
                core_language.create_attribute(f_var_name, '__setitem__'), [
                    core_language.create_str('stypy_declared_arg_number'),
                    core_language.create_num(len(function_def_node.args.args))
                ])

            info_storage_instr.append(subscript_call)
        else:
            info_storage_instr.append(
                core_language.create_Assign(
                    core_language.create_attribute(f_var_name,
                                                   'stypy_localization'),
                    localization_arg))
            info_storage_instr.append(
                core_language.create_Assign(
                    core_language.create_attribute(f_var_name,
                                                   'stypy_type_of_self'),
                    type_of_self_arg))

            info_storage_instr.append(
                core_language.create_Assign(
                    core_language.create_attribute(f_var_name,
                                                   'stypy_type_store'),
                    type_store_arg))
            info_storage_instr.append(
                core_language.create_Assign(
                    core_language.create_attribute(f_var_name,
                                                   'stypy_function_name'),
                    function_name_arg))
            info_storage_instr.append(
                core_language.create_Assign(
                    core_language.create_attribute(f_var_name,
                                                   'stypy_param_names_list'),
                    param_names_list_arg))

            info_storage_instr.append(
                core_language.create_Assign(
                    core_language.create_attribute(f_var_name,
                                                   'stypy_varargs_param_name'),
                    varargs_param_name))
            info_storage_instr.append(
                core_language.create_Assign(
                    core_language.create_attribute(f_var_name,
                                                   'stypy_kwargs_param_name'),
                    kwargs_param_name))
            info_storage_instr.append(
                core_language.create_Assign(
                    core_language.create_attribute(f_var_name,
                                                   'stypy_call_defaults'),
                    call_defaults))
            info_storage_instr.append(
                core_language.create_Assign(
                    core_language.create_attribute(f_var_name,
                                                   'stypy_call_varargs'),
                    call_varargs))
            info_storage_instr.append(
                core_language.create_Assign(
                    core_language.create_attribute(f_var_name,
                                                   'stypy_call_kwargs'),
                    call_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.create_Assign(args_test_resul, call)

    # After parameter number check call
    argument_errors = core_language.create_Name('arguments')
    is_error_type = core_language.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.create_if(if_test, body)

    return info_storage_instr + [assign, if_]