[Python-Dev] Subclassing in 'C'

Ronald Oussoren ronaldoussoren at mac.com
Sun Jun 26 15:26:35 CEST 2005


On 23-jun-2005, at 21:05, Robert W. Johnstone wrote:

> Hello,
>
> I'm trying to develop an extension module in C, but am having some
> difficulties.  I thought about posting to the general python mailling
> list, but since it has to do with the C API I figured this was the  
> correct
> location.

The python list would have been the right location.

>
> My difficulty lies with subclassing a class provided by another  
> external
> module.  I have access to the structure definition, so I can  
> determine the
> size fo the tp_basicsize member of the PyTypeObject structure.  What I
> can't seem to determine is value for the tp_base element.
>
> This is my current "best guess".
>
>     PyObject* superclass;
>     superclass = PyObject_GetAttrString( module, "SuperClass" );
>     if ( !superclass ) return;
>     type.tp_base = superclass->ob_type;

type.tp_base = (PyTypeObject*)superclass;

>     Py_DECREF( superclass );
And don't decref the superclass. You may want to add a typecheck that
checks if the superclass is a type.

Ronald


More information about the Python-Dev mailing list