@@ -26,12 +26,20 @@ PyAPI_FUNC(PyObject *) PyMethod_New(PyObject *, PyObject *);
26
26
PyAPI_FUNC (PyObject * ) PyMethod_Function (PyObject * );
27
27
PyAPI_FUNC (PyObject * ) PyMethod_Self (PyObject * );
28
28
29
- /* Macros for direct access to these values. Type checks are *not*
30
- done, so use with care. */
31
- #define PyMethod_GET_FUNCTION (meth ) \
32
- (((PyMethodObject *)meth) -> im_func)
33
- #define PyMethod_GET_SELF (meth ) \
34
- (((PyMethodObject *)meth) -> im_self)
29
+ #define _PyMethod_CAST (meth ) \
30
+ (assert(PyMethod_Check(meth)), _Py_CAST(PyMethodObject*, meth))
31
+
32
+ /* Static inline functions for direct access to these values.
33
+ Type checks are *not* done, so use with care. */
34
+ static inline PyObject * PyMethod_GET_FUNCTION (PyObject * meth ) {
35
+ return _PyMethod_CAST (meth )-> im_func ;
36
+ }
37
+ #define PyMethod_GET_FUNCTION (meth ) PyMethod_GET_FUNCTION(_PyObject_CAST(meth))
38
+
39
+ static inline PyObject * PyMethod_GET_SELF (PyObject * meth ) {
40
+ return _PyMethod_CAST (meth )-> im_self ;
41
+ }
42
+ #define PyMethod_GET_SELF (meth ) PyMethod_GET_SELF(_PyObject_CAST(meth))
35
43
36
44
typedef struct {
37
45
PyObject_HEAD
@@ -45,10 +53,16 @@ PyAPI_DATA(PyTypeObject) PyInstanceMethod_Type;
45
53
PyAPI_FUNC (PyObject * ) PyInstanceMethod_New (PyObject * );
46
54
PyAPI_FUNC (PyObject * ) PyInstanceMethod_Function (PyObject * );
47
55
48
- /* Macros for direct access to these values. Type checks are *not*
49
- done, so use with care. */
50
- #define PyInstanceMethod_GET_FUNCTION (meth ) \
51
- (((PyInstanceMethodObject *)meth) -> func)
56
+ #define _PyInstanceMethod_CAST (meth ) \
57
+ (assert(PyInstanceMethod_Check(meth)), \
58
+ _Py_CAST(PyInstanceMethodObject*, meth))
59
+
60
+ /* Static inline function for direct access to these values.
61
+ Type checks are *not* done, so use with care. */
62
+ static inline PyObject * PyInstanceMethod_GET_FUNCTION (PyObject * meth ) {
63
+ return _PyInstanceMethod_CAST (meth )-> func ;
64
+ }
65
+ #define PyInstanceMethod_GET_FUNCTION (meth ) PyInstanceMethod_GET_FUNCTION(_PyObject_CAST(meth))
52
66
53
67
#ifdef __cplusplus
54
68
}
0 commit comments