Subclassing in C

Michael Hudson mwh at python.net
Wed Sep 15 12:30:24 EDT 2004


Iker Arizmendi <iker at research.att.com> writes:

> Michael Hudson wrote:
> > OK, some questions.
> > 1) You must be assuming 2.2 or later, right?  tp_base doesn't make
> >    sense before then.
> 
> Yup, 2.2 or later (although I'm currently using 2.3).
> 
> > 2) Is this a third party base type?
> 
> Not really, it's written by another fellow here. But
> I have access to the source and am free to make small
> changes.

OK, then I suggest you start by updating this code to the 2.2 era...

Have you read

http://www.python.org/doc/ext/defining-new-types.html

?  If not, you definitely want to.

> > 3) If 2) is the third party assuming 2.2 or later?
> 
> I think it was written before 2.2, but I've made some
> changes that I think make it 2.2 compliant (eg, I added
> the Py_TPFLAGS_BASETYPE flag, prepared the type with
> PyType_Ready, and changed it to use tp_getattro instead
> of tp_getattr.

In the 2.2 era, tp_getattro should almost invariably be
PyObject_GenericGetAttr.  Fill out the tp_methods, tp_members, etc
slots of the base type.

> > If 2) but not 3), you might be in for some hacking (as you later came
> > up with).  You can't really subclass (cleanly) a type that's totally
> > unprepared for it.
> 
> So I guess I have (!2 && !3), but I think I've managed
> to change that to (!2 && 3). So assuming I have two types,
> both written in C, what's the clean way to do the
> subclassing? In particular, it would be nice if the C API
> did the search of my bases for me.

That's what PyObject_GenericGetAttr does.

> > Py_FindMethod() is SO 2001 :-)
> 
> Just got started with Python and put on the first thing
> I found. Didn't realize they were bell bottoms :)

Fair enough :)

> Is there a newer method?

See above.

Cheers,
mwh

-- 
  ARTHUR:  Ford, you're turning into a penguin, stop it.
                    -- The Hitch-Hikers Guide to the Galaxy, Episode 2



More information about the Python-list mailing list