Skip to content

Commit 3deb42d

Browse files
committed
Correctly delegate to synchronous subgenerators.
1 parent 1f26722 commit 3deb42d

1 file changed

Lines changed: 45 additions & 2 deletions

File tree

Objects/genobject.c

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2672,6 +2672,49 @@ async_gen_yield_from_iternext(PyObject *op)
26722672
return wrapped;
26732673
}
26742674

2675+
static PySendResult
2676+
async_gen_yield_from_send(PyObject *op, PyObject *arg, PyObject **presult)
2677+
{
2678+
assert(op != NULL);
2679+
_PyAsyncGenYieldFrom *self = _PyAsyncGenYieldFrom_CAST(op);
2680+
return PyIter_Send(self->agyf_iterator, arg, presult);
2681+
}
2682+
2683+
static PyAsyncMethods async_gen_yield_from_as_async = {
2684+
.am_send = async_gen_yield_from_send
2685+
};
2686+
2687+
#define AGYF_PROXY_METHOD(name, result_value) \
2688+
static PyObject * \
2689+
async_gen_yield_from_ ## name ## _method(PyObject *op, PyObject *args, PyObject *keywords) \
2690+
{ \
2691+
assert(op != NULL); \
2692+
_PyAsyncGenYieldFrom *self = _PyAsyncGenYieldFrom_CAST(op); \
2693+
PyObject *method = PyObject_GetAttr(self->agyf_iterator, &_Py_ID(name)); \
2694+
if (method == NULL) { \
2695+
return NULL; \
2696+
} \
2697+
PyObject *result = PyObject_Call(method, args, keywords); \
2698+
Py_DECREF(method); \
2699+
if (result == NULL) { \
2700+
return NULL; \
2701+
} \
2702+
return result_value; \
2703+
}
2704+
2705+
2706+
2707+
AGYF_PROXY_METHOD(send, _PyAsyncGenValueWrapperNew(_PyThreadState_GET(), result))
2708+
AGYF_PROXY_METHOD(throw, result)
2709+
AGYF_PROXY_METHOD(close, result)
2710+
2711+
static PyMethodDef async_gen_yield_from_methods[] = {
2712+
{"send", (PyCFunction)async_gen_yield_from_send_method, METH_VARARGS | METH_KEYWORDS},
2713+
{"throw", (PyCFunction)async_gen_yield_from_throw_method, METH_VARARGS | METH_KEYWORDS},
2714+
{"close", (PyCFunction)async_gen_yield_from_close_method, METH_VARARGS | METH_KEYWORDS},
2715+
{0}
2716+
};
2717+
26752718
PyTypeObject _PyAsyncGenYieldFrom_Type = {
26762719
PyVarObject_HEAD_INIT(&PyType_Type, 0)
26772720
"async_generator_yield_from", /* tp_name */
@@ -2682,7 +2725,7 @@ PyTypeObject _PyAsyncGenYieldFrom_Type = {
26822725
0, /* tp_vectorcall_offset */
26832726
0, /* tp_getattr */
26842727
0, /* tp_setattr */
2685-
0, /* tp_as_async */
2728+
&async_gen_yield_from_as_async, /* tp_as_async */
26862729
0, /* tp_repr */
26872730
0, /* tp_as_number */
26882731
0, /* tp_as_sequence */
@@ -2701,7 +2744,7 @@ PyTypeObject _PyAsyncGenYieldFrom_Type = {
27012744
0, /* tp_weaklistoffset */
27022745
PyObject_SelfIter, /* tp_iter */
27032746
async_gen_yield_from_iternext, /* tp_iternext */
2704-
0, /* tp_methods */
2747+
async_gen_yield_from_methods, /* tp_methods */
27052748
0, /* tp_members */
27062749
0, /* tp_getset */
27072750
0, /* tp_base */

0 commit comments

Comments
 (0)