Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

As of 3.12 _lsprof no longer calls _PyEval_SetProfile making sys.getprofile() useless with cProfile #130377

Open
sivel opened this issue Feb 20, 2025 · 1 comment
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error

Comments

@sivel
Copy link

sivel commented Feb 20, 2025

Bug report

Bug description:

Starting in 3.12 with the addition of sys.monitoring, _lsprof, specifically in profiler_enable, no longer calls _PyEval_SetProfile.

Very specifically, this impacts previous functionality where python -m cProfile -m whatever would result in sys.setprofile() being called, and subsequently the whatever module could access the profiler object via sys.getprofile()

To my knowledge this is now completely impossible as sys.monitoring provides no meaningful way to access the profiler object.

In my particular use case, profiling could be enabled within the application, or if python -m cProfile was used, the existing profiler could be used allowing profiling to happen earlier than the enablement point of cProfile within the application.

Something to the effect of:

p = sys.getprofile()
if p is None:
    p = cProfile.Profile()
    p.enable()

CPython versions tested on:

3.13

Operating systems tested on:

Linux

@sivel sivel added the type-bug An unexpected behavior, bug, or error label Feb 20, 2025
@gaogaotiantian
Copy link
Member

This is not a bug. sys.getprofile gets the profile function from sys.setprofile, cProfile before 3.12 happened to use that specific utility for profiling. It switched to sys.monitoring mechanism after 3.12, so sys.getprofile won't get anything for cProfile. Yes they both have the word profile in it, but there's never a guarantee that they will always be related. cProfile documentation does not provide a way to get the profile object from CLI either. This is just something unsupported that happened to work before 3.12.

If you just want something equivalent to your current method (which is not 100% accurate, it's only guarantees that if cProfile is running, sys.getprofile() will be not None), you can do sys.monitoring.get_tool(sys.monitoring.PROFILER_ID) - that will return a string if cProfile is running.

Still, cProfile does not provide an official way to access it's profiler object in CLI mode, if you need that, that's a feature request.

@picnixz picnixz added extension-modules C modules in the Modules dir 3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes labels Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants