diff --git a/pyinstrument/processors.py b/pyinstrument/processors.py index 89d02d4e..4ea142c8 100644 --- a/pyinstrument/processors.py +++ b/pyinstrument/processors.py @@ -291,3 +291,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 e4f35108..c25d3381 100644 --- a/pyinstrument/renderers/console.py +++ b/pyinstrument/renderers/console.py @@ -274,6 +274,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 ce73a7ec..2b6cf0f5 100644 --- a/pyinstrument/renderers/html.py +++ b/pyinstrument/renderers/html.py @@ -105,4 +105,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 493d17a9..b88b224f 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):