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 0aabdae..7076b36 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -447,7 +447,6 @@
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 @@
/* 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 @@
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 @@
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);
}