[Python-Dev] Make PyType_GenericNew and object_new the same

Jim Fulton jim at zope.com
Fri Jul 9 01:10:22 CEST 2004


At one time, object's tp_new slot was filled with PyType_GenericNew.

Later, someone decided to add a sanity check to object's new that
raises an exception if arguments are passed and __init__ was
interited from object. (object's __init__ takes no arguments.)
At this point, objects's tp_new became different from PyType_GenericNew.

Now, I have some C types that are meant to be used as mix-in
classes. I have to provide a tp_new slot filler if I want these
classes (or their subclasses) to be callable. The canonical way
to do this is to use PyType_GenericNew.  This makes the type's
tp_new different from object's, even though the intent is to have
them be basically the same.  If they are different, and you
you can run into trouble if subclasses, mixed with other base
classes try to override __new__.  You get errors like:

   TypeError: yourbaseclass.__new__(MetaInterface) is not safe,
   use object.__new__()

For reasons you don't want me to go into :)
this results from the fact that the base class __new__
and object.__new__ are different.

IMO opinion, the error checks in object_new would be just as
useful in PyType_GenericNew.  I propose one of:

1. Move the checks in object_new to PyType_GenericNew, get
    rid of object_new and fill object's tp_new slot with
    PyType_GenericNew, or

2. Make the canonical way to fill the tp_new slot of a
    type that doesn't want to provide it's own new implementation
    and that does want to be callable to just copy object's tp_new
    slot:

      MyType.tp_new = PyBaseObject_Type.tp_new;

If no one objects, I'll implement 1. Otherwise, I'll implement 2 by
updating the tutorial.

Jim

-- 
Jim Fulton           mailto:jim at zope.com       Python Powered!
CTO                  (540) 361-1714            http://www.python.org
Zope Corporation     http://www.zope.com       http://www.zope.org


More information about the Python-Dev mailing list