Skip to content

gh-76991: Allow accessing the ags_gen and agt_gen attrs of async generators #11166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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
9 changes: 9 additions & 0 deletions Lib/test/test_asyncgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,15 @@ async def gen():
self.assertTrue(inspect.isawaitable(aclose))
aclose.close()

def test_async_gen_ags_gen_agt_gen(self):
async def agen():
yield 1
g = agen()
asend = g.asend(1)
self.assertIsInstance(asend.ags_gen, types.AsyncGeneratorType)
athrow = g.athrow()
self.assertIsInstance(athrow.agt_gen, types.AsyncGeneratorType)


class AsyncGenAsyncioTest(unittest.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Allow accessing the ``ags_gen`` and ``agt_gen`` attributes of asynchronous
generators.
17 changes: 15 additions & 2 deletions Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#define _PY_INTERPRETER

#include "Python.h"
#include "structmember.h"
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_ceval.h" // _PyEval_EvalFrame()
#include "pycore_frame.h" // _PyInterpreterFrame
Expand Down Expand Up @@ -1848,6 +1849,12 @@ static PyMethodDef async_gen_asend_methods[] = {
};


static PyMemberDef async_gen_asend_memberlist[] = {
{"ags_gen", T_OBJECT, offsetof(PyAsyncGenASend, ags_gen), READONLY},
{NULL} /* Sentinel */
};


static PyAsyncMethods async_gen_asend_as_async = {
PyObject_SelfIter, /* am_await */
0, /* am_aiter */
Expand Down Expand Up @@ -1886,7 +1893,7 @@ PyTypeObject _PyAsyncGenASend_Type = {
PyObject_SelfIter, /* tp_iter */
(iternextfunc)async_gen_asend_iternext, /* tp_iternext */
async_gen_asend_methods, /* tp_methods */
0, /* tp_members */
async_gen_asend_memberlist, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
Expand Down Expand Up @@ -2279,6 +2286,12 @@ static PyMethodDef async_gen_athrow_methods[] = {
};


static PyMemberDef async_gen_athrow_memberlist[] = {
{"agt_gen", T_OBJECT, offsetof(PyAsyncGenAThrow, agt_gen), READONLY},
{NULL} /* Sentinel */
};


static PyAsyncMethods async_gen_athrow_as_async = {
PyObject_SelfIter, /* am_await */
0, /* am_aiter */
Expand Down Expand Up @@ -2317,7 +2330,7 @@ PyTypeObject _PyAsyncGenAThrow_Type = {
PyObject_SelfIter, /* tp_iter */
(iternextfunc)async_gen_athrow_iternext, /* tp_iternext */
async_gen_athrow_methods, /* tp_methods */
0, /* tp_members */
async_gen_athrow_memberlist, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
Expand Down