Skip to content

Commit

Permalink
Fixed filter issue and exception bug
Browse files Browse the repository at this point in the history
  • Loading branch information
AryazE committed Aug 1, 2024
1 parent 1c91b20 commit d8fdb37
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 13 deletions.
21 changes: 13 additions & 8 deletions src/dynapyt/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,18 @@ def filtered(self, func, f, args):
end = docs.find(END)
fltr = docs[start + len(START) : end].strip()
patterns = fltr.split(" -> ")[1].split(SEPERATOR)
if fltr.startswith("only ->") and any(
[getattr(arg, "__name__", None) in patterns for arg in sub_args]
):
return False
elif fltr.startswith("ignore ->") and any(
[getattr(arg, "__name__", None) in patterns for arg in sub_args]
):
return True
try:
if fltr.startswith("only ->") and any(
# [getattr(arg, "__name__", None) in patterns for arg in sub_args]
[arg.__name__ in patterns for arg in sub_args]
):
return False
elif fltr.startswith("ignore ->") and any(
[arg.__name__ in patterns for arg in sub_args]
):
return True
except AttributeError:
pass
docs = docs[end + len(END) :].lstrip()
return False

Expand Down Expand Up @@ -609,6 +613,7 @@ def _catch_(self, exception):
t, v, stack_trace = exc_info()
self.call_if_exists("runtime_event", "", -1)
self.call_if_exists("uncaught_exception", exception, stack_trace)
self.end_execution()
raise exception

def _read_(self, dyn_ast, iid, var_arg):
Expand Down
8 changes: 8 additions & 0 deletions tests/regression/filters_getattr/analysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from dynapyt.analyses.BaseAnalysis import BaseAnalysis
from dynapyt.instrument.filters import only


class TestAnalysis(BaseAnalysis):
@only(patterns=["foo"])
def pre_call(self, dyn_ast: str, iid: int, function, pos_args, kw_args) -> None:
print(f"pree call of {function.__name__}")
7 changes: 7 additions & 0 deletions tests/regression/filters_getattr/expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pree call of deferred_error
pree call of ValueError
pree call of new
pree call of DeferredError
pree call of print
OK
# Exception: Some error text
23 changes: 23 additions & 0 deletions tests/regression/filters_getattr/program.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class DeferredError:
def __init__(self, ex: BaseException):
self.ex = ex

def __getattr__(self, elt: str) -> None:
raise self.ex

@staticmethod
def new(ex: BaseException):
"""
Creates an object that raises the wrapped exception ``ex`` when used.
"""
return DeferredError(ex)


def deferred_error() -> None:
thing = DeferredError.new(ValueError("Some error text"))
print("OK")
x = thing.some_attr
print("Not OK")


deferred_error()
12 changes: 7 additions & 5 deletions tests/run_single_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from importlib import import_module
from os import sep, remove
from os import sep, remove, environ
from os.path import join, exists
from pathlib import Path
from shutil import copyfile, move, rmtree
Expand Down Expand Up @@ -122,6 +122,7 @@ def test_runner(directory_pair: Tuple[str, str], capsys):
)
except Exception as e:
exception = e
session_id = environ.get("DYNAPYT_SESSION_ID", None)

# check output
expected_file = join(abs_dir, "expected.txt")
Expand All @@ -131,10 +132,11 @@ def test_runner(directory_pair: Tuple[str, str], capsys):
captured = (
capsys.readouterr()
) # read stdout produced by running the analyzed program
output_res = correct_output(expected, captured.out, exception)
actual = captured.out
output_res = correct_output(expected, actual, exception)
if not output_res[0]:
pytest.fail(
f"Output of {rel_dir} does not match expected output on line {output_res[1]}.\n--> Expected:\n{expected}\n--> Actual:\n{captured.out}"
f"Output of {rel_dir} does not match expected output on line {output_res[1]}.\n--> Expected:\n{expected}\n--> Actual:\n{actual}"
)

expected_coverage = join(abs_dir, "exp_coverage.json")
Expand Down Expand Up @@ -171,6 +173,6 @@ def test_runner(directory_pair: Tuple[str, str], capsys):
if (Path(gettempdir()) / f"dynapyt_output-{session_id}").exists():
rmtree(Path(gettempdir()) / f"dynapyt_output-{session_id}")
except FileNotFoundError as fe:
print(f"File not found {fe} in {rel_dir}")
raise FileNotFoundError(f"File not found {fe} in {rel_dir}")
except Exception as e:
print(f"Something went wrong while cleaning up {rel_dir}: {e}")
raise Exception(f"Something went wrong while cleaning up {rel_dir}: {e}")

0 comments on commit d8fdb37

Please sign in to comment.