wlav / CPyCppyy

Other
23 stars 20 forks source link

Prevent `cppyy.InstanceMethod` from crashing due to Python 3.12 asserts #9

Closed guitargeek closed 10 months ago

guitargeek commented 11 months ago

Since Python 3.12, in the implementation of 'classobject.h' the function PyMethod_GET_SELF performs an assert to check that the passed function argument is a method:

\#define _PyMethod_CAST(meth) \
    (assert(PyMethod_Check(meth)), _Py_CAST(PyMethodObject*, meth))
[...]
static inline PyObject* PyMethod_GET_SELF(PyObject *meth) {
    return _PyMethod_CAST(meth)->im_self;
}

It's fair that the assert fails, because the Python type of meth in this context is not a PyMethod_Type, but the CustomInstanceMethod_Type from cppyy. However, as can be seen in the implementation of CustomInstanceMethod_New, the actual C++ type that implements this custom cppy type is just the regular PyMethodObject.

Hence, this commit suggests new assert-free CustomInstanceMethod_GET_* macros that replace the PyMethod_GET_* macros in the context of the CustomInstanceMethod implementation.

wlav commented 10 months ago

Thanks!