[Python-checkins] CVS: python/dist/src/Objects descrobject.c,2.6,2.7

Guido van Rossum gvanrossum@users.sourceforge.net
Fri, 24 Aug 2001 08:23:22 -0700


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

Modified Files:
	descrobject.c 
Log Message:
Change the getset type to take an optional third function argument:
the delete function.  (Question: should the attribute name also be
recorded in the getset object?  That makes the protocol more work, but
may give us better error messages.)


Index: descrobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/descrobject.c,v
retrieving revision 2.6
retrieving revision 2.7
diff -C2 -d -r2.6 -r2.7
*** descrobject.c	2001/08/24 10:17:36	2.6
--- descrobject.c	2001/08/24 15:23:20	2.7
***************
*** 869,872 ****
--- 869,873 ----
  	PyObject *get;
  	PyObject *set;
+ 	PyObject *del;
  } getsetobject;
  
***************
*** 878,881 ****
--- 879,883 ----
  	Py_XDECREF(gs->get);
  	Py_XDECREF(gs->set);
+ 	Py_XDECREF(gs->del);
  	self->ob_type->tp_free(self);
  }
***************
*** 901,914 ****
  {
  	getsetobject *gs = (getsetobject *)self;
! 	PyObject *res;
  
! 	if (gs->set == NULL) {
! 		PyErr_SetString(PyExc_AttributeError, "unsettable attribute");
  		return -1;
  	}
  	if (value == NULL)
! 		res = PyObject_CallFunction(gs->set, "(O)", obj);
  	else
! 		res = PyObject_CallFunction(gs->set, "(OO)", obj, value);
  	if (res == NULL)
  		return -1;
--- 903,923 ----
  {
  	getsetobject *gs = (getsetobject *)self;
! 	PyObject *func, *res;
  
! 	if (value == NULL)
! 		func = gs->del;
! 	else
! 		func = gs->set;
! 	if (func == NULL) {
! 		PyErr_SetString(PyExc_AttributeError,
! 				value == NULL ?
! 				"can't delete attribute" :
! 				"can't set attribute");
  		return -1;
  	}
  	if (value == NULL)
! 		res = PyObject_CallFunction(func, "(O)", obj);
  	else
! 		res = PyObject_CallFunction(func, "(OO)", obj, value);
  	if (res == NULL)
  		return -1;
***************
*** 920,927 ****
  getset_init(PyObject *self, PyObject *args, PyObject *kwds)
  {
! 	PyObject *get = NULL, *set = NULL;
  	getsetobject *gs = (getsetobject *)self;
  
! 	if (!PyArg_ParseTuple(args, "|OO:getset.__init__", &get, &set))
  		return -1;
  	if (get == Py_None)
--- 929,936 ----
  getset_init(PyObject *self, PyObject *args, PyObject *kwds)
  {
! 	PyObject *get = NULL, *set = NULL, *del = NULL;
  	getsetobject *gs = (getsetobject *)self;
  
! 	if (!PyArg_ParseTuple(args, "|OOO:getset.__init__", &get, &set, &del))
  		return -1;
  	if (get == Py_None)
***************
*** 931,946 ****
  	Py_XINCREF(get);
  	Py_XINCREF(set);
  	gs->get = get;
  	gs->set = set;
  	return 0;
  }
  
  static char getset_doc[] =
! "getset([getfunc[, setfunc]]) -> getset attribute\n"
  "Typical use to define a managed attribute x of C instances:\n"
  "class C(object):\n"
  "    def getx(self): return self.__x\n"
  "    def setx(self, value): self.__x = value\n"
! "    x = getset(getx, setx)";
  
  PyTypeObject PyGetSet_Type = {
--- 940,958 ----
  	Py_XINCREF(get);
  	Py_XINCREF(set);
+ 	Py_XINCREF(del);
  	gs->get = get;
  	gs->set = set;
+ 	gs->del = del;
  	return 0;
  }
  
  static char getset_doc[] =
! "getset([getfunc[, setfunc[, delfunc]]]) -> getset attribute\n"
  "Typical use to define a managed attribute x of C instances:\n"
  "class C(object):\n"
  "    def getx(self): return self.__x\n"
  "    def setx(self, value): self.__x = value\n"
! "    def delx(self): del self.__x\n"
! "    x = getset(getx, setx, delx)";
  
  PyTypeObject PyGetSet_Type = {