Skip to content

Commit 8332e85

Browse files
gh-116626: Emit CALL events for all INSTRUMENTED_CALL_FUNCTION_EX (GH-116627)
1 parent ba82a24 commit 8332e85

File tree

4 files changed

+45
-28
lines changed

4 files changed

+45
-28
lines changed

Lib/test/test_monitoring.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,6 +1807,21 @@ def test_gh108976(self):
18071807
sys.monitoring.set_events(0, E.LINE | E.INSTRUCTION)
18081808
sys.monitoring.set_events(0, 0)
18091809

1810+
def test_call_function_ex(self):
1811+
def f(a, b):
1812+
return a + b
1813+
args = (1, 2)
1814+
1815+
call_data = []
1816+
sys.monitoring.use_tool_id(0, "test")
1817+
self.addCleanup(sys.monitoring.free_tool_id, 0)
1818+
sys.monitoring.set_events(0, 0)
1819+
sys.monitoring.register_callback(0, E.CALL, lambda code, offset, callable, arg0: call_data.append((callable, arg0)))
1820+
sys.monitoring.set_events(0, E.CALL)
1821+
f(*args)
1822+
sys.monitoring.set_events(0, 0)
1823+
self.assertEqual(call_data[0], (f, 1))
1824+
18101825

18111826
class TestOptimizer(MonitoringTestBase, unittest.TestCase):
18121827

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ensure ``INSTRUMENTED_CALL_FUNCTION_EX`` always emits :monitoring-event:`CALL`

Python/bytecodes.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3729,27 +3729,28 @@ dummy_func(
37293729
}
37303730
assert(PyTuple_CheckExact(callargs));
37313731
EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_FUNCTION_EX, func);
3732-
if (opcode == INSTRUMENTED_CALL_FUNCTION_EX &&
3733-
!PyFunction_Check(func) && !PyMethod_Check(func)
3734-
) {
3732+
if (opcode == INSTRUMENTED_CALL_FUNCTION_EX) {
37353733
PyObject *arg = PyTuple_GET_SIZE(callargs) > 0 ?
37363734
PyTuple_GET_ITEM(callargs, 0) : Py_None;
37373735
int err = _Py_call_instrumentation_2args(
37383736
tstate, PY_MONITORING_EVENT_CALL,
37393737
frame, this_instr, func, arg);
37403738
if (err) GOTO_ERROR(error);
37413739
result = PyObject_Call(func, callargs, kwargs);
3742-
if (result == NULL) {
3743-
_Py_call_instrumentation_exc2(
3744-
tstate, PY_MONITORING_EVENT_C_RAISE,
3745-
frame, this_instr, func, arg);
3746-
}
3747-
else {
3748-
int err = _Py_call_instrumentation_2args(
3749-
tstate, PY_MONITORING_EVENT_C_RETURN,
3750-
frame, this_instr, func, arg);
3751-
if (err < 0) {
3752-
Py_CLEAR(result);
3740+
3741+
if (!PyFunction_Check(func) && !PyMethod_Check(func)) {
3742+
if (result == NULL) {
3743+
_Py_call_instrumentation_exc2(
3744+
tstate, PY_MONITORING_EVENT_C_RAISE,
3745+
frame, this_instr, func, arg);
3746+
}
3747+
else {
3748+
int err = _Py_call_instrumentation_2args(
3749+
tstate, PY_MONITORING_EVENT_C_RETURN,
3750+
frame, this_instr, func, arg);
3751+
if (err < 0) {
3752+
Py_CLEAR(result);
3753+
}
37533754
}
37543755
}
37553756
}

Python/generated_cases.c.h

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)