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

Neil Schemenauer nas@arctrix.com
Tue, 24 Oct 2000 04:56:11 -0700


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?

  Neil