From 75ef6bd438305ec88666fb81fa158cc9d9be3d51 Mon Sep 17 00:00:00 2001 From: furkanonder Date: Thu, 12 Oct 2023 02:28:14 +0300 Subject: [PATCH] move empty file checks from profiler to __main__.py --- src/akarsu/__main__.py | 35 ++++++++++++----------- src/akarsu/akarsu.py | 65 +++++++++++++++++++----------------------- tests/test_akarsu.py | 6 ---- 3 files changed, 48 insertions(+), 58 deletions(-) diff --git a/src/akarsu/__main__.py b/src/akarsu/__main__.py index 50da3f8..8ed1ad7 100644 --- a/src/akarsu/__main__.py +++ b/src/akarsu/__main__.py @@ -1,5 +1,4 @@ import argparse -import io from collections import Counter from typing import Final @@ -24,25 +23,27 @@ def main() -> None: args = parser.parse_args() if file := args.file: - with io.open(file) as fp: + with open(file) as fp: source = fp.read() - events = Akarsu(source, args.file).profile() - counter: Counter = Counter() - - print(f"{'Count':>10}{'Event Type':^20}{'Filename(function)':<50}") - for event, count in Counter(events).most_common(): - event_type, file_name, func_name = event - counter[event_type] += count - fmt = f"{count:>10}{event_type:^20}{f'{file_name}({func_name})':<50}" - if args.calls: - if event_type in CALL_EVENTS: + + if source := source.strip(): + events = Akarsu(source, args.file).profile() + counter: Counter = Counter() + + print(f"{'Count':>10}{'Event Type':^20}{'Filename(function)':<50}") + for event, count in Counter(events).most_common(): + event_type, file_name, func_name = event + counter[event_type] += count + fmt = f"{count:>10}{event_type:^20}{f'{file_name}({func_name})':<50}" + if args.calls: + if event_type in CALL_EVENTS: + print(fmt) + else: print(fmt) - else: - print(fmt) - print(f"\nTotal number of events: {counter.total()}") - for event_type, count in counter.most_common(): - print(f" {event_type} = {count}") + print(f"\nTotal number of events: {counter.total()}") + for event_type, count in counter.most_common(): + print(f" {event_type} = {count}") if __name__ == "__main__": diff --git a/src/akarsu/akarsu.py b/src/akarsu/akarsu.py index 886e30b..c014bec 100644 --- a/src/akarsu/akarsu.py +++ b/src/akarsu/akarsu.py @@ -35,45 +35,40 @@ def format_func_name(self, event: tuple[str, str, str]) -> tuple[str, str, str]: def profile(self) -> list[tuple[str, str, str]]: events = [] + indented_code = "\n".join(f"\t{line}" for line in self.code.splitlines()) + source = f"def ____wrapper____():\n{indented_code}\n____wrapper____()" + code = compile(source, self.file_name, "exec") - if code := self.code.strip(): - indented_code = "\n".join(f"\t{line}" for line in code.splitlines()) - source = f"def ____wrapper____():\n{indented_code}\n____wrapper____()" - code = compile(source, self.file_name, "exec") # type:ignore + for event, event_name in TRACKED_EVENTS: - for event, event_name in TRACKED_EVENTS: - - def record( - *args: tuple[types.CodeType, int], event_name: str = event_name - ) -> None: - code = cast(types.CodeType, args[0]) - events.append((event_name, code.co_filename, code.co_name)) - - MONITOR.register_callback(TOOL, event, record) - - def record_call( - code: types.CodeType, offset: int, obj: Any, arg: Any + def record( + *args: tuple[types.CodeType, int], event_name: str = event_name ) -> None: - file_name = code.co_filename - if isinstance(obj, PY_CALLABLES): - events.append(("PY_CALL", file_name, obj.__code__.co_name)) - else: - events.append(("C_CALL", file_name, str(obj))) + code = cast(types.CodeType, args[0]) + events.append((event_name, code.co_filename, code.co_name)) + + MONITOR.register_callback(TOOL, event, record) - MONITOR.use_tool_id(TOOL, "Akarsu") - MONITOR.register_callback(TOOL, EVENTS.CALL, record_call) - MONITOR.set_events(TOOL, EVENT_SET) - try: - exec(code) - except: - pass - MONITOR.set_events(TOOL, 0) - MONITOR.free_tool_id(TOOL) + def record_call(code: types.CodeType, offset: int, obj: Any, arg: Any) -> None: + file_name = code.co_filename + if isinstance(obj, PY_CALLABLES): + events.append(("PY_CALL", file_name, obj.__code__.co_name)) + else: + events.append(("C_CALL", file_name, str(obj))) - events = [ - self.format_func_name(event) - for event in events[2:-3] - if "____wrapper____" not in event[2] - ] + MONITOR.use_tool_id(TOOL, "Akarsu") + MONITOR.register_callback(TOOL, EVENTS.CALL, record_call) + MONITOR.set_events(TOOL, EVENT_SET) + try: + exec(code) + except: + pass + MONITOR.set_events(TOOL, 0) + MONITOR.free_tool_id(TOOL) + events = [ + self.format_func_name(event) + for event in events[2:-3] + if "____wrapper____" not in event[2] + ] return events diff --git a/tests/test_akarsu.py b/tests/test_akarsu.py index 9f832fb..e7d6957 100644 --- a/tests/test_akarsu.py +++ b/tests/test_akarsu.py @@ -49,12 +49,6 @@ def test_profile_generator(self): ] self.check_events(events, expected_events) - def test_profile_empty_code(self): - code = "" - events = Akarsu(code, "").profile() - expected_events = [] - self.check_events(events, expected_events) - def test_profile_nested_functions(self): source = textwrap.dedent(""" def foo():