Skip to content

Commit

Permalink
Minor changes to scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwinprasadme committed Jun 26, 2023
1 parent be84b17 commit 6f49454
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 45 deletions.
2 changes: 1 addition & 1 deletion scripts/benchmark_count.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
from pathlib import Path

microbench_folder_path = "../micro-benchmark/python_features"
microbench_folder_path = "../micro-benchmark/"

json_files = [_file for _file in sorted(Path(microbench_folder_path).rglob("*.json"))]
python_files = [_file for _file in sorted(Path(microbench_folder_path).rglob("*.py"))]
Expand Down
51 changes: 43 additions & 8 deletions scripts/collect_runtime_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,39 @@
import json
import pdb

_in = io.StringIO("step")
_out = io.StringIO()


class Debugger(pdb.Pdb):
def __init__(self, *args, **kwargs):
self.identifiers = {}
super().__init__(stdin=io.StringIO(), stdout=io.StringIO(), *args, **kwargs)
self.all_identifiers = None
super().__init__(*args, **kwargs)
# super().__init__(stdin=_in, stdout=_out, *args, **kwargs)

def user_line(self, frame):
# Ignore internal Python identifiers
valid_identifiers = {
k: v for k, v in frame.f_locals.items() if not k.startswith("__")
k: v
for k, v in frame.f_locals.items()
if (
not k.startswith("__")
and k
not in [
"io",
"json",
"pdb",
"_in",
"_out",
"Debugger",
"debugger",
"cmd",
"globals",
"locals",
]
)
}

# Collect local variable identifiers and their types
for identifier, value in valid_identifiers.items():
# Define the scope
Expand All @@ -23,16 +44,30 @@ def user_line(self, frame):
scope = frame.f_code.co_name # function name

# Add line number, scope, and identifier to the identifiers
self.identifiers[f"{frame.f_lineno}:{scope}:{identifier}"] = type(
value
).__name__

self.do_next(None) # Continue execution
if f"{frame.f_lineno-1}" not in self.identifiers:
self.identifiers[frame.f_lineno - 1] = {}

if identifier not in self.identifiers[frame.f_lineno - 1]:
self.identifiers[frame.f_lineno - 1][identifier] = {
"identifier": identifier,
"context": scope,
"type": [type(value).__name__],
}
else:
print("###############################")
self.identifiers[frame.f_lineno - 1][identifier]["type"].append(
type(value).__name__
)

# self.do_step(None) # Continue execution
# self.onecmd("step")
# _in.write("")


# Run the debugger
debugger = Debugger()
debugger.run(open("test.py").read())

# Print the collected identifiers and their types
print(json.dumps(debugger.identifiers, indent=4))
print(json.dumps(debugger.identifiers, indent=4, sort_keys=1))
41 changes: 23 additions & 18 deletions scripts/scalpel_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,31 @@ def list_python_files(folder_path):
return python_files


folder_path = "../micro-benchmark/python_features/functions"
folder_path = "../micro-benchmark/analysis_sensitivities/object_sensitivity"

python_files = list_python_files(folder_path)

for file in python_files:
print(file)
inferer = TypeInference(name=file, entry_point=file)
inferer.infer_types()
inferred = inferer.get_types()
print(inferred)
json_file_path = file.replace(".py", "_gt.json")

if os.path.exists(json_file_path):
print(f"JSON file already exists: {json_file_path}")
continue

with open(json_file_path, "w") as json_file:
# json.dump(inferred, json_file)
inferred_serializable = [
{k: list(v) if isinstance(v, set) else v for k, v in d.items()}
for d in inferred
]
json.dump(inferred_serializable, json_file, indent=4)
try:
json_file_path = file.replace(".py", "_gt.json")

if os.path.exists(json_file_path):
print(f"JSON file already exists: {json_file_path}")
continue

inferer = TypeInference(name=file, entry_point=file)
inferer.infer_types()
inferred = inferer.get_types()
print(inferred)

with open(json_file_path, "w") as json_file:
# json.dump(inferred, json_file)
inferred_serializable = [
{k: list(v) if isinstance(v, set) else v for k, v in d.items()}
for d in inferred
]
inferred_serializable.sort(key=lambda x: x["line_number"])
json.dump(inferred_serializable, json_file, indent=4)
except Exception as e:
print(e)
28 changes: 10 additions & 18 deletions scripts/test.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
# This program is an example of flow sensitivity as the behavior of the program is dependent on the flow of execution, specifically the values assigned to the 'a' and 'b' fields of the 'arith_op' object.
# This program is an example of context sensitivity as the behavior of the program is dependent on the context of function call execution, specifically the values assigned to the 'a' and 'b' fields of the 'ArithmeticOperation' class objects.
# Function which has a combined return type [int,str]
# The given code is context sensitive because it produces different results based on the context in which it is executed.


def arithmetic_op(a, b):
result = None
if a == 0:
result = b
elif b == 0:
result = a
def my_function(my_bool):
if my_bool:
x = 5
else:
result = a + b

if result <= 0:
result = 0
else:
result = "Positive"
return result
x = "Hello World!"
return x


result = arithmetic_op(5, 10)
result = arithmetic_op(1.0, 10.0)
result = arithmetic_op(-5, -10)
result2 = my_function(0)
result1 = my_function("False")
print()

0 comments on commit 6f49454

Please sign in to comment.