Skip to content

Commit 01de359

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 b708485 commit 01de359

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
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 @@ extern 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.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2615,8 +2615,10 @@ monitor_unwind(PyThreadState *tstate,
26152615
_PyInterpreterFrame *frame,
26162616
_Py_CODEUNIT *instr)
26172617
{
2618-
if (no_tools_for_global_event(tstate, PY_MONITORING_EVENT_PY_UNWIND)) {
2619-
return;
2618+
if (no_tools_for_local_event(tstate, frame, PY_MONITORING_EVENT_PY_UNWIND)) {
2619+
if (no_tools_for_global_event(tstate, PY_MONITORING_EVENT_PY_UNWIND)) {
2620+
return;
2621+
}
26202622
}
26212623
do_monitor_exc(tstate, frame, instr, PY_MONITORING_EVENT_PY_UNWIND);
26222624
}

0 commit comments

Comments
 (0)