Skip to content

Commit 52fbcf6

Browse files
authored
GH-107724: Fix the signature of PY_THROW callback functions. (GH-107725)
1 parent 2fb484e commit 52fbcf6

File tree

6 files changed

+39
-18
lines changed

6 files changed

+39
-18
lines changed

Include/internal/pycore_instruments.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,6 @@ extern int
9090
_Py_call_instrumentation_2args(PyThreadState *tstate, int event,
9191
_PyInterpreterFrame *frame, _Py_CODEUNIT *instr, PyObject *arg0, PyObject *arg1);
9292

93-
extern void
94-
_Py_call_instrumentation_exc0(PyThreadState *tstate, int event,
95-
_PyInterpreterFrame *frame, _Py_CODEUNIT *instr);
96-
9793
extern void
9894
_Py_call_instrumentation_exc2(PyThreadState *tstate, int event,
9995
_PyInterpreterFrame *frame, _Py_CODEUNIT *instr, PyObject *arg0, PyObject *arg1);

Lib/test/test_monitoring.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,13 @@ class ExceptionHandledRecorder(ExceptionRecorder):
743743
def __call__(self, code, offset, exc):
744744
self.events.append(("handled", type(exc)))
745745

746+
class ThrowRecorder(ExceptionRecorder):
747+
748+
event_type = E.PY_THROW
749+
750+
def __call__(self, code, offset, exc):
751+
self.events.append(("throw", type(exc)))
752+
746753
class ExceptionMonitoringTest(CheckEvents):
747754

748755

@@ -888,6 +895,31 @@ async def async_loop():
888895
func,
889896
recorders = self.exception_recorders)
890897

898+
def test_throw(self):
899+
900+
def gen():
901+
yield 1
902+
yield 2
903+
904+
def func():
905+
try:
906+
g = gen()
907+
next(g)
908+
g.throw(IndexError)
909+
except IndexError:
910+
pass
911+
912+
self.check_balanced(
913+
func,
914+
recorders = self.exception_recorders)
915+
916+
events = self.get_events(
917+
func,
918+
TEST_TOOL,
919+
self.exception_recorders + (ThrowRecorder,)
920+
)
921+
self.assertEqual(events[0], ("throw", IndexError))
922+
891923
class LineRecorder:
892924

893925
event_type = E.LINE
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
In pre-release versions of 3.12, up to rc1, the sys.monitoring callback
2+
function for the ``PY_THROW`` event was missing the third, exception
3+
argument. That is now fixed.

Python/ceval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2039,7 +2039,7 @@ monitor_throw(PyThreadState *tstate,
20392039
if (no_tools_for_event(tstate, frame, PY_MONITORING_EVENT_PY_THROW)) {
20402040
return;
20412041
}
2042-
_Py_call_instrumentation_exc0(tstate, PY_MONITORING_EVENT_PY_THROW, frame, instr);
2042+
do_monitor_exc(tstate, frame, instr, PY_MONITORING_EVENT_PY_THROW);
20432043
}
20442044

20452045
void

Python/instrumentation.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,16 +1081,6 @@ call_instrumentation_vector_protected(
10811081
assert(_PyErr_Occurred(tstate));
10821082
}
10831083

1084-
void
1085-
_Py_call_instrumentation_exc0(
1086-
PyThreadState *tstate, int event,
1087-
_PyInterpreterFrame *frame, _Py_CODEUNIT *instr)
1088-
{
1089-
assert(_PyErr_Occurred(tstate));
1090-
PyObject *args[3] = { NULL, NULL, NULL };
1091-
call_instrumentation_vector_protected(tstate, event, frame, instr, 2, args);
1092-
}
1093-
10941084
void
10951085
_Py_call_instrumentation_exc2(
10961086
PyThreadState *tstate, int event,

Python/legacy_tracing.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ sys_trace_func2(
163163
}
164164

165165
static PyObject *
166-
sys_trace_unwind(
166+
sys_trace_func3(
167167
_PyLegacyEventHandler *self, PyObject *const *args,
168168
size_t nargsf, PyObject *kwnames
169169
) {
@@ -445,7 +445,7 @@ _PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
445445
return -1;
446446
}
447447
if (set_callbacks(PY_MONITORING_SYS_TRACE_ID,
448-
(vectorcallfunc)sys_trace_func2, PyTrace_CALL,
448+
(vectorcallfunc)sys_trace_func3, PyTrace_CALL,
449449
PY_MONITORING_EVENT_PY_THROW, -1)) {
450450
return -1;
451451
}
@@ -470,7 +470,7 @@ _PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
470470
return -1;
471471
}
472472
if (set_callbacks(PY_MONITORING_SYS_TRACE_ID,
473-
(vectorcallfunc)sys_trace_unwind, PyTrace_RETURN,
473+
(vectorcallfunc)sys_trace_func3, PyTrace_RETURN,
474474
PY_MONITORING_EVENT_PY_UNWIND, -1)) {
475475
return -1;
476476
}

0 commit comments

Comments
 (0)