Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
Description
Feature or enhancement
Proposal:
I would like to propose making the PY_UNWIND monitoring event available as a local event. The rationale for doing this is that some tools can be interested in trapping per-function exit events. Currently, this is only possible with the PY_RETURN event when a function returns with a value. However, if a function is exited with an exception, there is no way of capturing that event on just that function. Making the PY_UNWIND event would allow monitoring tools to catch all function exit events.
importrandom, sysm, e=sys.monitoring, sys.monitoring.eventsm.use_tool_id(0, "debugger") defmonitor(event): def_(f): m.register_callback(0, event, f) return_@monitor(e.PY_START)def_(*_, **__): print("entering", sys._getframe(1)) @monitor(e.PY_UNWIND)def_(*_, **__): print("exiting ", sys._getframe(1), "with exception") deffoo(): raiseRuntimeError() defbar(): returnfoo() defbaz(): returnbar() m.set_local_events(0, random.choice([foo, bar, baz]).__code__, e.PY_UNWIND|e.PY_START) try: baz() exceptException: pass# entering <frame at 0x101602f80, file 'test_py_unwind_local.py', line 34, code baz># exiting <frame at 0x101602f80, file 'test_py_unwind_local.py', line 35, code baz> with exceptionA debugger is an example of a tool that can benefit from PY_UNWIND being a local event.
The proposed change will break the current assumption that local events always require bytecode instrumentation. Instead, the PY_UNWIND event will simply make a local tool check instead of a global one inside monitor_unwind. No changes to bytecode or any other parts are required.
This is an extension of the current implementation of PEP 669 whereby PY_UNWIND will be available as a local event. The behaviour of the already existing global event will not be touched. As such, the proposed change will be guaranteed to be backwards-compatible.
Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Links to previous discussion of this feature:
https://discuss.python.org/t/pep-669-low-impact-monitoring-for-cpython/13018/82