Ejemplo n.º 1
0
def insert_oracles(filename, while_limit):
    f = open(filename, 'r')
    g = open("target.py", 'w')
    g.write("hctest={}\n")
    g.write("hclocals={}\n")
    g.write("class InfiniteLoopException(Exception):\n    pass\n")

    #initialise
    if_counter = 0
    while_counter = 0
    branch_counts = [1]
    hcbranch = dict()
    first_function_met = False
    for line in f:
        indent = get_indent(line.rstrip())

        if indent >= len(branch_counts):
            branch_counts.append(0)
        if line.lstrip().startswith("def"):
            function_name = line.lstrip().replace("def ",
                                                  " ").split("(")[0].strip()

            if first_function_met:
                g.write("\n    hclocals['0'] = locals()\n")
            g.write(no_print_liner(line))
            g.write("\n    global hclocals\n")

            first_function_met = True

        elif line.lstrip().startswith('if'):
            branch_counts[indent] += 1
            for i in range(indent + 1, len(branch_counts), 1):
                branch_counts[i] = 0
            br = branch_reducer(branch_counts, indent)

            g.write((indent) * 4 * " ")
            g.write(f"hctest['{br}']=False\n")
            g.write(no_print_liner(line))

            hcbranch[br] = Branch.get_branch(br, line)
            g.write((indent + 1) * 4 * " ")
            g.write(f"hctest['{br}']=True\n")

        elif line.lstrip().startswith('while'):
            g.write((indent) * 4 * " ")
            branch_counts[indent] = branch_counts[indent] + 1
            br = branch_reducer(branch_counts, indent)
            g.write(f"hctest['{br}']=False\n")
            g.write((indent) * 4 * " ")
            g.write(f"hclooper =0\n")

            hcbranch[br] = Branch.get_branch(br, line)

            g.write(no_print_liner(line))
            g.write((indent + 1) * 4 * " ")
            g.write(f"hctest['{br}']=True\n")
            g.write((indent + 1) * 4 * " ")
            g.write("hclooper+=1\n")
            g.write((indent + 1) * 4 * " ")
            g.write(
                f"if hclooper>{while_limit}: raise InfiniteLoopException\n")
            g.write((indent + 1) * 4 * " ")
            g.write(f"if hclooper>{while_limit}: return\n")

        elif line.lstrip().startswith("return"):
            g.write((indent) * 4 * " ")
            g.write("hclocals['0'] =locals()\n")
            g.write(line)

        elif line.lstrip().startswith("for"):
            g.write((indent) * 4 * " ")
            branch_counts[indent] = branch_counts[indent] + 1
            br = branch_reducer(branch_counts, indent)
            g.write(f"hctest['{br}']=False\n")
            g.write((indent) * 4 * " ")
            g.write(f"hclooper =0\n")

            condition = line.split("in")[1].strip()[:-1].replace(
                "range", "").replace("(", "").replace(")", "")
            print(condition)
            hcbranch[br] = Branch.get_branch(br, f"if {condition}>0:")

            g.write(no_print_liner(line))
            print(line)
            g.write((indent + 1) * 4 * " ")
            g.write(f"hctest['{br}']=True\n")
            g.write((indent + 1) * 4 * " ")
            g.write("hclooper+=1\n")
            g.write((indent + 1) * 4 * " ")
            g.write(
                f"if hclooper>{while_limit}: raise InfiniteLoopException\n")
            g.write((indent + 1) * 4 * " ")
            g.write(f"if hclooper>{while_limit}: return\n")
        else:
            g.write(no_print_liner(line))
    if first_function_met:
        # g.write("\n    global hclocals\n")
        g.write("\n    hclocals['0'] = locals()\n")

    # g.write(function_name+inputs+"\n")
    g.close()
    return hcbranch