Title: [doc] sys.setprofile / sys.getprofile asymetry
Type: behavior Stage:
Components: Documentation Versions: Python 3.11, Python 3.10, Python 3.9
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, georg.brandl, stefan
Priority: normal Keywords:

Created on 2015-11-24 23:04 by stefan, last changed 2021-12-07 15:35 by iritkatriel.

Messages (2)
msg255301 - (view) Author: Stefan Seefeld (stefan) * (Python committer) Date: 2015-11-24 23:04
I'm using the `cProfile` module to profile my code.

I tried to temporarily disable the profiler by using:

  prof = sys.getprofile()

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.
msg260234 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2016-02-13 11:58
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.
Date User Action Args
2021-12-07 15:35:18iritkatrielsettitle: sys.setprofile / sys.getprofile asymetry -> [doc] sys.setprofile / sys.getprofile asymetry
versions: + Python 3.9, Python 3.10, Python 3.11, - Python 3.4
2016-02-13 11:58:36georg.brandlsetnosy: + docs@python
messages: + msg260234

assignee: docs@python
components: + Documentation
2016-02-13 03:54:12ned.deilysetnosy: + georg.brandl
2015-11-24 23:04:27stefancreate