[Python-checkins] CVS: python/dist/src/Objects typeobject.c,2.49,2.50

Guido van Rossum gvanrossum@users.sourceforge.net
2001年8月29日 08:47:08 -0700


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv32144
Modified Files:
	typeobject.c 
Log Message:
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).
Index: typeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/typeobject.c,v
retrieving revision 2.49
retrieving revision 2.50
diff -C2 -d -r2.49 -r2.50
*** typeobject.c	2001年08月28日 18:28:21	2.49
--- typeobject.c	2001年08月29日 15:47:06	2.50
***************
*** 3113,3117 ****
 typedef struct {
 	PyObject_HEAD
! 	PyObject *type;
 	PyObject *obj;
 } superobject;
--- 3113,3117 ----
 typedef struct {
 	PyObject_HEAD
! 	PyTypeObject *type;
 	PyObject *obj;
 } superobject;
***************
*** 3137,3148 ****
 		int i, n;
 
! 		mro = ((PyTypeObject *)(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))
 				break;
 		}
! 		assert(i < n);
 		i++;
 		res = NULL;
--- 3137,3162 ----
 		int i, n;
 
! 		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 ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
 				break;
 		}
! 		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;
***************
*** 3192,3196 ****
 {
 	superobject *su = (superobject *)self;
! 	PyObject *type, *obj = NULL;
 
 	if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
--- 3206,3211 ----
 {
 	superobject *su = (superobject *)self;
! 	PyTypeObject *type;
! 	PyObject *obj = NULL;
 
 	if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
***************
*** 3198,3205 ****
 	if (obj == Py_None)
 		obj = NULL;
! 	if (obj != NULL && !PyType_IsSubtype(obj->ob_type,
! 					 (PyTypeObject *)type)) {
 		PyErr_SetString(PyExc_TypeError,
! 			"super(type, obj) requires isinstance(obj, type)");
 		return -1;
 	}
--- 3213,3223 ----
 	if (obj == Py_None)
 		obj = NULL;
! 	if (obj != NULL &&
! 	 !PyType_IsSubtype(obj->ob_type, type) &&
! 	 !(PyType_Check(obj) &&
! 	 PyType_IsSubtype((PyTypeObject *)obj, type))) {
 		PyErr_SetString(PyExc_TypeError,
! 			"super(type, obj): "
! 			"obj must be an instance or subtype of type");
 		return -1;
 	}
***************
*** 3214,3217 ****
--- 3232,3236 ----
 "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"

AltStyle によって変換されたページ (->オリジナル) /