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

Guido van Rossum gvanrossum@users.sourceforge.net
Tue, 05 Jun 2001 04:41:25 -0700


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

Modified Files:
      Tag: descr-branch
	typeobject.c 
Log Message:
Modernize type_init() -- it was still the old "constructor" code,
returning NULL for errors instead -1.  This was unfortunately masked
by a cast.


Index: typeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/typeobject.c,v
retrieving revision 2.16.8.26
retrieving revision 2.16.8.27
diff -C2 -r2.16.8.26 -r2.16.8.27
*** typeobject.c	2001/06/05 10:49:24	2.16.8.26
--- typeobject.c	2001/06/05 11:41:23	2.16.8.27
***************
*** 147,155 ****
  
  /* TypeType's constructor is called when a type is subclassed */
! static PyObject *
! type_init(PyTypeObject *type, PyObject *args, PyObject *kwds)
  {
  	PyObject *name, *bases, *dict, *x, *slots;
! 	PyTypeObject *base;
  	char *dummy = NULL;
  	etype *et;
--- 147,155 ----
  
  /* TypeType's constructor is called when a type is subclassed */
! static int
! type_init(PyObject *self, PyObject *args, PyObject *kwds)
  {
  	PyObject *name, *bases, *dict, *x, *slots;
! 	PyTypeObject *type, *base;
  	char *dummy = NULL;
  	etype *et;
***************
*** 157,178 ****
  	int i, nslots, slotoffset, allocsize;
  
  	/* Check arguments */
  	if (!PyArg_ParseTupleAndKeywords(args, kwds, "SOO", &dummy,
  					 &name, &bases, &dict))
! 		return NULL;
  	if (!PyTuple_Check(bases) || !PyDict_Check(dict)) {
  		PyErr_SetString(PyExc_TypeError,
  				"usage: TypeType(name, bases, dict) ");
! 		return NULL;
  	}
  	if (PyTuple_GET_SIZE(bases) > 1) {
  		PyErr_SetString(PyExc_TypeError,
  				"can't multiple-inherit from types");
! 		return NULL;
  	}
  	if (PyTuple_GET_SIZE(bases) < 1) {
  		PyErr_SetString(PyExc_TypeError,
  				"can't create a new type without a base type");
! 		return NULL;
  	}
  	base = (PyTypeObject *)PyTuple_GET_ITEM(bases, 0);
--- 157,181 ----
  	int i, nslots, slotoffset, allocsize;
  
+ 	assert(PyType_Check(self));
+ 	type = (PyTypeObject *)self;
+ 
  	/* Check arguments */
  	if (!PyArg_ParseTupleAndKeywords(args, kwds, "SOO", &dummy,
  					 &name, &bases, &dict))
! 		return -1;
  	if (!PyTuple_Check(bases) || !PyDict_Check(dict)) {
  		PyErr_SetString(PyExc_TypeError,
  				"usage: TypeType(name, bases, dict) ");
! 		return -1;
  	}
  	if (PyTuple_GET_SIZE(bases) > 1) {
  		PyErr_SetString(PyExc_TypeError,
  				"can't multiple-inherit from types");
! 		return -1;
  	}
  	if (PyTuple_GET_SIZE(bases) < 1) {
  		PyErr_SetString(PyExc_TypeError,
  				"can't create a new type without a base type");
! 		return -1;
  	}
  	base = (PyTypeObject *)PyTuple_GET_ITEM(bases, 0);
***************
*** 180,189 ****
  		PyErr_SetString(PyExc_TypeError,
  				"base type must be a type");
! 		return NULL;
  	}
  	if (base->tp_init == NULL) {
  		PyErr_SetString(PyExc_TypeError,
  				"base type must have a constructor slot");
! 		return NULL;
  	}
  
--- 183,192 ----
  		PyErr_SetString(PyExc_TypeError,
  				"base type must be a type");
! 		return -1;
  	}
  	if (base->tp_init == NULL) {
  		PyErr_SetString(PyExc_TypeError,
  				"base type must have a constructor slot");
! 		return -1;
  	}
  
***************
*** 198,202 ****
  			slots = PySequence_Tuple(slots);
  		if (slots == NULL)
! 			return NULL;
  		nslots = PyTuple_GET_SIZE(slots);
  		for (i = 0; i < nslots; i++) {
--- 201,205 ----
  			slots = PySequence_Tuple(slots);
  		if (slots == NULL)
! 			return -1;
  		nslots = PyTuple_GET_SIZE(slots);
  		for (i = 0; i < nslots; i++) {
***************
*** 205,209 ****
  				"__slots__ must be a sequence of strings");
  				Py_DECREF(slots);
! 				return NULL;
  			}
  		}
--- 208,212 ----
  				"__slots__ must be a sequence of strings");
  				Py_DECREF(slots);
! 				return -1;
  			}
  		}
***************
*** 214,239 ****
  		nslots = 1;
  
! 	/* Allocate memory and construct a type object in it */
  	allocsize = sizeof(etype) + nslots*sizeof(struct memberlist);
! 	if (type == NULL) {
! 		et = PyObject_MALLOC(allocsize);
! 		if (et == NULL)
! 			return NULL;
! 		memset(et, '\0', allocsize);
! 		type = &et->type;
! 		PyObject_INIT(type, &PyType_Type);
! 	}
! 	else {
! 		if (type->ob_type->tp_basicsize < allocsize) {
! 			PyErr_Format(
! 				PyExc_SystemError,
! 				"insufficient allocated memory for subtype: "
! 				"allocated %d, needed %d",
! 				type->ob_type->tp_basicsize,
! 				allocsize);
! 			return NULL;
! 		}
! 		et = (etype *)type;
  	}
  	Py_INCREF(name);
  	et->name = name;
--- 217,232 ----
  		nslots = 1;
  
! 	/* Check allocation size and initialize the type object */
  	allocsize = sizeof(etype) + nslots*sizeof(struct memberlist);
! 	if (type->ob_type->tp_basicsize < allocsize) {
! 		PyErr_Format(
! 			PyExc_SystemError,
! 			"insufficient allocated memory for subtype: "
! 			"allocated %d, needed %d",
! 			type->ob_type->tp_basicsize,
! 			allocsize);
! 		return -1;
  	}
+ 	et = (etype *)type;
  	Py_INCREF(name);
  	et->name = name;
***************
*** 251,255 ****
  	if (PyType_InitDict(type) < 0) {
  		Py_DECREF(type);
! 		return NULL;
  	}
  
--- 244,248 ----
  	if (PyType_InitDict(type) < 0) {
  		Py_DECREF(type);
! 		return -1;
  	}
  
***************
*** 293,301 ****
  	if (x == NULL) {
  		Py_DECREF(type);
! 		return NULL;
  	}
  	Py_DECREF(x); /* throw away None */
  	override_slots(type, dict);
! 	return (PyObject *)type;
  }
  
--- 286,294 ----
  	if (x == NULL) {
  		Py_DECREF(type);
! 		return -1;
  	}
  	Py_DECREF(x); /* throw away None */
  	override_slots(type, dict);
! 	return 0;
  }
  
***************
*** 353,357 ****
  	0,					/* tp_descr_set */
  	offsetof(PyTypeObject, tp_dict),	/* tp_dictoffset */
! 	(initproc)type_init,			/* tp_init */
  	PyType_GenericAlloc,			/* tp_alloc */
  	PyType_GenericNew,			/* tp_new */
--- 346,350 ----
  	0,					/* tp_descr_set */
  	offsetof(PyTypeObject, tp_dict),	/* tp_dictoffset */
! 	type_init,				/* tp_init */
  	PyType_GenericAlloc,			/* tp_alloc */
  	PyType_GenericNew,			/* tp_new */