New vs Old Style Python Classes in C Extensions?

"Martin v. Löwis" martin at v.loewis.de
Sat Jan 27 13:34:39 CET 2007


Jeff Rush schrieb:
> from cextension import Context
> 
> class MyContext(Context):
> 
>     def __init__(self):
>         super(Context, self).__init__()
> 
> repeatedly and reliably failed with a corrupted C data structure, while
> this:
> 
> class MyContext(Context):
> 
>     def __init__(self):
>         Context.__init__()
> 
> worked without any problems.  As I understand it, the former uses
> new-style semantics while the latter uses old-style, 

This understanding is incorrect: Context is either a new-style
or an old-style class (if type(Context) is type, it's a new-style
class, if type(context) is named 'classobj', it's an old-style
class).

As you have implemented Context in an extension module, it likely
is a new-style class (i.e. a type).

Most likely, something is wrong with your tp_init slot. You
shouldn't be able to call Context.__init__(): that should
raise a type error, indicating that an object is needed
for the method __init__. That should hold whether Context
is a new-style or an old-style class.

HTH,
Martin



More information about the Python-list mailing list