Skip to content

Commit d905df7

Browse files
authored
bpo-39573: Add Py_IS_TYPE() function (GH-18488)
Co-Author: Neil Schemenauer <[email protected]>
1 parent 968dcd9 commit d905df7

32 files changed

+61
-46
lines changed

Doc/c-api/structures.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ the definition of all other Python objects.
7070
(((PyObject*)(o))->ob_type)
7171

7272

73+
.. c:function:: int Py_IS_TYPE(PyObject *o, PyTypeObject *type)
74+
75+
Return non-zero if the object *o* type is *type*. Return zero otherwise.
76+
Equivalent to: ``Py_TYPE(o) == type``.
77+
78+
.. versionadded:: 3.9
79+
80+
7381
.. c:function:: void Py_SET_TYPE(PyObject *o, PyTypeObject *type)
7482
7583
Set the object *o* type to *type*.

Include/boolobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ extern "C" {
99

1010
PyAPI_DATA(PyTypeObject) PyBool_Type;
1111

12-
#define PyBool_Check(x) (Py_TYPE(x) == &PyBool_Type)
12+
#define PyBool_Check(x) Py_IS_TYPE(x, &PyBool_Type)
1313

1414
/* Py_False and Py_True are the only two bools in existence.
1515
Don't forget to apply Py_INCREF() when returning either!!! */

Include/bytearrayobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ PyAPI_DATA(PyTypeObject) PyByteArrayIter_Type;
2424

2525
/* Type check macros */
2626
#define PyByteArray_Check(self) PyObject_TypeCheck(self, &PyByteArray_Type)
27-
#define PyByteArray_CheckExact(self) (Py_TYPE(self) == &PyByteArray_Type)
27+
#define PyByteArray_CheckExact(self) Py_IS_TYPE(self, &PyByteArray_Type)
2828

2929
/* Direct API functions */
3030
PyAPI_FUNC(PyObject *) PyByteArray_FromObject(PyObject *);

Include/bytesobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ PyAPI_DATA(PyTypeObject) PyBytesIter_Type;
3232

3333
#define PyBytes_Check(op) \
3434
PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_BYTES_SUBCLASS)
35-
#define PyBytes_CheckExact(op) (Py_TYPE(op) == &PyBytes_Type)
35+
#define PyBytes_CheckExact(op) Py_IS_TYPE(op, &PyBytes_Type)
3636

3737
PyAPI_FUNC(PyObject *) PyBytes_FromStringAndSize(const char *, Py_ssize_t);
3838
PyAPI_FUNC(PyObject *) PyBytes_FromString(const char *);

Include/cellobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ typedef struct {
1313

1414
PyAPI_DATA(PyTypeObject) PyCell_Type;
1515

16-
#define PyCell_Check(op) (Py_TYPE(op) == &PyCell_Type)
16+
#define PyCell_Check(op) Py_IS_TYPE(op, &PyCell_Type)
1717

1818
PyAPI_FUNC(PyObject *) PyCell_New(PyObject *);
1919
PyAPI_FUNC(PyObject *) PyCell_Get(PyObject *);

Include/code.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ typedef struct {
115115

116116
PyAPI_DATA(PyTypeObject) PyCode_Type;
117117

118-
#define PyCode_Check(op) (Py_TYPE(op) == &PyCode_Type)
118+
#define PyCode_Check(op) Py_IS_TYPE(op, &PyCode_Type)
119119
#define PyCode_GetNumFree(op) (PyTuple_GET_SIZE((op)->co_freevars))
120120

121121
/* Public interface */

Include/complexobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ typedef struct {
3939
PyAPI_DATA(PyTypeObject) PyComplex_Type;
4040

4141
#define PyComplex_Check(op) PyObject_TypeCheck(op, &PyComplex_Type)
42-
#define PyComplex_CheckExact(op) (Py_TYPE(op) == &PyComplex_Type)
42+
#define PyComplex_CheckExact(op) Py_IS_TYPE(op, &PyComplex_Type)
4343

4444
#ifndef Py_LIMITED_API
4545
PyAPI_FUNC(PyObject *) PyComplex_FromCComplex(Py_complex);

Include/context.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ PyAPI_DATA(PyTypeObject) PyContextToken_Type;
1717
typedef struct _pycontexttokenobject PyContextToken;
1818

1919

20-
#define PyContext_CheckExact(o) (Py_TYPE(o) == &PyContext_Type)
21-
#define PyContextVar_CheckExact(o) (Py_TYPE(o) == &PyContextVar_Type)
22-
#define PyContextToken_CheckExact(o) (Py_TYPE(o) == &PyContextToken_Type)
20+
#define PyContext_CheckExact(o) Py_IS_TYPE(o, &PyContext_Type)
21+
#define PyContextVar_CheckExact(o) Py_IS_TYPE(o, &PyContextVar_Type)
22+
#define PyContextToken_CheckExact(o) Py_IS_TYPE(o, &PyContextToken_Type)
2323

2424

2525
PyAPI_FUNC(PyObject *) PyContext_New(void);

Include/datetime.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,19 +196,19 @@ static PyDateTime_CAPI *PyDateTimeAPI = NULL;
196196

197197
/* Macros for type checking when not building the Python core. */
198198
#define PyDate_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DateType)
199-
#define PyDate_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->DateType)
199+
#define PyDate_CheckExact(op) Py_IS_TYPE(op, PyDateTimeAPI->DateType)
200200

201201
#define PyDateTime_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DateTimeType)
202-
#define PyDateTime_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->DateTimeType)
202+
#define PyDateTime_CheckExact(op) Py_IS_TYPE(op, PyDateTimeAPI->DateTimeType)
203203

204204
#define PyTime_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->TimeType)
205-
#define PyTime_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->TimeType)
205+
#define PyTime_CheckExact(op) Py_IS_TYPE(op, PyDateTimeAPI->TimeType)
206206

207207
#define PyDelta_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DeltaType)
208-
#define PyDelta_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->DeltaType)
208+
#define PyDelta_CheckExact(op) Py_IS_TYPE(op, PyDateTimeAPI->DeltaType)
209209

210210
#define PyTZInfo_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->TZInfoType)
211-
#define PyTZInfo_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->TZInfoType)
211+
#define PyTZInfo_CheckExact(op) Py_IS_TYPE(op, PyDateTimeAPI->TZInfoType)
212212

213213

214214
/* Macros for accessing constructors in a simplified fashion. */

Include/dictobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ PyAPI_DATA(PyTypeObject) PyDict_Type;
1616

1717
#define PyDict_Check(op) \
1818
PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_DICT_SUBCLASS)
19-
#define PyDict_CheckExact(op) (Py_TYPE(op) == &PyDict_Type)
19+
#define PyDict_CheckExact(op) Py_IS_TYPE(op, &PyDict_Type)
2020

2121
PyAPI_FUNC(PyObject *) PyDict_New(void);
2222
PyAPI_FUNC(PyObject *) PyDict_GetItem(PyObject *mp, PyObject *key);

0 commit comments

Comments
 (0)