[Python-checkins] python/dist/src/Objects typeobject.c,2.223,2.224

gvanrossum@users.sourceforge.net gvanrossum@users.sourceforge.net
Mon, 14 Apr 2003 14:46:05 -0700


Update of /cvsroot/python/python/dist/src/Objects
In directory sc8-pr-cvs1:/tmp/cvs-serv22940/Objects

Modified Files:
	typeobject.c 
Log Message:
Close off the "Verre Carlo hack" as discussed on python-dev.


Index: typeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/typeobject.c,v
retrieving revision 2.223
retrieving revision 2.224
diff -C2 -d -r2.223 -r2.224
*** typeobject.c	14 Apr 2003 21:20:26 -0000	2.223
--- typeobject.c	14 Apr 2003 21:46:02 -0000	2.224
***************
*** 3575,3578 ****
--- 3575,3596 ----
  }
  
+ /* Helper to check for object.__setattr__ or __delattr__ applied to a type.
+    This is called the Verre Carlo 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)
***************
*** 3584,3587 ****
--- 3602,3607 ----
  	if (!PyArg_ParseTuple(args, "OO", &name, &value))
  		return NULL;
+ 	if (!hackcheck(self, func, "__setattr__"))
+ 		return NULL;
  	res = (*func)(self, name, value);
  	if (res < 0)
***************
*** 3599,3602 ****
--- 3619,3624 ----
  
  	if (!PyArg_ParseTuple(args, "O", &name))
+ 		return NULL;
+ 	if (!hackcheck(self, func, "__delattr__"))
  		return NULL;
  	res = (*func)(self, name, NULL);