bpo-39573: Add Py_IS_TYPE() function (GH-18488)

Co-Author: Neil Schemenauer <nas-github@arctrix.com>
diff --git a/Doc/c-api/structures.rst b/Doc/c-api/structures.rst
index 083c374..fc3467b 100644
--- a/Doc/c-api/structures.rst
+++ b/Doc/c-api/structures.rst
@@ -70,6 +70,14 @@
       (((PyObject*)(o))->ob_type)
 
 
+.. c:function:: int Py_IS_TYPE(PyObject *o, PyTypeObject *type)
+
+   Return non-zero if the object *o* type is *type*. Return zero otherwise.
+   Equivalent to: ``Py_TYPE(o) == type``.
+
+   .. versionadded:: 3.9
+
+
 .. c:function:: void Py_SET_TYPE(PyObject *o, PyTypeObject *type)
 
    Set the object *o* type to *type*.
diff --git a/Include/boolobject.h b/Include/boolobject.h
index 7cc2f1f..bb8044a 100644
--- a/Include/boolobject.h
+++ b/Include/boolobject.h
@@ -9,7 +9,7 @@
 
 PyAPI_DATA(PyTypeObject) PyBool_Type;
 
-#define PyBool_Check(x) (Py_TYPE(x) == &PyBool_Type)
+#define PyBool_Check(x) Py_IS_TYPE(x, &PyBool_Type)
 
 /* Py_False and Py_True are the only two bools in existence.
 Don't forget to apply Py_INCREF() when returning either!!! */
diff --git a/Include/bytearrayobject.h b/Include/bytearrayobject.h
index 341ab38..9e95433 100644
--- a/Include/bytearrayobject.h
+++ b/Include/bytearrayobject.h
@@ -24,7 +24,7 @@
 
 /* Type check macros */
 #define PyByteArray_Check(self) PyObject_TypeCheck(self, &PyByteArray_Type)
-#define PyByteArray_CheckExact(self) (Py_TYPE(self) == &PyByteArray_Type)
+#define PyByteArray_CheckExact(self) Py_IS_TYPE(self, &PyByteArray_Type)
 
 /* Direct API functions */
 PyAPI_FUNC(PyObject *) PyByteArray_FromObject(PyObject *);
diff --git a/Include/bytesobject.h b/Include/bytesobject.h
index 27c31ee..5062d8d 100644
--- a/Include/bytesobject.h
+++ b/Include/bytesobject.h
@@ -32,7 +32,7 @@
 
 #define PyBytes_Check(op) \
                  PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_BYTES_SUBCLASS)
-#define PyBytes_CheckExact(op) (Py_TYPE(op) == &PyBytes_Type)
+#define PyBytes_CheckExact(op) Py_IS_TYPE(op, &PyBytes_Type)
 
 PyAPI_FUNC(PyObject *) PyBytes_FromStringAndSize(const char *, Py_ssize_t);
 PyAPI_FUNC(PyObject *) PyBytes_FromString(const char *);
diff --git a/Include/cellobject.h b/Include/cellobject.h
index 2f9b5b7..f12aa90 100644
--- a/Include/cellobject.h
+++ b/Include/cellobject.h
@@ -13,7 +13,7 @@
 
 PyAPI_DATA(PyTypeObject) PyCell_Type;
 
-#define PyCell_Check(op) (Py_TYPE(op) == &PyCell_Type)
+#define PyCell_Check(op) Py_IS_TYPE(op, &PyCell_Type)
 
 PyAPI_FUNC(PyObject *) PyCell_New(PyObject *);
 PyAPI_FUNC(PyObject *) PyCell_Get(PyObject *);
diff --git a/Include/code.h b/Include/code.h
index 3afddd2..107eba4 100644
--- a/Include/code.h
+++ b/Include/code.h
@@ -115,7 +115,7 @@
 
 PyAPI_DATA(PyTypeObject) PyCode_Type;
 
-#define PyCode_Check(op) (Py_TYPE(op) == &PyCode_Type)
+#define PyCode_Check(op) Py_IS_TYPE(op, &PyCode_Type)
 #define PyCode_GetNumFree(op) (PyTuple_GET_SIZE((op)->co_freevars))
 
 /* Public interface */
diff --git a/Include/complexobject.h b/Include/complexobject.h
index cb8c52c..9221f9c 100644
--- a/Include/complexobject.h
+++ b/Include/complexobject.h
@@ -39,7 +39,7 @@
 PyAPI_DATA(PyTypeObject) PyComplex_Type;
 
 #define PyComplex_Check(op) PyObject_TypeCheck(op, &PyComplex_Type)
-#define PyComplex_CheckExact(op) (Py_TYPE(op) == &PyComplex_Type)
+#define PyComplex_CheckExact(op) Py_IS_TYPE(op, &PyComplex_Type)
 
 #ifndef Py_LIMITED_API
 PyAPI_FUNC(PyObject *) PyComplex_FromCComplex(Py_complex);
diff --git a/Include/context.h b/Include/context.h
index 9581285..619746d 100644
--- a/Include/context.h
+++ b/Include/context.h
@@ -17,9 +17,9 @@
 typedef struct _pycontexttokenobject PyContextToken;
 
 
-#define PyContext_CheckExact(o) (Py_TYPE(o) == &PyContext_Type)
-#define PyContextVar_CheckExact(o) (Py_TYPE(o) == &PyContextVar_Type)
-#define PyContextToken_CheckExact(o) (Py_TYPE(o) == &PyContextToken_Type)
+#define PyContext_CheckExact(o) Py_IS_TYPE(o, &PyContext_Type)
+#define PyContextVar_CheckExact(o) Py_IS_TYPE(o, &PyContextVar_Type)
+#define PyContextToken_CheckExact(o) Py_IS_TYPE(o, &PyContextToken_Type)
 
 
 PyAPI_FUNC(PyObject *) PyContext_New(void);
diff --git a/Include/datetime.h b/Include/datetime.h
index 00507cb..5d9f255 100644
--- a/Include/datetime.h
+++ b/Include/datetime.h
@@ -196,19 +196,19 @@
 
 /* Macros for type checking when not building the Python core. */
 #define PyDate_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DateType)
-#define PyDate_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->DateType)
+#define PyDate_CheckExact(op) Py_IS_TYPE(op, PyDateTimeAPI->DateType)
 
 #define PyDateTime_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DateTimeType)
-#define PyDateTime_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->DateTimeType)
+#define PyDateTime_CheckExact(op) Py_IS_TYPE(op, PyDateTimeAPI->DateTimeType)
 
 #define PyTime_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->TimeType)
-#define PyTime_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->TimeType)
+#define PyTime_CheckExact(op) Py_IS_TYPE(op, PyDateTimeAPI->TimeType)
 
 #define PyDelta_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DeltaType)
-#define PyDelta_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->DeltaType)
+#define PyDelta_CheckExact(op) Py_IS_TYPE(op, PyDateTimeAPI->DeltaType)
 
 #define PyTZInfo_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->TZInfoType)
-#define PyTZInfo_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->TZInfoType)
+#define PyTZInfo_CheckExact(op) Py_IS_TYPE(op, PyDateTimeAPI->TZInfoType)
 
 
 /* Macros for accessing constructors in a simplified fashion. */
diff --git a/Include/dictobject.h b/Include/dictobject.h
index b37573a..c88b0aa 100644
--- a/Include/dictobject.h
+++ b/Include/dictobject.h
@@ -16,7 +16,7 @@
 
 #define PyDict_Check(op) \
                  PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_DICT_SUBCLASS)
-#define PyDict_CheckExact(op) (Py_TYPE(op) == &PyDict_Type)
+#define PyDict_CheckExact(op) Py_IS_TYPE(op, &PyDict_Type)
 
 PyAPI_FUNC(PyObject *) PyDict_New(void);
 PyAPI_FUNC(PyObject *) PyDict_GetItem(PyObject *mp, PyObject *key);
diff --git a/Include/floatobject.h b/Include/floatobject.h
index 0fb9fc4..917dfcc 100644
--- a/Include/floatobject.h
+++ b/Include/floatobject.h
@@ -21,7 +21,7 @@
 PyAPI_DATA(PyTypeObject) PyFloat_Type;
 
 #define PyFloat_Check(op) PyObject_TypeCheck(op, &PyFloat_Type)
-#define PyFloat_CheckExact(op) (Py_TYPE(op) == &PyFloat_Type)
+#define PyFloat_CheckExact(op) Py_IS_TYPE(op, &PyFloat_Type)
 
 #ifdef Py_NAN
 #define Py_RETURN_NAN return PyFloat_FromDouble(Py_NAN)
diff --git a/Include/funcobject.h b/Include/funcobject.h
index c6dd67d..c5cc9d2 100644
--- a/Include/funcobject.h
+++ b/Include/funcobject.h
@@ -43,7 +43,7 @@
 
 PyAPI_DATA(PyTypeObject) PyFunction_Type;
 
-#define PyFunction_Check(op) (Py_TYPE(op) == &PyFunction_Type)
+#define PyFunction_Check(op) Py_IS_TYPE(op, &PyFunction_Type)
 
 PyAPI_FUNC(PyObject *) PyFunction_New(PyObject *, PyObject *);
 PyAPI_FUNC(PyObject *) PyFunction_NewWithQualName(PyObject *, PyObject *, PyObject *);
diff --git a/Include/genobject.h b/Include/genobject.h
index 5ee9a28..b87a648 100644
--- a/Include/genobject.h
+++ b/Include/genobject.h
@@ -38,7 +38,7 @@
 PyAPI_DATA(PyTypeObject) PyGen_Type;
 
 #define PyGen_Check(op) PyObject_TypeCheck(op, &PyGen_Type)
-#define PyGen_CheckExact(op) (Py_TYPE(op) == &PyGen_Type)
+#define PyGen_CheckExact(op) Py_IS_TYPE(op, &PyGen_Type)
 
 PyAPI_FUNC(PyObject *) PyGen_New(struct _frame *);
 PyAPI_FUNC(PyObject *) PyGen_NewWithQualName(struct _frame *,
@@ -58,7 +58,7 @@
 PyAPI_DATA(PyTypeObject) PyCoro_Type;
 PyAPI_DATA(PyTypeObject) _PyCoroWrapper_Type;
 
-#define PyCoro_CheckExact(op) (Py_TYPE(op) == &PyCoro_Type)
+#define PyCoro_CheckExact(op) Py_IS_TYPE(op, &PyCoro_Type)
 PyObject *_PyCoro_GetAwaitableIter(PyObject *o);
 PyAPI_FUNC(PyObject *) PyCoro_New(struct _frame *,
     PyObject *name, PyObject *qualname);
@@ -89,7 +89,7 @@
 PyAPI_FUNC(PyObject *) PyAsyncGen_New(struct _frame *,
     PyObject *name, PyObject *qualname);
 
-#define PyAsyncGen_CheckExact(op) (Py_TYPE(op) == &PyAsyncGen_Type)
+#define PyAsyncGen_CheckExact(op) Py_IS_TYPE(op, &PyAsyncGen_Type)
 
 PyObject *_PyAsyncGenValueWrapperNew(PyObject *);
 
diff --git a/Include/internal/pycore_hamt.h b/Include/internal/pycore_hamt.h
index e65aef5..aaf6559 100644
--- a/Include/internal/pycore_hamt.h
+++ b/Include/internal/pycore_hamt.h
@@ -8,7 +8,7 @@
 #define _Py_HAMT_MAX_TREE_DEPTH 7
 
 
-#define PyHamt_Check(o) (Py_TYPE(o) == &_PyHamt_Type)
+#define PyHamt_Check(o) Py_IS_TYPE(o, &_PyHamt_Type)
 
 
 /* Abstract tree node. */
diff --git a/Include/iterobject.h b/Include/iterobject.h
index eec2ee2..51139bf 100644
--- a/Include/iterobject.h
+++ b/Include/iterobject.h
@@ -8,12 +8,12 @@
 PyAPI_DATA(PyTypeObject) PySeqIter_Type;
 PyAPI_DATA(PyTypeObject) PyCallIter_Type;
 
-#define PySeqIter_Check(op) (Py_TYPE(op) == &PySeqIter_Type)
+#define PySeqIter_Check(op) Py_IS_TYPE(op, &PySeqIter_Type)
 
 PyAPI_FUNC(PyObject *) PySeqIter_New(PyObject *);
 
 
-#define PyCallIter_Check(op) (Py_TYPE(op) == &PyCallIter_Type)
+#define PyCallIter_Check(op) Py_IS_TYPE(op, &PyCallIter_Type)
 
 PyAPI_FUNC(PyObject *) PyCallIter_New(PyObject *, PyObject *);
 
diff --git a/Include/listobject.h b/Include/listobject.h
index 34dfcf9..2a8a255 100644
--- a/Include/listobject.h
+++ b/Include/listobject.h
@@ -23,7 +23,7 @@
 
 #define PyList_Check(op) \
     PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_LIST_SUBCLASS)
-#define PyList_CheckExact(op) (Py_TYPE(op) == &PyList_Type)
+#define PyList_CheckExact(op) Py_IS_TYPE(op, &PyList_Type)
 
 PyAPI_FUNC(PyObject *) PyList_New(Py_ssize_t size);
 PyAPI_FUNC(Py_ssize_t) PyList_Size(PyObject *);
diff --git a/Include/memoryobject.h b/Include/memoryobject.h
index 990a716..306028f 100644
--- a/Include/memoryobject.h
+++ b/Include/memoryobject.h
@@ -11,7 +11,7 @@
 #endif
 PyAPI_DATA(PyTypeObject) PyMemoryView_Type;
 
-#define PyMemoryView_Check(op) (Py_TYPE(op) == &PyMemoryView_Type)
+#define PyMemoryView_Check(op) Py_IS_TYPE(op, &PyMemoryView_Type)
 
 #ifndef Py_LIMITED_API
 /* Get a pointer to the memoryview's private copy of the exporter's buffer. */
diff --git a/Include/methodobject.h b/Include/methodobject.h
index d9f8d4f..adb2d9e 100644
--- a/Include/methodobject.h
+++ b/Include/methodobject.h
@@ -13,7 +13,7 @@
 
 PyAPI_DATA(PyTypeObject) PyCFunction_Type;
 
-#define PyCFunction_Check(op) (Py_TYPE(op) == &PyCFunction_Type)
+#define PyCFunction_Check(op) Py_IS_TYPE(op, &PyCFunction_Type)
 
 typedef PyObject *(*PyCFunction)(PyObject *, PyObject *);
 typedef PyObject *(*_PyCFunctionFast) (PyObject *, PyObject *const *, Py_ssize_t);
diff --git a/Include/moduleobject.h b/Include/moduleobject.h
index e246fd2..cf9ad40 100644
--- a/Include/moduleobject.h
+++ b/Include/moduleobject.h
@@ -10,7 +10,7 @@
 PyAPI_DATA(PyTypeObject) PyModule_Type;
 
 #define PyModule_Check(op) PyObject_TypeCheck(op, &PyModule_Type)
-#define PyModule_CheckExact(op) (Py_TYPE(op) == &PyModule_Type)
+#define PyModule_CheckExact(op) Py_IS_TYPE(op, &PyModule_Type)
 
 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
 PyAPI_FUNC(PyObject *) PyModule_NewObject(
diff --git a/Include/object.h b/Include/object.h
index 11539ee..3d0da52 100644
--- a/Include/object.h
+++ b/Include/object.h
@@ -123,6 +123,11 @@
 #define Py_TYPE(ob)             (_PyObject_CAST(ob)->ob_type)
 #define Py_SIZE(ob)             (_PyVarObject_CAST(ob)->ob_size)
 
+static inline int _Py_IS_TYPE(PyObject *ob, PyTypeObject *type) {
+    return ob->ob_type == type;
+}
+#define Py_IS_TYPE(ob, type) _Py_IS_TYPE(_PyObject_CAST(ob), type)
+
 static inline void _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) {
     ob->ob_refcnt = refcnt;
 }
@@ -211,7 +216,7 @@
 /* Generic type check */
 PyAPI_FUNC(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *);
 #define PyObject_TypeCheck(ob, tp) \
-    (Py_TYPE(ob) == (tp) || PyType_IsSubtype(Py_TYPE(ob), (tp)))
+    (Py_IS_TYPE(ob, tp) || PyType_IsSubtype(Py_TYPE(ob), (tp)))
 
 PyAPI_DATA(PyTypeObject) PyType_Type; /* built-in 'type' */
 PyAPI_DATA(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */
@@ -623,7 +628,7 @@
 #define PyType_Check(op) _PyType_Check(_PyObject_CAST(op))
 
 static inline int _PyType_CheckExact(PyObject *op) {
-    return (Py_TYPE(op) == &PyType_Type);
+    return Py_IS_TYPE(op, &PyType_Type);
 }
 #define PyType_CheckExact(op) _PyType_CheckExact(_PyObject_CAST(op))
 
diff --git a/Include/odictobject.h b/Include/odictobject.h
index 35aff8a..e070413 100644
--- a/Include/odictobject.h
+++ b/Include/odictobject.h
@@ -19,7 +19,7 @@
 PyAPI_DATA(PyTypeObject) PyODictValues_Type;
 
 #define PyODict_Check(op) PyObject_TypeCheck(op, &PyODict_Type)
-#define PyODict_CheckExact(op) (Py_TYPE(op) == &PyODict_Type)
+#define PyODict_CheckExact(op) Py_IS_TYPE(op, &PyODict_Type)
 #define PyODict_SIZE(op) PyDict_GET_SIZE((op))
 
 PyAPI_FUNC(PyObject *) PyODict_New(void);
diff --git a/Include/pycapsule.h b/Include/pycapsule.h
index d9ecda7..fb5d503 100644
--- a/Include/pycapsule.h
+++ b/Include/pycapsule.h
@@ -22,7 +22,7 @@
 
 typedef void (*PyCapsule_Destructor)(PyObject *);
 
-#define PyCapsule_CheckExact(op) (Py_TYPE(op) == &PyCapsule_Type)
+#define PyCapsule_CheckExact(op) Py_IS_TYPE(op, &PyCapsule_Type)
 
 
 PyAPI_FUNC(PyObject *) PyCapsule_New(
diff --git a/Include/rangeobject.h b/Include/rangeobject.h
index 7e4dc28..d6af847 100644
--- a/Include/rangeobject.h
+++ b/Include/rangeobject.h
@@ -19,7 +19,7 @@
 PyAPI_DATA(PyTypeObject) PyRangeIter_Type;
 PyAPI_DATA(PyTypeObject) PyLongRangeIter_Type;
 
-#define PyRange_Check(op) (Py_TYPE(op) == &PyRange_Type)
+#define PyRange_Check(op) Py_IS_TYPE(op, &PyRange_Type)
 
 #ifdef __cplusplus
 }
diff --git a/Include/setobject.h b/Include/setobject.h
index fc0ea83..05a097e 100644
--- a/Include/setobject.h
+++ b/Include/setobject.h
@@ -88,18 +88,18 @@
 PyAPI_FUNC(PyObject *) PySet_Pop(PyObject *set);
 PyAPI_FUNC(Py_ssize_t) PySet_Size(PyObject *anyset);
 
-#define PyFrozenSet_CheckExact(ob) (Py_TYPE(ob) == &PyFrozenSet_Type)
+#define PyFrozenSet_CheckExact(ob) Py_IS_TYPE(ob, &PyFrozenSet_Type)
 #define PyAnySet_CheckExact(ob) \
-    (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type)
+    (Py_IS_TYPE(ob, &PySet_Type) || Py_IS_TYPE(ob, &PyFrozenSet_Type))
 #define PyAnySet_Check(ob) \
-    (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type || \
+    (Py_IS_TYPE(ob, &PySet_Type) || Py_IS_TYPE(ob, &PyFrozenSet_Type) || \
       PyType_IsSubtype(Py_TYPE(ob), &PySet_Type) || \
       PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
 #define PySet_Check(ob) \
-    (Py_TYPE(ob) == &PySet_Type || \
+    (Py_IS_TYPE(ob, &PySet_Type) || \
     PyType_IsSubtype(Py_TYPE(ob), &PySet_Type))
 #define   PyFrozenSet_Check(ob) \
-    (Py_TYPE(ob) == &PyFrozenSet_Type || \
+    (Py_IS_TYPE(ob, &PyFrozenSet_Type) || \
       PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
 
 #ifdef __cplusplus
diff --git a/Include/sliceobject.h b/Include/sliceobject.h
index aae6f3c..2c88950 100644
--- a/Include/sliceobject.h
+++ b/Include/sliceobject.h
@@ -28,7 +28,7 @@
 PyAPI_DATA(PyTypeObject) PySlice_Type;
 PyAPI_DATA(PyTypeObject) PyEllipsis_Type;
 
-#define PySlice_Check(op) (Py_TYPE(op) == &PySlice_Type)
+#define PySlice_Check(op) Py_IS_TYPE(op, &PySlice_Type)
 
 PyAPI_FUNC(PyObject *) PySlice_New(PyObject* start, PyObject* stop,
                                   PyObject* step);
diff --git a/Include/symtable.h b/Include/symtable.h
index 5dcfa7e..abd19a7 100644
--- a/Include/symtable.h
+++ b/Include/symtable.h
@@ -69,7 +69,7 @@
 
 PyAPI_DATA(PyTypeObject) PySTEntry_Type;
 
-#define PySTEntry_Check(op) (Py_TYPE(op) == &PySTEntry_Type)
+#define PySTEntry_Check(op) Py_IS_TYPE(op, &PySTEntry_Type)
 
 PyAPI_FUNC(int) PyST_GetScope(PySTEntryObject *, PyObject *);
 
diff --git a/Include/traceback.h b/Include/traceback.h
index b451927..0efbae8 100644
--- a/Include/traceback.h
+++ b/Include/traceback.h
@@ -13,7 +13,7 @@
 
 /* Reveal traceback type so we can typecheck traceback objects */
 PyAPI_DATA(PyTypeObject) PyTraceBack_Type;
-#define PyTraceBack_Check(v) (Py_TYPE(v) == &PyTraceBack_Type)
+#define PyTraceBack_Check(v) Py_IS_TYPE(v, &PyTraceBack_Type)
 
 
 #ifndef Py_LIMITED_API
diff --git a/Include/tupleobject.h b/Include/tupleobject.h
index 590902d..d3504b0 100644
--- a/Include/tupleobject.h
+++ b/Include/tupleobject.h
@@ -25,7 +25,7 @@
 
 #define PyTuple_Check(op) \
                  PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TUPLE_SUBCLASS)
-#define PyTuple_CheckExact(op) (Py_TYPE(op) == &PyTuple_Type)
+#define PyTuple_CheckExact(op) Py_IS_TYPE(op, &PyTuple_Type)
 
 PyAPI_FUNC(PyObject *) PyTuple_New(Py_ssize_t size);
 PyAPI_FUNC(Py_ssize_t) PyTuple_Size(PyObject *);
diff --git a/Include/unicodeobject.h b/Include/unicodeobject.h
index 4dea494..500ce24 100644
--- a/Include/unicodeobject.h
+++ b/Include/unicodeobject.h
@@ -113,7 +113,7 @@
 
 #define PyUnicode_Check(op) \
                  PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_UNICODE_SUBCLASS)
-#define PyUnicode_CheckExact(op) (Py_TYPE(op) == &PyUnicode_Type)
+#define PyUnicode_CheckExact(op) Py_IS_TYPE(op, &PyUnicode_Type)
 
 /* --- Constants ---------------------------------------------------------- */
 
diff --git a/Include/weakrefobject.h b/Include/weakrefobject.h
index 1705156..ac4b482 100644
--- a/Include/weakrefobject.h
+++ b/Include/weakrefobject.h
@@ -46,10 +46,10 @@
 
 #define PyWeakref_CheckRef(op) PyObject_TypeCheck(op, &_PyWeakref_RefType)
 #define PyWeakref_CheckRefExact(op) \
-        (Py_TYPE(op) == &_PyWeakref_RefType)
+        Py_IS_TYPE(op, &_PyWeakref_RefType)
 #define PyWeakref_CheckProxy(op) \
-        ((Py_TYPE(op) == &_PyWeakref_ProxyType) || \
-         (Py_TYPE(op) == &_PyWeakref_CallableProxyType))
+        (Py_IS_TYPE(op, &_PyWeakref_ProxyType) || \
+         Py_IS_TYPE(op, &_PyWeakref_CallableProxyType))
 
 #define PyWeakref_Check(op) \
         (PyWeakref_CheckRef(op) || PyWeakref_CheckProxy(op))
diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-02-13-01-30-22.bpo-39573.uTFj1m.rst b/Misc/NEWS.d/next/Core and Builtins/2020-02-13-01-30-22.bpo-39573.uTFj1m.rst
new file mode 100644
index 0000000..56e7e1b
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2020-02-13-01-30-22.bpo-39573.uTFj1m.rst
@@ -0,0 +1,2 @@
+Add :c:func:`Py_IS_TYPE` static inline function to check
+whether the object *o* type is *type*.
diff --git a/Objects/genobject.c b/Objects/genobject.c
index 0efd57d..0837698 100644
--- a/Objects/genobject.c
+++ b/Objects/genobject.c
@@ -255,7 +255,7 @@
         if (PyCoro_CheckExact(gen)) {
             msg = "coroutine raised StopIteration";
         }
-        else if PyAsyncGen_CheckExact(gen) {
+        else if (PyAsyncGen_CheckExact(gen)) {
             msg = "async generator raised StopIteration";
         }
         _PyErr_FormatFromCause(PyExc_RuntimeError, "%s", msg);