[Python-checkins] bpo-43181: Convert PyObject_TypeCheck to static inline function (GH-24533)

vstinner webhook-mailer at python.org
Mon Feb 15 11:19:32 EST 2021


https://github.com/python/cpython/commit/4bb2a1ebc569eee6f1b46ecef1965a26ae8cb76d
commit: 4bb2a1ebc569eee6f1b46ecef1965a26ae8cb76d
branch: master
author: Erlend Egeberg Aasland <erlend.aasland at innova.no>
committer: vstinner <vstinner at python.org>
date: 2021-02-15T17:19:24+01:00
summary:

bpo-43181: Convert PyObject_TypeCheck to static inline function (GH-24533)

files:
A Misc/NEWS.d/next/Core and Builtins/2021-02-11-11-37-14.bpo-43181.ydv33S.rst
M Doc/c-api/object.rst
M Include/object.h

diff --git a/Doc/c-api/object.rst b/Doc/c-api/object.rst
index a387b4a2df134..1100af1df2928 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 8d0039428e73a..0870e4c6f854c 100644
--- a/Include/object.h
+++ b/Include/object.h
@@ -235,8 +235,11 @@ 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)))
+
+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, type) _PyObject_TypeCheck(_PyObject_CAST(ob), type)
 
 PyAPI_DATA(PyTypeObject) PyType_Type; /* built-in 'type' */
 PyAPI_DATA(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */
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 0000000000000..0e0a5712930d7
--- /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 :c:func:`PyObject_TypeCheck` macro to a static inline function. Patch by
+Erlend E. Aasland.



More information about the Python-checkins mailing list