bpo-45250: fix docs regarding __iter__ and iterators being inconsistently required by CPython (GH-29170)

It is now considered a historical accident that e.g. `for` loops and the `iter()` built-in function do not require the iterators they work with to define `__iter__`, only `__next__`.
This commit is contained in:
Brett Cannon 2021-11-19 16:40:34 -08:00 committed by GitHub
parent 4c616911b6
commit be36e06340
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 51 additions and 47 deletions

View file

@ -9,8 +9,8 @@ There are two functions specifically for working with iterators.
.. c:function:: int PyIter_Check(PyObject *o)
Return non-zero if the object *o* supports the iterator protocol, and ``0``
otherwise. This function always succeeds.
Return non-zero if the object *o* can be safely passed to
:c:func:`PyIter_Next`, and ``0`` otherwise. This function always succeeds.
.. c:function:: int PyAIter_Check(PyObject *o)
@ -21,10 +21,11 @@ There are two functions specifically for working with iterators.
.. c:function:: PyObject* PyIter_Next(PyObject *o)
Return the next value from the iteration *o*. The object must be an iterator
(it is up to the caller to check this). If there are no remaining values,
returns ``NULL`` with no exception set. If an error occurs while retrieving
the item, returns ``NULL`` and passes along the exception.
Return the next value from the iterator *o*. The object must be an iterator
according to :c:func:`PyIter_Check` (it is up to the caller to check this).
If there are no remaining values, returns ``NULL`` with no exception set.
If an error occurs while retrieving the item, returns ``NULL`` and passes
along the exception.
To write a loop which iterates over an iterator, the C code should look
something like this::

View file

@ -1521,9 +1521,9 @@ and :c:type:`PyType_Type` effectively act as defaults.)
.. c:member:: getiterfunc PyTypeObject.tp_iter
An optional pointer to a function that returns an iterator for the object. Its
presence normally signals that the instances of this type are iterable (although
sequences may be iterable without this function).
An optional pointer to a function that returns an :term:`iterator` for the
object. Its presence normally signals that the instances of this type are
:term:`iterable` (although sequences may be iterable without this function).
This function has the same signature as :c:func:`PyObject_GetIter`::
@ -1536,8 +1536,8 @@ and :c:type:`PyType_Type` effectively act as defaults.)
.. c:member:: iternextfunc PyTypeObject.tp_iternext
An optional pointer to a function that returns the next item in an iterator.
The signature is::
An optional pointer to a function that returns the next item in an
:term:`iterator`. The signature is::
PyObject *tp_iternext(PyObject *self);
@ -2429,8 +2429,8 @@ Async Object Structures
PyObject *am_await(PyObject *self);
The returned object must be an iterator, i.e. :c:func:`PyIter_Check` must
return ``1`` for it.
The returned object must be an :term:`iterator`, i.e. :c:func:`PyIter_Check`
must return ``1`` for it.
This slot may be set to ``NULL`` if an object is not an :term:`awaitable`.