Example #1
0
    def add_expr(self, expr, score, var_dict):
        try:
            expr_res = fast_eval(expr, var_dict)

            if expr_res is not None:
                self.result_dict[expr] = expr_res
                if not isinstance(expr_res, collections.Hashable):
                    not_add = True
                    for res_key, expr_list in self.relation_dict.items():
                        if len(expr_list) > 0:
                            res = self.result_dict[expr_list[0]]
                            if expr_res == res:
                                expr_res = res_key
                                not_add = False
                                break
                    if not_add:
                        expr_res = expr
                if expr_res not in self.relation_dict.keys():
                    self.relation_dict[expr_res] = []
                if expr_res not in self.score_dict.keys():
                    self.score_dict[expr_res] = []
                self.relation_dict[expr_res].append(expr)
                self.score_dict[expr_res].append(score)
        except Exception:
            pass
Example #2
0
    def gen_expr_list_from_templates(self, var_dict):
        from basic_framework.holes import Holes

        expr_list = []
        type_dict = {}

        vari_nam_list = list(var_dict.keys())
        for vari_num, template_str in Holes.template_list:
            vari_tuple_list = list(combinations(vari_nam_list, vari_num))

            for vari_tuple in vari_tuple_list:
                vari_tuple_perm_list = permutations(list(vari_tuple),
                                                    len(vari_tuple))

                for vari_tuple_perm in vari_tuple_perm_list:
                    tmp_expr = template_str
                    for i in range(len(vari_tuple_perm)):
                        tmp_expr = tmp_expr.replace(
                            "{" + str(i) + "}",
                            "var_dict['" + vari_tuple_perm[i] + "']")

                    if "**" in tmp_expr:
                        pass
                    elif self.has_call(tmp_expr):
                        pass
                    else:
                        try:
                            res = fast_eval(tmp_expr, var_dict)
                            type_dict[tmp_expr] = str(type(res))
                            expr_list.append(tmp_expr)
                        except Exception as e:
                            pass

        return expr_list, type_dict
Example #3
0
    def vari_hist_hole(cls, var_dict):
        if cls.is_stop:
            raise cls.StopException()

        if cls.hist_stop:
            raise cls.HistTLEException()

        for k, v in var_dict.items():
            if k not in cls.vari_hist.keys():
                cls.vari_hist[k] = []
            if len(cls.vari_hist[k]) == 0 or \
                    not cls.is_object_equal(cls.vari_hist[k][-1], v):
                cls.vari_hist[k].append(v)

        for k1, v1 in var_dict.items():
            for k2, v2 in var_dict.items():
                if k1 != k2:
                    expr_str = k1 + "[" + k2 + "]"

                    comb_v = None
                    try:
                        comb_v = fast_eval(expr_str, var_dict)
                    except:
                        comb_v = None
                    if comb_v is not None:
                        if expr_str not in cls.vari_hist.keys():
                            cls.vari_hist[expr_str] = []
                        if len(cls.vari_hist[expr_str]) == 0 or not cls.is_object_equal(cls.vari_hist[expr_str][-1], comb_v):
                            cls.vari_hist[expr_str].append(comb_v)
Example #4
0
    def gen_assign_ss(self, pre_assign_str, var_dict, k_best, is_simple=False):
        # Generate expressions for the assignment statement
        expr_list = []
        expr_list.append(pre_assign_str)

        # Get the type of the statement in pre_assign_str
        pre_assign_type_str = ""
        try:
            pre_assign_type_str = str(type(fast_eval(
                pre_assign_str, var_dict)))  #copy.deepcopy(
        except Exception:
            pre_assign_type_str = ""

        # Extract method signature
        token_list = get_token_list(pre_assign_str)
        t_start = -1
        t_end = -1
        for i in range(len(token_list)):
            token = token_list[i]
            if token.string == "self" and tok_name[token.exact_type] == "NAME":
                t_start = token.end[1] + 1
            elif token.string == "(" and tok_name[token.exact_type] == "LPAR":
                t_end = token.start[1]
                break
        # If we find a method invocation, we do not change the assignment statement.
        if t_start > -1 and t_end > -1:
            method_signature = pre_assign_str[t_start:t_end]
            if method_signature in dir(var_dict['self']):
                return expr_list, [1]

        expr_list_gen = []
        type_dict = {}

        expr_list_gen, type_dict = self.gen_expr_list(var_dict,
                                                      is_simple=is_simple)

        for expr in expr_list_gen:
            expr_list.append(expr)

        # Misuse based expression
        from basic_framework.holes import Holes
        for constant in Holes.constant_list:
            for i in range(len(token_list)):
                token = token_list[i]
                if tok_name[token.exact_type] in ["NUMBER", "STRING"]:
                    tmp_cond = pre_assign_str[:token.start[
                        1]] + constant + pre_assign_str[token.end[1]:]
                    expr_list.append(tmp_cond)

        expr_list = list(set(expr_list))

        # Sort and filter
        r_expr_list, r_score_list = self.sort(expr_list, pre_assign_str,
                                              var_dict, k_best)
        return r_expr_list, r_score_list
Example #5
0
 def method_hole(cls, ln, pre_invoke_str, var_dict):
     return fast_eval(pre_invoke_str, var_dict)
Example #6
0
    def generic_hole(cls, ln, pre_expr_str, var_dict, ssf):
        if cls.is_stop:
            raise cls.StopException()

        start_time = time.process_time()

        # Extend var_dict
        var_dict = cls.extend_var_dict(var_dict)

        # Wrap pre_expr_str and get the time of execution
        pre_expr_str = cls.expr_wrapper(pre_expr_str, var_dict)
        times = cls.dt.get_times()

        # Test-equivalent analysis
        if not cls.curr_eg.lt_exists(times, ln):
            expr_list = []
            score_list = []

            times_list = list(cls.curr_eg.expr_dict.keys())
            times_list.sort()
            for times_a in times_list:
                if times_a > times and \
                        ln == cls.curr_eg.expr_dict[times_a][2]:

                    expr_rec = cls.curr_eg.get_expr_rec(times_a, ln)
                    expr_list = expr_rec.repr_expr_list
                    score_list = expr_rec.repr_score_list
                    break
            if len(expr_list) == 0:
                if cls.curr_ss.ln_exists(ln):
                    expr_list = cls.curr_ss.get_expr_list(ln)
                    score_list = cls.curr_ss.get_score_list(ln)
                else:

                    if ssf == "cond":
                        expr_list, score_list = cls.expr_gen.gen_cond_ss(pre_expr_str, var_dict, k_best = 50)
                    elif ssf == "assign":
                        expr_list, score_list = cls.expr_gen.gen_assign_ss(pre_expr_str, var_dict, k_best = 10)
                    elif ssf == "simple_assign":
                        expr_list, score_list = cls.expr_gen.gen_assign_ss(pre_expr_str, var_dict, k_best=5, is_simple=True)
                    elif ssf == "init":
                        expr_list, score_list = cls.expr_gen_init_ss(var_dict)
                    else:
                        raise cls.NoSSException()

            assert (len(expr_list) > 0)
            ter = TERelation()
            ter.add_expr_list_ws_p(expr_list, score_list, var_dict)
            expr_rec_list = ter.get_expr_rec_list()

            if len(expr_rec_list) > 0:
                cls.curr_eg.add_expr_rec_list(times, ln, expr_rec_list)
            else:
                raise cls.NoCandidateException()

        # Select one expr and run
        selected_expr = cls.curr_eg.get_expr_rec(times, ln).expr

        res = fast_eval(selected_expr, var_dict)
        cls.dt.update_times()

        cls.in_genhole_time += (time.process_time() - start_time)
        return res