Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dae3db1

Browse files
[3.12] gh-110514: Add PY_THROW to sys.setprofile events (GH-110524) (#110541)
gh-110514: Add PY_THROW to `sys.setprofile` events (GH-110524) (cherry picked from commit dd4bb05) Co-authored-by: Tian Gao <[email protected]>
1 parent fb8c041 commit dae3db1

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

Lib/test/test_sys_setprofile.py

+19
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,25 @@ def g(p):
255255
(1, 'return', g_ident),
256256
])
257257

258+
def test_unfinished_generator(self):
259+
def f():
260+
for i in range(2):
261+
yield i
262+
def g(p):
263+
next(f())
264+
265+
f_ident = ident(f)
266+
g_ident = ident(g)
267+
self.check_events(g, [(1, 'call', g_ident),
268+
(2, 'call', f_ident),
269+
(2, 'return', f_ident),
270+
# once more; the generator is being garbage collected
271+
# and it will do a PY_THROW
272+
(2, 'call', f_ident),
273+
(2, 'return', f_ident),
274+
(1, 'return', g_ident),
275+
])
276+
258277
def test_stop_iteration(self):
259278
def f():
260279
for i in range(2):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add ``PY_THROW`` to :func:`sys.setprofile` events

Python/legacy_tracing.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,11 @@ _PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
377377
PY_MONITORING_EVENT_PY_START, PY_MONITORING_EVENT_PY_RESUME)) {
378378
return -1;
379379
}
380+
if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID,
381+
(vectorcallfunc)sys_profile_func3, PyTrace_CALL,
382+
PY_MONITORING_EVENT_PY_THROW, -1)) {
383+
return -1;
384+
}
380385
if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID,
381386
(vectorcallfunc)sys_profile_func3, PyTrace_RETURN,
382387
PY_MONITORING_EVENT_PY_RETURN, PY_MONITORING_EVENT_PY_YIELD)) {
@@ -417,7 +422,8 @@ _PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
417422
events =
418423
(1 << PY_MONITORING_EVENT_PY_START) | (1 << PY_MONITORING_EVENT_PY_RESUME) |
419424
(1 << PY_MONITORING_EVENT_PY_RETURN) | (1 << PY_MONITORING_EVENT_PY_YIELD) |
420-
(1 << PY_MONITORING_EVENT_CALL) | (1 << PY_MONITORING_EVENT_PY_UNWIND);
425+
(1 << PY_MONITORING_EVENT_CALL) | (1 << PY_MONITORING_EVENT_PY_UNWIND) |
426+
(1 << PY_MONITORING_EVENT_PY_THROW);
421427
}
422428
return _PyMonitoring_SetEvents(PY_MONITORING_SYS_PROFILE_ID, events);
423429
}

0 commit comments

Comments
 (0)