]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] GH-107724: Fix the signature of `PY_THROW` callback functions. (GH-107725...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 11 Aug 2023 09:58:27 +0000 (02:58 -0700)
committerGitHub <noreply@github.com>
Fri, 11 Aug 2023 09:58:27 +0000 (11:58 +0200)
GH-107724: Fix the signature of `PY_THROW` callback functions. (GH-107725)
(cherry picked from commit 52fbcf61b5a70993c2d32332ff0ad9f369d968d3)

Co-authored-by: Mark Shannon <mark@hotpy.org>
Include/internal/pycore_instruments.h
Lib/test/test_monitoring.py
Misc/NEWS.d/next/Core and Builtins/2023-08-04-21-25-26.gh-issue-107724.EbBXMr.rst [new file with mode: 0644]
Python/ceval.c
Python/instrumentation.c
Python/legacy_tracing.c

index ccccd54a2f70a22200d98147480ee9e25a397280..56de9f8717148449226a04614d07ecc50e9c003f 100644 (file)
@@ -90,10 +90,6 @@ extern int
 _Py_call_instrumentation_2args(PyThreadState *tstate, int event,
     _PyInterpreterFrame *frame, _Py_CODEUNIT *instr, PyObject *arg0, PyObject *arg1);
 
-extern void
-_Py_call_instrumentation_exc0(PyThreadState *tstate, int event,
-    _PyInterpreterFrame *frame, _Py_CODEUNIT *instr);
-
 extern void
 _Py_call_instrumentation_exc2(PyThreadState *tstate, int event,
     _PyInterpreterFrame *frame, _Py_CODEUNIT *instr, PyObject *arg0, PyObject *arg1);
index 9d0ad6fa834bc739a711cac368bb672b862cae74..4c7438992f88e53524d185df9c4f362ab3cbb8ca 100644 (file)
@@ -743,6 +743,13 @@ class ExceptionHandledRecorder(ExceptionRecorder):
     def __call__(self, code, offset, exc):
         self.events.append(("handled", type(exc)))
 
+class ThrowRecorder(ExceptionRecorder):
+
+    event_type = E.PY_THROW
+
+    def __call__(self, code, offset, exc):
+        self.events.append(("throw", type(exc)))
+
 class ExceptionMonitoringTest(CheckEvents):
 
 
@@ -888,6 +895,31 @@ class ExceptionMonitoringTest(CheckEvents):
             func,
             recorders = self.exception_recorders)
 
+    def test_throw(self):
+
+        def gen():
+            yield 1
+            yield 2
+
+        def func():
+            try:
+                g = gen()
+                next(g)
+                g.throw(IndexError)
+            except IndexError:
+                pass
+
+        self.check_balanced(
+            func,
+            recorders = self.exception_recorders)
+
+        events = self.get_events(
+            func,
+            TEST_TOOL,
+            self.exception_recorders + (ThrowRecorder,)
+        )
+        self.assertEqual(events[0], ("throw", IndexError))
+
 class LineRecorder:
 
     event_type = E.LINE
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-08-04-21-25-26.gh-issue-107724.EbBXMr.rst b/Misc/NEWS.d/next/Core and Builtins/2023-08-04-21-25-26.gh-issue-107724.EbBXMr.rst
new file mode 100644 (file)
index 0000000..6e853cf
--- /dev/null
@@ -0,0 +1,3 @@
+In pre-release versions of 3.12, up to rc1, the sys.monitoring callback
+function for the ``PY_THROW`` event was missing the third, exception
+argument. That is now fixed.
index 88d6362bf48a1f06805b462968ca7b03be7c59ca..c6f54dd24052e873e3cae8e46d145f859e04d852 100644 (file)
@@ -2099,7 +2099,7 @@ monitor_throw(PyThreadState *tstate,
     if (no_tools_for_event(tstate, frame, PY_MONITORING_EVENT_PY_THROW)) {
         return;
     }
-    _Py_call_instrumentation_exc0(tstate, PY_MONITORING_EVENT_PY_THROW, frame, instr);
+    do_monitor_exc(tstate, frame, instr, PY_MONITORING_EVENT_PY_THROW);
 }
 
 void
index 05a53d04bf3cc4252f8786d78d4b95b3af0779fb..8714324e72c327368f0ea875ba648ea34931d5d8 100644 (file)
@@ -1070,16 +1070,6 @@ call_instrumentation_vector_protected(
     assert(_PyErr_Occurred(tstate));
 }
 
-void
-_Py_call_instrumentation_exc0(
-    PyThreadState *tstate, int event,
-    _PyInterpreterFrame *frame, _Py_CODEUNIT *instr)
-{
-    assert(_PyErr_Occurred(tstate));
-    PyObject *args[3] = { NULL, NULL, NULL };
-    call_instrumentation_vector_protected(tstate, event, frame, instr, 2, args);
-}
-
 void
 _Py_call_instrumentation_exc2(
     PyThreadState *tstate, int event,
index c1c70f667ccd29c81415e551acd93f4df0dc9c90..b0136d2ebc75548c459bdc6635e7c0003a657190 100644 (file)
@@ -163,7 +163,7 @@ sys_trace_func2(
 }
 
 static PyObject *
-sys_trace_unwind(
+sys_trace_func3(
     _PyLegacyEventHandler *self, PyObject *const *args,
     size_t nargsf, PyObject *kwnames
 ) {
@@ -446,7 +446,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_CALL,
+            (vectorcallfunc)sys_trace_func3, PyTrace_CALL,
                         PY_MONITORING_EVENT_PY_THROW, -1)) {
             return -1;
         }
@@ -471,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_unwind, PyTrace_RETURN,
+            (vectorcallfunc)sys_trace_func3, PyTrace_RETURN,
                         PY_MONITORING_EVENT_PY_UNWIND, -1)) {
             return -1;
         }