[Python-Dev] A small step to removing the type/class split

M.-A. Lemburg mal@lemburg.com
Tue, 24 Oct 2000 21:54:39 +0200


Neil Schemenauer wrote:
> 
> I've run into a problem with ExtensionClass which I believe can
> only be fixed by modifying Python.  I will try to explain.
> 
> I have an ExtensionClass which defines __cmp__.  Depending on the
> objects being compared, the __cmp__ method may never be called.
> This is due to code in object.c that looks like this:
> 
>     if (PyInstance_Check(v) || PyInstance_Check(w)) {
>         try to use use __cmp__ method
>     }
>     else {
>         try number coerce and fall back on type name comparison
>     }
> 
> Extension classes can never pass the PyInstance_Check predicate.
> I've searched for all occurances of PyInstance_Check in the 2.0
> source.  In many places that PyInstance_Check occurs a more
> general "is this an instance-like type" check would seem to be
> more suitable.
> 
> Here is my proposal:
> 
>   * Define a new type flag Py_TPFLAGS_INSTANCE.
>   * Create a new predicate Py_IsInstance which checks for this
>     flag.
>   * Set this flag on PyInstance_Type.
>   * Replace most occurances of PyInstance_Check with
>     Py_IsInstance.
> 
> Extension types (like ExtensionClass) can then define the type
> flag Py_TPLAGS_INSTANCE and be treated as an instance type by the
> Python interpreter.  This should make it quite a bit easier to make
> extension types behave like "real" classes.
> 
> Comments?

Sounds fine, except that the "isinstance()" behaviour would
have to be defined somewhere / somehow... a mapping of slots
to __special_methods__ would be a good start.

BTW, mxProxy does some of this mapping already... you may want to 
have a look.

-- 
Marc-Andre Lemburg
______________________________________________________________________
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/