Skip to content

Commit

Permalink
Trace generator: ignore objects imported from other modules
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesbraun committed Sep 30, 2024
1 parent 64d3a22 commit 58378be
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
28 changes: 16 additions & 12 deletions python/deps/pytrace-generator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ def format(self):
return [frame.format() for frame in self.frames]

class Heap:
def __init__(self):
def __init__(self, script_path):
self.script_path = script_path
self.memory = {}

def store(self, address, value):
Expand Down Expand Up @@ -202,7 +203,7 @@ def store(self, address, value):
except:
# Just ignore it in case getattr fails
continue
if should_ignore_on_heap(field, v):
if should_ignore_on_heap(field, v, self.script_path):
continue

prim_value = PrimitiveValue(v)
Expand Down Expand Up @@ -234,7 +235,7 @@ def format(self):
}


def should_ignore(variable_name, value, ignore_list = []):
def should_ignore(variable_name, value, script_path, ignore_list = []):
if variable_name in ignore_list or variable_name.startswith("__"):
return True
if inspect.isfunction(value) or inspect.ismethod(value) or inspect.isbuiltin(value):
Expand All @@ -245,24 +246,27 @@ def should_ignore(variable_name, value, ignore_list = []):
return True
if inspect.isframe(value):
return True
if hasattr(value, "__module__"):
if not sys.modules[value.__module__].__file__.startswith(script_path):
return True
return False

def should_ignore_on_stack(variable_name, value, ignore_list = []):
if should_ignore(variable_name, value, ignore_list):
def should_ignore_on_stack(variable_name, value, script_path, ignore_list = []):
if should_ignore(variable_name, value, script_path, ignore_list):
return True
return False

def should_ignore_on_heap(variable_name, value, ignore_list = []):
if should_ignore(variable_name, value, ignore_list):
def should_ignore_on_heap(variable_name, value, script_path, ignore_list = []):
if should_ignore(variable_name, value, script_path, ignore_list):
return True
return False


def generate_heap(frame, ignore):
heap = Heap()
def generate_heap(frame, script_path, ignore):
heap = Heap(script_path)
while True:
for variable_name in frame.f_locals:
if should_ignore_on_stack(variable_name, frame.f_locals[variable_name], ignore):
if should_ignore_on_stack(variable_name, frame.f_locals[variable_name], script_path, ignore):
continue
if primitive_type(type(frame.f_locals[variable_name])) != "ref":
continue
Expand Down Expand Up @@ -313,10 +317,10 @@ def trace_dispatch(self, frame, event, arg):
self.stack.pop_frame()
elif event == "line":
for variable_name in frame.f_locals:
if should_ignore_on_stack(variable_name, frame.f_locals[variable_name],self.stack_ignore):
if should_ignore_on_stack(variable_name, frame.f_locals[variable_name], self.filename, self.stack_ignore):
continue
self.stack.push(variable_name, frame.f_locals[variable_name])
heap = generate_heap(frame, self.stack_ignore)
heap = generate_heap(frame, self.filename, self.stack_ignore)

step = TraceStep(line, filename, copy.deepcopy(self.stack), copy.deepcopy(heap))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from wypp import *
24 changes: 24 additions & 0 deletions python/deps/pytrace-generator/test/test-cases/importWypp.py.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[
{
"line": 1,
"filePath": "importWypp.py",
"stack": [
{
"frameName": "<module>",
"locals": []
}
],
"heap": {}
},
{
"line": 2,
"filePath": "importWypp.py",
"stack": [
{
"frameName": "<module>",
"locals": []
}
],
"heap": {}
}
]
3 changes: 3 additions & 0 deletions python/deps/pytrace-generator/test/test-cases/module1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This test ensures that module2 can be imported and used
import module2
b = module2.myfun(1)
5 changes: 5 additions & 0 deletions python/deps/pytrace-generator/test/test-cases/module2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def myfun(i):
i += 1
i += 1
i += 1
return i

0 comments on commit 58378be

Please sign in to comment.