How to exclude pycallgraph2 from PyCallGraph context wrapper?

145 Views Asked by At

The following minimal working example

from pycallgraph2 import PyCallGraph
from pycallgraph2.output import GraphvizOutput

with PyCallGraph(output=GraphvizOutput()):
    None

produces this output:

enter image description here

Why does PyCallGraph2 describe itself while profiling "Hello, World"? showed that we can exclude certain things in the namespace via the command line. Now I want to do this inside a script.

1

There are 1 best solutions below

0
On

I found an answer while I formulating the question. The older documentation for pycallgraph shows how to filter. If we make some necessary changes such as referring to pycallgraph2 instead of pycallgraph, we have:

from pycallgraph2 import PyCallGraph
from pycallgraph2 import Config
from pycallgraph2 import GlobbingFilter
from pycallgraph2.output import GraphvizOutput

config = Config()
config.trace_filter = GlobbingFilter(exclude=[
    'pycallgraph2.*'
])

with PyCallGraph(output=GraphvizOutput(), config=config):
    None

This gives the desired result:

enter image description here