Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bfb576e

Browse files
authored
gh-111058: Change coro.cr_frame/gen.gi_frame to be None for a closed coroutine/generator. (#112428)
1 parent a65a3d4 commit bfb576e

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

Lib/test/test_coroutines.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2216,6 +2216,14 @@ async def f():
22162216
gen.cr_frame.clear()
22172217
gen.close()
22182218

2219+
def test_cr_frame_after_close(self):
2220+
async def f():
2221+
pass
2222+
gen = f()
2223+
self.assertIsNotNone(gen.cr_frame)
2224+
gen.close()
2225+
self.assertIsNone(gen.cr_frame)
2226+
22192227
def test_stack_in_coroutine_throw(self):
22202228
# Regression test for https://github.com/python/cpython/issues/93592
22212229
async def a():

Lib/test/test_inspect/test_inspect.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2384,6 +2384,10 @@ def test_closed_after_immediate_exception(self):
23842384
self.generator.throw(RuntimeError)
23852385
self.assertEqual(self._generatorstate(), inspect.GEN_CLOSED)
23862386

2387+
def test_closed_after_close(self):
2388+
self.generator.close()
2389+
self.assertEqual(self._generatorstate(), inspect.GEN_CLOSED)
2390+
23872391
def test_running(self):
23882392
# As mentioned on issue #10220, checking for the RUNNING state only
23892393
# makes sense inside the generator itself.
@@ -2493,6 +2497,10 @@ def test_closed_after_immediate_exception(self):
24932497
self.coroutine.throw(RuntimeError)
24942498
self.assertEqual(self._coroutinestate(), inspect.CORO_CLOSED)
24952499

2500+
def test_closed_after_close(self):
2501+
self.coroutine.close()
2502+
self.assertEqual(self._coroutinestate(), inspect.CORO_CLOSED)
2503+
24962504
def test_easy_debugging(self):
24972505
# repr() and str() of a coroutine state should contain the state name
24982506
names = 'CORO_CREATED CORO_RUNNING CORO_SUSPENDED CORO_CLOSED'.split()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Change coro.cr_frame/gen.gi_frame to return ``None`` after the coroutine/generator has been closed.
2+
This fixes a bug where :func:`~inspect.getcoroutinestate` and :func:`~inspect.getgeneratorstate`
3+
return the wrong state for a closed coroutine/generator.

Objects/genobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ _gen_getframe(PyGenObject *gen, const char *const name)
732732
if (PySys_Audit("object.__getattr__", "Os", gen, name) < 0) {
733733
return NULL;
734734
}
735-
if (gen->gi_frame_state == FRAME_CLEARED) {
735+
if (FRAME_STATE_FINISHED(gen->gi_frame_state)) {
736736
Py_RETURN_NONE;
737737
}
738738
return _Py_XNewRef((PyObject *)_PyFrame_GetFrameObject((_PyInterpreterFrame *)gen->gi_iframe));

0 commit comments

Comments
 (0)