[Python-checkins] CVS: python/dist/src/Objects descrobject.c,1.1.2.6,1.1.2.7 typeobject.c,2.16.8.7,2.16.8.8

Guido van Rossum gvanrossum@users.sourceforge.net
Tue, 01 May 2001 14:04:24 -0700


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

Modified Files:
      Tag: descr-branch
	descrobject.c typeobject.c 
Log Message:
There's a subtlety in type_getattr() that makes me feel the pain of
not having metaclasses -- or actually, the mistake of using
FooClass.barMethod to reference the unbound barMethod method of
FooClass instances (where Smalltalk puts the methods in
FooClass.methodDict).  To fix this, I added query functions to
determine whether a descriptor describes a method or data, and I use
this to implement the following priority rules when accessing the bar
attribute of type FooType:

1) methods in FooType.__dict__, unadorned
2) anything in FooType.__class__.__dict__, used as a descriptor
3) anything else in FooType.__dict__, unadorned

This means that if both have a __repr__, FooType.__repr__ is the
unbound __repr__ method for Foo objects, but FooType.__class__ is the
class (or type) of FooType, which is (usually) TypeType.

If you're confused by this, don't worry.  This is on a branch. :-)



Index: descrobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/Attic/descrobject.c,v
retrieving revision 1.1.2.6
retrieving revision 1.1.2.7
diff -C2 -r1.1.2.6 -r1.1.2.7
*** descrobject.c	2001/04/30 01:14:56	1.1.2.6
--- descrobject.c	2001/05/01 21:04:21	1.1.2.7
***************
*** 397,400 ****
--- 397,426 ----
  };
  
+ int
+ PyDescr_IsMethod(PyObject *d)
+ {
+ 	if (PyDescr_Check(d)) {
+ 		switch (((PyDescrObject *)d)->d_flavor) {
+ 		case DF_METHOD:
+ 		case DF_WRAPPER:
+ 			return 1;
+ 		}
+ 	}
+ 	return 0;
+ }
+ 
+ int
+ PyDescr_IsData(PyObject *d)
+ {
+ 	if (PyDescr_Check(d)) {
+ 		switch (((PyDescrObject *)d)->d_flavor) {
+ 		case DF_MEMBER:
+ 		case DF_GETSET:
+ 			return 1;
+ 		}
+ 	}
+ 	return 0;
+ }
+ 
  static PyDescrObject *
  PyDescr_New(PyTypeObject *type)

Index: typeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/typeobject.c,v
retrieving revision 2.16.8.7
retrieving revision 2.16.8.8
diff -C2 -r2.16.8.7 -r2.16.8.8
*** typeobject.c	2001/04/30 15:54:21	2.16.8.7
--- typeobject.c	2001/05/01 21:04:21	2.16.8.8
***************
*** 62,65 ****
--- 62,69 ----
  				return NULL;
  		}
+ 		descr = PyDict_GetItem(type->tp_dict, name);
+ 		if (descr != NULL && PyDescr_IsMethod(descr) &&
+ 		    (f = descr->ob_type->tp_descr_get) != NULL)
+ 			return (*f)(descr, NULL);
  	}