Skip to content

Commit

Permalink
[3.12] gh-110514: Add PY_THROW to sys.setprofile events (GH-110524) (
Browse files Browse the repository at this point in the history
…#110541)

gh-110514: Add PY_THROW to `sys.setprofile` events (GH-110524)
(cherry picked from commit dd4bb05)

Co-authored-by: Tian Gao <gaogaotiantian@hotmail.com>
  • Loading branch information
miss-islington and gaogaotiantian committed Oct 9, 2023
1 parent fb8c041 commit dae3db1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
19 changes: 19 additions & 0 deletions Lib/test/test_sys_setprofile.py
Expand Up @@ -255,6 +255,25 @@ def g(p):
(1, 'return', g_ident),
])

def test_unfinished_generator(self):
def f():
for i in range(2):
yield i
def g(p):
next(f())

f_ident = ident(f)
g_ident = ident(g)
self.check_events(g, [(1, 'call', g_ident),
(2, 'call', f_ident),
(2, 'return', f_ident),
# once more; the generator is being garbage collected
# and it will do a PY_THROW
(2, 'call', f_ident),
(2, 'return', f_ident),
(1, 'return', g_ident),
])

def test_stop_iteration(self):
def f():
for i in range(2):
Expand Down
@@ -0,0 +1 @@
Add ``PY_THROW`` to :func:`sys.setprofile` events
8 changes: 7 additions & 1 deletion Python/legacy_tracing.c
Expand Up @@ -377,6 +377,11 @@ _PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
PY_MONITORING_EVENT_PY_START, PY_MONITORING_EVENT_PY_RESUME)) {
return -1;
}
if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID,
(vectorcallfunc)sys_profile_func3, PyTrace_CALL,
PY_MONITORING_EVENT_PY_THROW, -1)) {
return -1;
}
if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID,
(vectorcallfunc)sys_profile_func3, PyTrace_RETURN,
PY_MONITORING_EVENT_PY_RETURN, PY_MONITORING_EVENT_PY_YIELD)) {
Expand Down Expand Up @@ -417,7 +422,8 @@ _PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
events =
(1 << PY_MONITORING_EVENT_PY_START) | (1 << PY_MONITORING_EVENT_PY_RESUME) |
(1 << PY_MONITORING_EVENT_PY_RETURN) | (1 << PY_MONITORING_EVENT_PY_YIELD) |
(1 << PY_MONITORING_EVENT_CALL) | (1 << PY_MONITORING_EVENT_PY_UNWIND);
(1 << PY_MONITORING_EVENT_CALL) | (1 << PY_MONITORING_EVENT_PY_UNWIND) |
(1 << PY_MONITORING_EVENT_PY_THROW);
}
return _PyMonitoring_SetEvents(PY_MONITORING_SYS_PROFILE_ID, events);
}
Expand Down

0 comments on commit dae3db1

Please sign in to comment.