]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.10] GH--93592: Fix frame chain when throwing exceptions into coroutines (GH-95207)
authorKristján Valur Jónsson <sweskman@gmail.com>
Tue, 23 Aug 2022 11:23:39 +0000 (11:23 +0000)
committerGitHub <noreply@github.com>
Tue, 23 Aug 2022 11:23:39 +0000 (12:23 +0100)
Lib/test/test_coroutines.py
Misc/NEWS.d/next/Core and Builtins/2022-07-24-19-23-23.gh-issue-93592.zdgp6o.rst [new file with mode: 0644]
Objects/genobject.c

index a6a199e323c5f9f99f0e0c4b066371afcbbaa9bb..a414a6f505c97eb9c7d59cf18a86cbd0cf22659e 100644 (file)
@@ -4,6 +4,7 @@ import inspect
 import pickle
 import sys
 import types
+import traceback
 import unittest
 import warnings
 from test import support
@@ -2119,6 +2120,29 @@ class CoroutineTest(unittest.TestCase):
             return 'end'
         self.assertEqual(run_async(run_gen()), ([], 'end'))
 
+    def test_stack_in_coroutine_throw(self):
+        # Regression test for https://github.com/python/cpython/issues/93592
+        async def a():
+            return await b()
+
+        async def b():
+            return await c()
+
+        @types.coroutine
+        def c():
+            try:
+                # traceback.print_stack()
+                yield len(traceback.extract_stack())
+            except ZeroDivisionError:
+                # traceback.print_stack()
+                yield len(traceback.extract_stack())
+
+        coro = a()
+        len_send = coro.send(None)
+        len_throw = coro.throw(ZeroDivisionError)
+        # before fixing, visible stack from throw would be shorter than from send.
+        self.assertEqual(len_send, len_throw)
+
 
 class CoroAsyncIOCompatTest(unittest.TestCase):
 
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-07-24-19-23-23.gh-issue-93592.zdgp6o.rst b/Misc/NEWS.d/next/Core and Builtins/2022-07-24-19-23-23.gh-issue-93592.zdgp6o.rst
new file mode 100644 (file)
index 0000000..89267ed
--- /dev/null
@@ -0,0 +1,2 @@
+``coroutine.throw()`` now properly initializes the ``frame.f_back`` when resuming a stack of coroutines.\r
+This allows e.g. ``traceback.print_stack()`` to work correctly when an exception (such as ``CancelledError``) is thrown into a coroutine.
index 123c17aae7e7c0900767955ae6f2edfb16cb361c..4f6fefdd8bfd899d2415fac7f393fedb1a84357d 100644 (file)
@@ -439,20 +439,25 @@ _gen_throw(PyGenObject *gen, int close_on_genexit,
             /* `yf` is a generator or a coroutine. */
             PyThreadState *tstate = _PyThreadState_GET();
             PyFrameObject *f = tstate->frame;
+            PyFrameObject *gf = gen->gi_frame;
 
             /* Since we are fast-tracking things by skipping the eval loop,
                we need to update the current frame so the stack trace
                will be reported correctly to the user. */
             /* XXX We should probably be updating the current frame
                somewhere in ceval.c. */
-            tstate->frame = gen->gi_frame;
+            assert(gf->f_back == NULL);
+            Py_XINCREF(f);
+            gf->f_back = f;
+            tstate->frame = gf;
             /* Close the generator that we are currently iterating with
                'yield from' or awaiting on with 'await'. */
-            PyFrameState state = gen->gi_frame->f_state;
-            gen->gi_frame->f_state = FRAME_EXECUTING;
+            PyFrameState state = gf->f_state;
+            gf->f_state = FRAME_EXECUTING;
             ret = _gen_throw((PyGenObject *)yf, close_on_genexit,
                              typ, val, tb);
-            gen->gi_frame->f_state = state;
+            gf->f_state = state;
+            Py_CLEAR(gf->f_back);
             tstate->frame = f;
         } else {
             /* `yf` is an iterator or a coroutine-like object. */