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

Guido van Rossum gvanrossum@users.sourceforge.net
2001年8月18日 23:59:51 -0700


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv11697
Modified Files:
 Tag: r22a2-branch
	typeobject.c 
Log Message:
Address SF bug #452832.
tp_new_wrapper(): add safety test to __new__ so that you can't do
something unsafe like object.__new__(type) or
object.__new__(dictionary), while still allowing
e.g. dictionary.__new__(X) where X is a subclass of dictionary.
Also improved the other error messages in the same function to give
the type name(s) involved.
Index: typeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/typeobject.c,v
retrieving revision 2.42
retrieving revision 2.42.2.1
diff -C2 -d -r2.42 -r2.42.2.1
*** typeobject.c	2001年08月17日 21:57:47	2.42
--- typeobject.c	2001年08月19日 06:59:49	2.42.2.1
***************
*** 2092,2096 ****
 tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
 {
! 	PyTypeObject *type, *subtype;
 	PyObject *arg0, *res;
 
--- 2092,2096 ----
 tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
 {
! 	PyTypeObject *type, *subtype, *staticbase;
 	PyObject *arg0, *res;
 
***************
*** 2099,2118 ****
 	type = (PyTypeObject *)self;
 	if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
! 		PyErr_SetString(PyExc_TypeError,
! 				"T.__new__(): not enough arguments");
 		return NULL;
 	}
 	arg0 = PyTuple_GET_ITEM(args, 0);
 	if (!PyType_Check(arg0)) {
! 		PyErr_SetString(PyExc_TypeError,
! 				"T.__new__(S): S is not a type object");
 		return NULL;
 	}
 	subtype = (PyTypeObject *)arg0;
 	if (!PyType_IsSubtype(subtype, type)) {
! 		PyErr_SetString(PyExc_TypeError,
! 				"T.__new__(S): S is not a subtype of T");
 		return NULL;
 	}
 	args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
 	if (args == NULL)
--- 2099,2141 ----
 	type = (PyTypeObject *)self;
 	if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
! 		PyErr_Format(PyExc_TypeError,
! 			 "%s.__new__(): not enough arguments",
! 			 type->tp_name);
 		return NULL;
 	}
 	arg0 = PyTuple_GET_ITEM(args, 0);
 	if (!PyType_Check(arg0)) {
! 		PyErr_Format(PyExc_TypeError,
! 			 "%s.__new__(X): X is not a type object (%s)",
! 			 type->tp_name,
! 			 arg0->ob_type->tp_name);
 		return NULL;
 	}
 	subtype = (PyTypeObject *)arg0;
 	if (!PyType_IsSubtype(subtype, type)) {
! 		PyErr_Format(PyExc_TypeError,
! 			 "%s.__new__(%s): %s is not a subtype of %s",
! 			 type->tp_name,
! 			 subtype->tp_name,
! 			 subtype->tp_name,
! 			 type->tp_name);
! 		return NULL;
! 	}
! 
! 	/* Check that the use doesn't do something silly and unsafe like
! 	 object.__new__(dictionary). To do this, we check that the
! 	 most derived base that's not a heap type is this type. */
! 	staticbase = subtype;
! 	while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
! 		staticbase = staticbase->tp_base;
! 	if (staticbase != type) {
! 		PyErr_Format(PyExc_TypeError,
! 			 "%s.__new__(%s) is not safe, use %s.__new__()",
! 			 type->tp_name,
! 			 subtype->tp_name,
! 			 staticbase == NULL ? "?" : staticbase->tp_name);
 		return NULL;
 	}
+ 
 	args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
 	if (args == NULL)

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