with ``__name__`` and ``__qualname__`` set to *name* and *qualname*.
A reference to *frame* is stolen by this function. The *frame* argument
must not be ``NULL``.
-
-.. c:function:: PySendResult PyGen_Send(PyGenObject *gen, PyObject *arg, PyObject **presult)
-
- Sends the *arg* value into the generator *gen*. Coroutine objects
- are also allowed to be as the *gen* argument but they need to be
- explicitly casted to PyGenObject*. Returns:
-
- - ``PYGEN_RETURN`` if generator returns. Return value is returned via *presult*.
- - ``PYGEN_NEXT`` if generator yields. Yielded value is returned via *presult*.
- - ``PYGEN_ERROR`` if generator has raised and exception. *presult* is set to ``NULL``.
PyGen_NewWithQualName:PyObject*:name:0:
PyGen_NewWithQualName:PyObject*:qualname:0:
-PyGen_Send:int:::
-PyGen_Send:PyGenObject*:gen:0:
-PyGen_Send:PyObject*:arg:0:
-PyGen_Send:PyObject**:presult:+1:
-
PyCoro_CheckExact:int:::
PyCoro_CheckExact:PyObject*:ob:0:
search function.
(Contributed by Hai Shi in :issue:`41842`.)
-* The :c:func:`PyIter_Send` and :c:func:`PyGen_Send` functions were added to allow
+* The :c:func:`PyIter_Send` function was added to allow
sending value into iterator without raising ``StopIteration`` exception.
(Contributed by Vladimir Matveev in :issue:`41756`.)
PyObject *_PyGen_yf(PyGenObject *);
PyAPI_FUNC(void) _PyGen_Finalize(PyObject *self);
-/* Sends the value into the generator or the coroutine. Returns:
- - PYGEN_RETURN (0) if generator has returned.
- 'result' parameter is filled with return value
- - PYGEN_ERROR (-1) if exception was raised.
- 'result' parameter is NULL
- - PYGEN_NEXT (1) if generator has yielded.
- 'result' parameter is filled with yielded value. */
-PyAPI_FUNC(PySendResult) PyGen_Send(PyGenObject *, PyObject *, PyObject **);
-
#ifndef Py_LIMITED_API
typedef struct {
_PyGenObject_HEAD(cr)
..
-.. bpo: 41756
-.. date: 2020-09-12-12-55-45
-.. nonce: 1h0tbV
-.. section: Core and Builtins
-
-Add PyGen_Send function to allow sending value into generator/coroutine
-without raising StopIteration exception to signal return
-
-..
-
.. bpo: 1635741
.. date: 2020-09-08-21-58-47
.. nonce: vdjSLH
return result;
}
-PySendResult
-PyIter_Send(PyObject *iter, PyObject *arg, PyObject **result)
-{
- _Py_IDENTIFIER(send);
- assert(result != NULL);
-
- if (PyGen_CheckExact(iter) || PyCoro_CheckExact(iter)) {
- return PyGen_Send((PyGenObject *)iter, arg, result);
- }
-
- if (arg == Py_None && PyIter_Check(iter)) {
- *result = Py_TYPE(iter)->tp_iternext(iter);
- }
- else {
- *result = _PyObject_CallMethodIdOneArg(iter, &PyId_send, arg);
- }
- if (*result != NULL) {
- return PYGEN_NEXT;
- }
- if (_PyGen_FetchStopIterationValue(result) == 0) {
- return PYGEN_RETURN;
- }
- return PYGEN_ERROR;
-}
-
/*
* Flatten a sequence of bytes() objects into a C array of
* NULL terminated string pointers with a NULL char* terminating the array.
}
PySendResult
-PyGen_Send(PyGenObject *gen, PyObject *arg, PyObject **result)
+PyIter_Send(PyObject *iter, PyObject *arg, PyObject **result)
{
- assert(PyGen_CheckExact(gen) || PyCoro_CheckExact(gen));
- assert(result != NULL);
+ _Py_IDENTIFIER(send);
assert(arg != NULL);
+ assert(result != NULL);
+
+ if (PyGen_CheckExact(iter) || PyCoro_CheckExact(iter)) {
+ return gen_send_ex2((PyGenObject *)iter, arg, result, 0, 0);
+ }
- return gen_send_ex2(gen, arg, result, 0, 0);
+ if (arg == Py_None && PyIter_Check(iter)) {
+ *result = Py_TYPE(iter)->tp_iternext(iter);
+ }
+ else {
+ *result = _PyObject_CallMethodIdOneArg(iter, &PyId_send, arg);
+ }
+ if (*result != NULL) {
+ return PYGEN_NEXT;
+ }
+ if (_PyGen_FetchStopIterationValue(result) == 0) {
+ return PYGEN_RETURN;
+ }
+ return PYGEN_ERROR;
}
static PyObject *