From: Andrew Svetlov Date: Wed, 8 Dec 2021 16:05:00 +0000 (+0200) Subject: bpo-45813: Drop redundant assertion from frame.clear() (GH-29990) X-Git-Tag: v3.11.0a3~6 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d4363d214097b3fca8b7910c2e0e91c8b0873fb2;p=thirdparty%2FPython%2Fcpython.git bpo-45813: Drop redundant assertion from frame.clear() (GH-29990) * bpo-45813: Drop redundant assertion from frame.clear() * Move assertion to frame_dealloc() --- diff --git a/Lib/test/test_coroutines.py b/Lib/test/test_coroutines.py index fc8b8bc9541e..3081853303b3 100644 --- a/Lib/test/test_coroutines.py +++ b/Lib/test/test_coroutines.py @@ -2191,13 +2191,22 @@ class CoroutineTest(unittest.TestCase): return 'end' self.assertEqual(run_async(run_gen()), ([], 'end')) - def test_bpo_45813(self): + def test_bpo_45813_1(self): 'This would crash the interpreter in 3.11a2' async def f(): pass - frame = f().cr_frame + with self.assertWarns(RuntimeWarning): + frame = f().cr_frame frame.clear() + def test_bpo_45813_2(self): + 'This would crash the interpreter in 3.11a2' + async def f(): + pass + gen = f() + with self.assertWarns(RuntimeWarning): + gen.cr_frame.clear() + class CoroAsyncIOCompatTest(unittest.TestCase): diff --git a/Objects/frameobject.c b/Objects/frameobject.c index 926a32a5100b..2197e07bc061 100644 --- a/Objects/frameobject.c +++ b/Objects/frameobject.c @@ -613,6 +613,10 @@ static PyGetSetDef frame_getsetlist[] = { static void frame_dealloc(PyFrameObject *f) { + /* It is the responsibility of the owning generator/coroutine + * to have cleared the generator pointer */ + assert(f->f_frame->generator == NULL); + if (_PyObject_GC_IS_TRACKED(f)) { _PyObject_GC_UNTRACK(f); } @@ -686,7 +690,6 @@ frame_clear(PyFrameObject *f, PyObject *Py_UNUSED(ignored)) } if (f->f_frame->generator) { _PyGen_Finalize(f->f_frame->generator); - assert(f->f_frame->generator == NULL); } (void)frame_tp_clear(f); Py_RETURN_NONE;