Skip to content

Commit 07a26bd

Browse files
committed
Make PY_UNWIND available as a local event
We make the PY_UNWIND monitoring event available as a code-local event to allow trapping on function exit events when an exception bubbles up. This complements the PY_RETURN event by allowing to catch any function exit event.
1 parent 52c0186 commit 07a26bd

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

Include/cpython/monitoring.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ extern "C" {
99

1010

1111
/* Local events.
12-
* These require bytecode instrumentation */
12+
* Some of these require bytecode instrumentation */
1313

1414
#define PY_MONITORING_EVENT_PY_START 0
1515
#define PY_MONITORING_EVENT_PY_RESUME 1
@@ -22,15 +22,18 @@ extern "C" {
2222
#define PY_MONITORING_EVENT_BRANCH_LEFT 8
2323
#define PY_MONITORING_EVENT_BRANCH_RIGHT 9
2424
#define PY_MONITORING_EVENT_STOP_ITERATION 10
25+
#define PY_MONITORING_EVENT_PY_UNWIND 11
2526

27+
// TODO: PY_UNWIND requires no instrumentation so this definition is not
28+
// entirely correct.
2629
#define PY_MONITORING_IS_INSTRUMENTED_EVENT(ev) \
27-
((ev) < _PY_MONITORING_LOCAL_EVENTS)
30+
((ev) <= _PY_MONITORING_LOCAL_EVENTS)
31+
2832

2933
/* Other events, mainly exceptions */
3034

31-
#define PY_MONITORING_EVENT_RAISE 11
3235
#define PY_MONITORING_EVENT_EXCEPTION_HANDLED 12
33-
#define PY_MONITORING_EVENT_PY_UNWIND 13
36+
#define PY_MONITORING_EVENT_RAISE 13
3437
#define PY_MONITORING_EVENT_PY_THROW 14
3538
#define PY_MONITORING_EVENT_RERAISE 15
3639

Include/internal/pycore_instruments.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ PyAPI_DATA(PyObject) _PyInstrumentation_DISABLE;
7474
/* Total tool ids available */
7575
#define PY_MONITORING_TOOL_IDS 8
7676
/* Count of all local monitoring events */
77-
#define _PY_MONITORING_LOCAL_EVENTS 11
77+
#define _PY_MONITORING_LOCAL_EVENTS 12
7878
/* Count of all "real" monitoring events (not derived from other events) */
7979
#define _PY_MONITORING_UNGROUPED_EVENTS 16
8080
/* Count of all monitoring events */

Python/ceval.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ monitor_unwind(PyThreadState *tstate,
431431
_PyInterpreterFrame *frame,
432432
_Py_CODEUNIT *instr)
433433
{
434-
if (no_tools_for_global_event(tstate, PY_MONITORING_EVENT_PY_UNWIND)) {
434+
if (no_tools_for_local_event(tstate, frame, PY_MONITORING_EVENT_PY_UNWIND)) {
435435
return;
436436
}
437437
do_monitor_exc(tstate, frame, instr, PY_MONITORING_EVENT_PY_UNWIND);

0 commit comments

Comments
 (0)