From: Guido van Rossum Date: Thu, 9 Aug 2001 19:38:15 +0000 (+0000) Subject: Thinking back to the 2.22 revision, I didn't like what I did there one X-Git-Tag: v2.2a3~702 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c11e192d416e2970e6a06cf06d4cf788f322c6ea;p=python Thinking back to the 2.22 revision, I didn't like what I did there one 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! --- diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 0aabdae666..7076b3631b 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -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); }