473,387 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 2861
* 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: James S | last post by:
Hi, Basically I've been fighting with this code for a few days now and can't seem to work around this problem. Included is the output, the program I use to get this error and the source code for...
11
by: Iker Arizmendi | last post by:
Hello all, I've defined a class in a C extension module that is (or rather, I would like to be) a subclass of another class. I do this by simply setting the tp_base pointer of my subclass's...
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
0
by: Fabiano Sidler | last post by:
Hello list! Writing a C extension module, which way is better to implement attribute retrieval, by a getattr function in the PyTypeObject struct or by an entry tp_methods? I don't need any black...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.