From 3f167de440f65eb03f795e33b98cf3e84627e213 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Fri, 28 Jul 2023 11:30:16 +0100 Subject: [3.12] GH-106898: Add the exception as an argument to the `PY_UNWIND` event callback function. (GH-107347) (GH-107382) --- Lib/test/test_monitoring.py | 6 +++--- .../2023-07-26-21-28-06.gh-issue-106898.8Wjuiv.rst | 3 +++ Python/ceval.c | 2 +- Python/legacy_tracing.c | 24 ++++++++++++++++++++-- 4 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-07-26-21-28-06.gh-issue-106898.8Wjuiv.rst diff --git a/Lib/test/test_monitoring.py b/Lib/test/test_monitoring.py index 9685a43..9d0ad6f 100644 --- a/Lib/test/test_monitoring.py +++ b/Lib/test/test_monitoring.py @@ -715,7 +715,7 @@ class CheckEvents(MonitoringTestBase, unittest.TestCase): self.assertIn(r0, ("raise", "reraise")) h0 = h[0] self.assertIn(h0, ("handled", "unwind")) - + self.assertEqual(r[1], h[1]) class StopiterationRecorder(ExceptionRecorder): @@ -733,8 +733,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): diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-07-26-21-28-06.gh-issue-106898.8Wjuiv.rst b/Misc/NEWS.d/next/Core and Builtins/2023-07-26-21-28-06.gh-issue-106898.8Wjuiv.rst new file mode 100644 index 0000000..f1b1c4c --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-07-26-21-28-06.gh-issue-106898.8Wjuiv.rst @@ -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. diff --git a/Python/ceval.c b/Python/ceval.c index 27dea27..4947420 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -2070,7 +2070,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); } diff --git a/Python/legacy_tracing.c b/Python/legacy_tracing.c index 5143b79..c1c70f6 100644 --- a/Python/legacy_tracing.c +++ b/Python/legacy_tracing.c @@ -65,6 +65,16 @@ sys_profile_func3( } 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, size_t nargsf, PyObject *kwnames @@ -153,6 +163,16 @@ sys_trace_func2( } 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, size_t nargsf, PyObject *kwnames @@ -363,7 +383,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; } @@ -451,7 +471,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; } -- cgit v0.12