Skip to content

Instantly share code, notes, and snippets.

@devdanzin
Created March 18, 2026 10:07
Show Gist options
  • Select an option

  • Save devdanzin/ed7691a99c26ede561a549ab1dba00b2 to your computer and use it in GitHub Desktop.

Select an option

Save devdanzin/ed7691a99c26ede561a549ab1dba00b2 to your computer and use it in GitHub Desktop.
_lsprof.c: Profiler.enable() clobbers exception from use_tool_id

_lsprof.c: Profiler.enable() clobbers exception from use_tool_id

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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment