C API - tp_getattro and tp_methods

  • Thread starter williams.jasonscott
  • Start date
W

williams.jasonscott

Hi,

I'm using a tp_getattro function to call into a C library and get
values the the lib keeps track of. This works:
'value'

Thats great but I also want to expose some instance methods and I'm
having trouble.

But when I set tp_methods nothing shows up? If I unset tp_getattro I
can use my methods. I'm not understanding the relationship between the
two, tp_methods and tp_getattro. Could someone point me in the right
direction?

thanks~
 
G

Guest

I'm using a tp_getattro function to call into a C library and get
values the the lib keeps track of. This works: [...]

Thats great but I also want to expose some instance methods and I'm
having trouble.

But when I set tp_methods nothing shows up? If I unset tp_getattro I
can use my methods. I'm not understanding the relationship between the
two, tp_methods and tp_getattro. Could someone point me in the right
direction?

tp_getattro is like defining __getattribute__, i.e. it gets called on every
attribute read access. You can use PyObject_GenericGetAttr inside the
function to find predefined attributes before applying your own rules.

nd
 
W

williams.jasonscott

André Malo said:
tp_getattro is like defining __getattribute__, i.e. it gets called on every
attribute read access. You can use PyObject_GenericGetAttr inside the
function to find predefined attributes before applying your own rules.

Thanks for the reply. I see and was afraid of that, I don't have a
predefinded list of attributes. I want to get them from the C library
as needed. Is there another way I should be accessing the data from my
C lib since it isn't known at compile time?

thanks again~
 
G

Guest

Thanks for the reply. I see and was afraid of that, I don't have a
predefinded list of attributes. I want to get them from the C library
as needed. Is there another way I should be accessing the data from my
C lib since it isn't known at compile time?

Well, methods *are* predefined attributes (which just happen to be callable
and bound to the instance).
You can use PyObject_GenericGetAttr like this:

static PyObject *
mytype_getattro(mytypeobject *self, PyObject *name)
{
PyObject *tmp;

if (!(tmp = PyObject_GenericGetAttr((PyObject *)self, name))) {
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
return NULL;
PyErr_Clear();
}
else
return tmp;

/* your code */
}

- or -

explicitly define __getattr__ in tp_methods (instead of tp_getattro), which
only gets called on unknown attributes then.

nd
 
W

williams.jasonscott

Ahhh... The the light clicks on in my head. I see what is happening
and both of these are great approaches.

Many thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,196
Latest member
NevilleFer

Latest Threads

Top