From 9f60af2cd2a2d472b566766b12b513a02e4fb62e Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 15 Feb 2021 15:04:57 +0100 Subject: [PATCH 1/6] bpo-40170: Remove macro variant of PyIter_Check --- Doc/c-api/iter.rst | 4 ++-- Include/cpython/abstract.h | 4 +--- Objects/abstract.c | 10 +++++----- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/Doc/c-api/iter.rst b/Doc/c-api/iter.rst index 74fb5578abd6e1..5706777c41db48 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/cpython/abstract.h b/Include/cpython/abstract.h index 7a4219c8b338b4..3e9e6039bbc598 100644 --- a/Include/cpython/abstract.h +++ b/Include/cpython/abstract.h @@ -327,9 +327,7 @@ 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) +PyAPI_FUNC(int) PyIter_Check(PyObject *); /* === Sequence protocol ================================================ */ diff --git a/Objects/abstract.c b/Objects/abstract.c index 74a73ee469866d..1d0ba99c759bfb 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. From 7c0339470809da5115980b7d66da4f429bc51e53 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 15 Feb 2021 15:06:53 +0100 Subject: [PATCH 2/6] Add NEWS --- .../Core and Builtins/2021-02-15-15-06-43.bpo-40170.ZYeSii.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2021-02-15-15-06-43.bpo-40170.ZYeSii.rst 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 00000000000000..18fff42a69c63b --- /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` macro is now a function, in order to hide implementation +details. The macro accessed :c:member:`PyTypeObject.tp_iternext` directly. +Patch by Erlend E. Aasland. From 780d6619b571be467618ea20190aae9ed4f50253 Mon Sep 17 00:00:00 2001 From: Erlend Egeberg Aasland Date: Tue, 16 Feb 2021 13:10:49 +0100 Subject: [PATCH 3/6] Add parens Co-authored-by: Victor Stinner --- Objects/abstract.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Objects/abstract.c b/Objects/abstract.c index 1d0ba99c759bfb..c93309b352774c 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -2736,8 +2736,8 @@ int PyIter_Check(PyObject *obj) { PyTypeObject *tp = Py_TYPE(obj); - return tp->tp_iternext != NULL && - tp->tp_iternext != &_PyObject_NextNotImplemented; + return (tp->tp_iternext != NULL && + tp->tp_iternext != &_PyObject_NextNotImplemented); } /* Return next item. From 0452b697fbb44fe85cd2b738c5560b9010dd6c53 Mon Sep 17 00:00:00 2001 From: Erlend Egeberg Aasland Date: Tue, 16 Feb 2021 13:14:47 +0100 Subject: [PATCH 4/6] Improve NEWS entry Co-authored-by: Victor Stinner --- .../Core and Builtins/2021-02-15-15-06-43.bpo-40170.ZYeSii.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 18fff42a69c63b..df6f3dcfc14b6f 100644 --- 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 @@ -1,3 +1,3 @@ -:c:func:`PyIter_Check` macro is now a function, in order to hide implementation +: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. From 4fb4918bb27362262bef9a897709c9ca48cf98c9 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 16 Feb 2021 13:16:06 +0100 Subject: [PATCH 5/6] Address review: Update comment --- Include/abstract.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Include/abstract.h b/Include/abstract.h index 0bd1ca936846fe..a47c944060d3d0 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 *); From fd85ca676d213dfbcfdc667f33af91e3a6c21486 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 16 Feb 2021 13:17:33 +0100 Subject: [PATCH 6/6] Address review: Remove duplicate expose --- Include/cpython/abstract.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Include/cpython/abstract.h b/Include/cpython/abstract.h index 3e9e6039bbc598..db5055d201107e 100644 --- a/Include/cpython/abstract.h +++ b/Include/cpython/abstract.h @@ -325,10 +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 ================================================ */ - -PyAPI_FUNC(int) PyIter_Check(PyObject *); - /* === Sequence protocol ================================================ */ /* Assume tp_as_sequence and sq_item exist and that 'i' does not