]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-140373: Correctly emit `PY_UNWIND` event when generator is closed (GH-14076...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sun, 30 Nov 2025 12:35:14 +0000 (13:35 +0100)
committerGitHub <noreply@github.com>
Sun, 30 Nov 2025 12:35:14 +0000 (14:35 +0200)
Co-authored-by: Mikhail Efimov <efimov.mikhail@gmail.com>
Include/internal/pycore_ceval.h
Lib/test/test_monitoring.py
Lib/test/test_sys_setprofile.py
Misc/NEWS.d/next/Core_and_Builtins/2025-10-29-20-59-10.gh-issue-140373.-uoaPP.rst [new file with mode: 0644]
Objects/genobject.c
Python/ceval.c

index cf5ad528217eab73485b8860074c07606ca55fa6..89504f6bfbbfb73015714a4097761b6157c26288 100644 (file)
@@ -296,6 +296,7 @@ PyAPI_FUNC(PyObject *) _PyEval_ImportName(PyThreadState *, _PyInterpreterFrame *
 PyAPI_FUNC(PyObject *)_PyEval_MatchClass(PyThreadState *tstate, PyObject *subject, PyObject *type, Py_ssize_t nargs, PyObject *kwargs);
 PyAPI_FUNC(PyObject *)_PyEval_MatchKeys(PyThreadState *tstate, PyObject *map, PyObject *keys);
 PyAPI_FUNC(void) _PyEval_MonitorRaise(PyThreadState *tstate, _PyInterpreterFrame *frame, _Py_CODEUNIT *instr);
+PyAPI_FUNC(bool) _PyEval_NoToolsForUnwind(PyThreadState *tstate);
 PyAPI_FUNC(int) _PyEval_UnpackIterableStackRef(PyThreadState *tstate, PyObject *v, int argcnt, int argcntafter, _PyStackRef *sp);
 PyAPI_FUNC(void) _PyEval_FrameClearAndPop(PyThreadState *tstate, _PyInterpreterFrame *frame);
 PyAPI_FUNC(PyObject **) _PyObjectArray_FromStackRefArray(_PyStackRef *input, Py_ssize_t nargs, PyObject **scratch);
index 98276a8ccaa610826f67adcd36e14484ae89e67c..de658c597a197d7599d72e4201c88a268840b35f 100644 (file)
@@ -1079,6 +1079,25 @@ class ExceptionMonitoringTest(CheckEvents):
 
         self.assertEqual(events, expected)
 
+    # gh-140373
+    def test_gen_unwind(self):
+        def gen():
+            yield 1
+
+        def f():
+            g = gen()
+            next(g)
+            g.close()
+
+        recorders = (
+            UnwindRecorder,
+        )
+        events = self.get_events(f, TEST_TOOL, recorders)
+        expected = [
+            ("unwind", GeneratorExit, "gen"),
+        ]
+        self.assertEqual(events, expected)
+
 class LineRecorder:
 
     event_type = E.LINE
index 345c022bd2374c763367f961771b91c874fc1b27..a22b728b56941969bdc5a3ac018d55134c9926e0 100644 (file)
@@ -272,6 +272,8 @@ class ProfileHookTestCase(TestCaseBase):
         self.check_events(g, [(1, 'call', g_ident, None),
                               (2, 'call', f_ident, None),
                               (2, 'return', f_ident, 0),
+                              (2, 'call', f_ident, None),
+                              (2, 'return', f_ident, None),
                               (1, 'return', g_ident, None),
                               ], check_args=True)
 
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-10-29-20-59-10.gh-issue-140373.-uoaPP.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-10-29-20-59-10.gh-issue-140373.-uoaPP.rst
new file mode 100644 (file)
index 0000000..c9a9703
--- /dev/null
@@ -0,0 +1,2 @@
+Correctly emit ``PY_UNWIND`` event when generator object is closed. Patch by
+Mikhail Efimov.
index f429bc47678fedee6deb76c68e6e3ec54a4b5943..575752fc84002a11487fbca59b67c5d3dcbc4bb6 100644 (file)
@@ -407,11 +407,12 @@ gen_close(PyObject *self, PyObject *args)
     }
     _PyInterpreterFrame *frame = &gen->gi_iframe;
     if (is_resume(frame->instr_ptr)) {
+        bool no_unwind_tools = _PyEval_NoToolsForUnwind(_PyThreadState_GET());
         /* We can safely ignore the outermost try block
          * as it is automatically generated to handle
          * StopIteration. */
         int oparg = frame->instr_ptr->op.arg;
-        if (oparg & RESUME_OPARG_DEPTH1_MASK) {
+        if (oparg & RESUME_OPARG_DEPTH1_MASK && no_unwind_tools) {
             // RESUME after YIELD_VALUE and exception depth is 1
             assert((oparg & RESUME_OPARG_LOCATION_MASK) != RESUME_AT_FUNC_START);
             gen->gi_frame_state = FRAME_COMPLETED;
index 4b1e7abe66dfad30e173433d9d1d47f0e01cf142..faca6fe81b977ad88766e70ceeef2ad48271a7f8 100644 (file)
@@ -2545,6 +2545,10 @@ monitor_unwind(PyThreadState *tstate,
     do_monitor_exc(tstate, frame, instr, PY_MONITORING_EVENT_PY_UNWIND);
 }
 
+bool
+_PyEval_NoToolsForUnwind(PyThreadState *tstate) {
+    return no_tools_for_global_event(tstate, PY_MONITORING_EVENT_PY_UNWIND);
+}
 
 static int
 monitor_handled(PyThreadState *tstate,