From bf3cfe5deee94ee54a96b3af81dd9d0bb5c2572c Mon Sep 17 00:00:00 2001 From: M Bussonnier Date: Mon, 29 Jul 2024 11:39:08 +0200 Subject: [PATCH] Hide IPython internal frame by default. This should help with #313 In most case it should have no effects as anyway all the root of the tree is removed as there is no time spent in it. Though with some compiled code and threads, some time may be affected to the root. --- pyinstrument/processors.py | 13 +++++++++++++ pyinstrument/renderers/console.py | 1 + pyinstrument/renderers/html.py | 1 + pyinstrument/session.py | 5 +++++ 4 files changed, 20 insertions(+) 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):