Mercurial > cpython-lru_cache-weakref
diff Objects/typeobject.c @ 19551:01a6901cd305 legacy-trunk
Fix super() so that it is usable for static methods (like __new__) as well.
In particular, the second argument can now be a subclass of the first
as well (normally it must be an instance though).
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 29 Aug 2001 15:47:06 +0000 |
| parents | e141b392b61d |
| children | 13a93a5dcb1d |
line wrap: on
line diff
--- a/Objects/typeobject.c Wed Aug 29 15:45:32 2001 +0000 +++ b/Objects/typeobject.c Wed Aug 29 15:47:06 2001 +0000 @@ -3112,7 +3112,7 @@ typedef struct { PyObject_HEAD - PyObject *type; + PyTypeObject *type; PyObject *obj; } superobject; @@ -3136,14 +3136,28 @@ descrgetfunc f; int i, n; - mro = ((PyTypeObject *)(su->obj->ob_type))->tp_mro; + mro = su->obj->ob_type->tp_mro; assert(mro != NULL && PyTuple_Check(mro)); n = PyTuple_GET_SIZE(mro); for (i = 0; i < n; i++) { - if (su->type == PyTuple_GET_ITEM(mro, i)) + if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i)) break; } - assert(i < n); + if (i >= n && PyType_Check(su->obj)) { + mro = ((PyTypeObject *)(su->obj))->tp_mro; + assert(mro != NULL && PyTuple_Check(mro)); + n = PyTuple_GET_SIZE(mro); + for (i = 0; i < n; i++) { + if ((PyObject *)(su->type) == + PyTuple_GET_ITEM(mro, i)) + break; + } + if (i >= n) { + PyErr_SetString(PyExc_TypeError, + "bogus super object"); + return NULL; + } + } i++; res = NULL; for (; i < n; i++) { @@ -3191,16 +3205,20 @@ super_init(PyObject *self, PyObject *args, PyObject *kwds) { superobject *su = (superobject *)self; - PyObject *type, *obj = NULL; + PyTypeObject *type; + PyObject *obj = NULL; if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj)) return -1; if (obj == Py_None) obj = NULL; - if (obj != NULL && !PyType_IsSubtype(obj->ob_type, - (PyTypeObject *)type)) { + if (obj != NULL && + !PyType_IsSubtype(obj->ob_type, type) && + !(PyType_Check(obj) && + PyType_IsSubtype((PyTypeObject *)obj, type))) { PyErr_SetString(PyExc_TypeError, - "super(type, obj) requires isinstance(obj, type)"); + "super(type, obj): " + "obj must be an instance or subtype of type"); return -1; } Py_INCREF(type); @@ -3213,6 +3231,7 @@ static char super_doc[] = "super(type) -> unbound super object\n" "super(type, obj) -> bound super object; requires isinstance(obj, type)\n" +"super(type, type2) -> bound super object; requires issubclass(type2, type)\n" "Typical use to call a cooperative superclass method:\n" "class C(B):\n" " def meth(self, arg):\n"
