[Python-checkins] CVS: python/dist/src/Lib types.py,1.14.10.1,1.14.10.2

Guido van Rossum gvanrossum@users.sourceforge.net
Sat, 05 May 2001 19:31:15 -0700


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

Modified Files:
      Tag: descr-branch
	types.py 
Log Message:
Finally we're having fun.  This set of changes makes it possible to
subclass (some) built-in types in Python.

- Introduce another metatype, the type of TypeType, dubbed TurtleType.
  Its reason for existence is so that its tp_call slot can be the
  function that is called when a regular type object is present in the
  list of base classes of a class statement; the (modified) Don
  Beaudry hook will call the type's type with (name, bases, dict), and
  the type's type happens to be TypeType; in order to make TypeType
  (itself) callable, it must have a type that defines the appropriate
  tp_call slot.  This is TurtleType.

- The tp_construct slot is changed to take args and kwds.
  TurtleType's tp_call passes those through, and so does TypeType's
  tp_call.  (The tp_constrict slots in the dict and list types don't
  do anything with their arguments yet.)

- The instance method type was generalized not to require that the
  class argument is an actual class.  Since the class wasn't used in
  an essential way, this was easy; only its repr() had to be changed.
  (It could stand some more cleanup, and should be moved to its own
  file.)

- Python function objects now have a tp_descr_get slot, which returns
  a bound (instance) method object when an object is given; without an
  object, this returns the function itself unchanged.  The latter
  behavior is not ideal, but the best I can do without changing the
  tp_descr_get API (since without an object, the class whose unbound
  method this is isn't known).

- PyGeneric_GetAttr no longer requires that all objects it finds in
  tp_dict are descriptors; non-descriptors are returned unchanged.

- The tp_construct slot for TypeType (which defines how to construct
  regular types) is defined; it takes an argument list of the form
  (name, bases, dict), so it can be used as the class creation
  function.  It currently requires that there's exactly one base,
  which must be a type object.  The subtypes it defines can override
  existing methods and define new methods, but it cannot yet override
  operators (__foo__ methods in the dict don't have an effect yet).

- The Don Beaudry hook is changed -- alternative bases now must be
  types.  If this condition is met, the same thing is done as before:
  the base's type is called with (name, bases, dict) to construct the
  new class.

Not yet implemented:

- New instance variables in subtypes (the subtypes don't have a
  __dict__ to store these).

- Overriding operators in subtypes with __foo__ methods.

- Any form of multiple inheritance.



Index: types.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/types.py,v
retrieving revision 1.14.10.1
retrieving revision 1.14.10.2
diff -C2 -r1.14.10.1 -r1.14.10.2
*** types.py	2001/05/04 16:51:40	1.14.10.1
--- types.py	2001/05/06 02:31:13	1.14.10.2
***************
*** 70,73 ****
--- 70,74 ----
  FunctionIterType = type(iter(lambda: 0, 0))
  DictProxyType = type(TypeType.__dict__)
+ TurtleType = type(TypeType)
  
  del sys, _f, _C, _x                     # Not for export