diff --git a/pyinstrument/processors.py b/pyinstrument/processors.py index 6894b44b..d5320a94 100644 --- a/pyinstrument/processors.py +++ b/pyinstrument/processors.py @@ -294,3 +294,16 @@ def is_runpy_frame(frame: Frame): result.remove_from_parent() return result + + +def group_ipython_frames_processor(frame, options: ProcessorOptions) -> Frame | None: + """ + In some case the IPython's magic show all the internal IPython frames, which makes + traces hard to read. + + This will collapse most of IPython internal frames. + """ + return group_library_frames_processor( + frame, + {"hide_regex": ".+((IPython)|(ipykernel.zmqshell)|(pyinstrument.magic.magic))"}, + ) diff --git a/pyinstrument/renderers/console.py b/pyinstrument/renderers/console.py index 7ed88fde..d47b96b6 100644 --- a/pyinstrument/renderers/console.py +++ b/pyinstrument/renderers/console.py @@ -216,6 +216,7 @@ def default_processors(self) -> ProcessorList: processors.remove_irrelevant_nodes, processors.remove_first_pyinstrument_frames_processor, processors.group_library_frames_processor, + processors.group_ipython_frames_processor, ] class colors_enabled: diff --git a/pyinstrument/renderers/html.py b/pyinstrument/renderers/html.py index 53bf3fa6..41d5577e 100644 --- a/pyinstrument/renderers/html.py +++ b/pyinstrument/renderers/html.py @@ -100,4 +100,5 @@ def default_processors(self) -> ProcessorList: processors.remove_irrelevant_nodes, processors.remove_first_pyinstrument_frames_processor, processors.group_library_frames_processor, + processors.group_ipython_frames_processor, ] diff --git a/pyinstrument/session.py b/pyinstrument/session.py index 45f56ca3..864710cf 100644 --- a/pyinstrument/session.py +++ b/pyinstrument/session.py @@ -127,6 +127,11 @@ def root_frame(self, trim_stem: bool = True) -> Frame | None: if trim_stem: root_frame = self._trim_stem(root_frame) + group_library_frames_processor( + root_frame, + {"hide_regex": ".+((IPython)|(ipykernel.zmqshell)|(pyinstrument.magic.magic))"}, + ) + return root_frame def _trim_stem(self, frame: Frame):