From 23a570100798843f8b6d45b514455647eacecbbb Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" <erlend.aasland@innova.no> Date: Thu, 11 Feb 2021 09:28:28 +0100 Subject: [PATCH 1/5] convert PyObject_TypeCheck to static inline function --- Doc/c-api/object.rst | 4 ++-- Include/object.h | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Doc/c-api/object.rst b/Doc/c-api/object.rst index a387b4a2df1342..1100af1df2928c 100644 --- a/Doc/c-api/object.rst +++ b/Doc/c-api/object.rst @@ -297,8 +297,8 @@ Object Protocol .. c:function:: int PyObject_TypeCheck(PyObject *o, PyTypeObject *type) - Return true if the object *o* is of type *type* or a subtype of *type*. Both - parameters must be non-``NULL``. + Return non-zero if the object *o* is of type *type* or a subtype of *type*, and + ``0`` otherwise. Both parameters must be non-``NULL``. .. c:function:: Py_ssize_t PyObject_Size(PyObject *o) diff --git a/Include/object.h b/Include/object.h index 8d0039428e73af..6da4c1ad2ca980 100644 --- a/Include/object.h +++ b/Include/object.h @@ -235,8 +235,12 @@ PyAPI_FUNC(void *) PyType_GetModuleState(struct _typeobject *); /* Generic type check */ PyAPI_FUNC(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *); -#define PyObject_TypeCheck(ob, tp) \ - (Py_IS_TYPE(ob, tp) || PyType_IsSubtype(Py_TYPE(ob), (tp))) + +PyAPI_FUNC(int) PyObject_TypeCheck(PyObject *, PyTypeObject *); +static inline int _PyObject_TypeCheck(PyObject *ob, PyTypeObject *tp) { + return Py_IS_TYPE(ob, tp) || PyType_IsSubtype(Py_TYPE(ob), (tp)); +} +#define PyObject_TypeCheck(ob, tp) _PyObject_TypeCheck(_PyObject_CAST(ob), tp) PyAPI_DATA(PyTypeObject) PyType_Type; /* built-in 'type' */ PyAPI_DATA(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */ From f3f224d5ed49152d89559f262793d2be997d039e Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" <erlend.aasland@innova.no> Date: Thu, 11 Feb 2021 11:39:19 +0100 Subject: [PATCH 2/5] Add NEWS --- .../Core and Builtins/2021-02-11-11-37-14.bpo-43181.ydv33S.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2021-02-11-11-37-14.bpo-43181.ydv33S.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-02-11-11-37-14.bpo-43181.ydv33S.rst b/Misc/NEWS.d/next/Core and Builtins/2021-02-11-11-37-14.bpo-43181.ydv33S.rst new file mode 100644 index 00000000000000..0c27609caaefc6 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2021-02-11-11-37-14.bpo-43181.ydv33S.rst @@ -0,0 +1,2 @@ +Convert :cfunc:`PyObject_TypeCheck` to a static inline function. Patch by +Erlend E. Aasland. From 36f742ace4f2cc855f940828490bf6d162790048 Mon Sep 17 00:00:00 2001 From: Erlend Egeberg Aasland <erlend.aasland@innova.no> Date: Mon, 15 Feb 2021 12:25:41 +0100 Subject: [PATCH 3/5] Improve NEWS text Co-authored-by: Victor Stinner <vstinner@python.org> --- .../Core and Builtins/2021-02-11-11-37-14.bpo-43181.ydv33S.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-02-11-11-37-14.bpo-43181.ydv33S.rst b/Misc/NEWS.d/next/Core and Builtins/2021-02-11-11-37-14.bpo-43181.ydv33S.rst index 0c27609caaefc6..1635719c48c1b4 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2021-02-11-11-37-14.bpo-43181.ydv33S.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2021-02-11-11-37-14.bpo-43181.ydv33S.rst @@ -1,2 +1,2 @@ -Convert :cfunc:`PyObject_TypeCheck` to a static inline function. Patch by +Convert :cfunc:`PyObject_TypeCheck` macro to a static inline function. Patch by Erlend E. Aasland. From b19bf6ac25bae4e07c5d208755909478812e5ec4 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" <erlend.aasland@innova.no> Date: Mon, 15 Feb 2021 12:28:35 +0100 Subject: [PATCH 4/5] Address review - Improve naming: Use 'type' iso. 'tp' - Remove PyAPI_FUNC def - Remove unneeded parens --- Include/object.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Include/object.h b/Include/object.h index 6da4c1ad2ca980..0870e4c6f854c3 100644 --- a/Include/object.h +++ b/Include/object.h @@ -236,11 +236,10 @@ PyAPI_FUNC(void *) PyType_GetModuleState(struct _typeobject *); /* Generic type check */ PyAPI_FUNC(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *); -PyAPI_FUNC(int) PyObject_TypeCheck(PyObject *, PyTypeObject *); -static inline int _PyObject_TypeCheck(PyObject *ob, PyTypeObject *tp) { - return Py_IS_TYPE(ob, tp) || PyType_IsSubtype(Py_TYPE(ob), (tp)); +static inline int _PyObject_TypeCheck(PyObject *ob, PyTypeObject *type) { + return Py_IS_TYPE(ob, type) || PyType_IsSubtype(Py_TYPE(ob), type); } -#define PyObject_TypeCheck(ob, tp) _PyObject_TypeCheck(_PyObject_CAST(ob), tp) +#define PyObject_TypeCheck(ob, type) _PyObject_TypeCheck(_PyObject_CAST(ob), type) PyAPI_DATA(PyTypeObject) PyType_Type; /* built-in 'type' */ PyAPI_DATA(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */ From ad499ae40d0295d4c71b67f0ec6a17fc950def10 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" <erlend.aasland@innova.no> Date: Mon, 15 Feb 2021 12:43:32 +0100 Subject: [PATCH 5/5] Fix ReST formatting --- .../Core and Builtins/2021-02-11-11-37-14.bpo-43181.ydv33S.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-02-11-11-37-14.bpo-43181.ydv33S.rst b/Misc/NEWS.d/next/Core and Builtins/2021-02-11-11-37-14.bpo-43181.ydv33S.rst index 1635719c48c1b4..0e0a5712930d7f 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2021-02-11-11-37-14.bpo-43181.ydv33S.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2021-02-11-11-37-14.bpo-43181.ydv33S.rst @@ -1,2 +1,2 @@ -Convert :cfunc:`PyObject_TypeCheck` macro to a static inline function. Patch by +Convert :c:func:`PyObject_TypeCheck` macro to a static inline function. Patch by Erlend E. Aasland.