[Python-checkins] CVS: python/dist/src/Objects dictobject.c,2.80.2.6,2.80.2.7 funcobject.c,2.37.4.3,2.37.4.4 listobject.c,2.92.6.4,2.92.6.5 moduleobject.c,2.31.6.2,2.31.6.3 typeobject.c,2.16.8.25,2.16.8.26

Guido van Rossum gvanrossum@users.sourceforge.net
Tue, 05 Jun 2001 03:49:26 -0700


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

Modified Files:
      Tag: descr-branch
	dictobject.c funcobject.c listobject.c moduleobject.c 
	typeobject.c 
Log Message:
Redo object creation; touching many files.

- Get rid of tp_construct, it had a bogus interface (this moves
  tp_dictoffset one slot up).

- New tp_ slots; the last two are "type methods" (their first
  argument is a type object, not an instance of that type):

  - tp_init is what tp_construct wanted to be, without the allocation;

  - tp_alloc does low-level allocation, initializing the object to its
    most basic form (up to and including registering it with the GC
    machinery);

  - tp_new does high-level object creation: it calls tp_alloc and then
    tp_init.

- New generic functions PyType_GenericAlloc() and PyType_GenericNew()
  provide default implementations for tp_alloc and tp_new that are
  usually sufficient.

- Used the above things to make standard list, dict and module objects
  subtypable as before.

- Remove tp_construct initializer spacer from funcobject.c.

- Add an __init__() override test to test_descr.py.



Index: dictobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/dictobject.c,v
retrieving revision 2.80.2.6
retrieving revision 2.80.2.7
diff -C2 -r2.80.2.6 -r2.80.2.7
*** dictobject.c	2001/05/22 04:00:17	2.80.2.6
--- dictobject.c	2001/06/05 10:49:24	2.80.2.7
***************
*** 1277,1285 ****
  staticforward PyObject *dictiter_new(dictobject *);
  
! static PyObject *
! dict_construct(PyDictObject *self, PyObject *args, PyObject *kw)
  {
- 	if (self == NULL)
- 		return PyDict_New();
  	self->ma_size = 0;
  	self->ma_poly = 0;
--- 1277,1283 ----
  staticforward PyObject *dictiter_new(dictobject *);
  
! static int
! dict_init(PyDictObject *self, PyObject *args, PyObject *kw)
  {
  	self->ma_size = 0;
  	self->ma_poly = 0;
***************
*** 1291,1295 ****
  	++created;
  #endif
! 	return (PyObject *)self;
  }
  
--- 1289,1293 ----
  	++created;
  #endif
! 	return 0;
  }
  
***************
*** 1330,1334 ****
  	0,					/* tp_descr_get */
  	0,					/* tp_descr_set */
! 	(ternaryfunc)dict_construct,		/* tp_construct */
  };
  
--- 1328,1335 ----
  	0,					/* tp_descr_get */
  	0,					/* tp_descr_set */
! 	0,					/* tp_dictoffset */
! 	(initproc)dict_init,			/* tp_init */
! 	PyType_GenericAlloc,			/* tp_alloc */
! 	PyType_GenericNew,			/* tp_new */
  };
  

Index: funcobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/funcobject.c,v
retrieving revision 2.37.4.3
retrieving revision 2.37.4.4
diff -C2 -r2.37.4.3 -r2.37.4.4
*** funcobject.c	2001/05/11 20:45:22	2.37.4.3
--- funcobject.c	2001/06/05 10:49:24	2.37.4.4
***************
*** 377,381 ****
  	func_descr_get,				/* tp_descr_get */
  	0,					/* tp_descr_set */
- 	0,					/* tp_construct */
  	offsetof(PyFunctionObject, func_dict),	/* tp_dictoffset */
  };
--- 377,380 ----

Index: listobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/listobject.c,v
retrieving revision 2.92.6.4
retrieving revision 2.92.6.5
diff -C2 -r2.92.6.4 -r2.92.6.5
*** listobject.c	2001/05/14 21:35:52	2.92.6.4
--- listobject.c	2001/06/05 10:49:24	2.92.6.5
***************
*** 1243,1246 ****
--- 1243,1247 ----
  	int err;
  	PyObject *compare = NULL;
+ 	PyTypeObject *savetype;
  
  	if (args != NULL) {
***************
*** 1248,1256 ****
  			return NULL;
  	}
  	self->ob_type = &immutable_list_type;
  	err = samplesortslice(self->ob_item,
  			      self->ob_item + self->ob_size,
  			      compare);
! 	self->ob_type = &PyList_Type;
  	if (err < 0)
  		return NULL;
--- 1249,1258 ----
  			return NULL;
  	}
+ 	savetype = self->ob_type;
  	self->ob_type = &immutable_list_type;
  	err = samplesortslice(self->ob_item,
  			      self->ob_item + self->ob_size,
  			      compare);
! 	self->ob_type = savetype;
  	if (err < 0)
  		return NULL;
***************
*** 1495,1506 ****
  }
  
! static PyObject *
! list_construct(PyListObject *self, PyObject *args, PyObject *kw)
  {
- 	if (self == NULL)
- 		return PyList_New(0);
  	self->ob_size = 0;
  	self->ob_item = NULL;
! 	return (PyObject *)self;
  }
  
--- 1497,1506 ----
  }
  
! static int
! list_init(PyListObject *self, PyObject *args, PyObject *kw)
  {
  	self->ob_size = 0;
  	self->ob_item = NULL;
! 	return 0;
  }
  
***************
*** 1586,1590 ****
  	0,					/* tp_descr_get */
  	0,					/* tp_descr_set */
! 	(ternaryfunc)list_construct,		/* tp_construct */
  };
  
--- 1586,1593 ----
  	0,					/* tp_descr_get */
  	0,					/* tp_descr_set */
! 	0,					/* tp_dictoffset */
! 	(initproc)list_init,			/* tp_init */
! 	PyType_GenericAlloc,			/* tp_alloc */
! 	PyType_GenericNew,			/* tp_new */
  };
  
***************
*** 1669,1673 ****
  	0,					/* tp_descr_get */
  	0,					/* tp_descr_set */
! 	(ternaryfunc)list_construct,		/* tp_construct */
  	/* NOTE: This is *not* the standard list_type struct! */
  };
--- 1672,1676 ----
  	0,					/* tp_descr_get */
  	0,					/* tp_descr_set */
! 	0,					/* tp_init */
  	/* NOTE: This is *not* the standard list_type struct! */
  };

Index: moduleobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/moduleobject.c,v
retrieving revision 2.31.6.2
retrieving revision 2.31.6.3
diff -C2 -r2.31.6.2 -r2.31.6.3
*** moduleobject.c	2001/05/12 20:40:47	2.31.6.2
--- moduleobject.c	2001/06/05 10:49:24	2.31.6.3
***************
*** 135,147 ****
  /* Methods */
  
! static PyObject *
! module_construct(PyModuleObject *m, PyObject *args, PyObject *kw)
  {
- 	if (m == NULL)
- 		return PyModule_New("?");
  	m->md_dict = PyDict_New();
  	if (m->md_dict == NULL)
! 		return NULL;
! 	return (PyObject *)m;
  }
  
--- 135,145 ----
  /* Methods */
  
! static int
! module_init(PyModuleObject *m, PyObject *args, PyObject *kw)
  {
  	m->md_dict = PyDict_New();
  	if (m->md_dict == NULL)
! 		return -1;
! 	return 0;
  }
  
***************
*** 226,230 ****
  	0,					/* tp_descr_get */
  	0,					/* tp_descr_set */
- 	(ternaryfunc)module_construct,		/* tp_construct */
  	offsetof(PyModuleObject, md_dict),	/* tp_dictoffset */
  };
--- 224,230 ----
  	0,					/* tp_descr_get */
  	0,					/* tp_descr_set */
  	offsetof(PyModuleObject, md_dict),	/* tp_dictoffset */
+ 	(initproc)module_init,			/* tp_init */
+ 	PyType_GenericAlloc,			/* tp_alloc */
+ 	PyType_GenericNew,			/* tp_new */
  };

Index: typeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/typeobject.c,v
retrieving revision 2.16.8.25
retrieving revision 2.16.8.26
diff -C2 -r2.16.8.25 -r2.16.8.26
*** typeobject.c	2001/05/22 19:53:15	2.16.8.25
--- typeobject.c	2001/06/05 10:49:24	2.16.8.26
***************
*** 56,64 ****
  type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
  {
! 	int size;
! 	void *mem;
! 	PyObject *obj, *res;
! 
! 	if (type->tp_construct == NULL) {
  		PyErr_Format(PyExc_TypeError,
  			     "cannot construct '%.100s' instances",
--- 56,60 ----
  type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
  {
! 	if (type->tp_new == NULL) {
  		PyErr_Format(PyExc_TypeError,
  			     "cannot construct '%.100s' instances",
***************
*** 67,70 ****
--- 63,76 ----
  	}
  
+ 	return type->tp_new(type, args, kwds);
+ }
+ 
+ PyObject *
+ PyType_GenericAlloc(PyTypeObject *type, PyObject *args, PyObject *kwds)
+ {
+ 	int size;
+ 	void *mem;
+ 	PyObject *obj;
+ 
  	/* Inline PyObject_New() so we can zero the memory */
  	size = _PyObject_SIZE(type);
***************
*** 80,92 ****
  		Py_INCREF(type);
  	PyObject_INIT(obj, type);
  
! 	res = (type->tp_construct)(obj, args, kwds);
! 	if (res == NULL) {
! 		Py_DECREF(obj);
  		return NULL;
  	}
! 	if (PyType_IS_GC(type))
! 		PyObject_GC_Init(res);
! 	return res;
  }
  
--- 86,107 ----
  		Py_INCREF(type);
  	PyObject_INIT(obj, type);
+ 	if (PyType_IS_GC(type))
+ 		PyObject_GC_Init(obj);
+ 	return obj;
+ }
  
! PyObject *
! PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
! {
! 	PyObject *self;
! 
! 	self = type->tp_alloc(type, args, kwds);
! 	if (self == NULL)
! 		return NULL;
! 	if (type->tp_init(self, args, kwds) < 0) {
! 		Py_DECREF(self);
  		return NULL;
  	}
! 	return self;
  }
  
***************
*** 133,137 ****
  /* TypeType's constructor is called when a type is subclassed */
  static PyObject *
! type_construct(PyTypeObject *type, PyObject *args, PyObject *kwds)
  {
  	PyObject *name, *bases, *dict, *x, *slots;
--- 148,152 ----
  /* 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;
***************
*** 167,171 ****
  		return NULL;
  	}
! 	if (base->tp_construct == NULL) {
  		PyErr_SetString(PyExc_TypeError,
  				"base type must have a constructor slot");
--- 182,186 ----
  		return NULL;
  	}
! 	if (base->tp_init == NULL) {
  		PyErr_SetString(PyExc_TypeError,
  				"base type must have a constructor slot");
***************
*** 337,342 ****
  	0,					/* tp_descr_get */
  	0,					/* tp_descr_set */
- 	(ternaryfunc)type_construct,		/* tp_construct */
  	offsetof(PyTypeObject, tp_dict),	/* tp_dictoffset */
  };
  
--- 352,359 ----
  	0,					/* tp_descr_get */
  	0,					/* tp_descr_set */
  	offsetof(PyTypeObject, tp_dict),	/* tp_dictoffset */
+ 	(initproc)type_init,			/* tp_init */
+ 	PyType_GenericAlloc,			/* tp_alloc */
+ 	PyType_GenericNew,			/* tp_new */
  };
  
***************
*** 580,585 ****
  		COPYSLOT(tp_descr_get);
  		COPYSLOT(tp_descr_set);
- 		COPYSLOT(tp_construct);
  		COPYSLOT(tp_dictoffset);
  	}
  
--- 597,604 ----
  		COPYSLOT(tp_descr_get);
  		COPYSLOT(tp_descr_set);
  		COPYSLOT(tp_dictoffset);
+ 		COPYSLOT(tp_init);
+ 		COPYSLOT(tp_alloc);
+ 		COPYSLOT(tp_new);
  	}
  
***************
*** 1137,1149 ****
  wrap_init(PyObject *self, PyObject *args, void *wrapped)
  {
! 	ternaryfunc func = (ternaryfunc)wrapped;
! 	PyObject *res;
  
  	/* XXX What about keyword arguments? */
! 	res = (*func)(self, args, NULL);
! 	if (res == NULL)
  		return NULL;
- 	/* tp_construct doesn't return a new object; it just returns self,
- 	   un-INCREF-ed */
  	Py_INCREF(Py_None);
  	return Py_None;
--- 1156,1164 ----
  wrap_init(PyObject *self, PyObject *args, void *wrapped)
  {
! 	initproc func = (initproc)wrapped;
  
  	/* XXX What about keyword arguments? */
! 	if (func(self, args, NULL) < 0)
  		return NULL;
  	Py_INCREF(Py_None);
  	return Py_None;
***************
*** 1243,1247 ****
  	ADD(type->tp_descr_get, tab_descr_get);
  	ADD(type->tp_descr_set, tab_descr_set);
! 	ADD(type->tp_construct, tab_init);
  
  	return 0;
--- 1258,1262 ----
  	ADD(type->tp_descr_get, tab_descr_get);
  	ADD(type->tp_descr_set, tab_descr_set);
! 	ADD(type->tp_init, tab_init);
  
  	return 0;
***************
*** 1514,1519 ****
  }
  
! static PyObject *
! slot_tp_construct(PyObject *self, PyObject *args, PyObject *kwds)
  {
  	PyObject *meth = PyObject_GetAttrString(self, "__init__");
--- 1529,1534 ----
  }
  
! static int
! slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
  {
  	PyObject *meth = PyObject_GetAttrString(self, "__init__");
***************
*** 1521,1531 ****
  
  	if (meth == NULL)
! 		return NULL;
  	res = PyObject_Call(meth, args, kwds);
  	Py_DECREF(meth);
  	if (res == NULL)
! 		return NULL;
  	Py_DECREF(res);
! 	return self;
  }
  
--- 1536,1546 ----
  
  	if (meth == NULL)
! 		return -1;
  	res = PyObject_Call(meth, args, kwds);
  	Py_DECREF(meth);
  	if (res == NULL)
! 		return -1;
  	Py_DECREF(res);
! 	return 0;
  }
  
***************
*** 1629,1632 ****
  	TPSLOT(get, tp_descr_get);
  	TPSLOT(set, tp_descr_set);
! 	TPSLOT(init, tp_construct);
  }
--- 1644,1647 ----
  	TPSLOT(get, tp_descr_get);
  	TPSLOT(set, tp_descr_set);
! 	TPSLOT(init, tp_init);
  }