Accessing __slots__ from C

Carl Banks pavlovevidence at gmail.com
Fri Sep 12 23:46:41 CEST 2008


On Sep 12, 10:01 am, Hrvoje Niksic <hnik... at xemacs.org> wrote:
> Chris <ceb... at users.sourceforge.net> 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.

Yep, was in too much of a hurry to waste too much time showing how to
do something slightly dubious.

The offsetof() macro would help for the third line (in fact, it's the
recommended standard way since there are some C implmentions,
somewhere, that the pointer artithmetic method fails).


Carl Banks



More information about the Python-list mailing list