470,594 Members | 1,444 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,594 developers. It's quick & easy.

C API - tp_getattro and tp_methods

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:
o = obj.Obj()
o.var

'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~

Apr 24 '06 #1
4 2744
* wi*****************@gmail.com wrote:
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
--
"Die Untergeschosse der Sempergalerie bleiben währenddessen aus
statistischen Gründen geflutet." -- Spiegel Online
Apr 24 '06 #2

Andr Malo wrote:
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~

Apr 24 '06 #3
* wi*****************@gmail.com wrote:
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?


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
--
die (eval q-qq:Just Another Perl Hacker
:-)

# André Malo, <http://pub.perlig.de/> #
Apr 24 '06 #4
Ahhh... The the light clicks on in my head. I see what is happening
and both of these are great approaches.

Many thanks!

Apr 24 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.