amh at BSD-DK.dk
Mon Sep 10 18:12:01 CEST 2001
I am stuck in a problem concerning python extensions. I am quite new to
python and will try to describe my problem as precisely as possible.
I want to export several C++ classes to python. I succeded for most of the
functionalities I want, but for one: inheritance. That is, I would like the wrapper to class A
to be able to access function in class B if B is a base of A. Also, I would like
a function accepting B as argument to be able to accept A. For that I need inheritance
information for my wrappers to use.
I can see easy ways to do that, but I guess there is a 'supported' way of doing
thoses things. I saw in the PyTypeObject structure a zone reserved to 'Attribute
descriptor and subclassing stuff' (as the comment says, Include/object.h l.275).
To be precise, the fields tp_base and tp_bases seems to be what I need.
Is it possible to set A's tp_base (tp_bases) to point to B so that magically if an attribute
is not found by A's getattr(), python will call B's getattr() ?
Is it possible to set A's tp_base (tp_bases?) to point to B so that my hand written
getattr() for A will also call any object found in A's tp_base ?
What are those fields used for ?
Also, looking through the python sources, I found a PyClass_Type definition.
As of now, my wrappers declare new instances of PyTypeObject (one per wrapped class)
and in the init function of the module, I explicitly set the ob_type field to PyType_Type.
Could not I set it to PyClass_Type and benefit from new python functionalities ?
Please keep in mind that I don't master python at all and that the python internals
are even more obscure to me.
(I am not part of the mailing list, so please CC me in replies)
More information about the Python-list