[Python-checkins] python/dist/src/Objects typeobject.c,2.126.4.33,2.126.4.34

gvanrossum@users.sourceforge.net gvanrossum@users.sourceforge.net
2003年4月24日 22:40:37 -0700


Update of /cvsroot/python/python/dist/src/Objects
In directory sc8-pr-cvs1:/tmp/cvs-serv25071/Objects
Modified Files:
 Tag: release22-maint
	typeobject.c 
Log Message:
Backport the Carlo Verre fix.
Index: typeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/typeobject.c,v
retrieving revision 2.126.4.33
retrieving revision 2.126.4.34
diff -C2 -d -r2.126.4.33 -r2.126.4.34
*** typeobject.c	23 Mar 2003 14:36:50 -0000	2.126.4.33
--- typeobject.c	25 Apr 2003 05:40:32 -0000	2.126.4.34
***************
*** 2595,2598 ****
--- 2595,2616 ----
 }
 
+ /* Helper to check for object.__setattr__ or __delattr__ applied to a type.
+ This is called the Carlo Verre hack after its discoverer. */
+ static int
+ hackcheck(PyObject *self, setattrofunc func, char *what)
+ {
+ 	PyTypeObject *type = self->ob_type;
+ 	while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)
+ 		type = type->tp_base;
+ 	if (type->tp_setattro != func) {
+ 		PyErr_Format(PyExc_TypeError,
+ 			 "can't apply this %s to %s object",
+ 			 what,
+ 			 type->tp_name);
+ 		return 0;
+ 	}
+ 	return 1;
+ }
+ 
 static PyObject *
 wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
***************
*** 2604,2607 ****
--- 2622,2627 ----
 	if (!PyArg_ParseTuple(args, "OO", &name, &value))
 		return NULL;
+ 	if (!hackcheck(self, func, "__setattr__"))
+ 		return NULL;
 	res = (*func)(self, name, value);
 	if (res < 0)
***************
*** 2619,2622 ****
--- 2639,2644 ----
 
 	if (!PyArg_ParseTuple(args, "O", &name))
+ 		return NULL;
+ 	if (!hackcheck(self, func, "__delattr__"))
 		return NULL;
 	res = (*func)(self, name, NULL);

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