Issue 25726: [doc] sys.setprofile / sys.getprofile asymetry (original) (raw)
I'm using the cProfile
module to profile my code.
I tried to temporarily disable the profiler by using:
prof = sys.getprofile() sys.setprofile(None) ... sys.setprofile(prof)
resulting in an error.
The reason is that with cProfile
, sys.getprofile
returns the profile object itself, which isn't suitable as argument for sys.setprofile
(which expects a callable).
Notice that if I use the profile
module instead of cProfile
, the above works fine.
Ok, this is because internally, sys.setprofile (or to be exact, PyEval_SetProfile) sets two things: a C function, and a "profileobj", which is the argument to setprofile().
sys.setprofile sets the C function to the "profile_trampoline", which supports calling Python profile functions. The profileobj is the Python profile function.
The C profiler sets the C function to a different callback, and uses the profileobj for storing the reference to the Profiler object.
sys.getprofile just returns the profileobj, which means that you can't save/restore the profiler state with the two functions when using cProfile.
There is not much we can do here except for explicitly documenting this.