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

Guido van Rossum gvanrossum@users.sourceforge.net
Thu, 09 Aug 2001 12:38:17 -0700


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

Modified Files:
	typeobject.c 
Log Message:
Thinking back to the 2.22 revision, I didn't like what I did there one
bit.  For one, this class:

    class C(object):
        def __new__(myclass, ...): ...

would have no way to call the __new__ method of its base class, and
the workaround (to create an intermediate base class whose __new__ you
can call) is ugly.

So, I've come up with a better solution that restores object.__new__,
but still solves the original problem, which is that built-in and
extension types shouldn't inherit object.__new__.  The solution is
simple: only "heap types" inherit tp_new.  Simpler, less code,
perfect!


Index: typeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/typeobject.c,v
retrieving revision 2.25
retrieving revision 2.26
diff -C2 -d -r2.25 -r2.26
*** typeobject.c	2001/08/08 22:26:22	2.25
--- typeobject.c	2001/08/09 19:38:15	2.26
***************
*** 448,452 ****
  staticforward void object_dealloc(PyObject *);
  staticforward int object_init(PyObject *, PyObject *, PyObject *);
- staticforward int add_tp_new_wrapper(PyTypeObject *);
  
  static PyObject *
--- 448,451 ----
***************
*** 673,686 ****
  	override_slots(type, type->tp_defined);
  
- 	/* Special hack for __new__ */
- 	if (type->tp_new == NULL) {
- 		/* Can't do this earlier, or some nasty recursion happens. */
- 		type->tp_new = PyType_GenericNew;
- 		if (add_tp_new_wrapper(type) < 0) {
- 			Py_DECREF(type);
- 			return NULL;
- 		}
- 	}
- 
  	return (PyObject *)type;
  }
--- 672,675 ----
***************
*** 914,918 ****
  	object_init,				/* tp_init */
  	PyType_GenericAlloc,			/* tp_alloc */
! 	0,					/* tp_new */
  	object_free,				/* tp_free */
  };
--- 903,907 ----
  	object_init,				/* tp_init */
  	PyType_GenericAlloc,			/* tp_alloc */
! 	PyType_GenericNew,			/* tp_new */
  	object_free,				/* tp_free */
  };
***************
*** 1164,1168 ****
  		COPYSLOT(tp_init);
  		COPYSLOT(tp_alloc);
! 		COPYSLOT(tp_new);
  		COPYSLOT(tp_free);
  	}
--- 1153,1159 ----
  		COPYSLOT(tp_init);
  		COPYSLOT(tp_alloc);
! 		if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
! 			COPYSLOT(tp_new);
! 		}
  		COPYSLOT(tp_free);
  	}