diff options
author | Guido van Rossum <guido@python.org> | 2001-08-09 19:38:15 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2001-08-09 19:38:15 (GMT) |
commit | c11e192d416e2970e6a06cf06d4cf788f322c6ea (patch) | |
tree | 2f7ddcb418dcc70bd1ac92f618ac2275d7823a1f | |
parent | a995c91243fe1980551b81b116ec93bb9a38a974 (diff) | |
download | cpython-c11e192d416e2970e6a06cf06d4cf788f322c6ea.zip cpython-c11e192d416e2970e6a06cf06d4cf788f322c6ea.tar.gz cpython-c11e192d416e2970e6a06cf06d4cf788f322c6ea.tar.bz2 |
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!
-rw-r--r-- | Objects/typeobject.c | 17 |
1 files changed, 4 insertions, 13 deletions
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 @@ 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); } |