Skip to content

GH-106898: Add the exception as an argument to the PY_UNWIND event callback function. #107347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Lib/test/test_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ def check_balanced(self, func, recorders):
self.assertIn(r0, ("raise", "reraise"))
h0 = h[0]
self.assertIn(h0, ("handled", "unwind"))

self.assertEqual(r[1], h[1])


class StopiterationRecorder(ExceptionRecorder):
Expand All @@ -683,8 +683,8 @@ class UnwindRecorder(ExceptionRecorder):

event_type = E.PY_UNWIND

def __call__(self, *args):
self.events.append(("unwind", None))
def __call__(self, code, offset, exc):
self.events.append(("unwind", type(exc)))

class ExceptionHandledRecorder(ExceptionRecorder):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add the exception as the third argument to ``PY_UNIND`` callbacks in
``sys.monitoring``. This makes the ``PY_UNWIND`` callback consistent with
the other exception hanlding callbacks.
2 changes: 1 addition & 1 deletion Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -1984,7 +1984,7 @@ monitor_unwind(PyThreadState *tstate,
if (no_tools_for_event(tstate, frame, PY_MONITORING_EVENT_PY_UNWIND)) {
return;
}
_Py_call_instrumentation_exc0(tstate, PY_MONITORING_EVENT_PY_UNWIND, frame, instr);
do_monitor_exc(tstate, frame, instr, PY_MONITORING_EVENT_PY_UNWIND);
}


Expand Down
24 changes: 22 additions & 2 deletions Python/legacy_tracing.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ sys_profile_func3(
return call_profile_func(self, args[2]);
}

static PyObject *
sys_profile_unwind(
_PyLegacyEventHandler *self, PyObject *const *args,
size_t nargsf, PyObject *kwnames
) {
assert(kwnames == NULL);
assert(PyVectorcall_NARGS(nargsf) == 3);
return call_profile_func(self, Py_None);
}

static PyObject *
sys_profile_call_or_return(
_PyLegacyEventHandler *self, PyObject *const *args,
Expand Down Expand Up @@ -152,6 +162,16 @@ sys_trace_func2(
return call_trace_func(self, Py_None);
}

static PyObject *
sys_trace_unwind(
_PyLegacyEventHandler *self, PyObject *const *args,
size_t nargsf, PyObject *kwnames
) {
assert(kwnames == NULL);
assert(PyVectorcall_NARGS(nargsf) == 3);
return call_trace_func(self, Py_None);
}

static PyObject *
sys_trace_return(
_PyLegacyEventHandler *self, PyObject *const *args,
Expand Down Expand Up @@ -362,7 +382,7 @@ _PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
return -1;
}
if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID,
(vectorcallfunc)sys_profile_func2, PyTrace_RETURN,
(vectorcallfunc)sys_profile_unwind, PyTrace_RETURN,
PY_MONITORING_EVENT_PY_UNWIND, -1)) {
return -1;
}
Expand Down Expand Up @@ -450,7 +470,7 @@ _PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
return -1;
}
if (set_callbacks(PY_MONITORING_SYS_TRACE_ID,
(vectorcallfunc)sys_trace_func2, PyTrace_RETURN,
(vectorcallfunc)sys_trace_unwind, PyTrace_RETURN,
PY_MONITORING_EVENT_PY_UNWIND, -1)) {
return -1;
}
Expand Down