[Python-checkins] CVS: python/dist/src/Objects classobject.c,2.127.2.6,2.127.2.7

Guido van Rossum gvanrossum@users.sourceforge.net
2001年7月02日 18:14:59 -0700


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv23303
Modified Files:
 Tag: descr-branch
	classobject.c 
Log Message:
Support class methods for classic classes. This is done by using the
tp_descr_get slot in class_getattr() and instance_getattr2() rather
than making explicit check for functions.
This is an important generalization, apart from the support for class
method! Any class attribute that supports the tp_descr_get slot is
now allowed to perform its own "bind" operation. This also opens the
way to supporting computed attributes using "get/set" descriptors
(which requires a only little bit more code I think -- mostly to
create a get/set descriptor from a pair of Python functions).
Index: classobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/classobject.c,v
retrieving revision 2.127.2.6
retrieving revision 2.127.2.7
diff -C2 -r2.127.2.6 -r2.127.2.7
*** classobject.c	2001年06月28日 16:46:51	2.127.2.6
--- classobject.c	2001年07月03日 01:14:57	2.127.2.7
***************
*** 162,165 ****
--- 162,167 ----
 	register char *sname = PyString_AsString(name);
 	PyClassObject *class;
+ 	descrgetfunc f;
+ 
 	if (sname[0] == '_' && sname[1] == '_') {
 		if (strcmp(sname, "__dict__") == 0) {
***************
*** 191,202 ****
 			 PyString_AS_STRING(op->cl_name), sname);
 		return NULL;
- 	}
- 	Py_INCREF(v);
- 	if (PyFunction_Check(v)) {
- 		PyObject *w = PyMethod_New(v, (PyObject *)NULL,
- 						 (PyObject *)class);
- 		Py_DECREF(v);
- 		v = w;
 	}
 	return v;
 }
--- 193,202 ----
 			 PyString_AS_STRING(op->cl_name), sname);
 		return NULL;
 	}
+ 	f = v->ob_type->tp_descr_get;
+ 	if (f == NULL)
+ 		Py_INCREF(v);
+ 	else
+ 		v = f(v, (PyObject *)NULL, (PyObject *)op);
 	return v;
 }
***************
*** 648,651 ****
--- 648,653 ----
 	register PyObject *v;
 	PyClassObject *class;
+ 	descrgetfunc f;
+ 
 	class = NULL;
 	v = PyDict_GetItem(inst->in_dict, name);
***************
*** 657,663 ****
 	Py_INCREF(v);
 	if (class != NULL) {
! 		if (PyFunction_Check(v)) {
! 			PyObject *w = PyMethod_New(v, (PyObject *)inst,
! 						 (PyObject *)class);
 			Py_DECREF(v);
 			v = w;
--- 659,666 ----
 	Py_INCREF(v);
 	if (class != NULL) {
! 		f = v->ob_type->tp_descr_get;
! 		if (f != NULL) {
! 			PyObject *w = f(v, (PyObject *)inst,
! 					(PyObject *)(inst->in_class));
 			Py_DECREF(v);
 			v = w;
***************
*** 2080,2083 ****
--- 2083,2087 ----
 			sklassname, sfuncname);
 	else {
+ 		/* XXX Shouldn't use repr() here! */
 		PyObject *selfrepr = PyObject_Repr(self);
 		if (selfrepr == NULL)

AltStyle によって変換されたページ (->オリジナル) /