[Python-checkins] CVS: python/dist/src/Python bltinmodule.c,2.198.2.5,2.198.2.6

Guido van Rossum gvanrossum@users.sourceforge.net
Tue, 03 Jul 2001 17:14:00 -0700


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

Modified Files:
      Tag: descr-branch
	bltinmodule.c 
Log Message:
Make the tp_new slot overridable by defining a function __new__ in the
class statement.  This revealed a bug in my original design: class
methods can't be used here because there is no way to make an upcall
while passing it the most derived type as argument, because the type
is *always* implicit for class methods.

So I had to add static methods as well.  I decided not to throw away
class methods, since I'd already done the implementation effort, and
one user (Thomas Heller (?)) already mailed me that he wants to check
them out.

Anyway, it now works, although it's not very useful as long as types
like tuple can't be subtyped.

A wart: you can now call a type's __new__ method with an argument that
is not derived from that type, e.g. list.__new__(dictionary).  Because
list.__new__() is really just PyType_GenericNew(), this creates a
perfectly valid dictionary, but I imagine there are cases where this
can do bad things.  We can fix this in two ways: either add an
explicit typecheck to each <sometype>_new() that makes assumptions, or
change the __new__ wrapper so that it knows the expected type, and
does a subtype check.

Another wart: currently, the metatype's tp_call() makes the call to
the type's tp_init(), after the type's tp_new() succeeds.  This code
had to be robustified: if tp_new() returned an object from a different
type, the wrong type's tp_init() was called.  Now it calls the
tp_init() of the type of the returned object.  But this still feels
fishy, because what if tp_new() returned an old object?  Then it will
be reinitialized!  Perhaps the call to tp_init() should be moved back
inside tp_new(), so that each type is responsible for calling its own
tp_init()?  (Problem with that is that it's repeated code -- each
tp_new() implementation has to do its own.



Index: bltinmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/bltinmodule.c,v
retrieving revision 2.198.2.5
retrieving revision 2.198.2.6
diff -C2 -r2.198.2.5 -r2.198.2.6
*** bltinmodule.c	2001/07/02 18:06:06	2.198.2.5
--- bltinmodule.c	2001/07/04 00:13:58	2.198.2.6
***************
*** 1816,1819 ****
--- 1816,1822 ----
  				 (PyObject *) &PyBaseObject_Type) < 0)
  		return NULL;
+ 	if (PyDict_SetItemString(dict, "staticmethod",
+ 				 (PyObject *) &PyStaticMethod_Type) < 0)
+ 		return NULL;
  	if (PyDict_SetItemString(dict, "str", (PyObject *) &PyString_Type) < 0)
  		return NULL;