Original gist: https://gist.github.com/devdanzin/ed7691a99c26ede561a549ab1dba00b2
Summary In Modules/_lsprof.c (lines 821-823), when sys.monitoring.use_tool_id() fails, PyErr_Format unconditionally replaces the real exception with a generic ValueError. Fix Remove the PyErr_Format call and let the original exception propagate. Reproducer import cProfile p1 = cProfile.Profile() p1.enable() p2 = cProfile.Profile() try: p2.enable() # ValueError: "Another profiling tool is already active" except ValueError as e: print(f"Generic message masks real error: {e}") finally: p1.disable()
In Modules/_lsprof.c (lines 821-823), when sys.monitoring.use_tool_id() fails, PyErr_Format unconditionally replaces the real exception with a generic ValueError.
Remove the PyErr_Format call and let the original exception propagate.
import cProfile p1 = cProfile.Profile() p1.enable() p2 = cProfile.Profile() try: p2.enable() # ValueError: "Another profiling tool is already active" except ValueError as e: print(f"Generic message masks real error: {e}") finally: p1.disable()
CPython main branch
No response
gh-153068: Preserve cprofile enable errors (#153070)
[3.14] gh-153068: Preserve cprofile enable errors (GH-153070) (#153242)
[3.13] gh-153068: Preserve cprofile enable errors (GH-153070) (#153240)
[3.15] gh-153068: Preserve cprofile enable errors (GH-153070) (#153241)
Activity
gh-153068: Preserve cprofile enable errors (#153070)
[3.14] gh-153068: Preserve cprofile enable errors (GH-153070) (#153242)
[3.13] gh-153068: Preserve cprofile enable errors (GH-153070) (#153240)
[3.15] gh-153068: Preserve cprofile enable errors (GH-153070) (#153241)