Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2879,6 +2879,17 @@ def _coroutinestate(self):
def test_created(self):
self.assertEqual(self._coroutinestate(), inspect.CORO_CREATED)

def test_generator_based_coroutine_introspection(self):
from inspect import getcoroutinestate
from types import coroutine

@coroutine
def gen_coro():
yield

# Must not raise AttributeError
getcoroutinestate(gen_coro())
Comment on lines +2890 to +2891
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an assertion for what the proper state should be.


def test_suspended(self):
self.coroutine.send(None)
self.assertEqual(self._coroutinestate(), inspect.CORO_SUSPENDED)
Expand Down Expand Up @@ -2927,6 +2938,7 @@ async def func(a=None):
{'a': None, 'gencoro': gencoro, 'b': 'spam'})



Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated change.

@support.requires_working_socket()
class TestGetAsyncGenState(unittest.IsolatedAsyncioTestCase):

Expand Down
35 changes: 35 additions & 0 deletions Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,36 @@ gen_getcode(PyObject *self, void *Py_UNUSED(ignored))
return _gen_getcode(gen, "gi_code");
}

static PyObject *
gen_get_cr_frame(PyObject *self, void *closure)
{
return gen_getframe((PyObject *)self, closure);
}

static PyObject *
gen_get_cr_code(PyObject *self, void *closure)
{
return gen_getcode((PyObject *)self, closure);
}

static PyObject *
gen_get_cr_running(PyObject *self, void *closure)
{
return gen_getrunning((PyObject *)self, closure);
}

static PyObject *
gen_get_cr_suspended(PyObject *self, void *closure)
{
return gen_getsuspended((PyObject *)self, closure);
}

static PyObject *
gen_get_cr_origin(PyObject *self, void *closure)
{
Py_RETURN_NONE;
}

static PyGetSetDef gen_getsetlist[] = {
{"__name__", gen_get_name, gen_set_name,
PyDoc_STR("name of the generator")},
Expand All @@ -946,6 +976,11 @@ static PyGetSetDef gen_getsetlist[] = {
{"gi_frame", gen_getframe, NULL, NULL},
{"gi_suspended", gen_getsuspended, NULL, NULL},
{"gi_code", gen_getcode, NULL, NULL},
{"cr_running", gen_get_cr_running, NULL, NULL},
{"cr_frame", gen_get_cr_frame, NULL, NULL},
{"cr_code", gen_get_cr_code, NULL, NULL},
{"cr_suspended", gen_get_cr_suspended, NULL, NULL},
{"cr_origin", gen_get_cr_origin, NULL, NULL},
Comment on lines +979 to +983
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong. Coroutine objects are not generator objects; they should not expose the coroutine interface solely for use in _GeneratorWrapper. _GeneratorWrapper should instead replicate these attributes.

{NULL} /* Sentinel */
};

Expand Down
Loading