How Can I Capture the Class Object for a Python Class Derived From My C API Metatype?
Rob Stewart
stewart at sig.com
Fri Nov 9 16:05:05 EST 2001
I need to create a metatype, using the C API, that can save the class
objects for Python classes derived from it. I need to be able to use
the class object to instantiate objects of that class under C (C++)
program control, and to discover the methods that have been defined to
override default behavior so C++ virtual function calls can be
forwarded to Python methods.
My current, flailing (and failing) attempt was to create a metatype,
for which ob_type is set to the address of a PyTypeObject which starts
out being a copy (via memcpy()) of PyType_Type. I set the tp_new slot
of that type object (the meta-metatype, if you will) to a function
that can capture the class object and inspect it for methods. I've
been tweaking the tp_bases and tp_mro fields of that type object, but
haven't found the right combination of values to get things working.
For example, type_new() was complaining at first because my
meta-metatype wasn't its own supertype. I tried setting tp_mro to
NULL, but that wreaked havoc elsewhere. So, I created a tuple
containing just the meta-metatype. That got me farther, but I then
found that I needed to put &PyType_Type in the tuple. Unfortunately,
having done that, I merely stumble upon other problems.
The point is, the approach I'm taking is a hack, and it's brittle,
assuming I can even make it work. That makes it highly undesirable.
Hopefully, then, there's a better way to go about this.
Thanks for any help you can offer.
Rob
More information about the Python-list
mailing list