Skip to content

Commit 509dd90

Browse files
authored
bpo-39542: Convert PyType_Check() to static inline function (GH-18364)
Convert PyType_HasFeature(), PyType_Check() and PyType_CheckExact() macros to static inline functions.
1 parent f58bd7c commit 509dd90

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

‎Doc/c-api/type.rst‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ Type Objects
2121

2222
.. c:function:: int PyType_Check(PyObject *o)
2323
24-
Return true if the object *o* is a type object, including instances of types
25-
derived from the standard type object. Return false in all other cases.
24+
Return non-zero if the object *o* is a type object, including instances of
25+
types derived from the standard type object. Return 0 in all other cases.
2626
2727
2828
.. c:function:: int PyType_CheckExact(PyObject *o)
2929
30-
Return true if the object *o* is a type object, but not a subtype of the
31-
standard type object. Return false in all other cases.
30+
Return non-zero if the object *o* is a type object, but not a subtype of the
31+
standard type object. Return 0 in all other cases.
3232
3333
3434
.. c:function:: unsigned int PyType_ClearCache()
@@ -57,8 +57,8 @@ Type Objects
5757
5858
.. c:function:: int PyType_HasFeature(PyTypeObject *o, int feature)
5959
60-
Return true if the type object *o* sets the feature *feature*. Type features
61-
are denoted by single bit flags.
60+
Return non-zero if the type object *o* sets the feature *feature*.
61+
Type features are denoted by single bit flags.
6262
6363
6464
.. c:function:: int PyType_IS_GC(PyTypeObject *o)

‎Include/cpython/object.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,6 @@ PyAPI_FUNC(int)
341341
_PyObject_GenericSetAttrWithDict(PyObject *, PyObject *,
342342
PyObject *, PyObject *);
343343

344-
#define PyType_HasFeature(t,f) (((t)->tp_flags & (f)) != 0)
345-
346344
PyAPI_FUNC(PyObject *) _PyObject_FunctionStr(PyObject *);
347345

348346
/* Safely decref `op` and set `op` to `op2`.

‎Include/object.h‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,6 @@ PyAPI_DATA(struct _typeobject) PySuper_Type; /* built-in 'super' */
207207

208208
PyAPI_FUNC(unsigned long) PyType_GetFlags(struct _typeobject*);
209209

210-
#define PyType_Check(op) \
211-
PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS)
212-
#define PyType_CheckExact(op) (Py_TYPE(op) == &PyType_Type)
213-
214210
PyAPI_FUNC(int) PyType_Ready(struct _typeobject *);
215211
PyAPI_FUNC(PyObject *) PyType_GenericAlloc(struct _typeobject *, Py_ssize_t);
216212
PyAPI_FUNC(PyObject *) PyType_GenericNew(struct _typeobject *,
@@ -342,11 +338,6 @@ given type object has a specified feature.
342338
/* Type structure has tp_finalize member (3.4) */
343339
#define Py_TPFLAGS_HAVE_FINALIZE (1UL << 0)
344340

345-
#ifdef Py_LIMITED_API
346-
# define PyType_HasFeature(t,f) ((PyType_GetFlags(t) & (f)) != 0)
347-
#endif
348-
#define PyType_FastSubclass(t,f) PyType_HasFeature(t,f)
349-
350341

351342
/*
352343
The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement
@@ -600,6 +591,28 @@ times.
600591
# undef Py_CPYTHON_OBJECT_H
601592
#endif
602593

594+
595+
static inline int
596+
PyType_HasFeature(PyTypeObject *type, unsigned long feature) {
597+
#ifdef Py_LIMITED_API
598+
return ((PyType_GetFlags(type) & feature) != 0);
599+
#else
600+
return ((type->tp_flags & feature) != 0);
601+
#endif
602+
}
603+
604+
#define PyType_FastSubclass(type, flag) PyType_HasFeature(type, flag)
605+
606+
static inline int _PyType_Check(PyObject *op) {
607+
return PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS);
608+
}
609+
#define PyType_Check(op) _PyType_Check(_PyObject_CAST(op))
610+
611+
static inline int _PyType_CheckExact(PyObject *op) {
612+
return (Py_TYPE(op) == &PyType_Type);
613+
}
614+
#define PyType_CheckExact(op) _PyType_CheckExact(_PyObject_CAST(op))
615+
603616
#ifdef __cplusplus
604617
}
605618
#endif
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Convert :c:func:`PyType_HasFeature`, :c:func:`PyType_Check` and
2+
:c:func:`PyType_CheckExact` macros to static inline functions.

0 commit comments

Comments
 (0)