Accessing __slots__ from C

Hrvoje Niksic hniksic at
Fri Sep 12 16:01:45 CEST 2008

Chris <ceball at> writes:

>> descr = GetAttrString(cls,"varname");
>> offset = descr->d_member->offset;
>> slotvar = (PyObject*)(((char*)obj)+offset)
> Unfortunately, I am inexperienced at this kind of thing, so I wasn't
> able to get something working. Maybe someone could tell me what's
> wrong with the code below (it gives the error "'struct _object' has no
> member named 'd_member'")?

You are getting that error because Carl forgot to cast the descriptor
to the appropriate C type, in this case PyMemberDescrObject.  The last
line is also incorrect, I think.  Try something like this:

  PyObject *descr = PyObject_GetAttrString(x,"attr_one");
  int offset = ((PyMemberDescrObject *) descr)->d_member->offset;
  PyObject *slotvar = *(PyObject **)(((char *) obj) + offset);

More information about the Python-list mailing list