Skip to content

GH-105162: Account for INSTRUMENTED_RESUME in gen.close/throw. #105187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Lib/test/test_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -1425,3 +1425,38 @@ def f():

def test_get_local_events_uninitialized(self):
self.assertEqual(sys.monitoring.get_local_events(TEST_TOOL, self.f.__code__), 0)

class TestRegressions(MonitoringTestBase, unittest.TestCase):

def test_105162(self):
caught = None

def inner():
nonlocal caught
try:
yield
except Exception:
caught = "inner"
yield

def outer():
nonlocal caught
try:
yield from inner()
except Exception:
caught = "outer"
yield

def run():
gen = outer()
gen.send(None)
gen.throw(Exception)
run()
self.assertEqual(caught, "inner")
caught = None
try:
sys.monitoring.set_events(TEST_TOOL, E.PY_RESUME)
run()
self.assertEqual(caught, "inner")
finally:
sys.monitoring.set_events(TEST_TOOL, 0)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed bug in generator.close()/throw() where an inner iterator would be
ignored when the outer iterator was instrumented.
18 changes: 15 additions & 3 deletions Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,18 @@ gen_close_iter(PyObject *yf)
return 0;
}

static inline bool
is_resume(_Py_CODEUNIT *instr)
{
return instr->op.code == RESUME || instr->op.code == INSTRUMENTED_RESUME;
}

static inline bool
is_yield(_Py_CODEUNIT *instr)
{
return instr->op.code == YIELD_VALUE || instr->op.code == INSTRUMENTED_YIELD_VALUE;
}

PyObject *
_PyGen_yf(PyGenObject *gen)
{
Expand All @@ -347,7 +359,7 @@ _PyGen_yf(PyGenObject *gen)
return NULL;
}
_Py_CODEUNIT next = frame->prev_instr[1];
if (next.op.code != RESUME || next.op.arg < 2)
if (!is_resume(&next) || next.op.arg < 2)
{
/* Not in a yield from */
return NULL;
Expand Down Expand Up @@ -382,8 +394,8 @@ gen_close(PyGenObject *gen, PyObject *args)
_PyInterpreterFrame *frame = (_PyInterpreterFrame *)gen->gi_iframe;
/* It is possible for the previous instruction to not be a
* YIELD_VALUE if the debugger has changed the lineno. */
if (err == 0 && frame->prev_instr[0].op.code == YIELD_VALUE) {
assert(frame->prev_instr[1].op.code == RESUME);
if (err == 0 && is_yield(frame->prev_instr)) {
assert(is_resume(frame->prev_instr + 1));
int exception_handler_depth = frame->prev_instr[0].op.code;
assert(exception_handler_depth > 0);
/* We can safely ignore the outermost try block
Expand Down