[Python-checkins] [3.12] GH-105162: Account for `INSTRUMENTED_RESUME` in gen.close/throw. (GH-105187) (#105378)
Yhg1s
webhook-mailer at python.org
Tue Jun 6 10:06:52 EDT 2023
https://github.com/python/cpython/commit/2d9ead219e940d3fd717cdb0841cfe8a97efe554
commit: 2d9ead219e940d3fd717cdb0841cfe8a97efe554
branch: 3.12
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Yhg1s <thomas at python.org>
date: 2023-06-06T14:06:44Z
summary:
[3.12] GH-105162: Account for `INSTRUMENTED_RESUME` in gen.close/throw. (GH-105187) (#105378)
GH-105162: Account for `INSTRUMENTED_RESUME` in gen.close/throw. (GH-105187)
(cherry picked from commit 601ae09f0c8eda213b9050892f5ce9b91f0aa522)
Co-authored-by: Mark Shannon <mark at hotpy.org>
files:
A Misc/NEWS.d/next/Core and Builtins/2023-06-01-11-37-03.gh-issue-105162.r8VCXk.rst
M Lib/test/test_monitoring.py
M Objects/genobject.c
diff --git a/Lib/test/test_monitoring.py b/Lib/test/test_monitoring.py
index 46b817d74f09..ef4a79aefd64 100644
--- a/Lib/test/test_monitoring.py
+++ b/Lib/test/test_monitoring.py
@@ -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)
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-06-01-11-37-03.gh-issue-105162.r8VCXk.rst b/Misc/NEWS.d/next/Core and Builtins/2023-06-01-11-37-03.gh-issue-105162.r8VCXk.rst
new file mode 100644
index 000000000000..adb4e8478d9c
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2023-06-01-11-37-03.gh-issue-105162.r8VCXk.rst
@@ -0,0 +1,2 @@
+Fixed bug in generator.close()/throw() where an inner iterator would be
+ignored when the outer iterator was instrumented.
diff --git a/Objects/genobject.c b/Objects/genobject.c
index 9252c6549345..b13b52edf5c5 100644
--- a/Objects/genobject.c
+++ b/Objects/genobject.c
@@ -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)
{
@@ -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;
@@ -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
More information about the Python-checkins
mailing list