From 407b9ba71fa323516587d688f994fd9a8c6df15e Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sat, 4 Apr 2020 23:49:26 +0200 Subject: [PATCH] bpo-40170: Remove PyIndex_Check() macro Always declare PyIndex_Check() as an opaque function to hide implementation details: remove PyIndex_Check() macro. The macro accessed directly the PyTypeObject.tp_as_number member. --- Include/cpython/abstract.h | 6 ------ .../next/C API/2020-04-04-23-51-59.bpo-40170.uXQ701.rst | 3 +++ Objects/abstract.c | 6 +++--- 3 files changed, 6 insertions(+), 9 deletions(-) create mode 100644 Misc/NEWS.d/next/C API/2020-04-04-23-51-59.bpo-40170.uXQ701.rst diff --git a/Include/cpython/abstract.h b/Include/cpython/abstract.h index 9d23c8c11be577..38e0689a1ec0b5 100644 --- a/Include/cpython/abstract.h +++ b/Include/cpython/abstract.h @@ -337,12 +337,6 @@ PyAPI_FUNC(void) PyBuffer_Release(Py_buffer *view); (Py_TYPE(obj)->tp_iternext != NULL && \ Py_TYPE(obj)->tp_iternext != &_PyObject_NextNotImplemented) -/* === Number Protocol ================================================== */ - -#define PyIndex_Check(obj) \ - (Py_TYPE(obj)->tp_as_number != NULL && \ - Py_TYPE(obj)->tp_as_number->nb_index != NULL) - /* === Sequence protocol ================================================ */ /* Assume tp_as_sequence and sq_item exist and that 'i' does not diff --git a/Misc/NEWS.d/next/C API/2020-04-04-23-51-59.bpo-40170.uXQ701.rst b/Misc/NEWS.d/next/C API/2020-04-04-23-51-59.bpo-40170.uXQ701.rst new file mode 100644 index 00000000000000..22bdc74904f41c --- /dev/null +++ b/Misc/NEWS.d/next/C API/2020-04-04-23-51-59.bpo-40170.uXQ701.rst @@ -0,0 +1,3 @@ +Always declare :c:func:`PyIndex_Check` as an opaque function to hide +implementation details: remove ``PyIndex_Check()`` macro. The macro accessed +directly the :c:member:`PyTypeObject.tp_as_number` member. diff --git a/Objects/abstract.c b/Objects/abstract.c index 02e4ad70718cd9..ca692cd6d3f016 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -1291,15 +1291,15 @@ PyNumber_Absolute(PyObject *o) return type_error("bad operand type for abs(): '%.200s'", o); } -#undef PyIndex_Check int PyIndex_Check(PyObject *obj) { - return Py_TYPE(obj)->tp_as_number != NULL && - Py_TYPE(obj)->tp_as_number->nb_index != NULL; + PyNumberMethods *tp_as_number = Py_TYPE(obj)->tp_as_number; + return (tp_as_number != NULL && tp_as_number->nb_index != NULL); } + /* Return a Python int from the object item. Raise TypeError if the result is not an int or if the object cannot be interpreted as an index.