Skip to content

Commit c11e192

Browse files
committed
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!
1 parent a995c91 commit c11e192

File tree

1 file changed

+4
-13
lines changed

1 file changed

+4
-13
lines changed

Objects/typeobject.c

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,6 @@ solid_base(PyTypeObject *type)
447447

448448
staticforward void object_dealloc(PyObject *);
449449
staticforward int object_init(PyObject *, PyObject *, PyObject *);
450-
staticforward int add_tp_new_wrapper(PyTypeObject *);
451450

452451
static PyObject *
453452
type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
@@ -672,16 +671,6 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
672671
/* Override slots that deserve it */
673672
override_slots(type, type->tp_defined);
674673

675-
/* Special hack for __new__ */
676-
if (type->tp_new == NULL) {
677-
/* Can't do this earlier, or some nasty recursion happens. */
678-
type->tp_new = PyType_GenericNew;
679-
if (add_tp_new_wrapper(type) < 0) {
680-
Py_DECREF(type);
681-
return NULL;
682-
}
683-
}
684-
685674
return (PyObject *)type;
686675
}
687676

@@ -913,7 +902,7 @@ PyTypeObject PyBaseObject_Type = {
913902
0, /* tp_dictoffset */
914903
object_init, /* tp_init */
915904
PyType_GenericAlloc, /* tp_alloc */
916-
0, /* tp_new */
905+
PyType_GenericNew, /* tp_new */
917906
object_free, /* tp_free */
918907
};
919908

@@ -1163,7 +1152,9 @@ inherit_slots(PyTypeObject *type, PyTypeObject *base)
11631152
COPYSLOT(tp_dictoffset);
11641153
COPYSLOT(tp_init);
11651154
COPYSLOT(tp_alloc);
1166-
COPYSLOT(tp_new);
1155+
if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
1156+
COPYSLOT(tp_new);
1157+
}
11671158
COPYSLOT(tp_free);
11681159
}
11691160

0 commit comments

Comments
 (0)