C Python API: check for PyMethodDescr_Type?

I’m inspecting a PyObject *obj (within the mpi4py module) by printing Py_TYPE(obj), which yields:

Type=<class 'method_descriptor'>

Keying in on the “method_descriptor” string and looking at CPython’s source code, obj appears to be a PyMethodDescrObject object, whose type is PyMethodDescr_Type.

For other types, there are C Python API calls to check whether or not obj is of that type, e.g. PyCFunction_Check, PyInstanceMethod_Check, etc. Is there a corresponding function to check whether obj is of type PyMethodDescr_Type? If not, what is the recommended way to do this?

Py_TYPE(obj) == PyMethodDescr_Type

This type is not subclassable.


So this is the recommended way to do things?

Py_TYPE(obj) == &PyMethodDescr_Type

I didn’t see any documentation on the type, so its not clear whether it was even meant to be exposed. Was PyMethodDescr_Type superseded by PyInstanceMethod or something? Its unclear to me their relationship

PyMethodDescr_Type is exposed as types.MethodDescriptorType in Python.

PyInstanceMethod_Type currently is not used.

1 Like