Accessing __slots__ from C

Chris ceball at users.sourceforge.net
Fri Sep 12 08:25:05 EDT 2008


Carl Banks <pavlovevidence <at> gmail.com> writes:
...
> You can determine the offset the of the slot in the object structure
> by
> querying the member descriptor of the type object.

That sounds like just the kind of thing we were looking for - thanks!

> 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'")?

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

where x is the class and attr_one is a slot (the full example is
appended to this message).  I guessed the type of offset; I'm not sure
what it should be.

I couldn't find any information about d_member on the web.


> There might be some macros to simplify this.

Sorry to say that I also have no idea about where to find such macros!
Maybe I should continue this thread on capi-sig?


Thanks for your help,
Chris


class MyObject(object):

  __slots__ = ['attr_one']

  def __init__(self,attr_one=1.0):
      self.attr_one = attr_one

import weave
def test():

  x = MyObject

  code = """
  PyObject *descr = PyObject_GetAttrString(x,"attr_one");
  int offset = descr->d_member->offset;
  //PyObject* slotvar = (PyObject*)(((char*)obj)+offset);
  """
  weave.inline(code,['x'],local_dict=locals(),verbose=1)

test()





More information about the Python-list mailing list