I am using a module written in C++ and want to get all data descriptors of an object.
Doing the following, the list descriptors
is empty.
import inspect
from imported_cpp_module import A
a = A()
descriptors = [
(desc, obj)
for desc, obj in inspect.getmembers(a)
if inspect.isdatadescriptor(obj)
]
I can see in the implementation of imported_cpp_module
that the members that I want, aaa
and bbb
, are in tp_getset
. How to distinguish these from, say __class__
, __delattr__
, __dir__
etc?
PyGetSetDef
A_PyObj::s_getset[] =
{
{"aaa", (getter)aaa_getter, (setter)aaa_setter, "aaa <-> str. Sets or queries the aaa member", nullptr}
, {"bbb", (getter)bbb_getter, nullptr, "bbb -> int. Returns the bbb member", nullptr}
, {NULL}
};
PyTypeObject
A_PyObj::s_PyTypeDef =
{
.ob_base = PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "A"
, .tp_basicsize = sizeof(A)
, .tp_dealloc = (destructor)dealloc
, .tp_flags = Py_TPFLAGS_DEFAULT
, .tp_doc = "An A object with members aaa and bbb"
, .tp_getset = s_getset
, .tp_base = &Base_PyObj::s_PyTypeDef
};