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

Guido van Rossum gvanrossum@users.sourceforge.net
Thu, 10 May 2001 14:41:36 -0700


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

Modified Files:
      Tag: descr-branch
	typeobject.c 
Log Message:
When a dynamic type is called (to create a new instance), after all
the C initialization is done, look for an __init__() method and call
it if it is defined.

Open issues:

- Should the args/kwds be passed on to type_call (which passes them on
  to the base class's tp_construct, which may use or ignore them or
  complain about them) as well as to __init__?

- Currently when there's no __init__ I don't enforce that the argument
  list is empty.  Should I?


Index: typeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/typeobject.c,v
retrieving revision 2.16.8.16
retrieving revision 2.16.8.17
diff -C2 -r2.16.8.16 -r2.16.8.17
*** typeobject.c	2001/05/10 21:15:42	2.16.8.16
--- typeobject.c	2001/05/10 21:41:34	2.16.8.17
***************
*** 142,145 ****
--- 142,146 ----
  
  /* Helpers for subtyping */
+ 
  static PyObject *
  subtype_construct(PyObject *self, PyObject *args, PyObject *kwds)
***************
*** 359,362 ****
--- 360,386 ----
  /* Support for dynamic types, created by type_construct() above */
  
+ static PyObject *
+ dtype_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
+ {
+ 	PyObject *newobj, *init, *res;
+ 
+ 	newobj = type_call(type, args, kwds);
+ 	if (newobj == NULL)
+ 		return NULL;
+ 	init = PyObject_GetAttrString(newobj, "__init__");
+ 	if (init == NULL) {
+ 		PyErr_Clear();
+ 		return newobj;
+ 	}
+ 	res = PyObject_Call(init, args, kwds);
+ 	Py_DECREF(init);
+ 	if (res == NULL) {
+ 		Py_DECREF(newobj);
+ 		return NULL;
+ 	}
+ 	Py_DECREF(res);
+ 	return newobj;
+ }
+ 
  static int
  dtype_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
***************
*** 429,433 ****
  	0,					/* tp_as_mapping */
  	0,					/* tp_hash */
! 	(ternaryfunc)type_call,			/* tp_call */
  	0,					/* tp_str */
  	(getattrofunc)type_getattro,		/* tp_getattro */
--- 453,457 ----
  	0,					/* tp_as_mapping */
  	0,					/* tp_hash */
! 	(ternaryfunc)dtype_call,		/* tp_call */
  	0,					/* tp_str */
  	(getattrofunc)type_getattro,		/* tp_getattro */