Skip to content

Commit 42f1b0e

Browse files
committed
bpo-39573: Add Py_IS_TYPE macro
1 parent 4fac7ed commit 42f1b0e

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

Doc/c-api/structures.rst

+11
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ the definition of all other Python objects.
7070
(((PyObject*)(o))->ob_type)
7171

7272

73+
.. c:macro:: Py_IS_TYPE(o, type)
74+
75+
This macro is used to check whether the type of object *o* is the *type*.
76+
It expands to::
77+
78+
(Py_TYPE(o) == type)
79+
80+
81+
.. versionadded:: 3.9
82+
83+
7384
.. c:function:: void Py_SET_TYPE(PyObject *o, PyTypeObject *type)
7485
7586
Set the object *o* type to *type*.

Include/object.h

+5
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ typedef struct {
123123
#define Py_TYPE(ob) (_PyObject_CAST(ob)->ob_type)
124124
#define Py_SIZE(ob) (_PyVarObject_CAST(ob)->ob_size)
125125

126+
static inline int _Py_IS_TYPE(PyObject *ob, PyTypeObject *type) {
127+
return ob->ob_type == type;
128+
}
129+
#define Py_IS_TYPE(ob, type) _Py_IS_TYPE(_PyObject_CAST(ob), type)
130+
126131
static inline void _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) {
127132
ob->ob_refcnt = refcnt;
128133
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add :c:macro:`Py_IS_TYPE` macro to check whether the type of object *o* is
2+
the *type*.

0 commit comments

Comments
 (0)