Skip to content

Commit d17f28f

Browse files
gh-140373: Correctly emit PY_UNWIND event when generator is closed (GH-140767)
1 parent ac1ffd7 commit d17f28f

File tree

6 files changed

+30
-1
lines changed

6 files changed

+30
-1
lines changed

Include/internal/pycore_ceval.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ PyAPI_FUNC(PyObject *) _PyEval_ImportName(PyThreadState *, _PyInterpreterFrame *
301301
PyAPI_FUNC(PyObject *)_PyEval_MatchClass(PyThreadState *tstate, PyObject *subject, PyObject *type, Py_ssize_t nargs, PyObject *kwargs);
302302
PyAPI_FUNC(PyObject *)_PyEval_MatchKeys(PyThreadState *tstate, PyObject *map, PyObject *keys);
303303
PyAPI_FUNC(void) _PyEval_MonitorRaise(PyThreadState *tstate, _PyInterpreterFrame *frame, _Py_CODEUNIT *instr);
304+
PyAPI_FUNC(bool) _PyEval_NoToolsForUnwind(PyThreadState *tstate);
304305
PyAPI_FUNC(int) _PyEval_UnpackIterableStackRef(PyThreadState *tstate, PyObject *v, int argcnt, int argcntafter, _PyStackRef *sp);
305306
PyAPI_FUNC(void) _PyEval_FrameClearAndPop(PyThreadState *tstate, _PyInterpreterFrame *frame);
306307
PyAPI_FUNC(PyObject **) _PyObjectArray_FromStackRefArray(_PyStackRef *input, Py_ssize_t nargs, PyObject **scratch);

Lib/test/test_monitoring.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,25 @@ def f():
10791079

10801080
self.assertEqual(events, expected)
10811081

1082+
# gh-140373
1083+
def test_gen_unwind(self):
1084+
def gen():
1085+
yield 1
1086+
1087+
def f():
1088+
g = gen()
1089+
next(g)
1090+
g.close()
1091+
1092+
recorders = (
1093+
UnwindRecorder,
1094+
)
1095+
events = self.get_events(f, TEST_TOOL, recorders)
1096+
expected = [
1097+
("unwind", GeneratorExit, "gen"),
1098+
]
1099+
self.assertEqual(events, expected)
1100+
10821101
class LineRecorder:
10831102

10841103
event_type = E.LINE

Lib/test/test_sys_setprofile.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ def g(p):
272272
self.check_events(g, [(1, 'call', g_ident, None),
273273
(2, 'call', f_ident, None),
274274
(2, 'return', f_ident, 0),
275+
(2, 'call', f_ident, None),
276+
(2, 'return', f_ident, None),
275277
(1, 'return', g_ident, None),
276278
], check_args=True)
277279

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Correctly emit ``PY_UNWIND`` event when generator object is closed. Patch by
2+
Mikhail Efimov.

Objects/genobject.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,12 @@ gen_close(PyObject *self, PyObject *args)
407407
}
408408
_PyInterpreterFrame *frame = &gen->gi_iframe;
409409
if (is_resume(frame->instr_ptr)) {
410+
bool no_unwind_tools = _PyEval_NoToolsForUnwind(_PyThreadState_GET());
410411
/* We can safely ignore the outermost try block
411412
* as it is automatically generated to handle
412413
* StopIteration. */
413414
int oparg = frame->instr_ptr->op.arg;
414-
if (oparg & RESUME_OPARG_DEPTH1_MASK) {
415+
if (oparg & RESUME_OPARG_DEPTH1_MASK && no_unwind_tools) {
415416
// RESUME after YIELD_VALUE and exception depth is 1
416417
assert((oparg & RESUME_OPARG_LOCATION_MASK) != RESUME_AT_FUNC_START);
417418
gen->gi_frame_state = FRAME_COMPLETED;

Python/ceval.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2466,6 +2466,10 @@ monitor_unwind(PyThreadState *tstate,
24662466
do_monitor_exc(tstate, frame, instr, PY_MONITORING_EVENT_PY_UNWIND);
24672467
}
24682468

2469+
bool
2470+
_PyEval_NoToolsForUnwind(PyThreadState *tstate) {
2471+
return no_tools_for_global_event(tstate, PY_MONITORING_EVENT_PY_UNWIND);
2472+
}
24692473

24702474
static int
24712475
monitor_handled(PyThreadState *tstate,

0 commit comments

Comments
 (0)