[Zope-dev] Subclassing ExtensionClass in C (this time with a solution)

Michel Pelletier michel@digicool.com
Thu, 02 Mar 2000 22:13:38 -0800


James Henstridge wrote:
> 
> Last week I asked a question about subclassing ExtensionClasses in C, but
> no one seemed to know how to do it.  I then tried asking Jim Fulton about
> it, but he hasn't responded yet.

He's on vacation.  Get in line. ;)
 
> So I decided to sit down and try to add the functionality myself.  I am
> posting it here as some people on the list expressed an interest.  I have
> tried to keep to the style of the current code, so those who have used
> ExtensionClass should be able to do Extension Subclasses in C without too
> much problem.  Also, the modification is backward compatible (both source
> and binary), so it shouldn't break any current code.

Ok, good.
 
> The one place where the programmer has to be careful is in making the
> size and layout of the instance structure of the new class is compatible
> with its base classes.  I didn't include the checks done when subclassing
> from python, as you may want to have more than one non-pure mixin base
> class, or to extend the size of the instance structure in the subclass.
> 
> To define a new Extension Subclass, follow the same steps as for a normal
> ExtensionClass, but instead of using PyExtensionClass_Export, use
> PyExtensionClass_ExportSubclass.  This new function is similar to
> PyExtensionClass_Export, except that it takes an extra argument -- a tuple
> of base classes.  The function takes ownership of the tuple.
> 
> In cases where you only want to do single inheritance, there is a
> convenience routine called PyExtensionClass_ExportSubclassSingle.  Its
> last argument is the PyExtensionClass structure for the base class.
> 
> I have also attached a test module called TESC that tests out subclassing
> in C.  Running it gives output something like:
> >>> import TESC
> >>> a = TESC.Cls()
> >>> b = TESC.Subcls()
> >>> a.m1(), a.m2()
> ('m1 of Cls called', 'm2 of Cls called')
> >>> b.m1(), b.m2(), b.m3()
> ('m1 of Subcls called', 'm2 of Cls called', 'm3 of Subcls called')
> 
> I have tested this module a fair amount, and it doesn't seem to introduce
> any problems with existing code, and is a fairly clean modification (I
> could have made the change a little cleaner, but that would have broken
> binary compatibility).  So I was wondering if my patch could be integrated
> into the official ExtensionClass distribution.  I am sure it is of use to
> more people than just me.

This will have to go by Jim, of course.  So far it sounds good to me,
but what do I know (hint, not much).

-Michel