diff options
author | Brett Cannon <brett@python.org> | 2021-11-20 00:40:34 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-20 00:40:34 (GMT) |
commit | be36e0634060c7d5dee8e8876fb888bbb53d992a (patch) | |
tree | dcf28e3105e12b272cdca00de2b260dec6805183 /Doc/c-api | |
parent | 4c616911b69ce07fb35da1721506bfaba0998c30 (diff) | |
download | cpython-be36e0634060c7d5dee8e8876fb888bbb53d992a.zip cpython-be36e0634060c7d5dee8e8876fb888bbb53d992a.tar.gz cpython-be36e0634060c7d5dee8e8876fb888bbb53d992a.tar.bz2 |
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__`.
Diffstat (limited to 'Doc/c-api')
-rw-r--r-- | Doc/c-api/iter.rst | 13 | ||||
-rw-r--r-- | Doc/c-api/typeobj.rst | 14 |
2 files changed, 14 insertions, 13 deletions
diff --git a/Doc/c-api/iter.rst b/Doc/c-api/iter.rst index f7106f4..3e388bb 100644 --- a/Doc/c-api/iter.rst +++ b/Doc/c-api/iter.rst @@ -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:: diff --git a/Doc/c-api/typeobj.rst b/Doc/c-api/typeobj.rst index 5412879..cd8723e 100644 --- a/Doc/c-api/typeobj.rst +++ b/Doc/c-api/typeobj.rst @@ -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`. |