Skip to content

Commit 08b562a

Browse files
authored
gh-89653: PEP 670: Convert PyCFunction macros to functions (#92302)
Convert the following macros to static inline functions: * PyCFunction_GET_CLASS() * PyCFunction_GET_FLAGS() * PyCFunction_GET_FUNCTION() * PyCFunction_GET_SELF() Limited C API version 3.11 no longer casts arguments.
1 parent 5212cbc commit 08b562a

File tree

1 file changed

+56
-24
lines changed

1 file changed

+56
-24
lines changed

Include/cpython/methodobject.h

+56-24
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,7 @@
22
# error "this header file must not be included directly"
33
#endif
44

5-
PyAPI_DATA(PyTypeObject) PyCMethod_Type;
6-
7-
#define PyCMethod_CheckExact(op) Py_IS_TYPE(op, &PyCMethod_Type)
8-
#define PyCMethod_Check(op) PyObject_TypeCheck(op, &PyCMethod_Type)
9-
10-
#define _PyCFunctionObject_CAST(func) \
11-
(assert(PyCFunction_Check(func)), \
12-
_Py_CAST(PyCFunctionObject*, (func)))
13-
#define _PyCMethodObject_CAST(func) \
14-
(assert(PyCMethod_Check(func)), \
15-
_Py_CAST(PyCMethodObject*, (func)))
16-
17-
/* Macros for direct access to these values. Type checks are *not*
18-
done, so use with care. */
19-
#define PyCFunction_GET_FUNCTION(func) \
20-
(_PyCFunctionObject_CAST(func)->m_ml->ml_meth)
21-
#define PyCFunction_GET_SELF(func) \
22-
(_PyCFunctionObject_CAST(func)->m_ml->ml_flags & METH_STATIC ? \
23-
NULL : _PyCFunctionObject_CAST(func)->m_self)
24-
#define PyCFunction_GET_FLAGS(func) \
25-
(_PyCFunctionObject_CAST(func)->m_ml->ml_flags)
26-
#define PyCFunction_GET_CLASS(func) \
27-
(_PyCFunctionObject_CAST(func)->m_ml->ml_flags & METH_METHOD ? \
28-
_PyCMethodObject_CAST(func)->mm_class : NULL)
5+
// PyCFunctionObject structure
296

307
typedef struct {
318
PyObject_HEAD
@@ -36,7 +13,62 @@ typedef struct {
3613
vectorcallfunc vectorcall;
3714
} PyCFunctionObject;
3815

16+
#define _PyCFunctionObject_CAST(func) \
17+
(assert(PyCFunction_Check(func)), \
18+
_Py_CAST(PyCFunctionObject*, (func)))
19+
20+
21+
// PyCMethodObject structure
22+
3923
typedef struct {
4024
PyCFunctionObject func;
4125
PyTypeObject *mm_class; /* Class that defines this method */
4226
} PyCMethodObject;
27+
28+
#define _PyCMethodObject_CAST(func) \
29+
(assert(PyCMethod_Check(func)), \
30+
_Py_CAST(PyCMethodObject*, (func)))
31+
32+
PyAPI_DATA(PyTypeObject) PyCMethod_Type;
33+
34+
#define PyCMethod_CheckExact(op) Py_IS_TYPE(op, &PyCMethod_Type)
35+
#define PyCMethod_Check(op) PyObject_TypeCheck(op, &PyCMethod_Type)
36+
37+
38+
/* Static inline functions for direct access to these values.
39+
Type checks are *not* done, so use with care. */
40+
static inline PyCFunction PyCFunction_GET_FUNCTION(PyObject *func) {
41+
return _PyCFunctionObject_CAST(func)->m_ml->ml_meth;
42+
}
43+
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
44+
# define PyCFunction_GET_FUNCTION(func) PyCFunction_GET_FUNCTION(_PyObject_CAST(func))
45+
#endif
46+
47+
static inline PyObject* PyCFunction_GET_SELF(PyObject *func_obj) {
48+
PyCFunctionObject *func = _PyCFunctionObject_CAST(func_obj);
49+
if (func->m_ml->ml_flags & METH_STATIC) {
50+
return _Py_NULL;
51+
}
52+
return func->m_self;
53+
}
54+
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
55+
# define PyCFunction_GET_SELF(func) PyCFunction_GET_SELF(_PyObject_CAST(func))
56+
#endif
57+
58+
static inline int PyCFunction_GET_FLAGS(PyObject *func) {
59+
return _PyCFunctionObject_CAST(func)->m_ml->ml_flags;
60+
}
61+
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
62+
# define PyCFunction_GET_FLAGS(func) PyCFunction_GET_FLAGS(_PyObject_CAST(func))
63+
#endif
64+
65+
static inline PyTypeObject* PyCFunction_GET_CLASS(PyObject *func_obj) {
66+
PyCFunctionObject *func = _PyCFunctionObject_CAST(func_obj);
67+
if (func->m_ml->ml_flags & METH_METHOD) {
68+
return _PyCMethodObject_CAST(func)->mm_class;
69+
}
70+
return _Py_NULL;
71+
}
72+
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
73+
# define PyCFunction_GET_CLASS(func) PyCFunction_GET_CLASS(_PyObject_CAST(func))
74+
#endif

0 commit comments

Comments
 (0)