Skip to content

Commit ac7a0f8

Browse files
authored
GH-106898: Add the exception as an argument to the PY_UNWIND event callback function. (GH-107347)
1 parent 9a7204b commit ac7a0f8

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

Lib/test/test_monitoring.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ def check_balanced(self, func, recorders):
715715
self.assertIn(r0, ("raise", "reraise"))
716716
h0 = h[0]
717717
self.assertIn(h0, ("handled", "unwind"))
718-
718+
self.assertEqual(r[1], h[1])
719719

720720

721721
class StopiterationRecorder(ExceptionRecorder):
@@ -733,8 +733,8 @@ class UnwindRecorder(ExceptionRecorder):
733733

734734
event_type = E.PY_UNWIND
735735

736-
def __call__(self, *args):
737-
self.events.append(("unwind", None))
736+
def __call__(self, code, offset, exc):
737+
self.events.append(("unwind", type(exc)))
738738

739739
class ExceptionHandledRecorder(ExceptionRecorder):
740740

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add the exception as the third argument to ``PY_UNIND`` callbacks in
2+
``sys.monitoring``. This makes the ``PY_UNWIND`` callback consistent with
3+
the other exception hanlding callbacks.

Python/ceval.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1987,7 +1987,7 @@ monitor_unwind(PyThreadState *tstate,
19871987
if (no_tools_for_event(tstate, frame, PY_MONITORING_EVENT_PY_UNWIND)) {
19881988
return;
19891989
}
1990-
_Py_call_instrumentation_exc0(tstate, PY_MONITORING_EVENT_PY_UNWIND, frame, instr);
1990+
do_monitor_exc(tstate, frame, instr, PY_MONITORING_EVENT_PY_UNWIND);
19911991
}
19921992

19931993

Python/legacy_tracing.c

+22-2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ sys_profile_func3(
6464
return call_profile_func(self, args[2]);
6565
}
6666

67+
static PyObject *
68+
sys_profile_unwind(
69+
_PyLegacyEventHandler *self, PyObject *const *args,
70+
size_t nargsf, PyObject *kwnames
71+
) {
72+
assert(kwnames == NULL);
73+
assert(PyVectorcall_NARGS(nargsf) == 3);
74+
return call_profile_func(self, Py_None);
75+
}
76+
6777
static PyObject *
6878
sys_profile_call_or_return(
6979
_PyLegacyEventHandler *self, PyObject *const *args,
@@ -152,6 +162,16 @@ sys_trace_func2(
152162
return call_trace_func(self, Py_None);
153163
}
154164

165+
static PyObject *
166+
sys_trace_unwind(
167+
_PyLegacyEventHandler *self, PyObject *const *args,
168+
size_t nargsf, PyObject *kwnames
169+
) {
170+
assert(kwnames == NULL);
171+
assert(PyVectorcall_NARGS(nargsf) == 3);
172+
return call_trace_func(self, Py_None);
173+
}
174+
155175
static PyObject *
156176
sys_trace_return(
157177
_PyLegacyEventHandler *self, PyObject *const *args,
@@ -362,7 +382,7 @@ _PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
362382
return -1;
363383
}
364384
if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID,
365-
(vectorcallfunc)sys_profile_func2, PyTrace_RETURN,
385+
(vectorcallfunc)sys_profile_unwind, PyTrace_RETURN,
366386
PY_MONITORING_EVENT_PY_UNWIND, -1)) {
367387
return -1;
368388
}
@@ -450,7 +470,7 @@ _PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
450470
return -1;
451471
}
452472
if (set_callbacks(PY_MONITORING_SYS_TRACE_ID,
453-
(vectorcallfunc)sys_trace_func2, PyTrace_RETURN,
473+
(vectorcallfunc)sys_trace_unwind, PyTrace_RETURN,
454474
PY_MONITORING_EVENT_PY_UNWIND, -1)) {
455475
return -1;
456476
}

0 commit comments

Comments
 (0)