]> granicus.if.org Git - python/commitdiff
Thinking back to the 2.22 revision, I didn't like what I did there one
authorGuido van Rossum <guido@python.org>
Thu, 9 Aug 2001 19:38:15 +0000 (19:38 +0000)
committerGuido van Rossum <guido@python.org>
Thu, 9 Aug 2001 19:38:15 +0000 (19:38 +0000)
bit.  For one, this class:

    class C(object):
        def __new__(myclass, ...): ...

would have no way to call the __new__ method of its base class, and
the workaround (to create an intermediate base class whose __new__ you
can call) is ugly.

So, I've come up with a better solution that restores object.__new__,
but still solves the original problem, which is that built-in and
extension types shouldn't inherit object.__new__.  The solution is
simple: only "heap types" inherit tp_new.  Simpler, less code,
perfect!

Objects/typeobject.c

index 0aabdae6662f7a39e01359adced49d00bbd8ed13..7076b3631b3990734f1edeb796b3075719529301 100644 (file)
@@ -447,7 +447,6 @@ solid_base(PyTypeObject *type)
 
 staticforward void object_dealloc(PyObject *);
 staticforward int object_init(PyObject *, PyObject *, PyObject *);
-staticforward int add_tp_new_wrapper(PyTypeObject *);
 
 static PyObject *
 type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
@@ -672,16 +671,6 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
        /* Override slots that deserve it */
        override_slots(type, type->tp_defined);
 
-       /* Special hack for __new__ */
-       if (type->tp_new == NULL) {
-               /* Can't do this earlier, or some nasty recursion happens. */
-               type->tp_new = PyType_GenericNew;
-               if (add_tp_new_wrapper(type) < 0) {
-                       Py_DECREF(type);
-                       return NULL;
-               }
-       }
-
        return (PyObject *)type;
 }
 
@@ -913,7 +902,7 @@ PyTypeObject PyBaseObject_Type = {
        0,                                      /* tp_dictoffset */
        object_init,                            /* tp_init */
        PyType_GenericAlloc,                    /* tp_alloc */
-       0,                                      /* tp_new */
+       PyType_GenericNew,                      /* tp_new */
        object_free,                            /* tp_free */
 };
 
@@ -1163,7 +1152,9 @@ inherit_slots(PyTypeObject *type, PyTypeObject *base)
                COPYSLOT(tp_dictoffset);
                COPYSLOT(tp_init);
                COPYSLOT(tp_alloc);
-               COPYSLOT(tp_new);
+               if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
+                       COPYSLOT(tp_new);
+               }
                COPYSLOT(tp_free);
        }