[Python-checkins] bpo-40170: Always define PyIter_Check() as a function (GH-24548)

vstinner webhook-mailer at python.org
Tue Feb 16 10:06:03 EST 2021


https://github.com/python/cpython/commit/cc54001c2eb3b14320c1667b22602d69c90d5865
commit: cc54001c2eb3b14320c1667b22602d69c90d5865
branch: master
author: Erlend Egeberg Aasland <erlend.aasland at innova.no>
committer: vstinner <vstinner at python.org>
date: 2021-02-16T16:05:58+01:00
summary:

bpo-40170: Always define PyIter_Check() as a function (GH-24548)

files:
A Misc/NEWS.d/next/Core and Builtins/2021-02-15-15-06-43.bpo-40170.ZYeSii.rst
M Doc/c-api/iter.rst
M Include/abstract.h
M Include/cpython/abstract.h
M Objects/abstract.c

diff --git a/Doc/c-api/iter.rst b/Doc/c-api/iter.rst
index 74fb5578abd6e..5706777c41db4 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 true if the object *o* supports the iterator protocol.  This
-   function always succeeds.
+   Return non-zero if the object *o* supports the iterator protocol, and ``0``
+   otherwise.  This function always succeeds.
 
 
 .. c:function:: PyObject* PyIter_Next(PyObject *o)
diff --git a/Include/abstract.h b/Include/abstract.h
index 0bd1ca936846f..a47c944060d3d 100644
--- a/Include/abstract.h
+++ b/Include/abstract.h
@@ -324,7 +324,7 @@ PyAPI_FUNC(PyObject *) PyObject_Format(PyObject *obj,
    returns itself. */
 PyAPI_FUNC(PyObject *) PyObject_GetIter(PyObject *);
 
-/* Returns 1 if the object 'obj' provides iterator protocols, and 0 otherwise.
+/* Returns non-zero if the object 'obj' provides iterator protocols, and 0 otherwise.
 
    This function always succeeds. */
 PyAPI_FUNC(int) PyIter_Check(PyObject *);
diff --git a/Include/cpython/abstract.h b/Include/cpython/abstract.h
index 7a4219c8b338b..db5055d201107 100644
--- a/Include/cpython/abstract.h
+++ b/Include/cpython/abstract.h
@@ -325,12 +325,6 @@ PyAPI_FUNC(int) PyBuffer_FillInfo(Py_buffer *view, PyObject *o, void *buf,
 /* Releases a Py_buffer obtained from getbuffer ParseTuple's "s*". */
 PyAPI_FUNC(void) PyBuffer_Release(Py_buffer *view);
 
-/* ==== Iterators ================================================ */
-
-#define PyIter_Check(obj) \
-    (Py_TYPE(obj)->tp_iternext != NULL && \
-     Py_TYPE(obj)->tp_iternext != &_PyObject_NextNotImplemented)
-
 /* === Sequence protocol ================================================ */
 
 /* Assume tp_as_sequence and sq_item exist and that 'i' does not
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-02-15-15-06-43.bpo-40170.ZYeSii.rst b/Misc/NEWS.d/next/Core and Builtins/2021-02-15-15-06-43.bpo-40170.ZYeSii.rst
new file mode 100644
index 0000000000000..df6f3dcfc14b6
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-02-15-15-06-43.bpo-40170.ZYeSii.rst	
@@ -0,0 +1,3 @@
+:c:func:`PyIter_Check` is now always declared as a function, in order to hide implementation
+details. The macro accessed :c:member:`PyTypeObject.tp_iternext` directly.
+Patch by Erlend E. Aasland.
diff --git a/Objects/abstract.c b/Objects/abstract.c
index 74a73ee469866..c93309b352774 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -2732,12 +2732,12 @@ PyObject_GetIter(PyObject *o)
     }
 }
 
-#undef PyIter_Check
-
-int PyIter_Check(PyObject *obj)
+int
+PyIter_Check(PyObject *obj)
 {
-    return Py_TYPE(obj)->tp_iternext != NULL &&
-           Py_TYPE(obj)->tp_iternext != &_PyObject_NextNotImplemented;
+    PyTypeObject *tp = Py_TYPE(obj);
+    return (tp->tp_iternext != NULL &&
+            tp->tp_iternext != &_PyObject_NextNotImplemented);
 }
 
 /* Return next item.



More information about the Python-checkins mailing list