]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-111058: Change coro.cr_frame/gen.gi_frame to be None for a closed coroutine/genera...
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>
Fri, 1 Dec 2023 12:57:31 +0000 (12:57 +0000)
committerGitHub <noreply@github.com>
Fri, 1 Dec 2023 12:57:31 +0000 (12:57 +0000)
Lib/test/test_coroutines.py
Lib/test/test_inspect/test_inspect.py
Misc/NEWS.d/next/Core and Builtins/2023-11-26-21-30-11.gh-issue-111058.q4DqDY.rst [new file with mode: 0644]
Objects/genobject.c

index 47145782c0f04f70f6cef7f78288ab446b631331..25c981d1511bc1e5a762cdc7507dd1c07917a7d5 100644 (file)
@@ -2216,6 +2216,14 @@ class CoroutineTest(unittest.TestCase):
             gen.cr_frame.clear()
         gen.close()
 
+    def test_cr_frame_after_close(self):
+        async def f():
+            pass
+        gen = f()
+        self.assertIsNotNone(gen.cr_frame)
+        gen.close()
+        self.assertIsNone(gen.cr_frame)
+
     def test_stack_in_coroutine_throw(self):
         # Regression test for https://github.com/python/cpython/issues/93592
         async def a():
index becbb0498bbb3fd7c475cac0dd51d74f81713996..e75682f881ab346842743ef5be550934403856ae 100644 (file)
@@ -2384,6 +2384,10 @@ class TestGetGeneratorState(unittest.TestCase):
             self.generator.throw(RuntimeError)
         self.assertEqual(self._generatorstate(), inspect.GEN_CLOSED)
 
+    def test_closed_after_close(self):
+        self.generator.close()
+        self.assertEqual(self._generatorstate(), inspect.GEN_CLOSED)
+
     def test_running(self):
         # As mentioned on issue #10220, checking for the RUNNING state only
         # makes sense inside the generator itself.
@@ -2493,6 +2497,10 @@ class TestGetCoroutineState(unittest.TestCase):
             self.coroutine.throw(RuntimeError)
         self.assertEqual(self._coroutinestate(), inspect.CORO_CLOSED)
 
+    def test_closed_after_close(self):
+        self.coroutine.close()
+        self.assertEqual(self._coroutinestate(), inspect.CORO_CLOSED)
+
     def test_easy_debugging(self):
         # repr() and str() of a coroutine state should contain the state name
         names = 'CORO_CREATED CORO_RUNNING CORO_SUSPENDED CORO_CLOSED'.split()
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-11-26-21-30-11.gh-issue-111058.q4DqDY.rst b/Misc/NEWS.d/next/Core and Builtins/2023-11-26-21-30-11.gh-issue-111058.q4DqDY.rst
new file mode 100644 (file)
index 0000000..de5661f
--- /dev/null
@@ -0,0 +1,3 @@
+Change coro.cr_frame/gen.gi_frame to return ``None`` after the coroutine/generator has been closed.
+This fixes a bug where :func:`~inspect.getcoroutinestate` and :func:`~inspect.getgeneratorstate`
+return the wrong state for a closed coroutine/generator.
index f98aa357cd2ce1adc6ac8b580865d472c81a6067..9614713883741c390208733dc73d68a2dd936628 100644 (file)
@@ -732,7 +732,7 @@ _gen_getframe(PyGenObject *gen, const char *const name)
     if (PySys_Audit("object.__getattr__", "Os", gen, name) < 0) {
         return NULL;
     }
-    if (gen->gi_frame_state == FRAME_CLEARED) {
+    if (FRAME_STATE_FINISHED(gen->gi_frame_state)) {
         Py_RETURN_NONE;
     }
     return _Py_XNewRef((PyObject *)_PyFrame_GetFrameObject((_PyInterpreterFrame *)gen->gi_iframe));