Back to Open Assistant

OpenBugger Example

notebooks/openbugger/openbugger_example.ipynb

0.0.16.8 KB
Original Source

OpenBugger Example

python
# First, we'll import the necessary libraries
import os
import subprocess
python
## only run from OpenAssistant not if you already cloned the github
# Next, we'll define the function that will clone the OpenBugger repository and install it.
def install_openbugger():
    # First, we'll get the current working directory. This is where the repository will be cloned to.
    cwd = os.getcwd()

    # Next, we'll use Git to clone the repository.
    subprocess.run(["git", "clone", "https://github.com/furlat/OpenBugger", cwd + "/OpenBugger"])

    # Now, we'll use pip to install the package from the local repository.
    subprocess.run(["python3", "-m", "pip", "install", "--editable", cwd + "/OpenBugger"])


# Now, we'll call the function to install OpenBugger.
install_openbugger()
python
# Finally, we'll import SyntaxBug and LogicBug.
from syntax.syntax_injector import SyntaxBug
from logic.logic_injector import LogicBug
python
# setup syntax bug
syntax_bug = SyntaxBug()

# Simple script
simple_script = """
def greet(name):
    print("Hello, " + name)

greet("Bob")
"""

# The simple script can be modified using the "easy" injection method because it only contains simple syntax and does not have any nested code blocks. This means that there are fewer characters (e.g. quotes, brackets, braces, parenthesis) that could be the target of syntax errors, and the "easy" injection method, which only injects errors that involve replacing or removing a single character, is sufficient to modify the script.
print(simple_script)
# Inject easy syntax errors into the simple script

modified_simple_script, errors, counter = syntax_bug.inject(simple_script, "easy", 1)
print("Modified version Easy", errors, counter)
print(modified_simple_script)
print("are they the same?", simple_script == modified_simple_script)
python
# Medium script
medium_script = """
def greet(name):
    print("Hello, " + name)
    
def greet_all(names):
    for name in names:
        greet(name)
        
greet_all(["Bob", "Alice", "Eve"])
"""
# The medium script can be modified using the "medium" injection method because it contains a nested code block (the for loop in the `greet_all` function). This means that there are more characters (e.g. quotes, brackets, braces, parenthesis) that could be the target of syntax errors, and the "medium" injection method, which injects errors that involve replacing, removing, or adding a single character, is sufficient to modify the script.
print(medium_script)
# Inject medium syntax errors into the medium script
modified_medium_script, errors, counter = syntax_bug.inject(medium_script, "medium", 3)
print("Modified version Medium", errors, counter)
print(modified_medium_script)
print("are they the same?", medium_script == modified_medium_script)
# Hard script
hard_script = """
class Greeting:
    def __init__(self, greeting):
        self.greeting = greeting
        
    def greet(self, name):
        print(self.greeting + ", " + name)
        
greeting = Greeting("Hello")
greeting.greet("Bob")
"""
python
# Hard script
hard_script = """
class Greeting:
    def __init__(self, greeting):
        self.greeting = greeting
        
    def greet(self, name):
        print(self.greeting + ", " + name)
        
greeting = Greeting("Hello")
greeting.greet("Bob")
"""

# The hard script can be modified using the "hard" injection method because it contains multiple nested code blocks (the `__init__` and `greet` methods in the `Greeting` class). This means that there are even more characters (e.g. quotes, brackets, braces, parenthesis) that could be the target of syntax errors, and the "hard" injection method, which injects errors that involve replacing, removing, adding, or swapping characters, is sufficient to modify the script.
print(hard_script)
# Inject hard syntax errors into the hard script
modified_hard_script, errors, counter = syntax_bug.inject(hard_script, "hard", 3)
print("Modified version Hard", errors, counter)
print(modified_hard_script)
print("are they the same?", hard_script == modified_hard_script)

Now we switch to testing the LogicBugs and show how to bug a function that is defined in the script without already having the string.

python
import inspect
import random

# Simple example script


def simple_script():
    # Choose two random integers
    num1 = random.randint(0, 10)
    num2 = random.randint(0, 10)

    # Compare the two numbers and print a message based on their relation
    if num1 > num2:
        print("num1 is greater than num2")
    elif num1 < num2:
        print("num1 is less than num2")
    else:
        print("num1 is equal to num2")


# Medium example script
def medium_script():
    # Choose a random integer and assign it to a variable
    num = random.randint(0, 10)

    # Use a loop to print all numbers from 0 to the chosen integer
    for i in range(num):
        print(i)


# Hard example script
def hard_script():
    # Choose a random integer and assign it to a variable
    num = random.randint(0, 10)

    # Use a loop to print the square of all numbers from 0 to the chosen integer
    for i in range(num):
        print(i**2)
python
# create an instance of the LogicBug class
logic_bug = LogicBug()

# get the source code of the simple_script function as a string
simple_script_str = inspect.getsource(simple_script)
print("Simple", simple_script_str)
# inject a logic error into the simple_script function
modified_simple_script, error, counter = logic_bug.inject(simple_script_str, "easy", num_errors=3)
print("Modified version Simple", error, counter)
# print the modified simple_script function
print(modified_simple_script)
print("are they the same?", simple_script_str == modified_simple_script)
python
# get the source code of the medium_script function as a string
medium_script_str = inspect.getsource(medium_script)
print("Medium", medium_script_str)
# inject a logic error into the medium_script function
modified_medium_script, error, counter = logic_bug.inject(medium_script_str, "medium", num_errors=3)

# print the modified medium_script function
print("Modified version Medium", error, counter)
print(modified_medium_script)
print("are they the same?", medium_script_str == modified_medium_script)
python
# get the source code of the hard_script function as a string
hard_script_str = inspect.getsource(hard_script)
print("Hard", hard_script_str)
# inject a logic error into the hard_script function
modified_hard_script, error, counter = logic_bug.inject(hard_script_str, "hard", num_errors=1)
print("Modified version Hard", error, counter)
# print the modified hard_script function
print(modified_hard_script)
print("are they the same?", hard_script_str == modified_hard_script)