From eaa3a67a3eaf2bf5b812b242bd45889de99687fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Sun, 24 Jul 2022 14:17:41 +0000 Subject: [PATCH 1/2] Fix issue 93592 --- Lib/test/test_coroutines.py | 24 ++++++++++++++++++++++++ Objects/genobject.c | 13 +++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_coroutines.py b/Lib/test/test_coroutines.py index a6a199e323c5f9f..a414a6f505c97eb 100644 --- a/Lib/test/test_coroutines.py +++ b/Lib/test/test_coroutines.py @@ -4,6 +4,7 @@ import pickle import sys import types +import traceback import unittest import warnings from test import support @@ -2119,6 +2120,29 @@ async def run_gen(): 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/Objects/genobject.c b/Objects/genobject.c index 123c17aae7e7c09..4f6fefdd8bfd899 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -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. */ From 03ac6ef72848386c9c3b7df6502144c5adb6f923 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 24 Jul 2022 19:23:24 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2022-07-24-19-23-23.gh-issue-93592.zdgp6o.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-07-24-19-23-23.gh-issue-93592.zdgp6o.rst 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 index 000000000000000..89267ed37da324f --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-07-24-19-23-23.gh-issue-93592.zdgp6o.rst @@ -0,0 +1,2 @@ +``coroutine.throw()`` now properly initializes the ``frame.f_back`` when resuming a stack of coroutines. +This allows e.g. ``traceback.print_stack()`` to work correctly when an exception (such as ``CancelledError``) is thrown into a coroutine.